1 | package geniusweb.events;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.mockito.Mockito.mock;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import org.junit.Test;
|
---|
11 |
|
---|
12 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
13 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
14 |
|
---|
15 | import geniusweb.actions.Action;
|
---|
16 | import geniusweb.actions.EndNegotiation;
|
---|
17 | import geniusweb.actions.PartyId;
|
---|
18 | import tudelft.utilities.junit.GeneralTests;
|
---|
19 |
|
---|
20 | public class ActionEventTest extends GeneralTests<ActionEvent> {
|
---|
21 | private static final long NOW = 101l;
|
---|
22 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
23 | private final String string = "{\"ActionEvent\":{\"action\":{\"EndNegotiation\":{\"actor\":\"party1\"}},\"time\":101}}";
|
---|
24 | private final PartyId id = new PartyId("party1");
|
---|
25 | private final Action action = new EndNegotiation(id);
|
---|
26 | private final ActionEvent evt = new ActionEvent(action, NOW);
|
---|
27 | private final ActionEvent evt1 = new ActionEvent(action, NOW);
|
---|
28 | private final ActionEvent evtb = new ActionEvent(action, NOW + 1);
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public List<List<ActionEvent>> getGeneralTestData() {
|
---|
32 | return Arrays.asList(Arrays.asList(evt, evt1), Arrays.asList(evtb));
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public List<String> getGeneralTestStrings() {
|
---|
37 | return Arrays.asList("ActionEvent\\[.*" + NOW + ".*EndNegotiation.*\\]",
|
---|
38 | "ActionEvent\\[.*" + (NOW + 1) + ".*EndNegotiation.*\\]");
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Test
|
---|
42 | public void smoketest() {
|
---|
43 | Action action = mock(EndNegotiation.class);
|
---|
44 | ActionEvent evt = new ActionEvent(action, 0l);
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Test
|
---|
48 | public void serializeTest() throws JsonProcessingException {
|
---|
49 | System.out.println(jackson.writeValueAsString(evt));
|
---|
50 | assertEquals(string, jackson.writeValueAsString(evt));
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Test
|
---|
54 | public void deserializeTestReadActionEvent() throws IOException {
|
---|
55 | ActionEvent evt1 = jackson.readValue(string, ActionEvent.class);
|
---|
56 | // compare fields, as evt is a derived/new inner class
|
---|
57 | assertEquals(EndNegotiation.class, evt1.getAction().getClass());
|
---|
58 | assertEquals((Long) 101l, evt1.getTime());
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Test
|
---|
62 | public void deserializeTestReadEvent() throws IOException {
|
---|
63 | NegotiationEvent evt1 = jackson.readValue(string,
|
---|
64 | NegotiationEvent.class);
|
---|
65 | assertEquals(ActionEvent.class, evt1.getClass());
|
---|
66 | ActionEvent ev1 = (ActionEvent) evt1;
|
---|
67 | assertEquals(EndNegotiation.class, ev1.getAction().getClass());
|
---|
68 | assertEquals((Long) 101l, ev1.getTime());
|
---|
69 | }
|
---|
70 |
|
---|
71 | }
|
---|