package geniusweb.inform; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; import java.util.Date; 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.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.progress.ProgressRounds; import geniusweb.references.Parameters; import geniusweb.references.PartyRef; import geniusweb.references.ProfileRef; import geniusweb.references.ProtocolRef; import tudelft.utilities.junit.GeneralTests; public class SettingsTest extends GeneralTests { private PartyId id = new PartyId("party1"); private PartyId id2 = new PartyId("party2"); private final String asJson = "{\"Settings\":" + "{\"id\":\"party1\",\"profile\":\"ws:profile1\"," + "\"protocol\":\"ws:localhost/protocol1\"," + "\"progress\":{\"rounds\":{\"duration\":10,\"currentRound\":0,\"endtime\":999}}," + "\"parameters\":{}}}"; private Settings negoinfo1, negoinfo1a; private Settings negoinfo2, negoinfo3, negoinfo4, negoinfo5; private final Date date = new Date(999); private final static ObjectMapper jackson = new ObjectMapper(); @Before public void before() throws URISyntaxException, JsonParseException, JsonMappingException, IOException { // we can't mock because we want to test the serializer ProfileRef profile1 = new ProfileRef(new URI("ws:profile1")); // jackson.readValue(serialized, Profile.class); ProtocolRef protocol1 = new ProtocolRef( new URI("ws:localhost/protocol1")); ProtocolRef protocol1a = new ProtocolRef( new URI("ws:localhost/protocol1")); ProtocolRef protocol2 = new ProtocolRef( new URI("ws:localhost/protocol2")); ProgressRounds progress1 = new ProgressRounds(10, 0, date); ProgressRounds progress1a = new ProgressRounds(10, 0, date); ProgressRounds progress2 = new ProgressRounds(12, 0, date); Parameters settings1 = new Parameters(); Parameters settings2 = new Parameters().with("a", 1); negoinfo1 = new Settings(id, profile1, protocol1, progress1, settings1); negoinfo1a = new Settings(id, profile1, protocol1a, progress1a, settings1); negoinfo2 = new Settings(id, profile1, protocol2, progress1, settings1); negoinfo3 = new Settings(id, profile1, protocol1, progress2, settings1); negoinfo4 = new Settings(id2, profile1, protocol1, progress2, settings1); negoinfo5 = new Settings(id, profile1, protocol1, progress1, settings2); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(negoinfo1, negoinfo1a), Arrays.asList(negoinfo2), Arrays.asList(negoinfo3), Arrays.asList(negoinfo4), Arrays.asList(negoinfo5)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "Settings.*party1.*ProtocolRef.*protocol1.*ProgressRounds.*0.*10.*", "Settings.*party1.*ProtocolRef.*protocol2.*ProgressRounds.*0.*10.*", "Settings.*party1.*ProtocolRef.*protocol1.*ProgressRounds.*0.*12.*", "Settings.*party2.*ProtocolRef.*protocol1.*ProgressRounds.*0.*12.*", "Settings.*party1.*ProtocolRef.*protocol1.*ProgressRounds.*0.*10.*"); } @Test public void smokeTest() throws URISyntaxException { new PartyRef(new URI("ws:localhost")); } @Test(expected = IllegalArgumentException.class) public void nullTest() throws URISyntaxException { new PartyRef((URI) null); } @Test public void testSerialize() throws JsonProcessingException { String json = jackson.writeValueAsString(negoinfo1); System.out.println(json); assertEquals(asJson, json); } @Test public void testDeserialize() throws JsonParseException, JsonMappingException, IOException { Settings p = (Settings) jackson.readValue(asJson, Inform.class); System.out.println(p); assertEquals(negoinfo1, p); } @Test public void testGeneralDict() throws JsonParseException, JsonMappingException, IOException { Map val = jackson.readValue("{\"a\":0.3,\"b\":{\"x\":3},\"c\":[1,2,3]}", HashMap.class); System.out.println(val); } }