package geniusweb.actions; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Before; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.DiscreteValue; import geniusweb.issuevalue.NumberValue; import geniusweb.issuevalue.Value; import tudelft.utilities.junit.GeneralTests; public class VoteTest extends GeneralTests { private final ObjectMapper jackson = new ObjectMapper(); private static final PartyId id = new PartyId("party1"); private static final PartyId id2 = new PartyId("party2"); private final static Map issuevalues = new HashMap(); private final static Map issuevaluesb = new HashMap(); private static Bid bid; private static Bid bidb; private final static String ISSUE1 = "issue1"; private final static Value VALUE1 = new DiscreteValue("value1"); private final static String ISSUE2 = "issue2"; private final static Value VALUE2 = new NumberValue("10"); // issue 2 is NUMBER and thus serializes with leading '=' private final String votestring = "{\"vote\":{\"actor\":\"party1\",\"bid\":{\"issuevalues\":{\"issue2\":10,\"issue1\":\"value1\"}},\"minPower\":1,\"maxPower\":2}}"; private static Vote vote1, vote1a, vote2, vote3, vote4, vote5; static { issuevalues.put(ISSUE1, VALUE1); issuevalues.put(ISSUE2, VALUE2); bid = new Bid(issuevalues); vote1 = new Vote(id, bid, 1, 2); vote1a = new Vote(id, bid, 1, 2); vote2 = new Vote(id2, bid, 1, 2); // values swapped, so different issuevalues. issuevaluesb.put(ISSUE1, VALUE2); issuevaluesb.put(ISSUE2, VALUE2); bidb = new Bid(issuevaluesb); vote3 = new Vote(id, bidb, 1, 2); vote4 = new Vote(id, bid, 2, 2); vote5 = new Vote(id, bid, 1, 3); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(vote1, vote1a), Arrays.asList(vote2), Arrays.asList(vote3), Arrays.asList(vote4), Arrays.asList(vote5)); } @Override public List getGeneralTestStrings() { return Arrays.asList("Vote.*party1.*issue2=10.*issue1=.value1.*1.*2.*", "Vote.*party2.*issue2=10.*issue1=.value1.*1.*2.*", "Vote.*party1.*issue2=10.*issue1=10.*1.*2.*", "Vote.*party1.*issue2=10.*issue1=.value1.*2.*2.*", "Vote.*party1.*issue2=10.*issue1=.value1.*1.*3.*"); } @Before public void before() { } @Test public void serializeTest() throws JsonProcessingException { System.out.println(jackson.writeValueAsString(vote1)); assertEquals(votestring, jackson.writeValueAsString(vote1)); } @Test public void deserializeAcceptTest() throws IOException { Action act = jackson.readValue(votestring, Action.class); assertEquals(vote1, act); } }