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.inform.Settings; import geniusweb.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.5.7"; 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()); } /** * @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); } }