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

Last change on this file since 4 was 1, checked in by bart, 5 years ago

Initial Release

File size: 6.7 KB
RevLine 
[1]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.party.inform.Settings;
28import geniusweb.party.inform.YourTurn;
29import geniusweb.progress.Progress;
30import geniusweb.progress.ProgressRounds;
31import geniusweb.references.ProfileRef;
32import geniusweb.references.ProtocolRef;
33
34/**
35 * End to end test starts server, tries to launch the random party. This
36 * demonstrates how to launch a party on the partiesserver and communicate
37 * through a websocket with it.
38 */
39public class JavaClientTest {
40// private static final String JSON = "{\"jobs\":[\"jobs1\"]}";
41// 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\"}}";
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 tomcat.stop();
62 }
63
64 @Test
65 public void tomcatSmokeTest() {
66
67 }
68
69 @Test
70 public void clientSmokeTest() throws IOException, InterruptedException {
71 startParty();
72 }
73
74 @Test
75 public void clientConnectTest()
76 throws IOException, InterruptedException, DeploymentException {
77 connectParty();
78 Thread.sleep(1000);
79 assertEquals("connected", client.getStatus());
80 }
81
82 @Test
83 public void sendSettingsTest()
84 throws IOException, InterruptedException, DeploymentException {
85 setupParty();
86 Thread.sleep(1000);
87 assertEquals("sent Settings", client.getStatus());
88
89 }
90
91 @Test
92 public void YourTurnTest()
93 throws IOException, InterruptedException, DeploymentException {
94 setupParty();
95 client.sendMessage(new YourTurn());
96 assertEquals("sent YourTurn", client.getStatus());
97 }
98
99 @Test
100 public void agentActs()
101 throws IOException, InterruptedException, DeploymentException {
102 String uri = startParty();
103 WebSocketContainer container = ContainerProvider
104 .getWebSocketContainer();
105 container.connectToServer(client, URI.create(uri));
106 Thread.sleep(2000);
107
108 client.sendMessage(new YourTurn());
109 Thread.sleep(1000);
110 assertEquals("sent YourTurn", client.getStatus());
111
112 }
113
114// @Test
115// public void testGetList() throws IOException, InterruptedException, WebSocketException {
116// final List<String> received = new LinkedList<>();
117// Thread.sleep(5000);
118// WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(20000);
119// WebSocket ws = factory.createSocket("ws://localhost:8080/profilesserver/websocket/liststream");
120//
121// ws.addListener(new WebSocketAdapter() {
122// @Override
123// public void onTextMessage(WebSocket websocket, String message) throws Exception {
124// System.out.println("received message: " + message);
125// received.add(message);
126// }
127// });
128// ws.connect();
129// assertTrue(ws.isOpen());
130//
131// Thread.sleep(2000);
132// assertEquals(1, received.size());
133// assertEquals(JSON, received.get(0));
134// }
135//
136// @Test
137// public void testExample() throws WebSocketException, InterruptedException, IOException {
138// DownloadProfileExample test = new DownloadProfileExample();
139// test.run();
140// assertEquals(JOBS1PROFILE, test.getReceived());
141// }
142//
143// @Test
144// public void testExample2() throws WebSocketException, InterruptedException, IOException {
145// DownloadProfileExample2 test = new DownloadProfileExample2();
146// assertEquals(JOBS1PROFILE, test.getReceived());
147// }
148
149 /**
150 * @return URL on which to contact the party
151 * @throws MalformedURLException
152 * @throws IOException
153 * @throws ProtocolException
154 * @throws InterruptedException
155 * @throws FileNotFoundException if the party does not exist on the server
156 */
157 private String startParty() throws IOException, InterruptedException {
158 URL url = new URL(
159 "http://localhost:8080/partiesserver/run/randomparty-1.0.0");
160 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
161 conn.setRequestMethod("GET");
162 BufferedReader rd = new BufferedReader(
163 new InputStreamReader(conn.getInputStream()));
164 String response = rd.readLine();
165 rd.close();
166 System.out.println("party available at:" + response);
167 return response;
168 }
169
170 private void connectParty()
171 throws IOException, InterruptedException, DeploymentException {
172 String uri = startParty();
173 WebSocketContainer container = ContainerProvider
174 .getWebSocketContainer();
175 System.out.println("Connecting to " + uri);
176 container.connectToServer(client, URI.create(uri));
177 }
178
179 private void setupParty() throws IOException, InterruptedException,
180 DeploymentException, JsonProcessingException {
181 connectParty();
182 Thread.sleep(1000);
183 Settings settings = new Settings(partyid, profile, protocol, progress);
184 client.sendMessage(settings);
185 }
186
187}
Note: See TracBrowser for help on using the repository browser.