Project

General

Profile

Download (6.63 KB) Statistics
| Branch: | Tag: | Revision:
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.Ignore;
23
import org.junit.Test;
24

    
25
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
26
import junit.framework.TestCase;
27

    
28
/**
29
 * @author pplitzner
30
 * @date 20.05.2014
31
 *
32
 */
33
public class GbifQueryServiceWrapperTest extends TestCase{
34

    
35
    private static final String LOCALITY_STRING = "Saddington Reservoir, Saddington Reservoir";
36

    
37
    private final String dummyJsonRecords = "{" +
38
    		"\"results\":" +
39
    		    "[" +
40
        		    "{" +
41
            		"\"key\": 252408386," +
42
            		"\"datasetKey\": \"26a49731-9457-45b2-9105-1b96063deb26\"," +
43
            		"\"publishingOrgKey\": \"07f617d0-c688-11d8-bf62-b8a03c50a862\"," +
44
            		"\"publishingCountry\": \"GB\"," +
45
            		"\"protocol\": \"BIOCASE\"," +
46
            		"\"lastCrawled\": \"2013-09-07T07:08:17.000+0000\"," +
47
                    "\"identifiers\": [ ],"+
48
                    "\"media\": [ ],"+
49
                    "\"facts\": [ ],"+
50
                    "\"relations\": [ ],"+
51
                    "\"basisOfRecord\": \"OBSERVATION\","+
52
                    "\"decimalLongitude\": -1.0193,"+
53
                    "\"decimalLatitude\": 52.51021,"+
54
                    "\"continent\": \"EUROPE\","+
55
                    "\"year\": 2006,"+
56
                    "\"month\": 6,"+
57
                    "\"day\": 30,"+
58
                    "\"eventDate\": \"2006-06-29T22:00:00.000+0000\","+
59
                    "\"issues\": [ ],"+
60
                    "\"lastInterpreted\": \"2014-04-01T23:31:15.462+0000\","+
61
                    "\"geodeticDatum\": \"WGS84\","+
62
                    "\"countryCode\": \"GB\","+
63
                    "\"country\": \"United Kingdom\","+
64
                    "\"gbifID\": \"252408386\","+
65
                    "\"institutionCode\": \"Leicestershire and Rutland Environmental Records Centre\","+
66
                    "\"catalogNumber\": \"70875196\","+
67
                    "\"recordedBy\": \"DA Lott\","+
68
                    "\"locality\": \""+LOCALITY_STRING+"\","+
69
                    "\"collectionCode\": \"7472\","+
70
                    "\"identifiedBy\": \"DA Lott\"" +
71
                    "}" +
72
                "]" +
73
            "}";
74

    
75
    private final String dummyJsonDataset = "[" +
76
    		"{"+
77
        "\"key\": 29596,"+
78
        "\"type\": \"BIOCASE\","+
79
        "\"url\": \"http://www.flora-mv.de/biocase/pywrapper.cgi?dsa=hoeherePflanzen\","+
80
        "\"createdBy\": \"crawler.gbif.org\","+
81
        "\"modifiedBy\": \"crawler.gbif.org\","+
82
        "\"created\": \"2014-01-13T10:32:05.170+0000\","+
83
        "\"modified\": \"2014-01-13T10:32:05.170+0000\","+
84
        "\"machineTags\": ["+
85
         "   {"+
86
         "       \"key\": 59486,"+
87
         "      \"namespace\": \"metasync.gbif.org\","+
88
         "       \"name\": \"conceptualSchema\","+
89
         "       \"value\": \"http://www.tdwg.org/schemas/abcd/2.06\","+
90
         "       \"createdBy\": \"crawler.gbif.org\","+
91
         "       \"created\": \"2014-01-13T10:32:05.172+0000\""+
92
         "   }"+
93
        "]"+
94
    "}"+
95
"]";
96

    
97
    @Test
98
    public void testJsonToCdmObject() throws URISyntaxException{
99
        Collection<GbifResponse> records = GbifJsonOccurrenceParser.parseJsonRecords(dummyJsonRecords);
100
        assertEquals("number of records found is incorrect", 1, records.size());
101
        GbifResponse gbifResponse = records.iterator().next();
102
        assertEquals("Locality is incorrect", LOCALITY_STRING, gbifResponse.getDerivedUnitFacade().getLocalityText());
103
        assertEquals("protocol is wrong", GbifDataSetProtocol.BIOCASE, gbifResponse.getDataSetProtocol());
104
        assertEquals("protocol is wrong", new URI("http://api.gbif.org/v1/dataset/26a49731-9457-45b2-9105-1b96063deb26/endpoint"), gbifResponse.getDataSetUri());
105
    }
106

    
107
    @Test
108
    public void testJsonOriginalDataSetUriParsing(){
109
        DataSetResponse response = GbifJsonOccurrenceParser.parseOriginalDataSetUri(dummyJsonDataset);
110
        assertEquals("Response protocol is incorrect!", GbifDataSetProtocol.BIOCASE, response.getProtocol());
111
        assertEquals("Response endpoint is incorrect!", URI.create("http://www.flora-mv.de/biocase/pywrapper.cgi?dsa=hoeherePflanzen"), response.getEndpoint());
112
    }
113

    
114
    @Test
115
    public void testQueryParameterConversion(){
116
        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));
117
        List<NameValuePair> queryParams = new GbifQueryGenerator().generateQueryParams(query);
118
        NameValuePair pair = new BasicNameValuePair("scientificName", "Campanula persicifolia");
119
        assertTrue("query parameter is missing", queryParams.contains(pair));
120
        //FIXME this will currently always fail because eventDate is still not supported by GBIF
121
        //"basisOfRecord" and "limit" is always set
122
        // + 8 from query (collectorsNumber will be represented in the two parameters "recordNumber" and "fieldNumber";
123
        // both dates are represented in one parameter "eventDate";
124
        // locality can still not be queried on GBIF web service)
125
//        assertEquals("Number of generated URI parameters is incorrect", 10, queryParams.size());
126
    }
127

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

    
146
    }
147

    
148
}
    (1-1/1)