1 | package geniusweb.inform;
|
---|
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.HashSet;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.junit.Test;
|
---|
12 |
|
---|
13 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
14 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
15 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
17 |
|
---|
18 | import geniusweb.actions.PartyId;
|
---|
19 | import geniusweb.actions.Vote;
|
---|
20 | import geniusweb.actions.Votes;
|
---|
21 | import geniusweb.issuevalue.Bid;
|
---|
22 | import geniusweb.issuevalue.DiscreteValue;
|
---|
23 | import tudelft.utilities.junit.GeneralTests;
|
---|
24 |
|
---|
25 | public class OptInTest extends GeneralTests<OptIn> {
|
---|
26 |
|
---|
27 | private Bid bid1 = new Bid("iss", new DiscreteValue("val1"));
|
---|
28 | private Bid bid2 = new Bid("iss", new DiscreteValue("val2"));
|
---|
29 | private PartyId partyA = new PartyId("partyA");
|
---|
30 | private PartyId partyB = new PartyId("partyB");
|
---|
31 |
|
---|
32 | private final Vote voteA1 = new Vote(partyA, bid1, 2, 9);
|
---|
33 | private final Vote voteB1 = new Vote(partyB, bid1, 2, 9);
|
---|
34 | private final Vote voteB2 = new Vote(partyB, bid2, 2, 9);
|
---|
35 |
|
---|
36 | private Votes votesA = new Votes(partyA, Collections.singleton(voteA1));
|
---|
37 | private Votes votesB = new Votes(partyB,
|
---|
38 | new HashSet<>(Arrays.asList(voteB1, voteB2)));
|
---|
39 |
|
---|
40 | private final OptIn optIn1 = new OptIn(Arrays.asList(votesA, votesB));
|
---|
41 | private final OptIn optIn1a = new OptIn(Arrays.asList(votesA, votesB));
|
---|
42 | private final OptIn optIn2 = new OptIn(Arrays.asList(votesA));
|
---|
43 |
|
---|
44 | private String asJson = "{\"OptIn\":{\"votes\":[{\"Votes\":{\"actor\":\"partyA\",\"votes\":[{\"Vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"iss\":\"val1\"}},\"minPower\":2,\"maxPower\":9}}]}},{\"Votes\":{\"actor\":\"partyB\",\"votes\":[{\"Vote\":{\"actor\":\"partyB\",\"bid\":{\"issuevalues\":{\"iss\":\"val2\"}},\"minPower\":2,\"maxPower\":9}},{\"Vote\":{\"actor\":\"partyB\",\"bid\":{\"issuevalues\":{\"iss\":\"val1\"}},\"minPower\":2,\"maxPower\":9}}]}}]}}";
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public List<List<OptIn>> getGeneralTestData() {
|
---|
48 | return Arrays.asList(Arrays.asList(optIn1, optIn1a),
|
---|
49 | Arrays.asList(optIn2));
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public List<String> getGeneralTestStrings() {
|
---|
54 | return Arrays.asList(
|
---|
55 | "OptIn.*Votes.*Vote.*partyA.*iss.*val1.*2.*Votes.*Vote.*partyB.*iss.*val2.*.*2.*Vote.*partyB.*iss.*val1.*2.*",
|
---|
56 | "OptIn.*Votes.*Vote.*partyA.*iss.*val.*2.*");
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Test
|
---|
60 | public void testSerialize() throws JsonProcessingException {
|
---|
61 | ObjectMapper jackson = new ObjectMapper();
|
---|
62 |
|
---|
63 | String json = jackson.writeValueAsString(optIn1);
|
---|
64 | System.out.println(json);
|
---|
65 | assertEquals(asJson, json);
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Test
|
---|
69 | public void testDeserialize()
|
---|
70 | throws JsonParseException, JsonMappingException, IOException {
|
---|
71 | ObjectMapper jackson = new ObjectMapper();
|
---|
72 | Inform p = jackson.readValue(asJson, Inform.class);
|
---|
73 | System.out.println(p);
|
---|
74 | assertEquals(optIn1, p);
|
---|
75 | }
|
---|
76 |
|
---|
77 | }
|
---|