[21] | 1 | package actions;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 |
|
---|
| 5 | import java.io.IOException;
|
---|
| 6 | import java.util.Arrays;
|
---|
| 7 | import java.util.HashMap;
|
---|
| 8 | import java.util.List;
|
---|
| 9 | import java.util.Map;
|
---|
| 10 |
|
---|
| 11 | import org.junit.Before;
|
---|
| 12 | import org.junit.Test;
|
---|
| 13 |
|
---|
| 14 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 15 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 16 |
|
---|
| 17 | import geniusweb.actions.Action;
|
---|
| 18 | import geniusweb.actions.PartyId;
|
---|
| 19 | import geniusweb.actions.Vote;
|
---|
| 20 | import geniusweb.issuevalue.Bid;
|
---|
| 21 | import geniusweb.issuevalue.DiscreteValue;
|
---|
| 22 | import geniusweb.issuevalue.NumberValue;
|
---|
| 23 | import geniusweb.issuevalue.Value;
|
---|
| 24 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 25 |
|
---|
| 26 | public class VoteTest extends GeneralTests<Vote> {
|
---|
| 27 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
| 28 |
|
---|
| 29 | private static final PartyId id = new PartyId("party1");
|
---|
| 30 | private static final PartyId id2 = new PartyId("party2");
|
---|
| 31 | private final static Map<String, Value> issuevalues = new HashMap<String, Value>();
|
---|
| 32 | private final static Map<String, Value> issuevaluesb = new HashMap<String, Value>();
|
---|
| 33 | private static Bid bid;
|
---|
| 34 | private static Bid bidb;
|
---|
| 35 | private final static String ISSUE1 = "issue1";
|
---|
| 36 | private final static Value VALUE1 = new DiscreteValue("value1");
|
---|
| 37 | private final static String ISSUE2 = "issue2";
|
---|
| 38 | private final static Value VALUE2 = new NumberValue("10");
|
---|
| 39 | // issue 2 is NUMBER and thus serializes with leading '='
|
---|
[22] | 40 | private final String votestring = "{\"vote\":{\"actor\":\"party1\",\"bid\":{\"issuevalues\":{\"issue2\":10,\"issue1\":\"value1\"}},\"minPower\":1,\"maxPower\":2}}";
|
---|
[21] | 41 |
|
---|
[22] | 42 | private static Vote vote1, vote1a, vote2, vote3, vote4, vote5;
|
---|
[21] | 43 |
|
---|
| 44 | static {
|
---|
| 45 | issuevalues.put(ISSUE1, VALUE1);
|
---|
| 46 | issuevalues.put(ISSUE2, VALUE2);
|
---|
| 47 | bid = new Bid(issuevalues);
|
---|
[22] | 48 | vote1 = new Vote(id, bid, 1, 2);
|
---|
| 49 | vote1a = new Vote(id, bid, 1, 2);
|
---|
[21] | 50 |
|
---|
[22] | 51 | vote2 = new Vote(id2, bid, 1, 2);
|
---|
[21] | 52 |
|
---|
| 53 | // values swapped, so different issuevalues.
|
---|
| 54 | issuevaluesb.put(ISSUE1, VALUE2);
|
---|
| 55 | issuevaluesb.put(ISSUE2, VALUE2);
|
---|
| 56 | bidb = new Bid(issuevaluesb);
|
---|
[22] | 57 | vote3 = new Vote(id, bidb, 1, 2);
|
---|
[21] | 58 |
|
---|
[22] | 59 | vote4 = new Vote(id, bid, 2, 2);
|
---|
| 60 | vote5 = new Vote(id, bid, 1, 3);
|
---|
[21] | 61 |
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | @Override
|
---|
| 65 | public List<List<Vote>> getGeneralTestData() {
|
---|
[22] | 66 | return Arrays.asList(Arrays.asList(vote1, vote1a), Arrays.asList(vote2),
|
---|
| 67 | Arrays.asList(vote3), Arrays.asList(vote4),
|
---|
| 68 | Arrays.asList(vote5));
|
---|
[21] | 69 | }
|
---|
| 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public List<String> getGeneralTestStrings() {
|
---|
[22] | 73 | return Arrays.asList("Vote.*party1.*issue2=10.*issue1=.value1.*1.*2.*",
|
---|
| 74 | "Vote.*party2.*issue2=10.*issue1=.value1.*1.*2.*",
|
---|
| 75 | "Vote.*party1.*issue2=10.*issue1=10.*1.*2.*",
|
---|
| 76 | "Vote.*party1.*issue2=10.*issue1=.value1.*2.*2.*",
|
---|
| 77 | "Vote.*party1.*issue2=10.*issue1=.value1.*1.*3.*");
|
---|
[21] | 78 | }
|
---|
| 79 |
|
---|
| 80 | @Before
|
---|
| 81 | public void before() {
|
---|
| 82 |
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | @Test
|
---|
| 86 | public void serializeTest() throws JsonProcessingException {
|
---|
| 87 | System.out.println(jackson.writeValueAsString(vote1));
|
---|
| 88 | assertEquals(votestring, jackson.writeValueAsString(vote1));
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | @Test
|
---|
| 92 | public void deserializeAcceptTest() throws IOException {
|
---|
| 93 | Action act = jackson.readValue(votestring, Action.class);
|
---|
| 94 | assertEquals(vote1, act);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | }
|
---|