3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.ext
.occurrence
.bioCase
;
12 import java
.io
.BufferedReader
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStream
;
15 import java
.io
.InputStreamReader
;
17 import java
.util
.HashSet
;
20 import junit
.framework
.TestCase
;
22 import org
.apache
.http
.client
.ClientProtocolException
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.junit
.Test
;
26 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
27 import eu
.etaxonomy
.cdm
.ext
.occurrence
.OccurenceQuery
;
34 public class BioCaseQueryServiceWrapperTest
extends TestCase
{
36 public static final Logger logger
= Logger
.getLogger(BioCaseQueryServiceWrapperTest
.class);
38 private static final int MAX_LINES_TO_READ
= 1000;
39 private static final int TIMEOUT
= 60000;
41 @Test(timeout
=TIMEOUT
)
42 public void testQuery() {
44 if(UriUtils
.isInternetAvailable(null)){
45 BioCaseQueryServiceWrapper queryService
= new BioCaseQueryServiceWrapper();
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"));
50 logger
.error("SKIPPING TEST: No response from BioCase provider");
53 BufferedReader reader
= new BufferedReader(new InputStreamReader(response
));
57 if(count
>MAX_LINES_TO_READ
){
58 fail("Service response did not include parameter to test.");
62 System
.out
.println(line
);
63 String recordAttr
= "recordCount=\"";
64 int index
= line
.indexOf(recordAttr
);
66 String recordCount
= line
.substring(index
+recordAttr
.length(), index
+recordAttr
.length()+1);
67 assertEquals("Incorrect number of occurrences", 2, Integer
.parseInt(recordCount
));
71 line
= reader
.readLine();
74 } catch (NumberFormatException e
) {
76 } catch (ClientProtocolException e
) {
78 } catch (IOException e
) {
82 logger
.error("SKIPPING TEST: no internet connectivity available");
87 @Test(timeout
=TIMEOUT
)
88 public void testQueryForUnitId(){
90 if(UriUtils
.isInternetAvailable(null)){
91 BioCaseQueryServiceWrapper service
= new BioCaseQueryServiceWrapper();
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"));
98 if(queryForSingleUnit
==null){
99 logger
.error("SKIPPING TEST: No response from BioCase provider");
102 BufferedReader reader
= new BufferedReader(new InputStreamReader(queryForSingleUnit
));
106 if(count
>MAX_LINES_TO_READ
){
107 fail("Service response did not include parameter to test.");
111 System
.out
.println(line
);
112 String recordAttr
= "recordCount=\"";
113 int index
= line
.indexOf(recordAttr
);
115 String recordCount
= line
.substring(index
+recordAttr
.length(), index
+recordAttr
.length()+1);
116 assertEquals("Incorrect number of occurrences", 1, Integer
.parseInt(recordCount
));
118 String unitId
= "<abcd:UnitID>";
119 int indexId
= line
.indexOf(unitId
);
121 String id
= line
.substring(indexId
+unitId
.length(), indexId
+unitId
.length()+5);
122 assertEquals("Incorrect UnitId", 29596, Integer
.parseInt(id
));
126 line
= reader
.readLine();
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");
139 reader
= new BufferedReader(new InputStreamReader(queryForSingleUnit
));
143 if(count
>MAX_LINES_TO_READ
){
144 fail("Service response did not include parameter to test.");
148 System
.out
.println(line
);
149 String recordAttr
= "recordCount=\"";
150 int index
= line
.indexOf(recordAttr
);
152 String recordCount
= line
.substring(index
+recordAttr
.length(), index
+recordAttr
.length()+1);
153 assertEquals("Incorrect number of occurrences", 2, Integer
.parseInt(recordCount
));
158 line
= reader
.readLine();
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());
169 logger
.error("SKIPPING TEST: no internet connectivity available");