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