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

Last change on this file since 20 was 13, checked in by bart, 4 years ago

Update to version 1.41

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