001/* 002 * Copyright 2020 Global Biodiversity Information Facility (GBIF) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.gbif.api.util; 017 018import static org.junit.jupiter.api.Assertions.assertEquals; 019import static org.junit.jupiter.api.Assertions.assertThrows; 020 021import java.util.Optional; 022import org.gbif.api.vocabulary.ContactType; 023import org.gbif.api.vocabulary.Country; 024import org.gbif.api.vocabulary.EndpointType; 025import org.gbif.api.vocabulary.IdentifierType; 026import org.gbif.api.vocabulary.TechnicalInstallationType; 027import org.junit.jupiter.api.Test; 028 029public class VocabularyUtilsTest { 030 031 @Test 032 public void testParseContactType() { 033 assertEquals(ContactType.AUTHOR, VocabularyUtils.parseContactType("author")); 034 assertEquals(ContactType.ADMINISTRATIVE_POINT_OF_CONTACT, 035 VocabularyUtils.parseContactType("ADMINISTRATIVE POINT_of_CONTACT")); 036 assertThrows(IllegalArgumentException.class, () -> VocabularyUtils.parseContactType("bad")); 037 } 038 039 @Test 040 public void testParseEndpointType() { 041 assertEquals(EndpointType.TAPIR, VocabularyUtils.parseEndpointType("Tapir")); 042 assertEquals(EndpointType.DWC_ARCHIVE, VocabularyUtils.parseEndpointType("DWC_ARCHIVE")); 043 assertThrows(IllegalArgumentException.class, () -> VocabularyUtils.parseEndpointType("bad")); 044 } 045 046 @Test 047 public void testParseIdentifierType() { 048 assertEquals(IdentifierType.LSID, VocabularyUtils.parseIdentifierType("lsid")); 049 assertEquals(IdentifierType.DOI, VocabularyUtils.parseIdentifierType("d.o.i.")); 050 assertThrows(IllegalArgumentException.class, () -> VocabularyUtils.parseIdentifierType("bad")); 051 } 052 053 @Deprecated 054 @Test 055 public void testParseTechnicalInstallationType() { 056 assertEquals(TechnicalInstallationType.IPT_INSTALLATION, 057 VocabularyUtils.parseTechnicalInstallationType("ipt installation")); 058 assertThrows(IllegalArgumentException.class, () -> VocabularyUtils.parseTechnicalInstallationType("bad")); 059 } 060 061 @Test 062 public void testReflectionLookup() { 063 assertEquals(Country.class, VocabularyUtils.lookupVocabulary(Country.class.getName())); 064 } 065 066 @Test 067 public void testReflectionLookupError() { 068 assertThrows(IllegalArgumentException.class, () -> VocabularyUtils.lookupVocabulary("WTFMate")); 069 } 070 071 @Test 072 public void testLookupUsingOptional() { 073 assertEquals(ContactType.AUTHOR, VocabularyUtils.lookup("author", ContactType.class).get()); 074 075 //Optional.absent() should be returned for all the following values 076 assertEquals(Optional.empty(), VocabularyUtils.lookup("thor", ContactType.class)); 077 assertEquals(Optional.empty(), VocabularyUtils.lookup("", ContactType.class)); 078 assertEquals(Optional.empty(), VocabularyUtils.lookup(null, ContactType.class)); 079 } 080}