1151c6a5b68daa9f5c01bfb84b1297b680f71269
[cdmlib.git] / cdmlib-ext / src / test / java / eu / etaxonomy / cdm / ext / occurrence / bioCase / BioCaseQueryServiceWrapperTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 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.bioCase;
11
12 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.InputStreamReader;
16 import java.net.URI;
17 import java.util.HashSet;
18 import java.util.Set;
19
20 import junit.framework.TestCase;
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.UriUtils;
27 import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
28
29 /**
30 * @author pplitzner
31 * @date 16.09.2013
32 *
33 */
34 public class BioCaseQueryServiceWrapperTest extends TestCase{
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);
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 do {
57 if(count>MAX_LINES_TO_READ){
58 fail("Service response did not include parameter to test.");
59 break;
60 }
61 if(line!=null){
62 System.out.println(line);
63 String recordAttr = "recordCount=\"";
64 int index = line.indexOf(recordAttr);
65 if(index>-1){
66 String recordCount = line.substring(index+recordAttr.length(), index+recordAttr.length()+1);
67 assertEquals("Incorrect number of occurrences", 2, Integer.parseInt(recordCount));
68 break;
69 }
70 }
71 line = reader.readLine();
72 count++;
73 } while (line!=null);
74 } catch (NumberFormatException e) {
75 fail(e.getMessage());
76 } catch (ClientProtocolException e) {
77 fail(e.getMessage());
78 } catch (IOException e) {
79 fail(e.getMessage());
80 }
81 } else {
82 logger.error("SKIPPING TEST: no internet connectivity available");
83 return;
84 }
85 }
86
87 @Test(timeout=TIMEOUT)
88 public void testQueryForUnitId(){
89
90 if(UriUtils.isInternetAvailable(null)){
91 BioCaseQueryServiceWrapper service = new BioCaseQueryServiceWrapper();
92 try {
93 Set<String[]> unitIds = new HashSet<String[]>();
94 String[] unitIdArray ={"29596"};
95 unitIds.add(unitIdArray);
96 InputStream queryForSingleUnit = service.query(new OccurenceQuery(unitIds), URI.create("http://www.flora-mv.de/biocase/pywrapper.cgi?dsa=hoeherePflanzen"));
97
98 if(queryForSingleUnit==null){
99 logger.error("SKIPPING TEST: No response from BioCase provider");
100 return;
101 }
102 BufferedReader reader = new BufferedReader(new InputStreamReader(queryForSingleUnit));
103 String line = null;
104 int count = 0;
105 do {
106 if(count>MAX_LINES_TO_READ){
107 fail("Service response did not include parameter to test.");
108 break;
109 }
110 if(line!=null){
111 System.out.println(line);
112 String recordAttr = "recordCount=\"";
113 int index = line.indexOf(recordAttr);
114 if(index>-1){
115 String recordCount = line.substring(index+recordAttr.length(), index+recordAttr.length()+1);
116 assertEquals("Incorrect number of occurrences", 1, Integer.parseInt(recordCount));
117 }
118 String unitId = "<abcd:UnitID>";
119 int indexId = line.indexOf(unitId);
120 if(indexId>-1){
121 String id = line.substring(indexId+unitId.length(), indexId+unitId.length()+5);
122 assertEquals("Incorrect UnitId", 29596, Integer.parseInt(id));
123 break;
124 }
125 }
126 line = reader.readLine();
127 count++;
128 } while (line!=null);
129 unitIds = new HashSet<String[]>();
130 String[] unitIdsArray = {"B -W 16385 -00 0"};
131 unitIds.add(unitIdsArray);
132 String[] unitIdsArray2 ={"B 10 0641985"};
133 unitIds.add(unitIdsArray2);
134 queryForSingleUnit = service.query(new OccurenceQuery(unitIds), URI.create("http://ww3.bgbm.org/biocase/pywrapper.cgi?dsa=Herbar"));
135 if(queryForSingleUnit==null){
136 logger.error("SKIPPING TEST: No response from BioCase provider");
137 return;
138 }
139 reader = new BufferedReader(new InputStreamReader(queryForSingleUnit));
140 line = null;
141 count = 0;
142 do {
143 if(count>MAX_LINES_TO_READ){
144 fail("Service response did not include parameter to test.");
145 break;
146 }
147 if(line!=null){
148 System.out.println(line);
149 String recordAttr = "recordCount=\"";
150 int index = line.indexOf(recordAttr);
151 if(index>-1){
152 String recordCount = line.substring(index+recordAttr.length(), index+recordAttr.length()+1);
153 assertEquals("Incorrect number of occurrences", 2, Integer.parseInt(recordCount));
154 }
155
156
157 }
158 line = reader.readLine();
159 count++;
160 } while (line!=null);
161 } catch (NumberFormatException e) {
162 fail(e.getMessage());
163 } catch (ClientProtocolException e) {
164 fail(e.getMessage());
165 } catch (IOException e) {
166 fail(e.getMessage());
167 }
168 } else {
169 logger.error("SKIPPING TEST: no internet connectivity available");
170 return;
171 }
172 }
173 }