Project

General

Profile

Download (6.64 KB) Statistics
| Branch: | Tag: | Revision:
1 3e66f0a7 Patric Plitzner
// $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 d692f2bf Patric Plitzner
import java.io.IOException;
13 a451f77f Patric Plitzner
import java.net.URI;
14 d692f2bf Patric Plitzner
import java.net.URISyntaxException;
15 723b34e5 Patric Plitzner
import java.util.Collection;
16 d692f2bf Patric Plitzner
import java.util.GregorianCalendar;
17
import java.util.List;
18 723b34e5 Patric Plitzner
19 d692f2bf Patric Plitzner
import org.apache.http.NameValuePair;
20
import org.apache.http.client.ClientProtocolException;
21
import org.apache.http.message.BasicNameValuePair;
22 5c6cfa03 Andreas Müller
import org.junit.Assert;
23 3e66f0a7 Patric Plitzner
import org.junit.Test;
24
25 d692f2bf Patric Plitzner
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
26 723b34e5 Patric Plitzner
27 3e66f0a7 Patric Plitzner
/**
28
 * @author pplitzner
29
 * @date 20.05.2014
30
 *
31
 */
32 5c6cfa03 Andreas Müller
public class GbifQueryServiceWrapperTest {
33 3e66f0a7 Patric Plitzner
34 723b34e5 Patric Plitzner
    private static final String LOCALITY_STRING = "Saddington Reservoir, Saddington Reservoir";
35
36 a451f77f Patric Plitzner
    private final String dummyJsonRecords = "{" +
37 723b34e5 Patric Plitzner
    		"\"results\":" +
38
    		    "[" +
39
        		    "{" +
40
            		"\"key\": 252408386," +
41
            		"\"datasetKey\": \"26a49731-9457-45b2-9105-1b96063deb26\"," +
42
            		"\"publishingOrgKey\": \"07f617d0-c688-11d8-bf62-b8a03c50a862\"," +
43
            		"\"publishingCountry\": \"GB\"," +
44 26489b6c Patric Plitzner
            		"\"protocol\": \"BIOCASE\"," +
45 723b34e5 Patric Plitzner
            		"\"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 3e66f0a7 Patric Plitzner
74 a451f77f Patric Plitzner
    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 3e66f0a7 Patric Plitzner
    @Test
97 26489b6c Patric Plitzner
    public void testJsonToCdmObject() throws URISyntaxException{
98 a451f77f Patric Plitzner
        Collection<GbifResponse> records = GbifJsonOccurrenceParser.parseJsonRecords(dummyJsonRecords);
99 5c6cfa03 Andreas Müller
        Assert.assertEquals("number of records found is incorrect", 1, records.size());
100 03b1779f Patric Plitzner
        GbifResponse gbifResponse = records.iterator().next();
101 5c6cfa03 Andreas Müller
        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 3e66f0a7 Patric Plitzner
    }
105
106 a451f77f Patric Plitzner
    @Test
107
    public void testJsonOriginalDataSetUriParsing(){
108
        DataSetResponse response = GbifJsonOccurrenceParser.parseOriginalDataSetUri(dummyJsonDataset);
109 5c6cfa03 Andreas Müller
        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 a451f77f Patric Plitzner
    }
112
113 d692f2bf Patric Plitzner
    @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 5c6cfa03 Andreas Müller
        Assert.assertTrue("query parameter is missing", queryParams.contains(pair));
119 6f32cdc9 Patric Plitzner
        //FIXME this will currently always fail because eventDate is still not supported by GBIF
120 d692f2bf Patric Plitzner
        //"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 63f16b17 Patric Plitzner
//        assertEquals("Number of generated URI parameters is incorrect", 10, queryParams.size());
125 d692f2bf Patric Plitzner
    }
126
127 a451f77f Patric Plitzner
    @Test
128 d692f2bf Patric Plitzner
    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 03b1779f Patric Plitzner
            Collection<GbifResponse> gbifResponse = service.query(query);
133 5c6cfa03 Andreas Müller
            Assert.assertEquals("Usually this query retrieves at least two units. " +
134 6f32cdc9 Patric Plitzner
            		"Test failure may also be due to GBIF!" +
135 0f5ddf4b Patrick Plitzner
            		"Check http://api.gbif.org/v1/occurrence/search?basisOfRecord=PRESERVED_SPECIMEN&limit=100&recordedBy=E.+J.+Palmer&scientificName=Campanula+persicifolia", 2, gbifResponse.size());
136 d692f2bf Patric Plitzner
        } catch (ClientProtocolException e) {
137 5c6cfa03 Andreas Müller
            Assert.fail(e.getMessage());
138 d692f2bf Patric Plitzner
        } catch (IOException e) {
139 5c6cfa03 Andreas Müller
            Assert.fail(e.getMessage());
140 d692f2bf Patric Plitzner
        } catch (URISyntaxException e) {
141 5c6cfa03 Andreas Müller
            Assert.fail(e.getMessage());
142 d692f2bf Patric Plitzner
        }
143
144
    }
145
146 3e66f0a7 Patric Plitzner
}