source: src/test/java/geniusweb/partiesserver/JavaClientTest.java@ 39

Last change on this file since 39 was 39, checked in by bart, 3 years ago

Added time-dependent parties for python and simpleRunner-GUI for java

File size: 4.3 KB
RevLine 
[39]1package geniusweb.partiesserver;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.BufferedReader;
6import java.io.FileNotFoundException;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.net.HttpURLConnection;
10import java.net.MalformedURLException;
11import java.net.ProtocolException;
12import java.net.URI;
13import java.net.URL;
14import java.util.Date;
15
16import javax.websocket.ContainerProvider;
17import javax.websocket.DeploymentException;
18import javax.websocket.WebSocketContainer;
19
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23
24import com.fasterxml.jackson.core.JsonProcessingException;
25
26import geniusweb.actions.PartyId;
27import geniusweb.inform.Settings;
28import geniusweb.inform.YourTurn;
29import geniusweb.progress.Progress;
30import geniusweb.progress.ProgressRounds;
31import geniusweb.references.Parameters;
32import geniusweb.references.ProfileRef;
33import 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 */
40public class JavaClientTest {
41 private static final String RANDOMPARTY = "http://localhost:8080/partiesserver/run/randomparty-2.0.8";
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}
Note: See TracBrowser for help on using the repository browser.