1 | package actions;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import org.junit.Before;
|
---|
13 | import org.junit.Test;
|
---|
14 |
|
---|
15 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
17 |
|
---|
18 | import geniusweb.actions.Action;
|
---|
19 | import geniusweb.actions.Comparison;
|
---|
20 | import geniusweb.actions.PartyId;
|
---|
21 | import geniusweb.issuevalue.Bid;
|
---|
22 | import geniusweb.issuevalue.DiscreteValue;
|
---|
23 | import geniusweb.issuevalue.NumberValue;
|
---|
24 | import geniusweb.issuevalue.Value;
|
---|
25 | import tudelft.utilities.junit.GeneralTests;
|
---|
26 |
|
---|
27 | public class ComparisonTest extends GeneralTests<Comparison> {
|
---|
28 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
29 |
|
---|
30 | private static final PartyId id = new PartyId("party1");
|
---|
31 | private static final PartyId id2 = new PartyId("party2");
|
---|
32 | private final static Map<String, Value> issuevalues = new HashMap<String, Value>();
|
---|
33 | private final static Map<String, Value> issuevaluesb = new HashMap<String, Value>();
|
---|
34 | private static Bid bid;
|
---|
35 | private static Bid bidb;
|
---|
36 | private final static String ISSUE1 = "issue1";
|
---|
37 | private final static Value VALUE1 = new DiscreteValue("value1");
|
---|
38 | private final static String ISSUE2 = "issue2";
|
---|
39 | private final static Value VALUE2 = new NumberValue("10");
|
---|
40 | // issue 2 is NUMBER and thus serializes with leading '='
|
---|
41 | private final String acceptstring = "{\"Comparison\":{\"bid\":{\"issuevalues\":{\"issue2\":10,\"issue1\":\"value1\"}},\"better\":[],\"worse\":[],\"actor\":\"party1\"}}";
|
---|
42 |
|
---|
43 | private static Comparison accept, accept1, accept2, acceptb, acceptc,
|
---|
44 | acceptd;
|
---|
45 |
|
---|
46 | @Before
|
---|
47 | public void before() {
|
---|
48 | issuevalues.put(ISSUE1, VALUE1);
|
---|
49 | issuevalues.put(ISSUE2, VALUE2);
|
---|
50 | bid = new Bid(issuevalues);
|
---|
51 | List<Bid> better = Collections.EMPTY_LIST;
|
---|
52 | List<Bid> worse = Collections.EMPTY_LIST;
|
---|
53 | accept = new Comparison(id, bid, better, worse);
|
---|
54 | accept1 = new Comparison(id, bid, better, worse);
|
---|
55 |
|
---|
56 | accept2 = new Comparison(id2, bid, better, worse);
|
---|
57 |
|
---|
58 | // values swapped, so different issuevalues.
|
---|
59 | issuevaluesb.put(ISSUE1, VALUE2);
|
---|
60 | issuevaluesb.put(ISSUE2, VALUE2);
|
---|
61 | bidb = new Bid(issuevaluesb);
|
---|
62 | acceptb = new Comparison(id, bidb, better, worse);
|
---|
63 |
|
---|
64 | List<Bid> better1 = Arrays.asList(bid);
|
---|
65 | List<Bid> worse1 = Arrays.asList(bidb);
|
---|
66 | acceptc = new Comparison(id, bidb, better1, worse);
|
---|
67 | acceptd = new Comparison(id, bidb, better, worse1);
|
---|
68 |
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public List<List<Comparison>> getGeneralTestData() {
|
---|
73 | return Arrays.asList(Arrays.asList(accept, accept1),
|
---|
74 | Arrays.asList(accept2), Arrays.asList(acceptb),
|
---|
75 | Arrays.asList(acceptc), Arrays.asList(acceptd));
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public List<String> getGeneralTestStrings() {
|
---|
80 | return Arrays.asList(
|
---|
81 | "Comparison.*Bid.*issue2=10.*issue1=\"value1\".*better=\\[\\].*worse=\\[\\].*",
|
---|
82 | "Comparison.*party2.*Bid.*issue2=10.*issue1=\"value1\".*better=\\[\\].*worse=\\[\\].*",
|
---|
83 | "Comparison.*party1.*Bid.*issue2=10.*issue1=10.*better=\\[\\].*worse=\\[\\].*",
|
---|
84 | "Comparison.*party1.*Bid.*issue2=10.*issue1=10.*better=\\[Bid.*issue2=10.*issue1=\"value1\".*\\].*worse=\\[\\].*",
|
---|
85 | "Comparison.*party1.*Bid.*issue2=10.*issue1=10.*better=\\[\\].*worse=\\[Bid.*issue2=10.*issue1=10.*\\].*");
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Test
|
---|
89 | public void serializeAcceptTest() throws JsonProcessingException {
|
---|
90 | System.out.println(jackson.writeValueAsString(accept));
|
---|
91 | assertEquals(acceptstring, jackson.writeValueAsString(accept));
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Test
|
---|
95 | public void deserializeAcceptTest() throws IOException {
|
---|
96 | Action act = jackson.readValue(acceptstring, Action.class);
|
---|
97 | assertEquals(accept, act);
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|