Project

General

Profile

Revision cb3409ef

IDcb3409ef6a89cbd28ba0548a1545c2469dfcb543
Parent abb1217f
Child 01538dbf, d4657a24

Added by Patrick Plitzner about 7 years ago

  • added timeout and null check

View differences:

cdmlib-ext/src/test/java/eu/etaxonomy/cdm/ext/occurrence/bioCase/BioCaseQueryServiceWrapperTest.java
1 1
// $Id$
2 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
*/
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 10
package eu.etaxonomy.cdm.ext.occurrence.bioCase;
11 11

  
12 12
import java.io.BufferedReader;
......
14 14
import java.io.InputStream;
15 15
import java.io.InputStreamReader;
16 16
import java.net.URI;
17
import java.net.URISyntaxException;
18 17

  
19 18
import junit.framework.TestCase;
20 19

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

  
......
35 33

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

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

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

  
43 42
        if(UriUtils.isInternetAvailable(null)){
......
45 44
            try {
46 45
                OccurenceQuery query = new OccurenceQuery("Campanula patula*", "L. Adamovic", null, null, null, null, null, null, null);
47 46
                InputStream response = queryService.query(query, URI.create("http://ww3.bgbm.org/biocase/pywrapper.cgi?dsa=Herbar"));
47
                if(response==null){
48
                    logger.error("SKIPPING TEST: No response from BioCase provider");
49
                    return;
50
                }
48 51
                BufferedReader reader = new BufferedReader(new InputStreamReader(response));
49 52
                String line = null;
50 53
                int count = 0;
......
74 77
                fail(e.getMessage());
75 78
            }
76 79
        } else {
77
            logger.warn("SKIPPING TEST: no internet connectivity available");
80
            logger.error("SKIPPING TEST: no internet connectivity available");
81
            return;
78 82
        }
79 83
    }
80 84

  
81
    @Test
85
    @Test(timeout=TIMEOUT)
82 86
    public void testQueryForUnitId(){
83
        BioCaseQueryServiceWrapper service = new BioCaseQueryServiceWrapper();
84
        try {
85
            InputStream queryForSingleUnit = service.query(new OccurenceQuery("29596"), new URIBuilder("http://www.flora-mv.de/biocase/pywrapper.cgi?dsa=hoeherePflanzen").build());
86
            BufferedReader reader = new BufferedReader(new InputStreamReader(queryForSingleUnit));
87
            String line = null;
88
            int count = 0;
89
            do {
90
                if(count>MAX_LINES_TO_READ){
91
                    fail("Service response did not include parameter to test.");
92
                    break;
87

  
88
        if(UriUtils.isInternetAvailable(null)){
89
            BioCaseQueryServiceWrapper service = new BioCaseQueryServiceWrapper();
90
            try {
91
                InputStream queryForSingleUnit = service.query(new OccurenceQuery("29596"), URI.create("http://www.flora-mv.de/biocase/pywrapper.cgi?dsa=hoeherePflanzen"));
92
                if(queryForSingleUnit==null){
93
                    logger.error("SKIPPING TEST: No response from BioCase provider");
94
                    return;
93 95
                }
94
                if(line!=null){
95
                    System.out.println(line);
96
                    String recordAttr = "recordCount=\"";
97
                    int index = line.indexOf(recordAttr);
98
                    if(index>-1){
99
                        String recordCount = line.substring(index+recordAttr.length(), index+recordAttr.length()+1);
100
                        assertEquals("Incorrect number of occurrences", 1, Integer.parseInt(recordCount));
96
                BufferedReader reader = new BufferedReader(new InputStreamReader(queryForSingleUnit));
97
                String line = null;
98
                int count = 0;
99
                do {
100
                    if(count>MAX_LINES_TO_READ){
101
                        fail("Service response did not include parameter to test.");
102
                        break;
101 103
                    }
102
                    String unitId = "<abcd:UnitID>";
103
                    int indexId = line.indexOf(unitId);
104
                    if(indexId>-1){
105
                        String id = line.substring(indexId+unitId.length(), indexId+unitId.length()+5);
106
                        assertEquals("Incorrect UnitId", 29596, Integer.parseInt(id));
104
                    if(line!=null){
105
                        System.out.println(line);
106
                        String recordAttr = "recordCount=\"";
107
                        int index = line.indexOf(recordAttr);
108
                        if(index>-1){
109
                            String recordCount = line.substring(index+recordAttr.length(), index+recordAttr.length()+1);
110
                            assertEquals("Incorrect number of occurrences", 1, Integer.parseInt(recordCount));
111
                        }
112
                        String unitId = "<abcd:UnitID>";
113
                        int indexId = line.indexOf(unitId);
114
                        if(indexId>-1){
115
                            String id = line.substring(indexId+unitId.length(), indexId+unitId.length()+5);
116
                            assertEquals("Incorrect UnitId", 29596, Integer.parseInt(id));
117
                            break;
118
                        }
107 119
                    }
108
                }
109
                line = reader.readLine();
110
                count++;
111
            } while (line!=null);
112
        } catch (NumberFormatException e) {
113
            fail(e.getMessage());
114
        } catch (ClientProtocolException e) {
115
            fail(e.getMessage());
116
        } catch (IOException e) {
117
            fail(e.getMessage());
118
        }catch (URISyntaxException e) {
119
            fail(e.getMessage());
120
                    line = reader.readLine();
121
                    count++;
122
                } while (line!=null);
123
            } catch (NumberFormatException e) {
124
                fail(e.getMessage());
125
            } catch (ClientProtocolException e) {
126
                fail(e.getMessage());
127
            } catch (IOException e) {
128
                fail(e.getMessage());
129
            }
130
        } else {
131
            logger.error("SKIPPING TEST: no internet connectivity available");
132
            return;
120 133
        }
121 134
    }
122 135
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)