source: src/test/java/geniusweb/partiesserver/websocket/PartySocketTest.java@ 14

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

Release 1.1.0

File size: 4.2 KB
RevLine 
[1]1package geniusweb.partiesserver.websocket;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Matchers.any;
5import static org.mockito.Mockito.mock;
6import static org.mockito.Mockito.times;
7import static org.mockito.Mockito.verify;
8import static org.mockito.Mockito.when;
9
10import java.io.IOException;
11
12import javax.websocket.CloseReason;
13import javax.websocket.RemoteEndpoint.Basic;
14import javax.websocket.Session;
15
16import org.junit.Before;
17import org.junit.Test;
18import org.mockito.ArgumentCaptor;
19
20import com.fasterxml.jackson.databind.ObjectMapper;
21
22import geniusweb.actions.Action;
23import geniusweb.actions.EndNegotiation;
24import geniusweb.actions.PartyId;
[8]25import geniusweb.connection.ConnectionEnd;
[1]26import geniusweb.partiesserver.repository.RunningPartiesRepo;
27import geniusweb.partiesserver.repository.RunningParty;
28import geniusweb.party.Party;
29import geniusweb.party.inform.Inform;
30import geniusweb.party.inform.YourTurn;
31import tudelft.utilities.logging.Reporter;
32
33/**
34 * Note this is a junit test. No real sockets are ever used here.
35 */
36public class PartySocketTest {
37 private PartySocket socket;
38
39 private Session session = mock(Session.class);
40
41 private PartyId party1id = new PartyId("party1");
42 private Basic basicremote = mock(Basic.class);
43
44 private RunningParty runningparty1 = mock(RunningParty.class);
45 private RunningPartiesRepo repo = mock(RunningPartiesRepo.class);
46 private final static ObjectMapper jackson = new ObjectMapper();
47
48 @Before
49 public void before() {
50 this.socket = new PartySocket(repo, mock(Reporter.class));
51 when(runningparty1.getID()).thenReturn(party1id);
52
53 when(session.getBasicRemote()).thenReturn(basicremote);
54 }
55
56 @Test
57 public void smokeTest() {
58 }
59
60 @Test
61 public void initTestNoSuchParty() throws IOException {
62 ArgumentCaptor<CloseReason> argument = ArgumentCaptor
63 .forClass(CloseReason.class);
64 socket.start(session, "test1");
65 verify(session, times(1)).close(argument.capture());
66 assertEquals("No such party: test1",
67 argument.getValue().getReasonPhrase());
68 }
69
70 @Test
71 public void initTest() throws IOException {
72 when(repo.get(any())).thenReturn(runningparty1);
73 socket.start(session, party1id.getName());
74 verify(session, times(0)).close(any());
75 }
76
77 private void initparty1(Party party1) throws IOException {
78 when(repo.get(any())).thenReturn(runningparty1);
79 when(runningparty1.getParty()).thenReturn(party1);
80
81 socket.start(session, "test2");
82
83 @SuppressWarnings("unchecked")
[8]84 ConnectionEnd<Inform, Action> connection = mock(ConnectionEnd.class);
[1]85 /*
86 * we must call runningparty directly on this one because
87 * SessionConnection can not be serialized. possible as this runs
88 * locally on the server.
89 */
90 party1.connect(connection);
91
92 }
93
94// class TestConnection extends DefaultListenable<Inform> implements Connection<Inform,Action> {
95// @Override
96// public void send(Action data) throws IOException {
97// // TODO Auto-generated method stub
98// }
99//
100// }
101
102 /**
103 * more tests needed for onMessage etc.
104 */
105 @Test
106 public void connectionMessageTest() throws IOException {
107 initparty1(mock(Party.class));
108 verify(session, times(0)).close(any());
109 }
110
111 /**
112 * Check that party is informed when we send it YourTurn.
113 */
114 @Test
115 public void checkInform() throws IOException {
116 initparty1(mock(Party.class));
117
118 ArgumentCaptor<Inform> argument = ArgumentCaptor.forClass(Inform.class);
119
120 Inform info = new YourTurn();
121 socket.onMessage(jackson.writeValueAsString(info), session);
122 // check that party.inform was actually called with YourTurn
123 verify(runningparty1, times(1)).inform(argument.capture());
124 // somewhere else we should test that that invokes party.inform
125 assertEquals(info, argument.getValue());
126
127 }
128
129 /**
130 * Check that action is forwarded properly as serialized json into the
131 * session stream.
132 *
133 * @throws InterruptedException
134 */
135 @Test
136 public void actionTest() throws IOException, InterruptedException {
137 initparty1(mock(Party.class));
138 EndNegotiation action = new EndNegotiation(party1id);
139 socket.send(action);
140 Thread.sleep(500);
141
142 ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
143 verify(basicremote, times(1)).sendText(argument.capture());
144 assertEquals("{\"endnegotiation\":{\"actor\":\"party1\"}}",
145 argument.getValue());
146 }
147
148}
Note: See TracBrowser for help on using the repository browser.