1 | package geniusweb.events;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import org.junit.Test;
|
---|
11 | import org.junit.experimental.theories.DataPoint;
|
---|
12 | import org.junit.experimental.theories.DataPoints;
|
---|
13 |
|
---|
14 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
15 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
16 |
|
---|
17 | import geniusweb.actions.PartyId;
|
---|
18 | import tudelft.utilities.junit.GeneralTests;
|
---|
19 |
|
---|
20 | public class SessionStartedTest extends GeneralTests<SessionStarted> {
|
---|
21 |
|
---|
22 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
23 |
|
---|
24 | private static final Long SESSIONNR = 1l;
|
---|
25 | protected static final Long NOW = (long) 101;
|
---|
26 | protected static final Long NOW1 = NOW + 1;
|
---|
27 |
|
---|
28 | private static final PartyId PARTY1 = new PartyId("party1");
|
---|
29 | private static final PartyId PARTY2 = new PartyId("party2");
|
---|
30 |
|
---|
31 | private static List<PartyId> parties = Arrays.asList(PARTY1, PARTY2);
|
---|
32 |
|
---|
33 | private static final SessionStarted sessionStartedLater = new SessionStarted(
|
---|
34 | SESSIONNR, parties, NOW1);
|
---|
35 | private static final SessionStarted othersessionStarted = new SessionStarted(
|
---|
36 | SESSIONNR + 1, parties, NOW);
|
---|
37 |
|
---|
38 | public static SessionStarted sessionStarted = new SessionStarted(SESSIONNR,
|
---|
39 | parties, NOW);
|
---|
40 | private static final SessionStarted sessionStarted1 = new SessionStarted(
|
---|
41 | SESSIONNR, parties, NOW);
|
---|
42 | // jackson expected format.
|
---|
43 | private static String sessionstartedstring = "{\"SessionStarted\":{\"sessionNumber\":1,\"parties\":[\"party1\",\"party2\"],\"time\":101}}";
|
---|
44 | // "{\"sessionstarted\":{\"time\":101,\"sessionNumber\":1,\"parties\":[\"party1\",\"party2\"]}}";
|
---|
45 | public static String expectedstring = "SessionStarted.*101.*";
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public List<List<SessionStarted>> getGeneralTestData() {
|
---|
49 | List<List<SessionStarted>> list = new LinkedList<>();
|
---|
50 | list.add(Arrays.asList(sessionStarted, sessionStarted1));
|
---|
51 | list.add(Arrays.asList(sessionStartedLater));
|
---|
52 | list.add(Arrays.asList(othersessionStarted));
|
---|
53 | return list;
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public List<String> getGeneralTestStrings() {
|
---|
58 | return Arrays.asList("SessionStarted.*" + SESSIONNR + ".*" + NOW + ".*",
|
---|
59 | "SessionStarted.*" + SESSIONNR + ".*" + NOW1 + ".*",
|
---|
60 | "SessionStarted.*" + (SESSIONNR + 1) + ".*" + NOW + ".*");
|
---|
61 | }
|
---|
62 |
|
---|
63 | @DataPoints("equal")
|
---|
64 | public static SessionStarted[] equal() {
|
---|
65 | return new SessionStarted[] { sessionStarted, sessionStarted1 };
|
---|
66 | }
|
---|
67 |
|
---|
68 | @DataPoints("notequal")
|
---|
69 | public static SessionStarted[] notequal() {
|
---|
70 | return new SessionStarted[] { sessionStartedLater,
|
---|
71 | sessionStartedLater };
|
---|
72 | }
|
---|
73 |
|
---|
74 | @DataPoint("equalstring")
|
---|
75 |
|
---|
76 | @Test
|
---|
77 | public void serializeStartedTest() throws JsonProcessingException {
|
---|
78 | System.out.println(jackson.writeValueAsString(sessionStarted));
|
---|
79 | assertEquals(sessionstartedstring,
|
---|
80 | jackson.writeValueAsString(sessionStarted));
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Test
|
---|
84 | public void deserializeStartedTest() throws IOException {
|
---|
85 | NegotiationEvent evt = jackson.readValue(sessionstartedstring,
|
---|
86 | NegotiationEvent.class);
|
---|
87 | assertEquals(SessionStarted.class, evt.getClass());
|
---|
88 | SessionStarted event = (SessionStarted) evt;
|
---|
89 | // compare fields, as sessionended is a derived/new inner class
|
---|
90 | assertEquals(NOW, event.getTime());
|
---|
91 | assertEquals(SESSIONNR, event.getSessionNumber());
|
---|
92 | assertEquals(2, event.getParties().size());
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|