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.vocabulary; 017 018import java.util.HashSet; 019import java.util.Set; 020 021import org.junit.jupiter.api.Test; 022 023import com.fasterxml.jackson.databind.ObjectMapper; 024import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 025import com.fasterxml.jackson.databind.annotation.JsonSerialize; 026 027import static org.junit.jupiter.api.Assertions.assertEquals; 028import static org.junit.jupiter.api.Assertions.assertFalse; 029import static org.junit.jupiter.api.Assertions.assertNotNull; 030import static org.junit.jupiter.api.Assertions.assertTrue; 031import static org.junit.jupiter.api.Assertions.fail; 032 033public class CountryTest { 034 035 @Test 036 public void testFromIsoCode() { 037 assertEquals(Country.ARGENTINA, Country.fromIsoCode("ar")); 038 assertEquals(Country.ARGENTINA, Country.fromIsoCode("AR")); 039 } 040 041 @Test 042 public void testGetIso2LetterCode() { 043 Set<String> codes = new HashSet<>(); 044 for (Country l : Country.values()) { 045 assertNotNull(l.getIso2LetterCode()); 046 // make sure its upper case 047 assertEquals(l.getIso2LetterCode().toUpperCase(), l.getIso2LetterCode()); 048 // make sure its unique 049 assertFalse(codes.contains(l.getIso2LetterCode())); 050 codes.add(l.getIso2LetterCode()); 051 } 052 assertEquals("DE", Country.GERMANY.getIso2LetterCode()); 053 assertEquals("GB", Country.UNITED_KINGDOM.getIso2LetterCode()); 054 } 055 056 @Test 057 public void testGetIso3LetterCode() { 058 Set<String> codes = new HashSet<>(); 059 for (Country l : Country.values()) { 060 assertNotNull(l.getIso3LetterCode()); 061 // make sure its upper case 062 assertEquals(l.getIso3LetterCode().toUpperCase(), l.getIso3LetterCode()); 063 // make sure its unique 064 assertFalse(codes.contains(l.getIso3LetterCode())); 065 codes.add(l.getIso3LetterCode()); 066 } 067 assertEquals("GBR", Country.UNITED_KINGDOM.getIso3LetterCode()); 068 assertEquals("DEU", Country.GERMANY.getIso3LetterCode()); 069 } 070 071 @Test 072 public void testGetIsoNumericalCode() { 073 Set<Integer> codes = new HashSet<>(); 074 for (Country l : Country.values()) { 075 assertNotNull(l.getIsoNumericalCode()); 076 // make sure its unique 077 assertFalse(codes.contains(l.getIsoNumericalCode())); 078 codes.add(l.getIsoNumericalCode()); 079 } 080 assertEquals("GBR", Country.UNITED_KINGDOM.getIso3LetterCode()); 081 assertEquals("DEU", Country.GERMANY.getIso3LetterCode()); 082 } 083 084 @Test 085 public void testIsCustomCode() { 086 for (Country l : Country.values()) { 087 if (l.isOfficial() && l != Country.KOSOVO) { 088 assertFalse(Country.isCustomCode(l.getIso2LetterCode())); 089 assertFalse(Country.isCustomCode(l.getIso3LetterCode())); 090 } else { 091 assertTrue(Country.isCustomCode(l.getIso2LetterCode())); 092 assertTrue(Country.isCustomCode(l.getIso3LetterCode())); 093 } 094 } 095 } 096 097 @Test 098 public void testIsOfficial() { 099 for (Country l : Country.OFFICIAL_COUNTRIES) { 100 assertTrue(l.isOfficial()); 101 } 102 int officialCountries = Country.OFFICIAL_COUNTRIES.size(); 103 int allCountries = Country.values().length; 104 assertTrue(allCountries > officialCountries); 105 assertEquals(250, officialCountries); 106 } 107 108 @Test 109 public void testgetTitle() { 110 for (Country l : Country.values()) { 111 assertNotNull(l.getTitle()); 112 assertTrue(l.getTitle().length() > 2); 113 } 114 assertEquals("United Kingdom of Great Britain and Northern Ireland", Country.UNITED_KINGDOM.getTitle()); 115 assertEquals("Germany", Country.GERMANY.getTitle()); 116 } 117 118 /** 119 * A container of Countries using 2 properties. 120 */ 121 private static class Container { 122 123 public Country country; 124 public Country island; // verify that the names don't matter 125 126 public Container(Country country, Country island) { 127 this.country = country; 128 this.island = island; 129 } 130 131 // used by serializer below 132 public Container() { 133 } 134 } 135 136 /** 137 * A test bean for trying the non default name serialization. 138 */ 139 private static class CountryNameBean { 140 141 @JsonSerialize(using = Country.TitleSerializer.class) 142 @JsonDeserialize(using = Country.TitleDeserializer.class) 143 public Country island; // verify that the names don't matter 144 145 public CountryNameBean(Country island) { 146 this.island = island; 147 } 148 149 // used by serializer below 150 public CountryNameBean() {} 151 } 152 153 @Test 154 public void testCodeUniqueness() { 155 Set<String> codes = new HashSet<>(); 156 for (Country c : Country.values()) { 157 assertFalse(codes.contains(c.getIso2LetterCode())); 158 assertFalse(codes.contains(c.getIso3LetterCode())); 159 160 codes.add(c.getIso2LetterCode()); 161 codes.add(c.getIso3LetterCode()); 162 163 if (c.getIsoNumericalCode() != null) { 164 assertFalse(codes.contains(c.getIsoNumericalCode().toString())); 165 codes.add(c.getIsoNumericalCode().toString()); 166 } 167 } 168 } 169 170 @Test 171 public void testTitleUniqueness() { 172 Set<String> names = new HashSet<>(); 173 for (Country c : Country.values()) { 174 assertFalse(names.contains(c.getTitle())); 175 names.add(c.getTitle()); 176 } 177 } 178 179 @Test 180 public void testSerDe() { 181 ObjectMapper mapper = new ObjectMapper(); 182 Container source = new Container(Country.DENMARK, Country.FALKLAND_ISLANDS); 183 try { 184 String json = mapper.writeValueAsString(source); 185 System.out.println(json); 186 Container target = mapper.readValue(json, Container.class); 187 assertEquals(source.country, target.country, "country differs"); 188 assertEquals(source.island, target.island, "island differs"); 189 } catch (Exception e) { 190 fail(e.getMessage()); 191 } 192 } 193 194 @Test 195 public void testSerDe2() { 196 ObjectMapper mapper = new ObjectMapper(); 197 CountryNameBean source = new CountryNameBean(Country.FALKLAND_ISLANDS); 198 try { 199 String json = mapper.writeValueAsString(source); 200 System.out.println(json); 201 CountryNameBean target = mapper.readValue(json, CountryNameBean.class); 202 assertEquals(source.island, target.island, "island differs"); 203 } catch (Exception e) { 204 fail(e.getMessage()); 205 } 206 } 207}