package geniusweb.protocol.session.mopac; 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 geniusweb.voting.VotingEvaluator; import geniusweb.voting.votingevaluators.LargestAgreement; import tudelft.utilities.junit.GeneralTests; import tudelft.utilities.logging.ReportToLogger; public class MOPACSettingsTest extends GeneralTests { private final ObjectMapper jackson = new ObjectMapper(); private final Parameters noparams = new Parameters(); private TeamInfo partyprof1 = mockParty("party1", noparams); private TeamInfo partyprof2 = mockParty("party2", noparams); private TeamInfo partyprof3 = mockParty("party3", noparams); private List participants2 = Arrays.asList(partyprof1, partyprof2); private List participants3 = Arrays.asList(partyprof1, partyprof2, partyprof3); private final VotingEvaluator ve = new LargestAgreement(); private DeadlineTime deadline = mock(DeadlineTime.class); private Deadline deadline2 = mock(Deadline.class); private MOPACSettings settings1 = new MOPACSettings(participants2, deadline, ve); private MOPACSettings settings1a = new MOPACSettings(participants2, deadline, ve); private MOPACSettings settings2 = new MOPACSettings(participants3, deadline, ve); private MOPACSettings settings3 = new MOPACSettings(participants2, deadline2, ve); private MOPACSettings sersettings; private final String serialized = "{\"MOPACSettings\":{\"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}},\"votingevaluator\":{\"LargestAgreement\":{}}}}"; private List participants; private PartyWithProfile partywithprof1; private PartyWithProfile partywithprof2; private TeamInfo mockParty(String partyname, Parameters params) { TeamInfo team = mock(TeamInfo.class); when(team.getSize()).thenReturn(1); PartyWithParameters pwithp = mock(PartyWithParameters.class); when(team.getParties()).thenReturn(Arrays .asList(new PartyWithProfile(pwithp, mock(ProfileRef.class)))); when(team.toString()).thenReturn(partyname); when(pwithp.toString()).thenReturn(partyname); when(pwithp.getParameters()).thenReturn(params); return team; } @Before public void before() throws URISyntaxException { when(deadline.getDuration()).thenReturn(1000l); when(deadline.toString()).thenReturn("deadline"); when(deadline2.toString()).thenReturn("deadline2"); // 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 MOPACSettings(participants, deadlinetime, ve); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(settings1, settings1a), Arrays.asList(settings2), Arrays.asList(settings3)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "MOPACSettings.*party1.*party2.*deadline.*LargestAgreement.*", "MOPACSettings.*party1.*party2.*deadline.*LargestAgreement.*", "MOPACSettings.*party1.*party2.*deadline2.*LargestAgreement.*"); } @Test public void getProtocolTest() { assertEquals("MOPAC", settings1.getProtocol(new ReportToLogger("test")) .getRef().getURI().toString()); } @Test(expected = IllegalArgumentException.class) public void constructorNoDeadlineTest() { new MOPACSettings(participants2, null, ve); } @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); MOPACSettings settings = new MOPACSettings(participants2, deadline, ve); 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() { MOPACSettings saop = new MOPACSettings(participants2, deadline, ve); SessionSettings saop2 = saop.with(partyprof3); assertEquals(3, saop2.getTeams().size()); } @Test public void testGetAllParties() { assertEquals(Arrays.asList(partywithprof1, partywithprof2), sersettings.getAllParties()); } }