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

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

Update to version 1.41

File size: 6.8 KB
Line 
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.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-1.4.1";
42 // private static final String JSON = "{\"jobs\":[\"jobs1\"]}";
43// 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\"}}";
44 private EmbeddedTomcat tomcat = new EmbeddedTomcat();
45 private JavaClient client = new JavaClient();
46 private static final PartyId partyid = new PartyId("party1");
47 private static ProfileRef profile;
48 private static ProtocolRef protocol;
49 private Progress progress = new ProgressRounds(120, 0, new Date(10000l));
50
51 @Before
52 public void before() throws Throwable {
53 tomcat.start();
54 tomcat.deploy("partiesserver");
55 profile = new ProfileRef(
56 new URI("file:src/test/resources/testprofile.json"));
57 protocol = new ProtocolRef(new URI("SAOP"));
58 }
59
60 @After
61 public void after() throws Throwable {
62 Thread.sleep(2000);
63 System.setSecurityManager(null);
64 tomcat.stop();
65 }
66
67 @Test
68 public void tomcatSmokeTest() {
69
70 }
71
72 @Test
73 public void clientSmokeTest() throws IOException, InterruptedException {
74 startParty();
75 }
76
77 @Test
78 public void clientConnectTest()
79 throws IOException, InterruptedException, DeploymentException {
80 connectParty();
81 Thread.sleep(1000);
82 assertEquals("connected", client.getStatus());
83 }
84
85 @Test
86 public void sendSettingsTest()
87 throws IOException, InterruptedException, DeploymentException {
88 setupParty();
89 Thread.sleep(1000);
90 assertEquals("sent Settings", client.getStatus());
91
92 }
93
94 @Test
95 public void YourTurnTest()
96 throws IOException, InterruptedException, DeploymentException {
97 setupParty();
98 client.sendMessage(new YourTurn());
99 assertEquals("sent YourTurn", client.getStatus());
100 }
101
102 @Test
103 public void agentActs()
104 throws IOException, InterruptedException, DeploymentException {
105 String uri = startParty();
106 WebSocketContainer container = ContainerProvider
107 .getWebSocketContainer();
108 container.connectToServer(client, URI.create(uri));
109 Thread.sleep(2000);
110
111 client.sendMessage(new YourTurn());
112 Thread.sleep(1000);
113 assertEquals("sent YourTurn", client.getStatus());
114
115 }
116
117// @Test
118// public void testGetList() throws IOException, InterruptedException, WebSocketException {
119// final List<String> received = new LinkedList<>();
120// Thread.sleep(5000);
121// WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(20000);
122// WebSocket ws = factory.createSocket("ws://localhost:8080/profilesserver/websocket/liststream");
123//
124// ws.addListener(new WebSocketAdapter() {
125// @Override
126// public void onTextMessage(WebSocket websocket, String message) throws Exception {
127// System.out.println("received message: " + message);
128// received.add(message);
129// }
130// });
131// ws.connect();
132// assertTrue(ws.isOpen());
133//
134// Thread.sleep(2000);
135// assertEquals(1, received.size());
136// assertEquals(JSON, received.get(0));
137// }
138//
139// @Test
140// public void testExample() throws WebSocketException, InterruptedException, IOException {
141// DownloadProfileExample test = new DownloadProfileExample();
142// test.run();
143// assertEquals(JOBS1PROFILE, test.getReceived());
144// }
145//
146// @Test
147// public void testExample2() throws WebSocketException, InterruptedException, IOException {
148// DownloadProfileExample2 test = new DownloadProfileExample2();
149// assertEquals(JOBS1PROFILE, test.getReceived());
150// }
151
152 /**
153 * @return URL on which to contact the party
154 * @throws MalformedURLException
155 * @throws IOException
156 * @throws ProtocolException
157 * @throws InterruptedException
158 * @throws FileNotFoundException if the party does not exist on the server
159 */
160 private String startParty() throws IOException, InterruptedException {
161 URL url = new URL(RANDOMPARTY);
162 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
163 conn.setRequestMethod("GET");
164 BufferedReader rd = new BufferedReader(
165 new InputStreamReader(conn.getInputStream()));
166 String response = rd.readLine();
167 rd.close();
168 System.out.println("party available at:" + response);
169 return response;
170 }
171
172 private void connectParty()
173 throws IOException, InterruptedException, DeploymentException {
174 String uri = startParty();
175 WebSocketContainer container = ContainerProvider
176 .getWebSocketContainer();
177 System.out.println("Connecting to " + uri);
178 container.connectToServer(client, URI.create(uri));
179 }
180
181 private void setupParty() throws IOException, InterruptedException,
182 DeploymentException, JsonProcessingException {
183 connectParty();
184 Thread.sleep(1000);
185 Settings settings = new Settings(partyid, profile, protocol, progress,
186 new Parameters());
187 client.sendMessage(settings);
188 }
189
190}
Note: See TracBrowser for help on using the repository browser.