[26] | 1 | package geniusweb.partiesserver;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 |
|
---|
| 5 | import java.io.BufferedReader;
|
---|
| 6 | import java.io.FileNotFoundException;
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 | import java.io.InputStreamReader;
|
---|
| 9 | import java.net.HttpURLConnection;
|
---|
| 10 | import java.net.MalformedURLException;
|
---|
| 11 | import java.net.ProtocolException;
|
---|
| 12 | import java.net.URI;
|
---|
| 13 | import java.net.URL;
|
---|
| 14 | import java.util.Date;
|
---|
| 15 |
|
---|
| 16 | import javax.websocket.ContainerProvider;
|
---|
| 17 | import javax.websocket.DeploymentException;
|
---|
| 18 | import javax.websocket.WebSocketContainer;
|
---|
| 19 |
|
---|
| 20 | import org.junit.After;
|
---|
| 21 | import org.junit.Before;
|
---|
| 22 | import org.junit.Test;
|
---|
| 23 |
|
---|
| 24 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 25 |
|
---|
| 26 | import geniusweb.actions.PartyId;
|
---|
| 27 | import geniusweb.inform.Settings;
|
---|
| 28 | import geniusweb.inform.YourTurn;
|
---|
| 29 | import geniusweb.progress.Progress;
|
---|
| 30 | import geniusweb.progress.ProgressRounds;
|
---|
| 31 | import geniusweb.references.Parameters;
|
---|
| 32 | import geniusweb.references.ProfileRef;
|
---|
| 33 | import geniusweb.references.ProtocolRef;
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * End to end test starts server, tries to launch the random party. This
|
---|
| 37 | * demonstrates how to launch a party on the partiesserver and communicate
|
---|
| 38 | * through a websocket with it.
|
---|
| 39 | */
|
---|
| 40 | public class JavaClientTest {
|
---|
| 41 | private static final String RANDOMPARTY = "http://localhost:8080/partiesserver/run/randomparty-1.5.7";
|
---|
| 42 | private EmbeddedTomcat tomcat = new EmbeddedTomcat();
|
---|
| 43 | private JavaClient client = new JavaClient();
|
---|
| 44 | private static final PartyId partyid = new PartyId("party1");
|
---|
| 45 | private static ProfileRef profile;
|
---|
| 46 | private static ProtocolRef protocol;
|
---|
| 47 | private Progress progress = new ProgressRounds(120, 0, new Date(10000l));
|
---|
| 48 |
|
---|
| 49 | @Before
|
---|
| 50 | public void before() throws Throwable {
|
---|
| 51 | tomcat.start();
|
---|
| 52 | tomcat.deploy("partiesserver");
|
---|
| 53 | profile = new ProfileRef(
|
---|
| 54 | new URI("file:src/test/resources/testprofile.json"));
|
---|
| 55 | protocol = new ProtocolRef(new URI("SAOP"));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @After
|
---|
| 59 | public void after() throws Throwable {
|
---|
| 60 | Thread.sleep(2000);
|
---|
| 61 | System.setSecurityManager(null);
|
---|
| 62 | tomcat.stop();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | @Test
|
---|
| 66 | public void tomcatSmokeTest() {
|
---|
| 67 |
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | @Test
|
---|
| 71 | public void clientSmokeTest() throws IOException, InterruptedException {
|
---|
| 72 | startParty();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | @Test
|
---|
| 76 | public void clientConnectTest()
|
---|
| 77 | throws IOException, InterruptedException, DeploymentException {
|
---|
| 78 | connectParty();
|
---|
| 79 | Thread.sleep(1000);
|
---|
| 80 | assertEquals("connected", client.getStatus());
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | @Test
|
---|
| 84 | public void sendSettingsTest()
|
---|
| 85 | throws IOException, InterruptedException, DeploymentException {
|
---|
| 86 | setupParty();
|
---|
| 87 | Thread.sleep(1000);
|
---|
| 88 | assertEquals("sent Settings", client.getStatus());
|
---|
| 89 |
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | @Test
|
---|
| 93 | public void YourTurnTest()
|
---|
| 94 | throws IOException, InterruptedException, DeploymentException {
|
---|
| 95 | setupParty();
|
---|
| 96 | client.sendMessage(new YourTurn());
|
---|
| 97 | assertEquals("sent YourTurn", client.getStatus());
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | @Test
|
---|
| 101 | public void agentActs()
|
---|
| 102 | throws IOException, InterruptedException, DeploymentException {
|
---|
| 103 | String uri = startParty();
|
---|
| 104 | WebSocketContainer container = ContainerProvider
|
---|
| 105 | .getWebSocketContainer();
|
---|
| 106 | container.connectToServer(client, URI.create(uri));
|
---|
| 107 | Thread.sleep(2000);
|
---|
| 108 |
|
---|
| 109 | client.sendMessage(new YourTurn());
|
---|
| 110 | Thread.sleep(1000);
|
---|
| 111 | assertEquals("sent YourTurn", client.getStatus());
|
---|
| 112 |
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /**
|
---|
| 116 | * @return URL on which to contact the party
|
---|
| 117 | * @throws MalformedURLException
|
---|
| 118 | * @throws IOException
|
---|
| 119 | * @throws ProtocolException
|
---|
| 120 | * @throws InterruptedException
|
---|
| 121 | * @throws FileNotFoundException if the party does not exist on the server
|
---|
| 122 | */
|
---|
| 123 | private String startParty() throws IOException, InterruptedException {
|
---|
| 124 | URL url = new URL(RANDOMPARTY);
|
---|
| 125 | HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
---|
| 126 | conn.setRequestMethod("GET");
|
---|
| 127 | BufferedReader rd = new BufferedReader(
|
---|
| 128 | new InputStreamReader(conn.getInputStream()));
|
---|
| 129 | String response = rd.readLine();
|
---|
| 130 | rd.close();
|
---|
| 131 | System.out.println("party available at:" + response);
|
---|
| 132 | return response;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | private void connectParty()
|
---|
| 136 | throws IOException, InterruptedException, DeploymentException {
|
---|
| 137 | String uri = startParty();
|
---|
| 138 | WebSocketContainer container = ContainerProvider
|
---|
| 139 | .getWebSocketContainer();
|
---|
| 140 | System.out.println("Connecting to " + uri);
|
---|
| 141 | container.connectToServer(client, URI.create(uri));
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | private void setupParty() throws IOException, InterruptedException,
|
---|
| 145 | DeploymentException, JsonProcessingException {
|
---|
| 146 | connectParty();
|
---|
| 147 | Thread.sleep(1000);
|
---|
| 148 | Settings settings = new Settings(partyid, profile, protocol, progress,
|
---|
| 149 | new Parameters());
|
---|
| 150 | client.sendMessage(settings);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | }
|
---|