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