Handle errors and do not hard-code count
[yaz4j-moved-to-github.git] / src / test / java / org / yaz4j / AsyncConnectionsTest.java
1 /*
2  * Copyright (c) 1995-2015, Index Data
3  * All rights reserved.
4  * See the file LICENSE for details.
5  */
6 package org.yaz4j;
7
8 import org.junit.After;
9 import org.junit.AfterClass;
10 import org.junit.Before;
11 import org.junit.BeforeClass;
12 import org.junit.Test;
13 import static org.junit.Assert.*;
14 import org.yaz4j.exception.ZoomException;
15
16 import static java.lang.System.out;
17
18 /**
19  *
20  * @author jakub
21  */
22 public class AsyncConnectionsTest {
23   
24   class Box<T> {
25     T item;
26
27     public Box() {
28     }
29
30     public Box(T item) {
31       this.item = item;
32     }
33     
34     T getItem() {
35       return item;
36     }
37     
38     void setItem(T item) {
39       this.item = item;
40     }
41   } 
42   
43   public AsyncConnectionsTest() {
44   }
45   
46   @BeforeClass
47   public static void setUpClass() {
48   }
49   
50   @AfterClass
51   public static void tearDownClass() {
52   }
53   
54   @Before
55   public void setUp() {
56   }
57   
58   @After
59   public void tearDown() {
60   }
61   
62   /**
63    * Test async ZOOM operation.
64    */
65   @Test
66   public void testBadDatabaseTarget() {
67     out.println("Trying bad async connection...");
68     AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/doesnotexist", 0);
69     conn.option("count", "100");
70     AsyncConnections conns = new AsyncConnections();
71     conns.add(conn);
72     final Box<Boolean> hadError = new Box<Boolean>(false);
73     try {
74       conn.setSyntax("sutrs");
75       conn.connect();
76       conn.search(new PrefixQuery("@attr 1=4 utah"));
77       conn
78         .onError(new AsyncConnection.ErrorHandler() {
79
80         public void handle(ZoomException e) {
81           out.println("There was an error "+e.getMessage());
82           hadError.setItem(true);
83         }
84       });
85       
86     } catch (ZoomException ex) {
87       fail(ex.getMessage());
88     }
89     conns.start();
90     assertTrue(hadError.item);
91   }
92   
93     /**
94    * Test async ZOOM operation.
95    */
96   @Test
97   public void testBadTarget() {
98     out.println("Trying bad target async connection...");
99     AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:70000/doesnotexist", 0);
100     conn.option("count", "100");
101     AsyncConnections conns = new AsyncConnections();
102     conns.add(conn);
103     final Box<Boolean> hadError = new Box<Boolean>(false);
104     try {
105       conn.setSyntax("sutrs");
106       conn.connect();
107       conn.search(new PrefixQuery("@attr 1=4 utah"));
108       conn
109         .onError(new AsyncConnection.ErrorHandler() {
110
111         public void handle(ZoomException e) {
112           out.println("There was an error "+e.getMessage());
113           hadError.setItem(true);
114         }
115       });
116       
117     } catch (ZoomException ex) {
118       fail(ex.getMessage());
119     }
120     conns.start();
121     assertTrue(hadError.item);
122   }
123
124   /**
125    * Test async ZOOM operation.
126    */
127   @Test
128   public void testSingleTarget() {
129     out.println("Trying async connection...");
130     AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/gils", 0);
131     conn.option("count", "100");
132     AsyncConnections conns = new AsyncConnections();
133     conns.add(conn);
134     int expectedHitCount = 9;
135     final Box<Long> actualHitCount = new Box<Long>();
136     final Box<Integer> actualRecordCounter =  new Box<Integer>(0);
137     try {
138       conn.setSyntax("sutrs");
139       conn.connect();
140       conn.search(new PrefixQuery("@attr 1=4 utah"));
141       conn
142         .onSearch(new AsyncConnection.SearchHandler() {
143         public void handle(ResultSet rs) {
144           out.println("Received search, hit count "+rs.getHitCount());
145           actualHitCount.setItem(rs.getHitCount());
146         }
147       })
148         .onRecord(new AsyncConnection.RecordHandler() {
149         public void handle(Record r) {
150           out.println("Received a record of type "+r.getSyntax());
151           actualRecordCounter.setItem(actualRecordCounter.getItem()+1);
152         }
153       });
154       
155     } catch (ZoomException ex) {
156       fail(ex.getMessage());
157     }
158     conns.start();
159     assertEquals(expectedHitCount, actualHitCount.item);
160     assertEquals(expectedHitCount, actualRecordCounter.item);
161     
162   }
163   
164   
165   /**
166    * Test async ZOOM operation.
167    */
168   @Test
169   public void testMulitTarget() {
170     out.println("Trying async with multile connections...");
171     AsyncConnections conns = new AsyncConnections();
172     AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/gils", 0);
173     conn.option("count", "100");
174     conns.add(conn);
175     AsyncConnection conn2 = new AsyncConnection("z3950.indexdata.dk:210/marc", 0);
176     conn2.option("count", "100");
177     conns.add(conn2);
178     int expectedHitCount = 19; //for both
179     final Box<Long> actualHitCount = new Box<Long>(0L);
180     final Box<Integer> actualRecordCounter =  new Box<Integer>(0);
181     try {
182       //we need to simplify the API for multiple
183       conn.setSyntax("sutrs");
184       conn.connect();
185       conn.search(new PrefixQuery("@attr 1=4 utah"));
186       conn
187         .onSearch(new AsyncConnection.SearchHandler() {
188         public void handle(ResultSet rs) {
189           out.println("Received search, hit count "+rs.getHitCount());
190           actualHitCount.setItem(actualHitCount.getItem() + rs.getHitCount());
191         }
192       })
193         .onRecord(new AsyncConnection.RecordHandler() {
194         public void handle(Record r) {
195           out.println("Received a record of type "+r.getSyntax());
196           actualRecordCounter.setItem(actualRecordCounter.getItem()+1);
197         }
198       });
199       conn2.setSyntax("marc21");
200       conn2.connect();
201       conn2.search(new PrefixQuery("@attr 1=4 computer"));
202       conn2
203         .onSearch(new AsyncConnection.SearchHandler() {
204         public void handle(ResultSet rs) {
205           out.println("Received search, hit count "+rs.getHitCount());
206           actualHitCount.setItem(actualHitCount.getItem() + rs.getHitCount());
207         }
208       })
209         .onRecord(new AsyncConnection.RecordHandler() {
210         public void handle(Record r) {
211           out.println("Received a record of type "+r.getSyntax());
212           actualRecordCounter.setItem(actualRecordCounter.getItem()+1);
213         }
214        })
215         .onError(new AsyncConnection.ErrorHandler() {
216
217         public void handle(ZoomException e) {
218           out.println("Caught error: "+e.getMessage());
219         }
220       });
221       
222     } catch (ZoomException ex) {
223       fail(ex.getMessage());
224     }
225     conns.start();
226     assertEquals(expectedHitCount, actualHitCount.item);
227     assertEquals(expectedHitCount, actualRecordCounter.item);
228     
229   }
230 }