package geniusweb.inform; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import geniusweb.actions.PartyId; import geniusweb.actions.VoteWithValue; import geniusweb.actions.VotesWithValue; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.DiscreteValue; import tudelft.utilities.junit.GeneralTests; public class OptInWithValueTest extends GeneralTests { private Bid bid1 = new Bid("iss", new DiscreteValue("val1")); private Bid bid2 = new Bid("iss", new DiscreteValue("val2")); private PartyId partyA = new PartyId("partyA"); private PartyId partyB = new PartyId("partyB"); private final VoteWithValue voteA1 = new VoteWithValue(partyA, bid1, 2, 9, 100); private final VoteWithValue voteB1 = new VoteWithValue(partyB, bid1, 2, 9, 60); private final VoteWithValue voteB2 = new VoteWithValue(partyB, bid2, 2, 9, 40); private VotesWithValue votesA = new VotesWithValue(partyA, Collections.singleton(voteA1)); private VotesWithValue votesB = new VotesWithValue(partyB, new HashSet<>(Arrays.asList(voteB1, voteB2))); private final OptInWithValue optIn1 = new OptInWithValue( Arrays.asList(votesA, votesB)); private final OptInWithValue optIn1a = new OptInWithValue( Arrays.asList(votesA, votesB)); private final OptInWithValue optIn2 = new OptInWithValue( Arrays.asList(votesA)); private String asJson = "{\"OptInWithValue\":{\"votes\":[{\"VotesWithValue\":{\"actor\":\"partyA\",\"votes\":[{\"VoteWithValue\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"iss\":\"val1\"}},\"minPower\":2,\"maxPower\":9,\"value\":100}}]}},{\"VotesWithValue\":{\"actor\":\"partyB\",\"votes\":[{\"VoteWithValue\":{\"actor\":\"partyB\",\"bid\":{\"issuevalues\":{\"iss\":\"val1\"}},\"minPower\":2,\"maxPower\":9,\"value\":60}},{\"VoteWithValue\":{\"actor\":\"partyB\",\"bid\":{\"issuevalues\":{\"iss\":\"val2\"}},\"minPower\":2,\"maxPower\":9,\"value\":40}}]}}]}}"; @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(optIn1, optIn1a), Arrays.asList(optIn2)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "OptInWithValue.*VotesWithValue.*partyA.*VoteWithValue.*partyA.*iss.*val1.*2.*VotesWithValue.*partyB.*VoteWithValue.*partyB.*iss.*val1.*.*2.*VoteWithValue.*partyB.*iss.*val2.*2.*", "OptIn.*Votes.*Vote.*partyA.*iss.*val.*2.*"); } @Test public void testSerialize() throws JsonProcessingException { ObjectMapper jackson = new ObjectMapper(); String json = jackson.writeValueAsString(optIn1); System.out.println(json); assertEquals(asJson, json); } @Test public void testDeserialize() throws JsonParseException, JsonMappingException, IOException { ObjectMapper jackson = new ObjectMapper(); Inform p = jackson.readValue(asJson, Inform.class); System.out.println(p); assertEquals(optIn1, p); } }