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