[43] | 1 | package geniusweb.protocol.session.amop;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.mockito.Mockito.mock;
|
---|
| 5 | import static org.mockito.Mockito.when;
|
---|
| 6 |
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 | import java.net.URISyntaxException;
|
---|
| 9 | import java.util.Arrays;
|
---|
| 10 | import java.util.List;
|
---|
| 11 |
|
---|
| 12 | import org.junit.Before;
|
---|
| 13 | import org.junit.Test;
|
---|
| 14 |
|
---|
| 15 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 17 |
|
---|
| 18 | import geniusweb.deadline.Deadline;
|
---|
| 19 | import geniusweb.deadline.DeadlineRounds;
|
---|
| 20 | import geniusweb.deadline.DeadlineTime;
|
---|
| 21 | import geniusweb.protocol.session.SessionSettings;
|
---|
| 22 | import geniusweb.protocol.session.TeamInfo;
|
---|
| 23 | import geniusweb.references.Parameters;
|
---|
| 24 | import geniusweb.references.PartyRef;
|
---|
| 25 | import geniusweb.references.PartyWithParameters;
|
---|
| 26 | import geniusweb.references.PartyWithProfile;
|
---|
| 27 | import geniusweb.references.ProfileRef;
|
---|
| 28 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 29 | import tudelft.utilities.logging.ReportToLogger;
|
---|
| 30 |
|
---|
| 31 | public class AMOPSettingsTest extends GeneralTests<AMOPSettings> {
|
---|
| 32 |
|
---|
| 33 | private TeamInfo partyprof1 = mock(TeamInfo.class);
|
---|
| 34 | private TeamInfo partyprof2 = mock(TeamInfo.class);
|
---|
| 35 | private TeamInfo partyprof3 = mock(TeamInfo.class);
|
---|
| 36 | private List<TeamInfo> participants2 = Arrays.asList(partyprof1,
|
---|
| 37 | partyprof2);
|
---|
| 38 | private List<TeamInfo> participants3 = Arrays.asList(partyprof1, partyprof2,
|
---|
| 39 | partyprof3);
|
---|
| 40 |
|
---|
| 41 | private DeadlineTime deadline = mock(DeadlineTime.class);
|
---|
| 42 | private Deadline deadline2 = mock(Deadline.class);
|
---|
| 43 | private AMOPSettings settings1 = new AMOPSettings(participants2, deadline);
|
---|
| 44 | private AMOPSettings settings1a = new AMOPSettings(participants2, deadline);
|
---|
| 45 | private AMOPSettings settings2 = new AMOPSettings(participants3, deadline);
|
---|
| 46 | private AMOPSettings settings3 = new AMOPSettings(participants2, deadline2);
|
---|
| 47 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
| 48 |
|
---|
| 49 | private AMOPSettings sersettings;
|
---|
| 50 | 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}}}}";
|
---|
| 51 | private List<TeamInfo> participants;
|
---|
| 52 | private PartyWithProfile partywithprof1, partywithprof2;
|
---|
| 53 |
|
---|
| 54 | @Before
|
---|
| 55 | public void before() throws URISyntaxException {
|
---|
| 56 | when(deadline.getDuration()).thenReturn(1000l);
|
---|
| 57 | when(deadline.toString()).thenReturn("deadline");
|
---|
| 58 | when(deadline2.toString()).thenReturn("deadline2");
|
---|
| 59 |
|
---|
| 60 | when(partyprof1.toString()).thenReturn("party and profile 1");
|
---|
| 61 | when(partyprof2.toString()).thenReturn("party and profile 2");
|
---|
| 62 | when(partyprof3.toString()).thenReturn("party and profile 3");
|
---|
| 63 |
|
---|
| 64 | when(partyprof3.getSize()).thenReturn(1);
|
---|
| 65 |
|
---|
| 66 | // SERIALIZABLE version with REAL objects. Still no workaround for
|
---|
| 67 | // this...
|
---|
| 68 | PartyWithParameters party1 = new PartyWithParameters(
|
---|
| 69 | new PartyRef("http://party1"), new Parameters());
|
---|
| 70 | ProfileRef profile1 = new ProfileRef("http://profile1");
|
---|
| 71 | PartyWithParameters party2 = new PartyWithParameters(
|
---|
| 72 | new PartyRef("http://party2"), new Parameters());
|
---|
| 73 | ProfileRef profile2 = new ProfileRef("http://profile2");
|
---|
| 74 | partywithprof1 = new PartyWithProfile(party1, profile1);
|
---|
| 75 | partywithprof2 = new PartyWithProfile(party2, profile2);
|
---|
| 76 | participants = Arrays.asList(
|
---|
| 77 | new TeamInfo(Arrays.asList(partywithprof1)),
|
---|
| 78 | new TeamInfo(Arrays.asList(partywithprof2)));
|
---|
| 79 |
|
---|
| 80 | Deadline deadlinetime = new DeadlineTime(100);
|
---|
| 81 | sersettings = new AMOPSettings(participants, deadlinetime);
|
---|
| 82 |
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | @Override
|
---|
| 86 | public List<List<AMOPSettings>> getGeneralTestData() {
|
---|
| 87 | return Arrays.asList(Arrays.asList(settings1, settings1a),
|
---|
| 88 | Arrays.asList(settings2), Arrays.asList(settings3));
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | @Override
|
---|
| 92 | public List<String> getGeneralTestStrings() {
|
---|
| 93 | return Arrays.asList(
|
---|
| 94 | "AMOPSettings.party and profile 1, party and profile 2.,deadline.",
|
---|
| 95 | "AMOPSettings.party and profile 1, party and profile 2, party and profile 3.,deadline.",
|
---|
| 96 | "AMOPSettings.party and profile 1, party and profile 2.,deadline2.");
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | @Test
|
---|
| 100 | public void getProtocolTest() {
|
---|
| 101 | assertEquals("AMOP", settings1.getProtocol(new ReportToLogger("test"))
|
---|
| 102 | .getRef().getURI().toString());
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | @SuppressWarnings("unused")
|
---|
| 106 | @Test(expected = IllegalArgumentException.class)
|
---|
| 107 | public void constructorNoDeadlineTest() {
|
---|
| 108 | new AMOPSettings(participants2, null);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | @Test
|
---|
| 112 | public void testMaxRuntime() {
|
---|
| 113 | when(deadline.getDuration()).thenReturn(234l);
|
---|
| 114 | assertEquals((Double) .234d, settings1.getMaxRunTime());
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | @Test
|
---|
| 118 | public void testMaxRuntimeRounds() {
|
---|
| 119 | deadline = mock(DeadlineRounds.class);
|
---|
| 120 | when(deadline.getDuration()).thenReturn(12000l);
|
---|
| 121 |
|
---|
| 122 | AMOPSettings settings = new AMOPSettings(participants2, deadline);
|
---|
| 123 |
|
---|
| 124 | assertEquals((Double) 12d, settings.getMaxRunTime());
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | @Test
|
---|
| 128 | public void testDeserialize() throws IOException {
|
---|
| 129 | SessionSettings obj = jackson.readValue(serialized,
|
---|
| 130 | SessionSettings.class);
|
---|
| 131 | System.out.println(obj);
|
---|
| 132 | assertEquals(sersettings, obj);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | @Test
|
---|
| 136 | public void testSerialize()
|
---|
| 137 | throws JsonProcessingException, URISyntaxException {
|
---|
| 138 |
|
---|
| 139 | String string = jackson.writeValueAsString(sersettings);
|
---|
| 140 | System.out.println(string);
|
---|
| 141 | assertEquals(serialized, string);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | @Test
|
---|
| 145 | public void testWith() {
|
---|
| 146 | AMOPSettings saop = new AMOPSettings(participants2, deadline);
|
---|
| 147 | SessionSettings saop2 = saop.with(partyprof3);
|
---|
| 148 | assertEquals(3, saop2.getTeams().size());
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | @Test
|
---|
| 152 | public void testGetAllParties() {
|
---|
| 153 | assertEquals(Arrays.asList(partywithprof1, partywithprof2),
|
---|
| 154 | sersettings.getAllParties());
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | }
|
---|