source: src/test/java/geniusweb/partiesserver/repository/RunningPartyTest.java@ 41

Last change on this file since 41 was 41, checked in by bart, 3 years ago

refactor to help reusing partiesserver

File size: 4.6 KB
Line 
1package geniusweb.partiesserver.repository;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Mockito.mock;
5import static org.mockito.Mockito.times;
6import static org.mockito.Mockito.verify;
7
8import java.util.Arrays;
9import java.util.Date;
10import java.util.List;
11
12import org.junit.Test;
13import org.mockito.ArgumentCaptor;
14
15import com.fasterxml.jackson.core.JsonProcessingException;
16import com.fasterxml.jackson.databind.ObjectMapper;
17
18import geniusweb.actions.PartyId;
19import geniusweb.inform.Inform;
20import geniusweb.partiesserver.websocket.PartySocket;
21import geniusweb.party.Capabilities;
22import geniusweb.party.DefaultParty;
23import geniusweb.party.Party;
24import tudelft.utilities.junit.GeneralTests;
25
26public class RunningPartyTest extends GeneralTests<RunningParty> {
27
28 private static final int DEFAULT_RUNTIME = 10000;
29 private static final String NAME1 = "name1";
30 private final Party party1 = mock(Party.class);
31 private final Party party2 = mock(Party.class);
32 private final PartyId id1 = new PartyId("party1");
33 private final PartyId id2 = new PartyId("party2");
34 private final Date now = new Date();
35 private final Date end = new Date(now.getTime() + DEFAULT_RUNTIME);
36 private final RunningParty running1 = new RunningParty(party1, id1, NAME1,
37 now, end, null),
38 running1a = new RunningParty(party1, id1, NAME1, now, end, null),
39 running2 = new RunningParty(party2, id2, "name2", now, end, null),
40 running3 = new RunningParty(party2, id2, "name1", now, end, null);
41 private final String asString = "{\"id\":\"party1\",\"startDate\":"
42 + now.getTime() + ",\"endDate\":" + end.getTime()
43 + ",\"name\":\"name1\"}";
44
45 @Override
46 public List<List<RunningParty>> getGeneralTestData() {
47 return Arrays.asList(Arrays.asList(running1, running1a),
48 Arrays.asList(running2), Arrays.asList(running3));
49 }
50
51 @Override
52 public List<String> getGeneralTestStrings() {
53 return Arrays.asList(
54 "RunningParty.*party.*class geniusweb.party.Party.*name1.*",
55 "RunningParty.*party.*class geniusweb.party.Party.*name2.*",
56 "RunningParty.*party.*class geniusweb.party.Party.*name1.*");
57 }
58
59 @Test
60 public void constructorTest() {
61 new RunningParty(party1, id1, "name1", now, end, null);
62 }
63
64 @Test
65 public void constructor2Test()
66 throws ClassNotFoundException, InstantiationFailedException {
67 AvailableParty availableParty = new AvailableJavaParty("simple party",
68 SimpleParty.class);
69 // mocking AvailableParty gives troubles with implemening
70 // getPartyClass...
71 RunningParty.create(availableParty, DEFAULT_RUNTIME);
72 }
73
74 @Test
75 public void testSerialize() throws JsonProcessingException {
76 ObjectMapper jackson = new ObjectMapper();
77
78 String json = jackson.writeValueAsString(running1).trim();
79 System.out.println(json);
80 assertEquals(asString, json);
81 }
82
83 // no deserializer test for RunningParty because it can't be deserialized.
84
85 @Test
86 public void testConnect() {
87 RunningParty rparty = new RunningParty(party1, id1, "name1", now, end,
88 null);
89 PartySocket connection = mock(PartySocket.class);
90 rparty = rparty.withConnection(connection);
91 }
92
93 @Test(expected = IllegalStateException.class)
94 public void testConnectTwice() {
95 RunningParty rparty = new RunningParty(party1, id1, "name1", now, end,
96 null);
97 PartySocket connection = mock(PartySocket.class);
98 rparty = rparty.withConnection(connection);
99 rparty = rparty.withConnection(connection);
100 }
101
102 /**
103 * Check that inform messages are forwarded to actual party
104 */
105 @Test
106 public void testInform() {
107 RunningParty rparty = new RunningParty(party1, id1, "name1", now, end,
108 null);
109 PartySocket connection = mock(PartySocket.class);
110 rparty = rparty.withConnection(connection);
111
112 Inform info = mock(Inform.class);
113 rparty.inform(info);
114
115 ArgumentCaptor<Inform> argument = ArgumentCaptor.forClass(Inform.class);
116 verify(connection, times(1)).notifyListeners(argument.capture());
117 assertEquals(info, argument.getValue());
118 }
119
120 @Test
121 public void testSquash() {
122 assertEquals("12__3", running1.squash("12&$3"));
123 }
124
125 @Test
126 public void testSquash1() {
127 assertEquals("p12__3", running1.squash1("12&$3"));
128 assertEquals("party_32_12", running1.squash1("party+32=12"));
129 assertEquals("party_32_12", running1.squash1("party 32 12"));
130 }
131
132 @Test
133 public void testWeirdNames() {
134 new RunningParty(party1, id1, "bla#@12- @1/3", now, end, null);
135 new RunningParty(party1, id1, "12-bla#@12- @1/3", now, end, null);
136 }
137
138}
139
140class SimpleParty extends DefaultParty {
141
142 @Override
143 public Capabilities getCapabilities() {
144 return null;
145 }
146
147 @Override
148 public String getDescription() {
149 return null;
150 }
151
152 @Override
153 public void notifyChange(Inform data) {
154
155 }
156
157}
Note: See TracBrowser for help on using the repository browser.