package geniusweb.partiesserver; import static org.junit.Assert.assertEquals; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URI; import java.net.URL; import java.util.Date; import javax.websocket.ContainerProvider; import javax.websocket.DeploymentException; import javax.websocket.WebSocketContainer; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; import geniusweb.actions.PartyId; import geniusweb.party.inform.Settings; import geniusweb.party.inform.YourTurn; import geniusweb.progress.Progress; import geniusweb.progress.ProgressRounds; import geniusweb.references.Parameters; import geniusweb.references.ProfileRef; import geniusweb.references.ProtocolRef; /** * End to end test starts server, tries to launch the random party. This * demonstrates how to launch a party on the partiesserver and communicate * through a websocket with it. */ public class JavaClientTest { private static final String RANDOMPARTY = "http://localhost:8080/partiesserver/run/randomparty-1.2.0"; // private static final String JSON = "{\"jobs\":[\"jobs1\"]}"; // private static final String JOBS1PROFILE = "{\"LinearAdditiveUtilitySpace\":{\"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},\"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\"}}"; private EmbeddedTomcat tomcat = new EmbeddedTomcat(); private JavaClient client = new JavaClient(); private static final PartyId partyid = new PartyId("party1"); private static ProfileRef profile; private static ProtocolRef protocol; private Progress progress = new ProgressRounds(120, 0, new Date(10000l)); @Before public void before() throws Throwable { tomcat.start(); tomcat.deploy("partiesserver"); profile = new ProfileRef( new URI("file:src/test/resources/testprofile.json")); protocol = new ProtocolRef(new URI("SAOP")); } @After public void after() throws Throwable { Thread.sleep(2000); System.setSecurityManager(null); tomcat.stop(); } @Test public void tomcatSmokeTest() { } @Test public void clientSmokeTest() throws IOException, InterruptedException { startParty(); } @Test public void clientConnectTest() throws IOException, InterruptedException, DeploymentException { connectParty(); Thread.sleep(1000); assertEquals("connected", client.getStatus()); } @Test public void sendSettingsTest() throws IOException, InterruptedException, DeploymentException { setupParty(); Thread.sleep(1000); assertEquals("sent Settings", client.getStatus()); } @Test public void YourTurnTest() throws IOException, InterruptedException, DeploymentException { setupParty(); client.sendMessage(new YourTurn()); assertEquals("sent YourTurn", client.getStatus()); } @Test public void agentActs() throws IOException, InterruptedException, DeploymentException { String uri = startParty(); WebSocketContainer container = ContainerProvider .getWebSocketContainer(); container.connectToServer(client, URI.create(uri)); Thread.sleep(2000); client.sendMessage(new YourTurn()); Thread.sleep(1000); assertEquals("sent YourTurn", client.getStatus()); } // @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()); // assertEquals(JSON, received.get(0)); // } // // @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()); // } /** * @return URL on which to contact the party * @throws MalformedURLException * @throws IOException * @throws ProtocolException * @throws InterruptedException * @throws FileNotFoundException if the party does not exist on the server */ private String startParty() throws IOException, InterruptedException { URL url = new URL(RANDOMPARTY); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader rd = new BufferedReader( new InputStreamReader(conn.getInputStream())); String response = rd.readLine(); rd.close(); System.out.println("party available at:" + response); return response; } private void connectParty() throws IOException, InterruptedException, DeploymentException { String uri = startParty(); WebSocketContainer container = ContainerProvider .getWebSocketContainer(); System.out.println("Connecting to " + uri); container.connectToServer(client, URI.create(uri)); } private void setupParty() throws IOException, InterruptedException, DeploymentException, JsonProcessingException { connectParty(); Thread.sleep(1000); Settings settings = new Settings(partyid, profile, protocol, progress, new Parameters()); client.sendMessage(settings); } }