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

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

Initial Release

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