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