package geniusweb.protocol.session.amop; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; import org.junit.Before; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import geniusweb.deadline.Deadline; import geniusweb.deadline.DeadlineRounds; import geniusweb.deadline.DeadlineTime; import geniusweb.protocol.session.SessionSettings; import geniusweb.protocol.session.TeamInfo; import geniusweb.references.Parameters; import geniusweb.references.PartyRef; import geniusweb.references.PartyWithParameters; import geniusweb.references.PartyWithProfile; import geniusweb.references.ProfileRef; import tudelft.utilities.junit.GeneralTests; import tudelft.utilities.logging.ReportToLogger; public class AMOPSettingsTest extends GeneralTests { private TeamInfo partyprof1 = mock(TeamInfo.class); private TeamInfo partyprof2 = mock(TeamInfo.class); private TeamInfo partyprof3 = mock(TeamInfo.class); private List participants2 = Arrays.asList(partyprof1, partyprof2); private List participants3 = Arrays.asList(partyprof1, partyprof2, partyprof3); private DeadlineTime deadline = mock(DeadlineTime.class); private Deadline deadline2 = mock(Deadline.class); private AMOPSettings settings1 = new AMOPSettings(participants2, deadline); private AMOPSettings settings1a = new AMOPSettings(participants2, deadline); private AMOPSettings settings2 = new AMOPSettings(participants3, deadline); private AMOPSettings settings3 = new AMOPSettings(participants2, deadline2); private final ObjectMapper jackson = new ObjectMapper(); private AMOPSettings sersettings; private final String serialized = "{\"AMOPSettings\":{\"participants\":[{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party1\",\"parameters\":{}},\"profile\":\"http://profile1\"}]}},{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party2\",\"parameters\":{}},\"profile\":\"http://profile2\"}]}}],\"deadline\":{\"DeadlineTime\":{\"durationms\":100}}}}"; private List participants; private PartyWithProfile partywithprof1, partywithprof2; @Before public void before() throws URISyntaxException { when(deadline.getDuration()).thenReturn(1000l); when(deadline.toString()).thenReturn("deadline"); when(deadline2.toString()).thenReturn("deadline2"); when(partyprof1.toString()).thenReturn("party and profile 1"); when(partyprof2.toString()).thenReturn("party and profile 2"); when(partyprof3.toString()).thenReturn("party and profile 3"); when(partyprof3.getSize()).thenReturn(1); // SERIALIZABLE version with REAL objects. Still no workaround for // this... PartyWithParameters party1 = new PartyWithParameters( new PartyRef("http://party1"), new Parameters()); ProfileRef profile1 = new ProfileRef("http://profile1"); PartyWithParameters party2 = new PartyWithParameters( new PartyRef("http://party2"), new Parameters()); ProfileRef profile2 = new ProfileRef("http://profile2"); partywithprof1 = new PartyWithProfile(party1, profile1); partywithprof2 = new PartyWithProfile(party2, profile2); participants = Arrays.asList( new TeamInfo(Arrays.asList(partywithprof1)), new TeamInfo(Arrays.asList(partywithprof2))); Deadline deadlinetime = new DeadlineTime(100); sersettings = new AMOPSettings(participants, deadlinetime); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(settings1, settings1a), Arrays.asList(settings2), Arrays.asList(settings3)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "AMOPSettings.party and profile 1, party and profile 2.,deadline.", "AMOPSettings.party and profile 1, party and profile 2, party and profile 3.,deadline.", "AMOPSettings.party and profile 1, party and profile 2.,deadline2."); } @Test public void getProtocolTest() { assertEquals("AMOP", settings1.getProtocol(new ReportToLogger("test")) .getRef().getURI().toString()); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void constructorNoDeadlineTest() { new AMOPSettings(participants2, null); } @Test public void testMaxRuntime() { when(deadline.getDuration()).thenReturn(234l); assertEquals((Double) .234d, settings1.getMaxRunTime()); } @Test public void testMaxRuntimeRounds() { deadline = mock(DeadlineRounds.class); when(deadline.getDuration()).thenReturn(12000l); AMOPSettings settings = new AMOPSettings(participants2, deadline); assertEquals((Double) 12d, settings.getMaxRunTime()); } @Test public void testDeserialize() throws IOException { SessionSettings obj = jackson.readValue(serialized, SessionSettings.class); System.out.println(obj); assertEquals(sersettings, obj); } @Test public void testSerialize() throws JsonProcessingException, URISyntaxException { String string = jackson.writeValueAsString(sersettings); System.out.println(string); assertEquals(serialized, string); } @Test public void testWith() { AMOPSettings saop = new AMOPSettings(participants2, deadline); SessionSettings saop2 = saop.with(partyprof3); assertEquals(3, saop2.getTeams().size()); } @Test public void testGetAllParties() { assertEquals(Arrays.asList(partywithprof1, partywithprof2), sersettings.getAllParties()); } }