1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2014 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.ext.occurrence.gbif;
|
11
|
|
12
|
import java.io.IOException;
|
13
|
import java.net.URI;
|
14
|
import java.net.URISyntaxException;
|
15
|
import java.util.Collection;
|
16
|
import java.util.GregorianCalendar;
|
17
|
import java.util.List;
|
18
|
|
19
|
import org.apache.http.NameValuePair;
|
20
|
import org.apache.http.client.ClientProtocolException;
|
21
|
import org.apache.http.message.BasicNameValuePair;
|
22
|
import org.junit.Assert;
|
23
|
import org.junit.Test;
|
24
|
|
25
|
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
|
26
|
|
27
|
/**
|
28
|
* @author pplitzner
|
29
|
* @date 20.05.2014
|
30
|
*
|
31
|
*/
|
32
|
public class GbifQueryServiceWrapperTest {
|
33
|
|
34
|
private static final String LOCALITY_STRING = "Saddington Reservoir, Saddington Reservoir";
|
35
|
|
36
|
private final String dummyJsonRecords = "{" +
|
37
|
"\"results\":" +
|
38
|
"[" +
|
39
|
"{" +
|
40
|
"\"key\": 252408386," +
|
41
|
"\"datasetKey\": \"26a49731-9457-45b2-9105-1b96063deb26\"," +
|
42
|
"\"publishingOrgKey\": \"07f617d0-c688-11d8-bf62-b8a03c50a862\"," +
|
43
|
"\"publishingCountry\": \"GB\"," +
|
44
|
"\"protocol\": \"BIOCASE\"," +
|
45
|
"\"lastCrawled\": \"2013-09-07T07:08:17.000+0000\"," +
|
46
|
"\"identifiers\": [ ],"+
|
47
|
"\"media\": [ ],"+
|
48
|
"\"facts\": [ ],"+
|
49
|
"\"relations\": [ ],"+
|
50
|
"\"basisOfRecord\": \"OBSERVATION\","+
|
51
|
"\"decimalLongitude\": -1.0193,"+
|
52
|
"\"decimalLatitude\": 52.51021,"+
|
53
|
"\"continent\": \"EUROPE\","+
|
54
|
"\"year\": 2006,"+
|
55
|
"\"month\": 6,"+
|
56
|
"\"day\": 30,"+
|
57
|
"\"eventDate\": \"2006-06-29T22:00:00.000+0000\","+
|
58
|
"\"issues\": [ ],"+
|
59
|
"\"lastInterpreted\": \"2014-04-01T23:31:15.462+0000\","+
|
60
|
"\"geodeticDatum\": \"WGS84\","+
|
61
|
"\"countryCode\": \"GB\","+
|
62
|
"\"country\": \"United Kingdom\","+
|
63
|
"\"gbifID\": \"252408386\","+
|
64
|
"\"institutionCode\": \"Leicestershire and Rutland Environmental Records Centre\","+
|
65
|
"\"catalogNumber\": \"70875196\","+
|
66
|
"\"recordedBy\": \"DA Lott\","+
|
67
|
"\"locality\": \""+LOCALITY_STRING+"\","+
|
68
|
"\"collectionCode\": \"7472\","+
|
69
|
"\"identifiedBy\": \"DA Lott\"" +
|
70
|
"}" +
|
71
|
"]" +
|
72
|
"}";
|
73
|
|
74
|
private final String dummyJsonDataset = "[" +
|
75
|
"{"+
|
76
|
"\"key\": 29596,"+
|
77
|
"\"type\": \"BIOCASE\","+
|
78
|
"\"url\": \"http://www.flora-mv.de/biocase/pywrapper.cgi?dsa=hoeherePflanzen\","+
|
79
|
"\"createdBy\": \"crawler.gbif.org\","+
|
80
|
"\"modifiedBy\": \"crawler.gbif.org\","+
|
81
|
"\"created\": \"2014-01-13T10:32:05.170+0000\","+
|
82
|
"\"modified\": \"2014-01-13T10:32:05.170+0000\","+
|
83
|
"\"machineTags\": ["+
|
84
|
" {"+
|
85
|
" \"key\": 59486,"+
|
86
|
" \"namespace\": \"metasync.gbif.org\","+
|
87
|
" \"name\": \"conceptualSchema\","+
|
88
|
" \"value\": \"http://www.tdwg.org/schemas/abcd/2.06\","+
|
89
|
" \"createdBy\": \"crawler.gbif.org\","+
|
90
|
" \"created\": \"2014-01-13T10:32:05.172+0000\""+
|
91
|
" }"+
|
92
|
"]"+
|
93
|
"}"+
|
94
|
"]";
|
95
|
|
96
|
@Test
|
97
|
public void testJsonToCdmObject() throws URISyntaxException{
|
98
|
Collection<GbifResponse> records = GbifJsonOccurrenceParser.parseJsonRecords(dummyJsonRecords);
|
99
|
Assert.assertEquals("number of records found is incorrect", 1, records.size());
|
100
|
GbifResponse gbifResponse = records.iterator().next();
|
101
|
Assert.assertEquals("Locality is incorrect", LOCALITY_STRING, gbifResponse.getDerivedUnitFacade().getLocalityText());
|
102
|
Assert.assertEquals("protocol is wrong", GbifDataSetProtocol.BIOCASE, gbifResponse.getDataSetProtocol());
|
103
|
Assert.assertEquals("protocol is wrong", new URI("http://api.gbif.org/v1/dataset/26a49731-9457-45b2-9105-1b96063deb26/endpoint"), gbifResponse.getDataSetUri());
|
104
|
}
|
105
|
|
106
|
@Test
|
107
|
public void testJsonOriginalDataSetUriParsing(){
|
108
|
DataSetResponse response = GbifJsonOccurrenceParser.parseOriginalDataSetUri(dummyJsonDataset);
|
109
|
Assert.assertEquals("Response protocol is incorrect!", GbifDataSetProtocol.BIOCASE, response.getProtocol());
|
110
|
Assert.assertEquals("Response endpoint is incorrect!", URI.create("http://www.flora-mv.de/biocase/pywrapper.cgi?dsa=hoeherePflanzen"), response.getEndpoint());
|
111
|
}
|
112
|
|
113
|
@Test
|
114
|
public void testQueryParameterConversion(){
|
115
|
OccurenceQuery query = new OccurenceQuery("Campanula persicifolia", "T. Henning", "1234", "ACC-2", "BGBM", "DE", "pollen herbarium", new GregorianCalendar(2014, 05, 27), new GregorianCalendar(2014,05,28));
|
116
|
List<NameValuePair> queryParams = new GbifQueryGenerator().generateQueryParams(query);
|
117
|
NameValuePair pair = new BasicNameValuePair("scientificName", "Campanula persicifolia");
|
118
|
Assert.assertTrue("query parameter is missing", queryParams.contains(pair));
|
119
|
//FIXME this will currently always fail because eventDate is still not supported by GBIF
|
120
|
//"basisOfRecord" and "limit" is always set
|
121
|
// + 8 from query (collectorsNumber will be represented in the two parameters "recordNumber" and "fieldNumber";
|
122
|
// both dates are represented in one parameter "eventDate";
|
123
|
// locality can still not be queried on GBIF web service)
|
124
|
// assertEquals("Number of generated URI parameters is incorrect", 10, queryParams.size());
|
125
|
}
|
126
|
|
127
|
@Test
|
128
|
public void testGbifWebService() {
|
129
|
OccurenceQuery query = new OccurenceQuery("Campanula persicifolia", "E. J. Palmer", null, null, null, null, null, null, null);
|
130
|
GbifQueryServiceWrapper service = new GbifQueryServiceWrapper();
|
131
|
try {
|
132
|
Collection<GbifResponse> gbifResponse = service.query(query);
|
133
|
Assert.assertEquals("Usually this query retrieves at least two units. " +
|
134
|
"Test failure may also be due to GBIF!" +
|
135
|
"Check http://api.gbif.org/v1/occurrence/search?basisOfRecord=PRESERVED_SPECIMEN&limit=100&recordedBy=E.+J.+Palmer&scientificName=Campanula+persicifolia", 2, gbifResponse.size());
|
136
|
} catch (ClientProtocolException e) {
|
137
|
Assert.fail(e.getMessage());
|
138
|
} catch (IOException e) {
|
139
|
Assert.fail(e.getMessage());
|
140
|
} catch (URISyntaxException e) {
|
141
|
Assert.fail(e.getMessage());
|
142
|
}
|
143
|
|
144
|
}
|
145
|
|
146
|
}
|