[1] | 1 | package geniusweb.clienttest;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.junit.Assert.assertTrue;
|
---|
| 5 |
|
---|
| 6 | import java.io.IOException;
|
---|
[3] | 7 | import java.util.HashMap;
|
---|
[1] | 8 | import java.util.LinkedList;
|
---|
| 9 | import java.util.List;
|
---|
[3] | 10 | import java.util.Map;
|
---|
[1] | 11 |
|
---|
| 12 | import org.junit.AfterClass;
|
---|
| 13 | import org.junit.BeforeClass;
|
---|
| 14 | import org.junit.Test;
|
---|
| 15 | import org.python.util.PythonInterpreter;
|
---|
| 16 |
|
---|
[3] | 17 | import com.fasterxml.jackson.core.type.TypeReference;
|
---|
| 18 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
[1] | 19 | import com.neovisionaries.ws.client.WebSocket;
|
---|
| 20 | import com.neovisionaries.ws.client.WebSocketAdapter;
|
---|
| 21 | import com.neovisionaries.ws.client.WebSocketException;
|
---|
| 22 | import com.neovisionaries.ws.client.WebSocketFactory;
|
---|
| 23 |
|
---|
| 24 | import geniusweb.examples.DownloadProfileExample;
|
---|
| 25 | import geniusweb.examples.DownloadProfileExample2;
|
---|
| 26 |
|
---|
| 27 | public 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 {
|
---|
[5] | 62 | WebSocketFactory factory = new WebSocketFactory()
|
---|
| 63 | .setConnectionTimeout(5000);
|
---|
| 64 | WebSocket ws = factory.createSocket(
|
---|
| 65 | "ws://localhost:8080/profilesserver/websocket/liststream");
|
---|
[1] | 66 | }
|
---|
| 67 |
|
---|
| 68 | @Test
|
---|
[5] | 69 | public void testGetList()
|
---|
| 70 | throws IOException, InterruptedException, WebSocketException {
|
---|
[1] | 71 | final List<String> received = new LinkedList<>();
|
---|
| 72 | Thread.sleep(5000);
|
---|
[5] | 73 | WebSocketFactory factory = new WebSocketFactory()
|
---|
| 74 | .setConnectionTimeout(20000);
|
---|
| 75 | WebSocket ws = factory.createSocket(
|
---|
| 76 | "ws://localhost:8080/profilesserver/websocket/liststream");
|
---|
[1] | 77 |
|
---|
| 78 | ws.addListener(new WebSocketAdapter() {
|
---|
| 79 | @Override
|
---|
[5] | 80 | public void onTextMessage(WebSocket websocket, String message)
|
---|
| 81 | throws Exception {
|
---|
[1] | 82 | System.out.println("received message: " + message);
|
---|
| 83 | received.add(message);
|
---|
| 84 | }
|
---|
| 85 | });
|
---|
| 86 | ws.connect();
|
---|
| 87 | assertTrue(ws.isOpen());
|
---|
| 88 |
|
---|
[5] | 89 | Thread.sleep(15000);
|
---|
[3] | 90 |
|
---|
| 91 | assertEquals(1, received.size()); // 1 answer received
|
---|
| 92 |
|
---|
| 93 | ObjectMapper mapper = new ObjectMapper();
|
---|
| 94 | TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
|
---|
| 95 | };
|
---|
| 96 | Map<String, Object> map = mapper.readValue(received.get(0), typeRef);
|
---|
| 97 |
|
---|
[5] | 98 | List<String> domains = new LinkedList<>();
|
---|
| 99 | for (String key : map.keySet()) {
|
---|
[3] | 100 | domains.add(key.substring(key.indexOf("get")));
|
---|
[5] | 101 | }
|
---|
[3] | 102 | assertTrue(domains.contains("get/jobs"));
|
---|
| 103 | assertTrue(domains.contains("get/7issues"));
|
---|
[1] | 104 | }
|
---|
| 105 |
|
---|
| 106 | @Test
|
---|
[5] | 107 | public void testExample()
|
---|
| 108 | throws WebSocketException, InterruptedException, IOException {
|
---|
[1] | 109 | DownloadProfileExample test = new DownloadProfileExample();
|
---|
| 110 | test.run();
|
---|
| 111 | assertEquals(JOBS1PROFILE, test.getReceived());
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | @Test
|
---|
[5] | 115 | public void testExample2()
|
---|
| 116 | throws WebSocketException, InterruptedException, IOException {
|
---|
[1] | 117 | DownloadProfileExample2 test = new DownloadProfileExample2();
|
---|
| 118 | assertEquals(JOBS1PROFILE, test.getReceived());
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | @Test
|
---|
| 122 | public void testPythonExample() {
|
---|
| 123 | // runs in Jython. Start up Jython and run the example
|
---|
| 124 | PythonInterpreter interpreter = new PythonInterpreter();
|
---|
| 125 | interpreter.exec("import sys");
|
---|
| 126 | addPath("src/test/java/geniusweb/examples", interpreter);
|
---|
| 127 |
|
---|
| 128 | // Create factory and coerce Jython calculator object
|
---|
[5] | 129 | JythonObjectFactory factory = new JythonObjectFactory(
|
---|
| 130 | WebSocketAdapter.class, "PythonDownloadProfile",
|
---|
[1] | 131 | "PythonDownloadProfile");
|
---|
| 132 | factory.createObject();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | private void addPath(String relpath, PythonInterpreter interpreter) {
|
---|
| 136 | String path = System.getProperty("user.dir") + "/" + relpath + "/";
|
---|
| 137 |
|
---|
| 138 | System.out.println("adding python path " + path);
|
---|
| 139 | interpreter.exec("sys.path.insert(0, '" + path + "')");
|
---|
| 140 |
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | }
|
---|