source: src/test/java/geniusweb/clienttest/JavaClientTest.java@ 3

Last change on this file since 3 was 3, checked in by bart, 5 years ago

Small fixes

File size: 5.3 KB
Line 
1package geniusweb.clienttest;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertTrue;
5
6import java.io.IOException;
7import java.util.HashMap;
8import java.util.LinkedList;
9import java.util.List;
10import java.util.Map;
11
12import org.junit.AfterClass;
13import org.junit.BeforeClass;
14import org.junit.Test;
15import org.python.util.PythonInterpreter;
16
17import com.fasterxml.jackson.core.type.TypeReference;
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.neovisionaries.ws.client.WebSocket;
20import com.neovisionaries.ws.client.WebSocketAdapter;
21import com.neovisionaries.ws.client.WebSocketException;
22import com.neovisionaries.ws.client.WebSocketFactory;
23
24import geniusweb.examples.DownloadProfileExample;
25import geniusweb.examples.DownloadProfileExample2;
26
27public class JavaClientTest {
28 private static final String JSON = "\\{\"ws.*/jobs\":\\[\"ws.*/jobs/jobs1\".*\"ws.*/jobs/jobs2\"\\]\\}";
29
30 private static final String JOBS1PROFILE = "{\"LinearAdditiveUtilitySpace\":{"
31 + "\"domain\":{\"name\":\"jobs\",\"issuesValues\":{\"lease car\":{\"values\":[\"yes\",\"no\"]},\"permanent contract\":{\"values\":[\"yes\",\"no\"]},\"career development opportunities\":{\"values\":[\"low\",\"medium\",\"high\"]},\"fte\":{\"values\":[\"0.6\",\"0.8\",\"1.0\"]},\"salary\":{\"values\":[\"2000\",\"2500\",\"3000\",\"3500\",\"4000\"]},\"work from home\":{\"values\":[\"0\",\"1\",\"2\"]}}},"
32 + "\"name\":\"jobs1\"," + "\"issueUtilities\":{"
33 + "\"lease car\":{\"discreteutils\":{\"valueUtilities\":{\"no\":0,\"yes\":1}}},"
34 + "\"permanent contract\":{\"discreteutils\":{\"valueUtilities\":{\"no\":0,\"yes\":1}}},"
35 + "\"career development opportunities\":{\"discreteutils\":{\"valueUtilities\":{\"high\":1,\"low\":0,\"medium\":0.5}}},"
36 + "\"fte\":{\"discreteutils\":{\"valueUtilities\":{\"1.0\":0.75,\"0.6\":0.25,\"0.8\":0.5}}},"
37 + "\"salary\":{\"discreteutils\":{\"valueUtilities\":{\"4000\":1.0,\"2500\":0.25,\"3500\":0.75,\"2000\":0,\"3000\":0.3}}},"
38 + "\"work from home\":{\"discreteutils\":{\"valueUtilities\":{\"1\":0.5,\"2\":0.666666666666,\"0\":0.333333333}}}},"
39 + "\"issueWeights\":{\"lease car\":0.06,\"permanent contract\":0.16,\"career development opportunities\":0.04,\"fte\":0.32,\"salary\":0.24,\"work from home\":0.18},"
40 + "\"reservationBid\":{\"issuevalues\":{\"lease car\":\"no\",\"permanent contract\":\"no\",\"career development opportunities\":\"medium\",\"fte\":\"0.8\",\"salary\":\"300\",\"work from home\":\"0\"}}}}";
41 private static EmbeddedTomcat tomcat = new EmbeddedTomcat();
42
43 @BeforeClass
44 public static void before() throws Throwable {
45 tomcat.start();
46 tomcat.deploy("profilesserver");
47 }
48
49 @AfterClass
50 public final static void after() throws Throwable {
51 Thread.sleep(2000);
52 tomcat.stop();
53 }
54
55 @Test
56 public void tomcatSmokeTest() {
57
58 }
59
60 @Test
61 public void clientSmokeTest() throws IOException {
62 WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(5000);
63 WebSocket ws = factory.createSocket("ws://localhost:8080/profilesserver/websocket/liststream");
64 }
65
66 @Test
67 public void testGetList() throws IOException, InterruptedException, WebSocketException {
68 final List<String> received = new LinkedList<>();
69 Thread.sleep(5000);
70 WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(20000);
71 WebSocket ws = factory.createSocket("ws://localhost:8080/profilesserver/websocket/liststream");
72
73 ws.addListener(new WebSocketAdapter() {
74 @Override
75 public void onTextMessage(WebSocket websocket, String message) throws Exception {
76 System.out.println("received message: " + message);
77 received.add(message);
78 }
79 });
80 ws.connect();
81 assertTrue(ws.isOpen());
82
83 Thread.sleep(2000);
84
85 assertEquals(1, received.size()); // 1 answer received
86
87 ObjectMapper mapper = new ObjectMapper();
88 TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
89 };
90 Map<String, Object> map = mapper.readValue(received.get(0), typeRef);
91
92 List<String> domains= new LinkedList<>();
93 for (String key: map.keySet()) {
94 domains.add(key.substring(key.indexOf("get")));
95 }
96 assertTrue(domains.contains("get/jobs"));
97 assertTrue(domains.contains("get/7issues"));
98 }
99
100 @Test
101 public void testExample() throws WebSocketException, InterruptedException, IOException {
102 DownloadProfileExample test = new DownloadProfileExample();
103 test.run();
104 assertEquals(JOBS1PROFILE, test.getReceived());
105 }
106
107 @Test
108 public void testExample2() throws WebSocketException, InterruptedException, IOException {
109 DownloadProfileExample2 test = new DownloadProfileExample2();
110 assertEquals(JOBS1PROFILE, test.getReceived());
111 }
112
113 @Test
114 public void testPythonExample() {
115 // runs in Jython. Start up Jython and run the example
116 PythonInterpreter interpreter = new PythonInterpreter();
117 interpreter.exec("import sys");
118 addPath("src/test/java/geniusweb/examples", interpreter);
119
120 // Create factory and coerce Jython calculator object
121 JythonObjectFactory factory = new JythonObjectFactory(WebSocketAdapter.class, "PythonDownloadProfile",
122 "PythonDownloadProfile");
123 factory.createObject();
124 }
125
126 private void addPath(String relpath, PythonInterpreter interpreter) {
127 String path = System.getProperty("user.dir") + "/" + relpath + "/";
128
129 System.out.println("adding python path " + path);
130 interpreter.exec("sys.path.insert(0, '" + path + "')");
131
132 }
133
134}
Note: See TracBrowser for help on using the repository browser.