Project

General

Profile

Download (7.48 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.logging.log4j.LogManager;
24
import org.apache.logging.log4j.Logger;
25
import org.junit.Test;
26

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

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

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

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

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

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

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

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

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

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

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

    
180

    
181
}
    (1-1/1)