source: events/src/test/java/geniusweb/inform/OptInWithValueTest.java@ 33

Last change on this file since 33 was 33, checked in by bart, 3 years ago

Major update. Changes in json serialization, maven dependencies and BOA. Fix in stand-alone runner.

File size: 3.2 KB
Line 
1package geniusweb.inform;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.IOException;
6import java.util.Arrays;
7import java.util.Collections;
8import java.util.HashSet;
9import java.util.List;
10
11import org.junit.Test;
12
13import com.fasterxml.jackson.core.JsonParseException;
14import com.fasterxml.jackson.core.JsonProcessingException;
15import com.fasterxml.jackson.databind.JsonMappingException;
16import com.fasterxml.jackson.databind.ObjectMapper;
17
18import geniusweb.actions.PartyId;
19import geniusweb.actions.VoteWithValue;
20import geniusweb.actions.VotesWithValue;
21import geniusweb.issuevalue.Bid;
22import geniusweb.issuevalue.DiscreteValue;
23import tudelft.utilities.junit.GeneralTests;
24
25public class OptInWithValueTest extends GeneralTests<OptInWithValue> {
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 VoteWithValue voteA1 = new VoteWithValue(partyA, bid1, 2, 9,
33 100);
34 private final VoteWithValue voteB1 = new VoteWithValue(partyB, bid1, 2, 9,
35 60);
36 private final VoteWithValue voteB2 = new VoteWithValue(partyB, bid2, 2, 9,
37 40);
38
39 private VotesWithValue votesA = new VotesWithValue(partyA,
40 Collections.singleton(voteA1));
41 private VotesWithValue votesB = new VotesWithValue(partyB,
42 new HashSet<>(Arrays.asList(voteB1, voteB2)));
43
44 private final OptInWithValue optIn1 = new OptInWithValue(
45 Arrays.asList(votesA, votesB));
46 private final OptInWithValue optIn1a = new OptInWithValue(
47 Arrays.asList(votesA, votesB));
48 private final OptInWithValue optIn2 = new OptInWithValue(
49 Arrays.asList(votesA));
50
51 private String asJson = "{\"OptInWithValue\":{\"votes\":[{\"VotesWithValue\":{\"actor\":\"partyA\",\"votes\":[{\"VoteWithValue\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"iss\":\"val1\"}},\"minPower\":2,\"maxPower\":9,\"value\":100}}]}},{\"VotesWithValue\":{\"actor\":\"partyB\",\"votes\":[{\"VoteWithValue\":{\"actor\":\"partyB\",\"bid\":{\"issuevalues\":{\"iss\":\"val1\"}},\"minPower\":2,\"maxPower\":9,\"value\":60}},{\"VoteWithValue\":{\"actor\":\"partyB\",\"bid\":{\"issuevalues\":{\"iss\":\"val2\"}},\"minPower\":2,\"maxPower\":9,\"value\":40}}]}}]}}";
52
53 @Override
54 public List<List<OptInWithValue>> getGeneralTestData() {
55 return Arrays.asList(Arrays.asList(optIn1, optIn1a),
56 Arrays.asList(optIn2));
57 }
58
59 @Override
60 public List<String> getGeneralTestStrings() {
61 return Arrays.asList(
62 "OptInWithValue.*VotesWithValue.*partyA.*VoteWithValue.*partyA.*iss.*val1.*2.*VotesWithValue.*partyB.*VoteWithValue.*partyB.*iss.*val1.*.*2.*VoteWithValue.*partyB.*iss.*val2.*2.*",
63 "OptIn.*Votes.*Vote.*partyA.*iss.*val.*2.*");
64 }
65
66 @Test
67 public void testSerialize() throws JsonProcessingException {
68 ObjectMapper jackson = new ObjectMapper();
69
70 String json = jackson.writeValueAsString(optIn1);
71 System.out.println(json);
72 assertEquals(asJson, json);
73 }
74
75 @Test
76 public void testDeserialize()
77 throws JsonParseException, JsonMappingException, IOException {
78 ObjectMapper jackson = new ObjectMapper();
79 Inform p = jackson.readValue(asJson, Inform.class);
80 System.out.println(p);
81 assertEquals(optIn1, p);
82 }
83
84}
Note: See TracBrowser for help on using the repository browser.