Add initial async support (Search, Record)
[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 testSingleTarget() {
67     out.println("Trying async connection...");
68     AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/gils", 0);
69     AsyncConnections conns = new AsyncConnections();
70     conns.add(conn);
71     int expectedHitCount = 9;
72     final Box<Long> actualHitCount = new Box<Long>();
73     final Box<Integer> actualRecordCounter =  new Box<Integer>(0);
74     try {
75       conn.setSyntax("sutrs");
76       conn.connect();
77       conn.search(new PrefixQuery("@attr 1=4 utah"));
78       conn
79         .onSearch(new AsyncConnection.SearchHandler() {
80         public void handle(ResultSet rs) {
81           out.println("Received search, hit count "+rs.getHitCount());
82           actualHitCount.setItem(rs.getHitCount());
83         }
84       })
85         .onRecord(new AsyncConnection.RecordHandler() {
86         public void handle(Record r) {
87           out.println("Received a record of type "+r.getSyntax());
88           actualRecordCounter.setItem(actualRecordCounter.getItem()+1);
89         }
90       });
91       
92     } catch (ZoomException ex) {
93       fail(ex.getMessage());
94     }
95     conns.start();
96     assertEquals(expectedHitCount, actualHitCount.item);
97     assertEquals(expectedHitCount, actualRecordCounter.item);
98     
99   }
100   
101   
102   /**
103    * Test async ZOOM operation.
104    */
105   @Test
106   public void testMulitTarget() {
107     out.println("Trying async with multile connections...");
108     AsyncConnections conns = new AsyncConnections();
109     AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/gils", 0);
110     conns.add(conn);
111     AsyncConnection conn2 = new AsyncConnection("z3950.indexdata.dk:210/marc", 0);
112     conns.add(conn2);
113     int expectedHitCount = 19; //for both
114     final Box<Long> actualHitCount = new Box<Long>(0L);
115     final Box<Integer> actualRecordCounter =  new Box<Integer>(0);
116     try {
117       //we need to simplify the API for multiple
118       conn.setSyntax("sutrs");
119       conn.connect();
120       conn.search(new PrefixQuery("@attr 1=4 utah"));
121       conn
122         .onSearch(new AsyncConnection.SearchHandler() {
123         public void handle(ResultSet rs) {
124           out.println("Received search, hit count "+rs.getHitCount());
125           actualHitCount.setItem(actualHitCount.getItem() + rs.getHitCount());
126         }
127       })
128         .onRecord(new AsyncConnection.RecordHandler() {
129         public void handle(Record r) {
130           out.println("Received a record of type "+r.getSyntax());
131           actualRecordCounter.setItem(actualRecordCounter.getItem()+1);
132         }
133       });
134       conn2.setSyntax("marc21");
135       conn2.connect();
136       conn2.search(new PrefixQuery("@attr 1=4 computer"));
137       conn2
138         .onSearch(new AsyncConnection.SearchHandler() {
139         public void handle(ResultSet rs) {
140           out.println("Received search, hit count "+rs.getHitCount());
141           actualHitCount.setItem(actualHitCount.getItem() + rs.getHitCount());
142         }
143       })
144         .onRecord(new AsyncConnection.RecordHandler() {
145         public void handle(Record r) {
146           out.println("Received a record of type "+r.getSyntax());
147           actualRecordCounter.setItem(actualRecordCounter.getItem()+1);
148         }
149       })
150         .onError(new AsyncConnection.ErrorHandler() {
151
152         public void handle(ZoomException e) {
153           out.println("Caught error: "+e.getMessage());
154         }
155       });
156       
157     } catch (ZoomException ex) {
158       fail(ex.getMessage());
159     }
160     conns.start();
161     assertEquals(expectedHitCount, actualHitCount.item);
162     assertEquals(expectedHitCount, actualRecordCounter.item);
163     
164   }
165 }