package geniusweb.clienttest; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.LinkedList; import java.util.List; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.python.util.PythonInterpreter; import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocketAdapter; import com.neovisionaries.ws.client.WebSocketException; import com.neovisionaries.ws.client.WebSocketFactory; import geniusweb.examples.DownloadProfileExample; import geniusweb.examples.DownloadProfileExample2; public class JavaClientTest { private static final String JSON = "\\{\"ws.*/jobs\":\\[\"ws.*/jobs/jobs1\".*\"ws.*/jobs/jobs2\"\\]\\}"; 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\":\"no\",\"career development opportunities\":\"medium\",\"fte\":\"0.8\",\"salary\":\"300\",\"work from home\":\"0\"}}}}"; private static EmbeddedTomcat tomcat = new EmbeddedTomcat(); @BeforeClass public static void before() throws Throwable { tomcat.start(); tomcat.deploy("profilesserver"); } @AfterClass public final static void after() throws Throwable { Thread.sleep(2000); tomcat.stop(); } @Test public void tomcatSmokeTest() { } @Test public void clientSmokeTest() throws IOException { WebSocketFactory factory = new WebSocketFactory() .setConnectionTimeout(5000); WebSocket ws = factory.createSocket( "ws://localhost:8080/profilesserver/websocket/liststream"); } @Test public void testGetList() throws IOException, InterruptedException, WebSocketException { final List received = new LinkedList<>(); Thread.sleep(5000); WebSocketFactory factory = new WebSocketFactory() .setConnectionTimeout(20000); WebSocket ws = factory.createSocket( "ws://localhost:8080/profilesserver/websocket/liststream"); ws.addListener(new WebSocketAdapter() { @Override public void onTextMessage(WebSocket websocket, String message) throws Exception { System.out.println("received message: " + message); received.add(message); } }); ws.connect(); assertTrue(ws.isOpen()); Thread.sleep(2000); assertEquals(1, received.size()); assertTrue(received.get(0).matches(JSON)); System.out.println(JSON); } @Test public void testExample() throws WebSocketException, InterruptedException, IOException { DownloadProfileExample test = new DownloadProfileExample(); test.run(); assertEquals(JOBS1PROFILE, test.getReceived()); } @Test public void testExample2() throws WebSocketException, InterruptedException, IOException { DownloadProfileExample2 test = new DownloadProfileExample2(); assertEquals(JOBS1PROFILE, test.getReceived()); } @Test public void testPythonExample() { // runs in Jython. Start up Jython and run the example PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("import sys"); addPath("src/test/java/geniusweb/examples", interpreter); // Create factory and coerce Jython calculator object JythonObjectFactory factory = new JythonObjectFactory( WebSocketAdapter.class, "PythonDownloadProfile", "PythonDownloadProfile"); factory.createObject(); } private void addPath(String relpath, PythonInterpreter interpreter) { String path = System.getProperty("user.dir") + "/" + relpath + "/"; System.out.println("adding python path " + path); interpreter.exec("sys.path.insert(0, '" + path + "')"); } }