[44] | 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.VoteWithValue;
|
---|
| 20 | import geniusweb.actions.VotesWithValue;
|
---|
| 21 | import geniusweb.issuevalue.Bid;
|
---|
| 22 | import geniusweb.issuevalue.DiscreteValue;
|
---|
| 23 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 24 |
|
---|
| 25 | public 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 | }
|
---|