]> git.evergreen-ils.org Git - working/Hatch.git/blob - src/org/evergreen_ils/hatch/TestHatch.java
Native Messaging WIP - docs, tweaks
[working/Hatch.git] / src / org / evergreen_ils / hatch / TestHatch.java
1 /* -----------------------------------------------------------------------
2  * Copyright 2016 King County Library System
3  * Bill Erickson <berickxx@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * -----------------------------------------------------------------------
15  */
16 package org.evergreen_ils.hatch;
17
18 import java.util.logging.Logger;
19 import org.json.*;
20
21 public class TestHatch {
22     static MessageIO io;
23     static final Logger logger = Hatch.getLogger();
24
25     public static void pause() {
26         try {
27             Thread.sleep(1000);
28         } catch (InterruptedException e) {}
29     }
30
31     public static void doSends() {
32         int msgid = 1;
33         int clientid = 1;
34
35         // initialize connection to set origin info
36         JSONObject obj = new JSONObject();
37         obj.put("msgid", msgid++);
38         obj.put("clientid", clientid);
39         obj.put("action", "init");
40         obj.put("origin", "https://test.hatch.evergreen-ils.org");
41         io.sendMessage(obj);
42
43         pause();
44
45         // get a list of stored keys
46         obj = new JSONObject();
47         obj.put("msgid", msgid++);
48         obj.put("clientid", clientid);
49         obj.put("action", "keys");
50         io.sendMessage(obj);
51
52         pause();
53
54         // store a string
55         obj = new JSONObject();
56         obj.put("msgid", msgid++);
57         obj.put("clientid", clientid);
58         obj.put("action", "set");
59         obj.put("key", "eg.hatch.test.key1");
60         obj.put("content", "Rando content, now with cheese");
61         io.sendMessage(obj);
62
63         pause();
64
65         // store a value
66         obj = new JSONObject();
67         obj.put("msgid", msgid++);
68         obj.put("clientid", clientid);
69         obj.put("action", "get");
70         obj.put("key", "eg.hatch.test.key1");
71         io.sendMessage(obj);
72
73         // store an array
74         obj = new JSONObject();
75         obj.put("msgid", msgid++);
76         obj.put("clientid", clientid);
77         obj.put("action", "set");
78         obj.put("key", "eg.hatch.test.key2");
79         JSONArray arr = new JSONArray();
80         arr.put(123);
81         arr.put("23 Skidoo");
82         obj.put("content", arr);
83         io.sendMessage(obj);
84
85         pause();
86
87         obj = new JSONObject();
88         obj.put("msgid", msgid++);
89         obj.put("clientid", clientid);
90         obj.put("action", "get");
91         obj.put("key", "eg.hatch.test.key2");
92         io.sendMessage(obj);
93
94         pause();
95
96         obj = new JSONObject();
97         obj.put("msgid", msgid++);
98         obj.put("clientid", clientid);
99         obj.put("action", "keys");
100         io.sendMessage(obj);
101
102         pause();
103
104         // get a list of printers
105         obj = new JSONObject();
106         obj.put("msgid", msgid++);
107         obj.put("clientid", clientid);
108         obj.put("action", "printers");
109         io.sendMessage(obj);
110
111         pause();
112
113         /*
114         // Printing tests
115         
116         obj = new JSONObject();
117         obj.put("msgid", msgid++);
118         obj.put("clientid", clientid);
119         obj.put("action", "print");
120         obj.put("contentType", "text/plain");
121         obj.put("content", "Hello, World!");
122         obj.put("showDialog", true); // avoid auto-print while testing
123         io.sendMessage(obj);
124
125         pause();
126
127         obj = new JSONObject();
128         obj.put("msgid", msgid++);
129         obj.put("clientid", clientid);
130         obj.put("action", "print");
131         obj.put("contentType", "text/html");
132         obj.put("content", "<html><body><b>HELLO WORLD</b><img src='" +
133             "http://evergreen-ils.org/wp-content/uploads/2013/09/copy-Evergreen_Logo_sm072.jpg"
134             + "'/></body></html>");
135         obj.put("showDialog", true); // avoid auto-print while testing
136
137         JSONObject settings = new JSONObject();
138         settings.put("copies", 2);
139         obj.put("settings", settings);
140         io.sendMessage(obj);
141
142         pause();
143         
144         */
145     }
146
147     /**
148      * Log all received message as a JSON string
149      */
150     public static void doReceive() {
151         while (true) {
152             JSONObject resp = io.recvMessage();
153             logger.info("TestJSON:doReceive(): " + resp.toString());
154         }
155     }
156
157     public static void main (String[] args) {
158         io = new MessageIO();
159         io.listen();
160
161         if (args.length > 0 && args[0].equals("receive")) {
162             doReceive();
163         } else {
164             doSends();
165         }
166     }
167 }
168