source: events/src/test/java/actions/ComparisonTest.java@ 13

Last change on this file since 13 was 10, checked in by bart, 5 years ago

Update 28 jan 2020

File size: 3.5 KB
RevLine 
[10]1package actions;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.IOException;
6import java.util.Arrays;
7import java.util.Collections;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11
12import org.junit.Before;
13import org.junit.Test;
14
15import com.fasterxml.jackson.core.JsonProcessingException;
16import com.fasterxml.jackson.databind.ObjectMapper;
17
18import geniusweb.actions.Action;
19import geniusweb.actions.Comparison;
20import geniusweb.actions.PartyId;
21import geniusweb.issuevalue.Bid;
22import geniusweb.issuevalue.DiscreteValue;
23import geniusweb.issuevalue.NumberValue;
24import geniusweb.issuevalue.Value;
25import tudelft.utilities.junit.GeneralTests;
26
27public 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}
Note: See TracBrowser for help on using the repository browser.