package actions; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.util.Arrays; import java.util.Collections; 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.actions.Action; import geniusweb.actions.Comparison; import geniusweb.actions.PartyId; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.DiscreteValue; import geniusweb.issuevalue.NumberValue; import geniusweb.issuevalue.Value; import tudelft.utilities.junit.GeneralTests; public class ComparisonTest 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 acceptstring = "{\"Comparison\":{\"bid\":{\"issuevalues\":{\"issue2\":10,\"issue1\":\"value1\"}},\"better\":[],\"worse\":[],\"actor\":\"party1\"}}"; private static Comparison accept, accept1, accept2, acceptb, acceptc, acceptd; @Before public void before() { issuevalues.put(ISSUE1, VALUE1); issuevalues.put(ISSUE2, VALUE2); bid = new Bid(issuevalues); List better = Collections.EMPTY_LIST; List worse = Collections.EMPTY_LIST; accept = new Comparison(id, bid, better, worse); accept1 = new Comparison(id, bid, better, worse); accept2 = new Comparison(id2, bid, better, worse); // values swapped, so different issuevalues. issuevaluesb.put(ISSUE1, VALUE2); issuevaluesb.put(ISSUE2, VALUE2); bidb = new Bid(issuevaluesb); acceptb = new Comparison(id, bidb, better, worse); List better1 = Arrays.asList(bid); List worse1 = Arrays.asList(bidb); acceptc = new Comparison(id, bidb, better1, worse); acceptd = new Comparison(id, bidb, better, worse1); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(accept, accept1), Arrays.asList(accept2), Arrays.asList(acceptb), Arrays.asList(acceptc), Arrays.asList(acceptd)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "Comparison.*Bid.*issue2=10.*issue1=\"value1\".*better=\\[\\].*worse=\\[\\].*", "Comparison.*party2.*Bid.*issue2=10.*issue1=\"value1\".*better=\\[\\].*worse=\\[\\].*", "Comparison.*party1.*Bid.*issue2=10.*issue1=10.*better=\\[\\].*worse=\\[\\].*", "Comparison.*party1.*Bid.*issue2=10.*issue1=10.*better=\\[Bid.*issue2=10.*issue1=\"value1\".*\\].*worse=\\[\\].*", "Comparison.*party1.*Bid.*issue2=10.*issue1=10.*better=\\[\\].*worse=\\[Bid.*issue2=10.*issue1=10.*\\].*"); } @Test public void serializeAcceptTest() throws JsonProcessingException { System.out.println(jackson.writeValueAsString(accept)); assertEquals(acceptstring, jackson.writeValueAsString(accept)); } @Test public void deserializeAcceptTest() throws IOException { Action act = jackson.readValue(acceptstring, Action.class); assertEquals(accept, act); } }