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.List;
|
---|
9 | import java.util.Map;
|
---|
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.Offer;
|
---|
19 | import geniusweb.actions.PartyId;
|
---|
20 | import geniusweb.issuevalue.Bid;
|
---|
21 | import geniusweb.issuevalue.DiscreteValue;
|
---|
22 | import tudelft.utilities.junit.GeneralTests;
|
---|
23 |
|
---|
24 | public class VotingTest extends GeneralTests<Voting> {
|
---|
25 |
|
---|
26 | private PartyId party1 = new PartyId("party1");
|
---|
27 | private Bid bid1 = new Bid("iss", new DiscreteValue("val1"));
|
---|
28 | private Bid bid2 = new Bid("iss", new DiscreteValue("val2"));
|
---|
29 |
|
---|
30 | private Map<PartyId, Integer> powers1 = Collections.singletonMap(party1, 2);
|
---|
31 | private Map<PartyId, Integer> powers2 = Collections.singletonMap(party1, 3);
|
---|
32 |
|
---|
33 | private final Voting voting1 = new Voting(
|
---|
34 | Arrays.asList(new Offer(party1, bid1)), powers1);
|
---|
35 | private final Voting voting1a = new Voting(
|
---|
36 | Arrays.asList(new Offer(party1, bid1)), powers1);
|
---|
37 | private final Voting voting2 = new Voting(
|
---|
38 | Arrays.asList(new Offer(party1, bid2)), powers1);
|
---|
39 | private final Voting voting3 = new Voting(
|
---|
40 | Arrays.asList(new Offer(party1, bid1)), powers2);
|
---|
41 |
|
---|
42 | private String asJson = "{\"Voting\":{\"offers\":[{\"Offer\":{\"actor\":\"party1\",\"bid\":{\"issuevalues\":{\"iss\":\"val1\"}}}}],\"powers\":{\"party1\":2}}}";
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public List<List<Voting>> getGeneralTestData() {
|
---|
46 | return Arrays.asList(Arrays.asList(voting1, voting1a),
|
---|
47 | Arrays.asList(voting2), Arrays.asList(voting3));
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public List<String> getGeneralTestStrings() {
|
---|
52 | return Arrays.asList("Voting.*Bid.*iss.*val1.*party1.*2.*",
|
---|
53 | "Voting.*Bid.*iss.*val2.*party1.*2.*",
|
---|
54 | "Voting.*Bid.*iss.*val1.*party1.*3.*");
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Test
|
---|
58 | public void testSerialize() throws JsonProcessingException {
|
---|
59 | ObjectMapper jackson = new ObjectMapper();
|
---|
60 |
|
---|
61 | String json = jackson.writeValueAsString(voting1);
|
---|
62 | System.out.println(json);
|
---|
63 | assertEquals(asJson, json);
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Test
|
---|
67 | public void testDeserialize()
|
---|
68 | throws JsonParseException, JsonMappingException, IOException {
|
---|
69 | ObjectMapper jackson = new ObjectMapper();
|
---|
70 | Inform p = jackson.readValue(asJson, Inform.class);
|
---|
71 | System.out.println(p);
|
---|
72 | assertEquals(voting1, p);
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|