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

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

Initial Release

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