- no change (just updated "last edited" for svn)
[cdmlib.git] / cdmlib-ext / src / test / java / eu / etaxonomy / cdm / ext / ipni / IpniServiceTest.java
1 /**
2 *
3 */
4 package eu.etaxonomy.cdm.ext.ipni;
5
6 import java.io.InputStream;
7 import java.util.List;
8
9 import org.apache.log4j.Logger;
10 import org.junit.Assert;
11 import org.junit.Before;
12 import org.junit.BeforeClass;
13 import org.junit.Ignore;
14 import org.junit.Test;
15
16 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
17 import eu.etaxonomy.cdm.common.UriUtils;
18 import eu.etaxonomy.cdm.ext.ipni.IIpniService;
19 import eu.etaxonomy.cdm.ext.ipni.IpniService;
20 import eu.etaxonomy.cdm.ext.ipni.IpniServiceAuthorConfigurator;
21 import eu.etaxonomy.cdm.ext.ipni.IpniServiceNamesConfigurator;
22 import eu.etaxonomy.cdm.ext.ipni.IpniServicePublicationConfigurator;
23 import eu.etaxonomy.cdm.ext.ipni.IIpniService.DelimitedFormat;
24 import eu.etaxonomy.cdm.ext.ipni.IpniService.IpniRank;
25 import eu.etaxonomy.cdm.model.agent.Person;
26 import eu.etaxonomy.cdm.model.common.Extension;
27 import eu.etaxonomy.cdm.model.name.BotanicalName;
28 import eu.etaxonomy.cdm.model.name.Rank;
29 import eu.etaxonomy.cdm.model.reference.Reference;
30
31 /**
32 * @author a.mueller
33 *
34 */
35
36 public class IpniServiceTest {
37 @SuppressWarnings("unused")
38 private static final Logger logger = Logger.getLogger(IpniServiceTest.class);
39
40 private IpniService service1;
41 private static boolean internetIsAvailable = true;
42
43 @BeforeClass
44 public static void setUpClass() throws Exception {
45 internetIsAvailable = true;
46 }
47
48
49
50 /**
51 * @throws java.lang.Exception
52 */
53 @Before
54 public void setUp() throws Exception {
55 service1 = new IpniService();
56 }
57
58 // ******************************* TESTS ******************************************************/
59
60 /**
61 * Test method for {@link eu.etaxonomy.cdm.ext.ipni.IpniService#IpniService(java.net.URL)}.
62 */
63 @Test
64 public void testIpniService() {
65 Assert.assertNotNull("Service should not be null", service1);
66 Assert.assertNotNull("URL1 should not be null", service1.getServiceUrl(IIpniService.ADVANCED_NAME_SERVICE_URL));
67 }
68
69 @Test
70 public void testGetAuthors(){
71 ICdmApplicationConfiguration services = null;
72 IpniServiceAuthorConfigurator config = new IpniServiceAuthorConfigurator();
73 config.setFormat(DelimitedFormat.EXTENDED);
74 List<Person> authorList = service1.getAuthors(null, "Greuter", null, null, services, config);
75 //expected web service result: 3379-1%1.1%Greuter%Werner Rodolfo%Greuter%PS%1938-%>Greuter, Werner Rodolfo
76
77 if (testInternetConnectivity(authorList)){
78 Assert.assertEquals("There should be exactly 1 result for 'Greuter'", 1, authorList.size());
79 Person author = authorList.get(0);
80 //title cache
81 Assert.assertEquals("Title Cache for Greuter should be 'Werner Rodolfo Greuter'", "Werner Rodolfo Greuter", author.getTitleCache());
82 //alternative names
83 Assert.assertEquals("One extension for the alternative name should exist", 1, author.getExtensions().size());
84 Extension alternativeName = author.getExtensions().iterator().next();
85 Assert.assertEquals("Alternative name should be ", "Greuter, Werner Rodolfo", alternativeName.getValue());
86 //dates
87 String year = author.getLifespan().getYear();
88 Assert.assertNotNull("Year should be not null", year);
89 Assert.assertEquals("Year should be 1938", "1938", year);
90
91 authorList = service1.getAuthors(null, "Greu*", null, null, null, config);
92
93 //29367-1%1.1%Greuet%Claude%Greuet%A%%>Greuet, Claude
94 //20000981-1%1.1%Greuning%J.V. van%Greuning%M%1993%>Greuning, J.V. van
95 //3379-1%1.1%Greuter%Werner Rodolfo%Greuter%PS%1938-%>Greuter, Werner Rodolfo
96 Assert.assertEquals("There should be exactly 3 result for 'Greu*'. But maybe this changes over time", 3, authorList.size());
97 // for (Person person : authorList){
98 // System.out.println(person.getTitleCache() + "; " + person.getNomenclaturalTitle());
99 // }
100 //
101 config.setFormat(DelimitedFormat.MINIMAL);
102 authorList = service1.getAuthors(null, "Greuter", null, null, services, config);
103 Assert.assertEquals("There should be exactly 1 result for 'Greuter'", 1, authorList.size());
104 author = authorList.get(0);
105 Assert.assertTrue("No alternative names should exist in the minimal version", author.getExtensions().isEmpty());
106 }
107 }
108
109
110 @Test
111 public void testGetNamesSimple(){
112 ICdmApplicationConfiguration services = null;
113 IpniServiceNamesConfigurator config = null;
114 List<BotanicalName> nameList = service1.getNamesSimple("Abies albertiana", services, config);
115 //expected web service result: 3379-1%1.1%Greuter%Werner Rodolfo%Greuter%PS%1938-%>Greuter, Werner Rodolfo
116
117 if (testInternetConnectivity(nameList)){
118 Assert.assertEquals("There should be exactly 1 result for 'Abies albertiana'", 1, nameList.size());
119 BotanicalName name = nameList.get(0);
120 //title cache
121 Assert.assertEquals("Title Cache for Abies albertiana should be 'Abies albertiana'", "Abies albertiana A.Murr.", name.getTitleCache());
122
123 // for (BotanicalName listName : nameList){
124 // System.out.println(name.getFullTitleCache());
125 // }
126 }
127
128 }
129
130 @Test
131 public void testGetNamesAdvanced(){
132 ICdmApplicationConfiguration services = null;
133 IpniServiceNamesConfigurator config = new IpniServiceNamesConfigurator();
134
135 //http://www.uk.ipni.org/ipni/advPlantNameSearch.do?find_family=&find_genus=Abies&find_species=alba&find_infrafamily=&find_infragenus=&find_infraspecies=&find_authorAbbrev=B*&find_includePublicationAuthors=on&find_includePublicationAuthors=off&find_includeBasionymAuthors=on&find_includeBasionymAuthors=off&find_publicationTitle=&find_isAPNIRecord=on&find_isAPNIRecord=false&find_isGCIRecord=on&find_isGCIRecord=false&find_isIKRecord=on&find_isIKRecord=false&find_rankToReturn=infraspec&output_format=normal&find_sortByFamily=on&find_sortByFamily=off&query_type=by_query&back_page=plantsearch
136 String family = "";
137 String genus = "Abies";
138 String species = "alba";
139 String infraFamily = "";
140 String infraGenus = "";
141 String infraSpecies = "";
142 String authorAbbrev = "B*";
143 Boolean includePublicationAuthors = null;
144 Boolean includeBasionymAuthors = null;
145 String publicationTitle = "";
146 Boolean isAPNIRecord = null;
147 Boolean isGCIRecord = null;
148 Boolean isIKRecord = null;
149 IpniRank rankToReturn = IpniRank.valueOf(Rank.SUBSPECIES());
150 Boolean sortByFamily = null;
151
152 List<BotanicalName> nameList = service1.getNamesAdvanced(family, genus, species, infraFamily, infraGenus, infraSpecies, authorAbbrev, includePublicationAuthors, includeBasionymAuthors, publicationTitle, isAPNIRecord, isGCIRecord, isIKRecord, rankToReturn, sortByFamily, config, services);
153 //expected web service result: 3379-1%1.1%Greuter%Werner Rodolfo%Greuter%PS%1938-%>Greuter, Werner Rodolfo
154
155
156 if (testInternetConnectivity(nameList)){
157
158 Assert.assertEquals("There should be exactly 1 result for 'Abies', 'alba', 'B*', Infraspecific ", 1, nameList.size());
159 BotanicalName name = nameList.get(0);
160 //title cache
161 Assert.assertEquals("Title Cache for 'Abies', 'alba', 'ap*' should be 'Abies alba subsp. apennina Brullo, Scelsi & Spamp.'", "Abies alba subsp. apennina Brullo, Scelsi & Spamp.", name.getTitleCache());
162
163 // for (BotanicalName listName : nameList){
164 // System.out.println(name.getFullTitleCache());
165 // }
166 }
167 }
168
169 @Test
170 public void testPublications(){
171 ICdmApplicationConfiguration services = null;
172 IpniServicePublicationConfigurator config = null;
173 List<Reference> refList = service1.getPublications("Species Plantarum, Edition 3", "Sp. Pl.", services, config);
174 //20009158-1%1.2%Pinaceae%%N%Abies%%N%alba%apennina%subsp.%Brullo, Scelsi & Spamp.%%Brullo, Scelsi & Spamp.%Abies alba subsp. apennina%Vegetaz. Aspromonte%41 (2001)%2001%%%%%%Italy%tax. nov.
175
176 if (testInternetConnectivity(refList)){
177
178 Assert.assertEquals("There should be exactly 1 result for 'Species Plantarum, Edition 3'", 1, refList.size());
179 Reference ref = refList.get(0);
180 //title cache
181 //the author title may be improved in future
182 Assert.assertEquals("Title Cache should be 'Linnaeus, Carl, Species Plantarum, Edition 3'", "Linnaeus, Carl, Species Plantarum, Edition 3. 1764", ref.getTitleCache());
183
184
185 refList = service1.getPublications("Flora of Macar", null, services, config);
186 Assert.assertNotNull("Empty resultset should not throw exception and should not be null", refList);
187
188 refList = service1.getPublications("Flora Europaea [ed. 2]", null, services, config);
189 Assert.assertEquals("There should be exactly 1 result for 'Flora Europaea [ed. 2]'", 1, refList.size());
190 ref = refList.get(0);
191 Assert.assertEquals("", "Tutin, Thomas Gaskell", ref.getAuthorTeam().getTitleCache());
192
193
194 // for (Reference ref : refList){
195 // System.out.println(ref.getTitleCache());
196 // }
197 }
198
199 }
200
201 @Test
202 //@Ignore
203 public void testNameID(){
204 ICdmApplicationConfiguration services = null;
205 IpniServiceNamesConfigurator config = null;
206 InputStream content = service1.getNamesById("416415-1");
207
208
209 Assert.assertNotNull(content);
210 }
211
212
213 /**
214 * Test method for {@link eu.etaxonomy.cdm.ext.ipni.IpniService#getServiceUrl()}.
215 */
216 @Test
217 public void testGetServiceUrl() {
218 Assert.assertNotNull("Service should not be null", service1);
219 Assert.assertNotNull("URL1 should not be null", service1.getServiceUrl(IIpniService.ADVANCED_NAME_SERVICE_URL));
220 }
221
222
223 private boolean testInternetConnectivity(List<?> list) {
224 if (list == null || list.isEmpty()){
225 boolean result = internetIsAvailable && UriUtils.isInternetAvailable(null);
226 internetIsAvailable = result;
227 return result;
228
229 }
230 return true;
231 }
232
233 }