[48] | 1 | package geniusweb.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.issuevalue.Bid;
|
---|
| 18 | import geniusweb.issuevalue.DiscreteValue;
|
---|
| 19 | import geniusweb.issuevalue.NumberValue;
|
---|
| 20 | import geniusweb.issuevalue.Value;
|
---|
| 21 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 22 |
|
---|
| 23 | public class VoteWithValueTest extends GeneralTests<VoteWithValue> {
|
---|
| 24 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
| 25 |
|
---|
| 26 | private static final PartyId id = new PartyId("party1");
|
---|
| 27 | private static final PartyId id2 = new PartyId("party2");
|
---|
| 28 | private final static Map<String, Value> issuevalues = new HashMap<String, Value>();
|
---|
| 29 | private final static Map<String, Value> issuevaluesb = new HashMap<String, Value>();
|
---|
| 30 | private static Bid bid;
|
---|
| 31 | private static Bid bidb;
|
---|
| 32 | private final static String ISSUE1 = "issue1";
|
---|
| 33 | private final static Value VALUE1 = new DiscreteValue("value1");
|
---|
| 34 | private final static String ISSUE2 = "issue2";
|
---|
| 35 | private final static Value VALUE2 = new NumberValue("10");
|
---|
| 36 | // issue 2 is NUMBER and thus serializes with leading '='
|
---|
| 37 | private final String votestring = "{\"VoteWithValue\":{\"actor\":\"party1\",\"bid\":{\"issuevalues\":{\"issue2\":10,\"issue1\":\"value1\"}},\"minPower\":1,\"maxPower\":2,\"value\":10}}";
|
---|
| 38 |
|
---|
| 39 | private static VoteWithValue vote1, vote1a, vote2, vote3, vote4, vote5,
|
---|
| 40 | vote6;
|
---|
| 41 |
|
---|
| 42 | static {
|
---|
| 43 | issuevalues.put(ISSUE1, VALUE1);
|
---|
| 44 | issuevalues.put(ISSUE2, VALUE2);
|
---|
| 45 | bid = new Bid(issuevalues);
|
---|
| 46 | vote1 = new VoteWithValue(id, bid, 1, 2, 10);
|
---|
| 47 | vote1a = new VoteWithValue(id, bid, 1, 2, 10);
|
---|
| 48 |
|
---|
| 49 | vote2 = new VoteWithValue(id2, bid, 1, 2, 10);
|
---|
| 50 |
|
---|
| 51 | // values swapped, so different issuevalues.
|
---|
| 52 | issuevaluesb.put(ISSUE1, VALUE2);
|
---|
| 53 | issuevaluesb.put(ISSUE2, VALUE2);
|
---|
| 54 | bidb = new Bid(issuevaluesb);
|
---|
| 55 | vote3 = new VoteWithValue(id, bidb, 1, 2, 10);
|
---|
| 56 |
|
---|
| 57 | vote4 = new VoteWithValue(id, bid, 2, 2, 10);
|
---|
| 58 | vote5 = new VoteWithValue(id, bid, 1, 3, 10);
|
---|
| 59 |
|
---|
| 60 | vote6 = new VoteWithValue(id, bid, 1, 2, 20);
|
---|
| 61 |
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | @Override
|
---|
| 65 | public List<List<VoteWithValue>> getGeneralTestData() {
|
---|
| 66 | return Arrays.asList(Arrays.asList(vote1, vote1a), Arrays.asList(vote2),
|
---|
| 67 | Arrays.asList(vote3), Arrays.asList(vote4),
|
---|
| 68 | Arrays.asList(vote5), Arrays.asList(vote6));
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public List<String> getGeneralTestStrings() {
|
---|
| 73 | return Arrays.asList(
|
---|
| 74 | "VoteWithValue.*party1.*issue2=10.*issue1=.value1.*1.*2.*10.*",
|
---|
| 75 | "VoteWithValue.*party2.*issue2=10.*issue1=.value1.*1.*2.*10.*",
|
---|
| 76 | "VoteWithValue.*party1.*issue2=10.*issue1=10.*1.*2.*10.*",
|
---|
| 77 | "VoteWithValue.*party1.*issue2=10.*issue1=.value1.*2.*2.*10.*",
|
---|
| 78 | "VoteWithValue.*party1.*issue2=10.*issue1=.value1.*1.*3.*10.*",
|
---|
| 79 | "VoteWithValue.*party1.*issue2=10.*issue1=.value1.*1.*2.*20.*");
|
---|
| 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 | }
|
---|