package geniusweb.protocol.tournament.allpermutations; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.math.BigInteger; import java.net.URISyntaxException; import java.util.Arrays; import java.util.Collections; 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.DeadlineTime; import geniusweb.protocol.NegoSettings; import geniusweb.protocol.session.saop.SAOPSettings; import geniusweb.protocol.tournament.ProfileList; import geniusweb.protocol.tournament.Team; import geniusweb.protocol.tournament.TournamentSettings; import geniusweb.references.Parameters; import geniusweb.references.PartyRef; import geniusweb.references.PartyWithParameters; import geniusweb.references.ProfileRef; import tudelft.utilities.junit.GeneralTests; import tudelft.utilities.logging.ReportToLogger; public class AllPermutationsSettingsTest extends GeneralTests { private final ObjectMapper jackson = new ObjectMapper(); private final String serialized = "{\"AllPermutationsSettings\":{\"teams\":[{\"Team\":[{\"partyref\":\"party1\",\"parameters\":{}}]},{\"Team\":[{\"partyref\":\"party2\",\"parameters\":{}}]}],\"profileslists\":[{\"ProfileList\":[\"profile1\"]},{\"ProfileList\":[\"profile2\"]},{\"ProfileList\":[\"profile3\"]}],\"reuseTeams\":false,\"teamsPerSession\":2,\"sessionsettings\":{\"SAOPSettings\":{\"participants\":[],\"deadline\":{\"deadlinetime\":{\"durationms\":10000}}}},\"numberTournaments\":1}}"; private AllPermutationsSettings settings, settings1a, settings2, settings3, settings4, settings5; // use directly because there is some hard coding currently. private SAOPSettings saopsettings; private ProfileRef profile1, profile2, profile3; private ProfileList profiles1, profiles2, profiles3; private List parties; @Before public void before() throws URISyntaxException { PartyRef partyref1 = new PartyRef("party1"); PartyRef partyref2 = new PartyRef("party2"); PartyRef partyref3 = new PartyRef("party3"); Parameters params1 = new Parameters(); Parameters params2 = new Parameters(); Parameters params3 = new Parameters(); Team partywithparams1 = new Team( Arrays.asList(new PartyWithParameters(partyref1, params1))); Team partywithparams2 = new Team( Arrays.asList(new PartyWithParameters(partyref2, params2))); Team partywithparams3 = new Team( Arrays.asList(new PartyWithParameters(partyref3, params3))); parties = Arrays.asList(partywithparams1, partywithparams2); DeadlineTime deadline = new DeadlineTime(10000); saopsettings = new SAOPSettings(Collections.emptyList(), deadline); profile1 = new ProfileRef("profile1"); profile2 = new ProfileRef("profile2"); profile3 = new ProfileRef("profile3"); profiles1 = new ProfileList(Arrays.asList(profile1)); profiles2 = new ProfileList(Arrays.asList(profile2)); profiles3 = new ProfileList(Arrays.asList(profile3)); List profiles = Arrays.asList(profiles1, profiles2, profiles3); settings = new AllPermutationsSettings(parties, profiles, false, 2, saopsettings, 1); settings1a = new AllPermutationsSettings(parties, profiles, false, 2, saopsettings, 1); settings2 = new AllPermutationsSettings(Arrays.asList(partywithparams1, partywithparams2, partywithparams3), profiles, false, 2, saopsettings, 1); settings3 = new AllPermutationsSettings(parties, profiles, true, 2, saopsettings, 1); settings4 = new AllPermutationsSettings(parties, Arrays.asList(profiles1, profiles2, profiles3, profiles1), false, 2, saopsettings, 1); settings5 = new AllPermutationsSettings(parties, profiles, false, 2, saopsettings, 2); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(settings, settings1a), Arrays.asList(settings2), Arrays.asList(settings3), Arrays.asList(settings4), Arrays.asList(settings5)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "AllPermutationsSettings.*party1.*party2.*,false,.*profile1.*profile2.*profile3.*,2,SAOPSettings.*1.*", "AllPermutationsSettings.*party1.*party2.*party3.*,false,.*profile1.*profile2.*profile3.*,2,SAOPSettings.*1.*", "AllPermutationsSettings.*party1.*party2.*,true,.*profile1.*profile2.*profile3.*,2,SAOPSettings.*1.*", "AllPermutationsSettings.*party1.*party2.*,false,.*profile1.*profile2.*profile3.*profile1.*,2,SAOPSettings.*1.*", "AllPermutationsSettings.*party1.*party2.*,false,.*profile1.*profile2.*profile3.*,2,SAOPSettings.*2.*"); } @Test public void getProtocolTest() { assertEquals("APP", settings.getProtocol(new ReportToLogger("test")) .getRef().getURI().toString()); } @Test public void testDeserialize() throws IOException { TournamentSettings obj = (TournamentSettings) jackson .readValue(serialized, NegoSettings.class); System.out.println(obj); assertEquals(settings, obj); } @Test public void testSerialize() throws JsonProcessingException, URISyntaxException { String string = jackson.writeValueAsString(settings); System.out.println(string); assertEquals(serialized, string); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testNullParties() { new AllPermutationsSettings(null, Arrays.asList(profiles1, profiles2, profiles3, profiles1), true, 2, saopsettings, 1); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testNoParties() { new AllPermutationsSettings(Collections.emptyList(), Arrays.asList(profiles1, profiles2, profiles3, profiles1), true, 2, saopsettings, 1); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testNullProfiles() { new AllPermutationsSettings(parties, null, true, 2, saopsettings, 1); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testInsufficientProfiles() { new AllPermutationsSettings(parties, Arrays.asList(profiles1, profiles2, profiles3, profiles1), true, 4, saopsettings, 1); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testNullSessionSettings() { new AllPermutationsSettings(parties, Arrays.asList(profiles1, profiles2, profiles3, profiles1), true, 4, null, 1); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testOnePartyPerSession() { new AllPermutationsSettings(parties, Arrays.asList(profiles1, profiles2, profiles3, profiles1), true, 1, saopsettings, 1); } @Test public void getMaxRuntimeTest() { assertEquals(BigInteger.valueOf(6), settings.permutations().size()); assertEquals((Double) (6d * 12), settings.getMaxRunTime()); } }