[42] | 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.HashMap;
|
---|
| 8 | import java.util.LinkedList;
|
---|
| 9 | import java.util.List;
|
---|
| 10 | import java.util.Map;
|
---|
| 11 |
|
---|
| 12 | import org.junit.AfterClass;
|
---|
| 13 | import org.junit.BeforeClass;
|
---|
| 14 | import org.junit.Test;
|
---|
| 15 | import org.python.util.PythonInterpreter;
|
---|
| 16 |
|
---|
| 17 | import com.fasterxml.jackson.core.type.TypeReference;
|
---|
| 18 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 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\":{\"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\":{\"DiscreteValueSetUtilities\":{\"valueUtilities\":{\"no\":0,\"yes\":1}}},\"permanent contract\":{\"DiscreteValueSetUtilities\":{\"valueUtilities\":{\"no\":0,\"yes\":1}}},\"career development opportunities\":{\"DiscreteValueSetUtilities\":{\"valueUtilities\":{\"high\":1,\"low\":0,\"medium\":0.5}}},\"fte\":{\"DiscreteValueSetUtilities\":{\"valueUtilities\":{\"1.0\":0.75,\"0.6\":0.25,\"0.8\":0.5}}},\"salary\":{\"DiscreteValueSetUtilities\":{\"valueUtilities\":{\"4000\":1.0,\"2500\":0.25,\"3500\":0.75,\"2000\":0,\"3000\":0.3}}},\"work from home\":{\"DiscreteValueSetUtilities\":{\"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 |
|
---|
| 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 {
|
---|
| 53 | WebSocketFactory factory = new WebSocketFactory()
|
---|
| 54 | .setConnectionTimeout(5000);
|
---|
| 55 | WebSocket ws = factory.createSocket(
|
---|
| 56 | "ws://localhost:8080/profilesserver/websocket/liststream");
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | @Test
|
---|
| 60 | public void testGetList()
|
---|
| 61 | throws IOException, InterruptedException, WebSocketException {
|
---|
| 62 | final List<String> received = new LinkedList<>();
|
---|
| 63 | Thread.sleep(5000);
|
---|
| 64 | WebSocketFactory factory = new WebSocketFactory()
|
---|
| 65 | .setConnectionTimeout(20000);
|
---|
| 66 | WebSocket ws = factory.createSocket(
|
---|
| 67 | "ws://localhost:8080/profilesserver/websocket/liststream");
|
---|
| 68 |
|
---|
| 69 | ws.addListener(new WebSocketAdapter() {
|
---|
| 70 | @Override
|
---|
| 71 | public void onTextMessage(WebSocket websocket, String message)
|
---|
| 72 | throws Exception {
|
---|
| 73 | System.out.println("received message: " + message);
|
---|
| 74 | received.add(message);
|
---|
| 75 | }
|
---|
| 76 | });
|
---|
| 77 | ws.connect();
|
---|
| 78 | assertTrue(ws.isOpen());
|
---|
| 79 |
|
---|
| 80 | Thread.sleep(15000);
|
---|
| 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 |
|
---|
| 89 | List<String> domains = new LinkedList<>();
|
---|
| 90 | for (String key : map.keySet()) {
|
---|
| 91 | domains.add(key.substring(key.indexOf("get")));
|
---|
| 92 | }
|
---|
| 93 | assertTrue(domains.contains("get/jobs"));
|
---|
| 94 | assertTrue(domains.contains("get/7issues"));
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | @Test
|
---|
| 98 | public void testExample()
|
---|
| 99 | throws WebSocketException, InterruptedException, IOException {
|
---|
| 100 | DownloadProfileExample test = new DownloadProfileExample();
|
---|
| 101 | test.run();
|
---|
| 102 | assertEquals(JOBS1PROFILE, test.getReceived());
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | @Test
|
---|
| 106 | public void testExample2()
|
---|
| 107 | throws WebSocketException, InterruptedException, IOException {
|
---|
| 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
|
---|
| 120 | JythonObjectFactory factory = new JythonObjectFactory(
|
---|
| 121 | 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 | }
|
---|