1 | package geniusweb.party.inform;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.math.BigDecimal;
|
---|
7 | import java.net.URISyntaxException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import org.junit.Before;
|
---|
14 | import org.junit.Test;
|
---|
15 |
|
---|
16 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
17 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
18 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
19 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
20 |
|
---|
21 | import geniusweb.actions.Accept;
|
---|
22 | import geniusweb.actions.PartyId;
|
---|
23 | import geniusweb.inform.ActionDone;
|
---|
24 | import geniusweb.inform.Inform;
|
---|
25 | import geniusweb.issuevalue.Bid;
|
---|
26 | import geniusweb.issuevalue.DiscreteValue;
|
---|
27 | import geniusweb.issuevalue.NumberValue;
|
---|
28 | import geniusweb.issuevalue.Value;
|
---|
29 | import tudelft.utilities.junit.GeneralTests;
|
---|
30 |
|
---|
31 | public class ActionDoneTest extends GeneralTests<ActionDone> {
|
---|
32 |
|
---|
33 | private ActionDone finished1, finished1a, finished2;
|
---|
34 |
|
---|
35 | private String asJson = "{\"ActionDone\":{\"action\":{\"accept\":{\"actor\":\"me\",\"bid\":{\"issuevalues\":{\"issue3\":9012,\"issue2\":1,\"issue1\":\"b\"}}}}}}";
|
---|
36 |
|
---|
37 | private PartyId me = new PartyId("me");
|
---|
38 |
|
---|
39 | @Before
|
---|
40 | public void before() throws URISyntaxException {
|
---|
41 | final String ISSUE1 = "issue1";
|
---|
42 | final String ISSUE2 = "issue2";
|
---|
43 | final Value VALUE1 = new DiscreteValue("value1");
|
---|
44 | final Value VALUE2 = new NumberValue("10");
|
---|
45 |
|
---|
46 | Map<String, Value> issuevalues = new HashMap<>();
|
---|
47 | issuevalues.put(ISSUE1, new DiscreteValue("b"));
|
---|
48 | issuevalues.put(ISSUE2, new NumberValue(BigDecimal.ONE));
|
---|
49 | issuevalues.put("issue3", new NumberValue(new BigDecimal("9012")));
|
---|
50 | Bid bid = new Bid(issuevalues);
|
---|
51 |
|
---|
52 | Map<String, Value> issuevaluesb = new HashMap<>();
|
---|
53 | issuevaluesb.put(ISSUE1, new DiscreteValue("b"));
|
---|
54 | issuevaluesb.put(ISSUE2, new NumberValue(BigDecimal.ONE));
|
---|
55 | Bid bidb = new Bid(issuevaluesb);
|
---|
56 |
|
---|
57 | // bidc and bidd have values swapped, so different issuevalues.
|
---|
58 | Map<String, Value> issuevaluesc = new HashMap<>();
|
---|
59 | issuevaluesc.put(ISSUE1, VALUE1);
|
---|
60 | issuevaluesc.put(ISSUE2, VALUE2);
|
---|
61 |
|
---|
62 | finished1 = new ActionDone(new Accept(me, bid));
|
---|
63 | finished1a = new ActionDone(new Accept(me, bid));
|
---|
64 | finished2 = new ActionDone(new Accept(me, bidb));
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public List<List<ActionDone>> getGeneralTestData() {
|
---|
69 | return Arrays.asList(Arrays.asList(finished1, finished1a),
|
---|
70 | Arrays.asList(finished2));
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public List<String> getGeneralTestStrings() {
|
---|
75 | return Arrays.asList("ActionDone.*Accept.*", "ActionDone.*Accept.*");
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Test
|
---|
79 | public void nullTest() throws URISyntaxException {
|
---|
80 | new ActionDone(null);
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Test
|
---|
84 | public void testSerialize() throws JsonProcessingException {
|
---|
85 | ObjectMapper jackson = new ObjectMapper();
|
---|
86 |
|
---|
87 | String json = jackson.writeValueAsString(finished1);
|
---|
88 | System.out.println(json);
|
---|
89 | assertEquals(asJson, json);
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Test
|
---|
93 | public void testDeserialize()
|
---|
94 | throws JsonParseException, JsonMappingException, IOException {
|
---|
95 | ObjectMapper jackson = new ObjectMapper();
|
---|
96 | ActionDone p = (ActionDone) jackson.readValue(asJson, Inform.class);
|
---|
97 | System.out.println(p);
|
---|
98 | assertEquals(finished1, p);
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|