1 | package geniusweb.profileconnection;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertNotNull;
|
---|
5 | import static org.mockito.ArgumentMatchers.any;
|
---|
6 | import static org.mockito.Mockito.mock;
|
---|
7 | import static org.mockito.Mockito.when;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.net.URI;
|
---|
11 | import java.net.URISyntaxException;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.nio.file.Files;
|
---|
14 | import java.nio.file.Paths;
|
---|
15 |
|
---|
16 | import javax.websocket.DeploymentException;
|
---|
17 | import javax.websocket.Session;
|
---|
18 | import javax.websocket.WebSocketContainer;
|
---|
19 |
|
---|
20 | import org.junit.Test;
|
---|
21 |
|
---|
22 | import geniusweb.profile.Profile;
|
---|
23 | import tudelft.utilities.logging.Reporter;
|
---|
24 |
|
---|
25 | public class WebsocketProfileConnectorTest {
|
---|
26 | private final Session session = mock(Session.class);
|
---|
27 | private final Reporter reporter = mock(Reporter.class);
|
---|
28 | private final WebSocketContainer wscontainer = mock(
|
---|
29 | WebSocketContainer.class);
|
---|
30 | private final static String testfilepath = "src/test/resources/jobs1_20.json";
|
---|
31 |
|
---|
32 | @Test(expected = IOException.class)
|
---|
33 | public void testNoSuchURI()
|
---|
34 | throws IOException, DeploymentException, URISyntaxException {
|
---|
35 | when(wscontainer.connectToServer(any(Object.class), any(URI.class)))
|
---|
36 | .thenThrow(new IOException("not available"));
|
---|
37 | new WebsocketProfileConnector(new URI("no://where"), reporter,
|
---|
38 | wscontainer);
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Test
|
---|
42 | public void onOpenSmokeTest()
|
---|
43 | throws IOException, DeploymentException, URISyntaxException {
|
---|
44 | WebsocketProfileConnector connector = new WebsocketProfileConnector(
|
---|
45 | new URI("ws://someprofile"), reporter, wscontainer);
|
---|
46 | connector.onOpen(session);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Test
|
---|
50 | public void testProcessMessage()
|
---|
51 | throws IOException, DeploymentException, URISyntaxException {
|
---|
52 | WebsocketProfileConnector connector = new WebsocketProfileConnector(
|
---|
53 | new URI("ws://someprofile"), reporter, wscontainer);
|
---|
54 | connector.onOpen(session);
|
---|
55 | connector.processMessage(readTestProfile(), true);
|
---|
56 |
|
---|
57 | Profile profile = connector.getProfile();
|
---|
58 | assertNotNull(profile);
|
---|
59 | assertEquals("jobs1_20", connector.getProfile().getName());
|
---|
60 | assertEquals("jobs", connector.getProfile().getDomain().getName());
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Test
|
---|
64 | public void testGetProfileWaiting()
|
---|
65 | throws DeploymentException, IOException, URISyntaxException {
|
---|
66 | /**
|
---|
67 | * Test that getProfile waits a little if necessary. We assume it waits
|
---|
68 | * at least 1 second for the profile to arrive
|
---|
69 | */
|
---|
70 | WebsocketProfileConnector connector = new WebsocketProfileConnector(
|
---|
71 | new URI("ws://someprofile"), reporter, wscontainer);
|
---|
72 | connector.onOpen(session);
|
---|
73 |
|
---|
74 | new Thread(new Runnable() {
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public void run() {
|
---|
78 | try {
|
---|
79 | Thread.sleep(1500);
|
---|
80 | connector.processMessage(readTestProfile(), true);
|
---|
81 | } catch (InterruptedException | IOException e) {
|
---|
82 | e.printStackTrace();
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }).start();
|
---|
86 | Profile profile = connector.getProfile();
|
---|
87 | assertNotNull(profile);
|
---|
88 | assertEquals("jobs1_20", connector.getProfile().getName());
|
---|
89 | assertEquals("jobs", connector.getProfile().getDomain().getName());
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Test(expected = IOException.class)
|
---|
93 | public void testGetProfileTimeout()
|
---|
94 | throws DeploymentException, IOException, URISyntaxException {
|
---|
95 | /**
|
---|
96 | * Test that getProfile does time out if the profile does not come in
|
---|
97 | * quick enough.
|
---|
98 | */
|
---|
99 | WebsocketProfileConnector connector = new WebsocketProfileConnector(
|
---|
100 | new URI("ws://someprofile"), reporter, wscontainer);
|
---|
101 | connector.onOpen(session);
|
---|
102 |
|
---|
103 | new Thread(new Runnable() {
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public void run() {
|
---|
107 | try {
|
---|
108 | Thread.sleep(6000);
|
---|
109 | connector.processMessage(readTestProfile(), true);
|
---|
110 | } catch (InterruptedException | IOException e) {
|
---|
111 | e.printStackTrace();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }).start();
|
---|
115 | connector.getProfile();
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Test(expected = IllegalArgumentException.class)
|
---|
119 | public void testNullArg1()
|
---|
120 | throws DeploymentException, IOException, URISyntaxException {
|
---|
121 | new WebsocketProfileConnector(null, reporter, wscontainer);
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Test(expected = IllegalArgumentException.class)
|
---|
125 | public void testNullArg2()
|
---|
126 | throws DeploymentException, IOException, URISyntaxException {
|
---|
127 | new WebsocketProfileConnector(new URI("ws://someprofile"), null,
|
---|
128 | wscontainer);
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Test(expected = IllegalArgumentException.class)
|
---|
132 | public void testNullArg3()
|
---|
133 | throws DeploymentException, IOException, URISyntaxException {
|
---|
134 | new WebsocketProfileConnector(new URI("ws://someprofile"), reporter,
|
---|
135 | null);
|
---|
136 | }
|
---|
137 |
|
---|
138 | private String readTestProfile() throws IOException {
|
---|
139 | return new String(Files.readAllBytes(Paths.get(testfilepath)),
|
---|
140 | StandardCharsets.UTF_8);
|
---|
141 |
|
---|
142 | }
|
---|
143 | }
|
---|