Project

General

Profile

Download (7.42 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2013 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.bioCase;
10

    
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertTrue;
13
import static org.junit.Assert.fail;
14

    
15
import java.io.BufferedReader;
16
import java.io.IOException;
17
import java.io.InputStream;
18
import java.io.InputStreamReader;
19
import java.util.HashSet;
20
import java.util.Set;
21

    
22
import org.apache.http.client.ClientProtocolException;
23
import org.apache.log4j.Logger;
24
import org.junit.Test;
25

    
26
import eu.etaxonomy.cdm.common.URI;
27
import eu.etaxonomy.cdm.common.UriUtils;
28
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
29

    
30
/**
31
 * @author pplitzner
32
 * @since 16.09.2013
33
 */
34
public class BioCaseQueryServiceWrapperTest {
35

    
36
    public static final Logger logger = Logger.getLogger(BioCaseQueryServiceWrapperTest.class);
37

    
38
    private static final int MAX_LINES_TO_READ = 1000;
39
    private static final int TIMEOUT = 60000;
40

    
41
    @Test(timeout=TIMEOUT)
42
    public void testQuery() {
43

    
44
        if(UriUtils.isInternetAvailable(null)){
45
            BioCaseQueryServiceWrapper queryService = new BioCaseQueryServiceWrapper();
46
            try {
47
                OccurenceQuery query = new OccurenceQuery("Campanula patula*", null, null, null, null, null, null, null, null, false);
48
                InputStream response = queryService.query(query, URI.create("http://ww3.bgbm.org/biocase/pywrapper.cgi?dsa=Herbar"));
49
                if(response==null){
50
                    logger.error("SKIPPING TEST: No response from BioCase provider");
51
                    return;
52
                }
53
                BufferedReader reader = new BufferedReader(new InputStreamReader(response));
54
                String line = null;
55
                int count = 0;
56
                boolean isCorrectFormat = false;
57
                do {
58
                    if(count>MAX_LINES_TO_READ){
59
                        fail("Service response did not include parameter to test.");
60
                        break;
61
                    }
62
                    if(line!=null){
63
                        if(logger.isTraceEnabled()){
64
                            System.out.println(line);
65
                        }
66
                        //just check for recordCount attribute to see if a valid response was returned
67
                        String recordAttr = "recordCount=\"";
68
                        int index = line.indexOf(recordAttr);
69
                        if(index>-1){
70
                            isCorrectFormat = true;
71
                            break;
72
                        }
73
                    }
74
                    line = reader.readLine();
75
                    count++;
76
                } while (line!=null);
77
                assertTrue("BioCase response did not have the expected format", isCorrectFormat);
78
            } catch (NumberFormatException e) {
79
                fail(e.getMessage());
80
            } catch (ClientProtocolException e) {
81
                fail(e.getMessage());
82
            } catch (IOException e) {
83
                fail(e.getMessage());
84
            }
85
        } else {
86
            logger.error("SKIPPING TEST: no internet connectivity available");
87
            return;
88
        }
89
    }
90

    
91
    @Test//(timeout=TIMEOUT)
92
    public void testQueryForUnitId(){
93

    
94
        if(UriUtils.isInternetAvailable(null)){
95
            BioCaseQueryServiceWrapper service = new BioCaseQueryServiceWrapper();
96
            try {
97

    
98
                Set<String[]> unitIds = new HashSet<>();
99
                String[] unitIdArray ={"B 10 0463639"};
100

    
101
                unitIds.add(unitIdArray);
102
                InputStream queryForSingleUnit = service.query(new OccurenceQuery(unitIds), URI.create("https://ww3.bgbm.org/biocase/pywrapper.cgi?dsa=Herbar"));
103

    
104
                if(queryForSingleUnit==null){
105
                    logger.error("SKIPPING TEST: No response from BioCase provider");
106
                    return;
107
                }
108
                BufferedReader reader = new BufferedReader(new InputStreamReader(queryForSingleUnit));
109
                String line = null;
110
                int count = 0;
111
                do {
112
                    if(count>MAX_LINES_TO_READ){
113
                        fail("Service response did not include parameter to test.");
114
                        break;
115
                    }
116
                    if(line!=null){
117
                        if(logger.isTraceEnabled()){
118
                            System.out.println(line);
119
                        }
120
                        String recordAttr = "recordCount=\"";
121
                        int index = line.indexOf(recordAttr);
122
                        if(index>-1){
123
                            String recordCount = line.substring(index+recordAttr.length(), index+recordAttr.length()+1);
124
                            assertEquals("Incorrect number of occurrences", 1, Integer.parseInt(recordCount.trim()));
125
                        }
126
                        String unitId = "<abcd:UnitID>";
127
                        int indexId = line.indexOf(unitId);
128
                        if(indexId>-1){
129
                            @SuppressWarnings("unused")
130
                            String id = line.substring(indexId+unitId.length(), indexId+unitId.length()+5);
131
                           // assertEquals("Incorrect UnitId", 29596, Integer.parseInt(id));
132
                           // break;
133
                        }
134
                    }
135
                    line = reader.readLine();
136
                    count++;
137
                } while (line!=null);
138
                unitIds = new HashSet<>();
139
                String[] unitIdsArray = {"B -W 16385 -01 0"};
140
                unitIds.add(unitIdsArray);
141
                String[] unitIdsArray2 ={"B 10 0641985"};
142
                unitIds.add(unitIdsArray2);
143
                queryForSingleUnit = service.query(new OccurenceQuery(unitIds), URI.create("http://ww3.bgbm.org/biocase/pywrapper.cgi?dsa=Herbar"));
144
                if(queryForSingleUnit==null){
145
                    logger.error("SKIPPING TEST: No response from BioCase provider");
146
                    return;
147
                }
148
                reader = new BufferedReader(new InputStreamReader(queryForSingleUnit));
149
                line = null;
150
                count = 0;
151
                do {
152
                    if(count>MAX_LINES_TO_READ){
153
                        fail("Service response did not include parameter to test.");
154
                        break;
155
                    }
156
                    if(line!=null){
157
                        if(logger.isTraceEnabled()){
158
                            System.out.println(line);
159
                        }
160
                        String recordAttr = "recordCount=\"";
161
                        int index = line.indexOf(recordAttr);
162
                        if(index>-1){
163
                            String recordCount = line.substring(index+recordAttr.length(), index+recordAttr.length()+1);
164
                            assertEquals("Incorrect number of occurrences", 2, Integer.parseInt(recordCount));
165
                        }
166
                    }
167
                    line = reader.readLine();
168
                    count++;
169
                } while (line!=null);
170
            } catch (NumberFormatException | IOException e) {
171
                fail(e.getMessage());
172
            }
173
        } else {
174
            logger.error("SKIPPING TEST: no internet connectivity available");
175
            return;
176
        }
177
    }
178

    
179

    
180
}
    (1-1/1)