package actions; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; 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.actions.Action; import geniusweb.actions.PartyId; import geniusweb.actions.Vote; 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}}"; private static Vote vote1, vote1a, vote2, vote3, vote4; static { issuevalues.put(ISSUE1, VALUE1); issuevalues.put(ISSUE2, VALUE2); bid = new Bid(issuevalues); vote1 = new Vote(id, bid, 1); vote1a = new Vote(id, bid, 1); vote2 = new Vote(id2, bid, 1); // values swapped, so different issuevalues. issuevaluesb.put(ISSUE1, VALUE2); issuevaluesb.put(ISSUE2, VALUE2); bidb = new Bid(issuevaluesb); vote3 = new Vote(id, bidb, 1); vote4 = new Vote(id, bid, 2); } @Override public List> getGeneralTestData() { List> list = new LinkedList<>(); list.add(Arrays.asList(vote1, vote1a)); list.add(Arrays.asList(vote2)); list.add(Arrays.asList(vote3)); list.add(Arrays.asList(vote4)); return list; } @Override public List getGeneralTestStrings() { return Arrays.asList("Vote.*party1.*issue2=10.*issue1=.value1.*1.*", "Vote.*party2.*issue2=10.*issue1=.value1.*1.*", "Vote.*party1.*issue2=10.*issue1=10.*1.*", "Vote.*party1.*issue2=10.*issue1=.value1.*2.*"); } @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); } }