Project

General

Profile

Download (7.18 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2014 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.ext.occurrence.gbif;
10

    
11
import java.io.IOException;
12
import java.net.URI;
13
import java.net.URISyntaxException;
14
import java.util.Collection;
15
import java.util.GregorianCalendar;
16
import java.util.List;
17

    
18
import org.apache.http.NameValuePair;
19
import org.apache.http.client.ClientProtocolException;
20
import org.apache.http.message.BasicNameValuePair;
21
import org.junit.Assert;
22
import org.junit.Ignore;
23
import org.junit.Test;
24

    
25
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
26

    
27
/**
28
 * @author pplitzner
29
 * @since 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), false);
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
    @Ignore
129
    public void testGbifWebService() {
130
        OccurenceQuery query = new OccurenceQuery("Campanula persicifolia", "E. J. Palmer", null, null, null, null, null, null, null, false);
131
        GbifQueryServiceWrapper service = new GbifQueryServiceWrapper();
132
        try {
133
            Collection<GbifResponse> gbifResponse = service.query(query);
134
            Assert.assertEquals("Usually this query retrieves at least two units. " +
135
            		"Test failure may also be due to GBIF!" +
136
            		"Check http://api.gbif.org/v1/occurrence/search?basisOfRecord=PRESERVED_SPECIMEN&limit=100&recordedBy=E.+J.+Palmer&scientificName=Campanula+persicifolia", 2, gbifResponse.size());
137
        } catch (ClientProtocolException e) {
138
            Assert.fail(e.getMessage());
139
        } catch (IOException e) {
140
            Assert.fail(e.getMessage());
141
        } catch (URISyntaxException e) {
142
            Assert.fail(e.getMessage());
143
        }
144

    
145
    }
146

    
147
    @Test
148
    public void testGbifWebServiceNoParameters() {
149
    	OccurenceQuery query = new OccurenceQuery(null, null, null, null, null, null, null, null, null, false);
150
    	GbifQueryServiceWrapper service = new GbifQueryServiceWrapper();
151
    	try {
152
    		service.query(query);
153
    	} catch (ClientProtocolException e) {
154
    		Assert.fail(e.getMessage());
155
    	} catch (IOException e) {
156
    		Assert.fail(e.getMessage());
157
    	} catch (URISyntaxException e) {
158
    		Assert.fail(e.getMessage());
159
    	}
160

    
161
    }
162

    
163
}
    (1-1/1)