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.party.inform.Settings;
|
---|
28 | import geniusweb.party.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 JSON = "{\"jobs\":[\"jobs1\"]}";
|
---|
42 | // 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\"}}";
|
---|
43 | private EmbeddedTomcat tomcat = new EmbeddedTomcat();
|
---|
44 | private JavaClient client = new JavaClient();
|
---|
45 | private static final PartyId partyid = new PartyId("party1");
|
---|
46 | private static ProfileRef profile;
|
---|
47 | private static ProtocolRef protocol;
|
---|
48 | private Progress progress = new ProgressRounds(120, 0, new Date(10000l));
|
---|
49 |
|
---|
50 | @Before
|
---|
51 | public void before() throws Throwable {
|
---|
52 | tomcat.start();
|
---|
53 | tomcat.deploy("partiesserver");
|
---|
54 | profile = new ProfileRef(
|
---|
55 | new URI("file:src/test/resources/testprofile.json"));
|
---|
56 | protocol = new ProtocolRef(new URI("SAOP"));
|
---|
57 | }
|
---|
58 |
|
---|
59 | @After
|
---|
60 | public void after() throws Throwable {
|
---|
61 | Thread.sleep(2000);
|
---|
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 | // @Test
|
---|
116 | // public void testGetList() throws IOException, InterruptedException, WebSocketException {
|
---|
117 | // final List<String> received = new LinkedList<>();
|
---|
118 | // Thread.sleep(5000);
|
---|
119 | // WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(20000);
|
---|
120 | // WebSocket ws = factory.createSocket("ws://localhost:8080/profilesserver/websocket/liststream");
|
---|
121 | //
|
---|
122 | // ws.addListener(new WebSocketAdapter() {
|
---|
123 | // @Override
|
---|
124 | // public void onTextMessage(WebSocket websocket, String message) throws Exception {
|
---|
125 | // System.out.println("received message: " + message);
|
---|
126 | // received.add(message);
|
---|
127 | // }
|
---|
128 | // });
|
---|
129 | // ws.connect();
|
---|
130 | // assertTrue(ws.isOpen());
|
---|
131 | //
|
---|
132 | // Thread.sleep(2000);
|
---|
133 | // assertEquals(1, received.size());
|
---|
134 | // assertEquals(JSON, received.get(0));
|
---|
135 | // }
|
---|
136 | //
|
---|
137 | // @Test
|
---|
138 | // public void testExample() throws WebSocketException, InterruptedException, IOException {
|
---|
139 | // DownloadProfileExample test = new DownloadProfileExample();
|
---|
140 | // test.run();
|
---|
141 | // assertEquals(JOBS1PROFILE, test.getReceived());
|
---|
142 | // }
|
---|
143 | //
|
---|
144 | // @Test
|
---|
145 | // public void testExample2() throws WebSocketException, InterruptedException, IOException {
|
---|
146 | // DownloadProfileExample2 test = new DownloadProfileExample2();
|
---|
147 | // assertEquals(JOBS1PROFILE, test.getReceived());
|
---|
148 | // }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * @return URL on which to contact the party
|
---|
152 | * @throws MalformedURLException
|
---|
153 | * @throws IOException
|
---|
154 | * @throws ProtocolException
|
---|
155 | * @throws InterruptedException
|
---|
156 | * @throws FileNotFoundException if the party does not exist on the server
|
---|
157 | */
|
---|
158 | private String startParty() throws IOException, InterruptedException {
|
---|
159 | URL url = new URL(
|
---|
160 | "http://localhost:8080/partiesserver/run/randomparty-1.0.0");
|
---|
161 | HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
---|
162 | conn.setRequestMethod("GET");
|
---|
163 | BufferedReader rd = new BufferedReader(
|
---|
164 | new InputStreamReader(conn.getInputStream()));
|
---|
165 | String response = rd.readLine();
|
---|
166 | rd.close();
|
---|
167 | System.out.println("party available at:" + response);
|
---|
168 | return response;
|
---|
169 | }
|
---|
170 |
|
---|
171 | private void connectParty()
|
---|
172 | throws IOException, InterruptedException, DeploymentException {
|
---|
173 | String uri = startParty();
|
---|
174 | WebSocketContainer container = ContainerProvider
|
---|
175 | .getWebSocketContainer();
|
---|
176 | System.out.println("Connecting to " + uri);
|
---|
177 | container.connectToServer(client, URI.create(uri));
|
---|
178 | }
|
---|
179 |
|
---|
180 | private void setupParty() throws IOException, InterruptedException,
|
---|
181 | DeploymentException, JsonProcessingException {
|
---|
182 | connectParty();
|
---|
183 | Thread.sleep(1000);
|
---|
184 | Settings settings = new Settings(partyid, profile, protocol, progress,
|
---|
185 | new Parameters());
|
---|
186 | client.sendMessage(settings);
|
---|
187 | }
|
---|
188 |
|
---|
189 | }
|
---|