1 | package geniusweb.protocol.session.learn;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.mockito.Mockito.mock;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.net.URISyntaxException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.junit.Before;
|
---|
12 | import org.junit.Test;
|
---|
13 |
|
---|
14 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
15 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
16 |
|
---|
17 | import geniusweb.deadline.Deadline;
|
---|
18 | import geniusweb.deadline.DeadlineRounds;
|
---|
19 | import geniusweb.protocol.session.SessionSettings;
|
---|
20 | import geniusweb.protocol.session.TeamInfo;
|
---|
21 | import geniusweb.references.Parameters;
|
---|
22 | import geniusweb.references.PartyRef;
|
---|
23 | import geniusweb.references.PartyWithParameters;
|
---|
24 | import geniusweb.references.PartyWithProfile;
|
---|
25 | import geniusweb.references.ProfileRef;
|
---|
26 | import tudelft.utilities.junit.GeneralTests;
|
---|
27 | import tudelft.utilities.logging.Reporter;
|
---|
28 |
|
---|
29 | public class LearnSettingsTest extends GeneralTests<LearnSettings> {
|
---|
30 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
31 | LearnSettings settings1, settings1a, settings2, settings3;
|
---|
32 | private DeadlineRounds deadline;
|
---|
33 | private List<TeamInfo> participants, participants2;
|
---|
34 | private Parameters params;
|
---|
35 |
|
---|
36 | private final String serialized = "{\"LearnSettings\":{\"participants\":[{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"party1\",\"parameters\":{\"persistentstate\":\"6bb5f909-0079-43ac-a8ac-a31794391074\",\"negotiationdata\":[\"12b5f909-0079-43ac-a8ac-a31794391012\"]}},\"profile\":\"http://prof1\"}]}}],\"deadline\":{\"DeadlineRounds\":{\"rounds\":10,\"durationms\":10000}}}}";
|
---|
37 |
|
---|
38 | @Before
|
---|
39 | public void before() throws URISyntaxException, JsonProcessingException {
|
---|
40 |
|
---|
41 | params = new Parameters();
|
---|
42 | params = params.with("persistentstate",
|
---|
43 | "6bb5f909-0079-43ac-a8ac-a31794391074");
|
---|
44 | params = params.with("negotiationdata",
|
---|
45 | Arrays.asList(("12b5f909-0079-43ac-a8ac-a31794391012")));
|
---|
46 |
|
---|
47 | // define participants
|
---|
48 | participants = createTeam(params, 1);
|
---|
49 |
|
---|
50 | // participants2
|
---|
51 | // ProfileRef profile2 = new ProfileRef("http://prof2");
|
---|
52 | // PartyWithProfile partywithp2 = new PartyWithProfile(party1, profile2);
|
---|
53 | // List<PartyWithProfile> team2pp = Arrays.asList(partywithp2);
|
---|
54 | // TeamInfo team2 = new TeamInfo(team2pp);
|
---|
55 | participants2 = createTeam(params, 2);
|
---|
56 |
|
---|
57 | deadline = new DeadlineRounds(10, 10000);
|
---|
58 | Deadline deadline2 = new DeadlineRounds(20, 10000);
|
---|
59 |
|
---|
60 | settings1 = new LearnSettings(participants, deadline);
|
---|
61 | settings1a = new LearnSettings(participants, deadline);
|
---|
62 | settings2 = new LearnSettings(participants2, deadline);
|
---|
63 | settings3 = new LearnSettings(participants, deadline2);
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | *
|
---|
69 | * @param params the parameters for the party
|
---|
70 | * @param partynr the serialnr for the party, eg 1 or 2.
|
---|
71 | * @return a List of TeamInfo with just 1 team.
|
---|
72 | */
|
---|
73 | private List<TeamInfo> createTeam(Parameters params, int partynr)
|
---|
74 | throws URISyntaxException {
|
---|
75 | PartyRef party1ref = new PartyRef("party" + partynr);
|
---|
76 | PartyWithParameters party1 = new PartyWithParameters(party1ref, params);
|
---|
77 | ProfileRef profile1 = new ProfileRef("http://prof" + partynr);
|
---|
78 | PartyWithProfile partywithp1 = new PartyWithProfile(party1, profile1);
|
---|
79 | List<PartyWithProfile> team1pp = Arrays.asList(partywithp1);
|
---|
80 | TeamInfo team1 = new TeamInfo(team1pp);
|
---|
81 | return Arrays.asList(team1);
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public List<List<LearnSettings>> 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 | "LearnSettings.TeamInfo.*PartyWithProfile.*PartyRef.*party1.*persistentstate=6b.*, negotiationdata=\\[12.*..*ProfileRef.*prof1.*,DeadlineRounds.*10,10000.*",
|
---|
95 | "LearnSettings.TeamInfo.*PartyWithProfile.*PartyRef.*party2.*persistentstate=6b.*, negotiationdata=\\[12.*..*ProfileRef.*prof2.*,DeadlineRounds.*10,10000.*",
|
---|
96 | "LearnSettings.TeamInfo.*PartyWithProfile.*PartyRef.*party1.*persistentstate=6b.*, negotiationdata=\\[12.*..*ProfileRef.*prof1.*,DeadlineRounds.*20,10000.*");
|
---|
97 | }
|
---|
98 |
|
---|
99 | @SuppressWarnings("unused")
|
---|
100 | @Test(expected = IllegalArgumentException.class)
|
---|
101 | public void testNullParticipants() {
|
---|
102 | new LearnSettings(null, deadline);
|
---|
103 | }
|
---|
104 |
|
---|
105 | @SuppressWarnings("unused")
|
---|
106 | @Test(expected = IllegalArgumentException.class)
|
---|
107 | public void testNullDeadline() {
|
---|
108 | new LearnSettings(participants, null);
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Test
|
---|
112 | public void getMaxRuntimeTest() {
|
---|
113 | assertEquals(10d, settings1.getMaxRunTime(), 0.00000001);
|
---|
114 | }
|
---|
115 |
|
---|
116 | @SuppressWarnings("unused")
|
---|
117 | @Test
|
---|
118 | public void getProtocolTest() {
|
---|
119 | // this will throw if not Learn protocol or another problem.
|
---|
120 | Learn protocol = (Learn) settings1.getProtocol(mock(Reporter.class));
|
---|
121 | }
|
---|
122 |
|
---|
123 | @SuppressWarnings("unused")
|
---|
124 | @Test(expected = IllegalArgumentException.class)
|
---|
125 | public void badPersistentTest() throws URISyntaxException {
|
---|
126 | // hack. #1933.
|
---|
127 | Parameters newparams = params.with("persistentstate",
|
---|
128 | "\"notproperUUID\"");
|
---|
129 | participants = createTeam(newparams, 1);
|
---|
130 |
|
---|
131 | new LearnSettings(participants, deadline);
|
---|
132 | }
|
---|
133 |
|
---|
134 | @SuppressWarnings("unused")
|
---|
135 | @Test(expected = IllegalArgumentException.class)
|
---|
136 | public void persistentNotStringTest() throws URISyntaxException {
|
---|
137 | Parameters newparams = params.with("persistentstate", 32);
|
---|
138 | participants = createTeam(newparams, 1);
|
---|
139 |
|
---|
140 | new LearnSettings(participants, deadline);
|
---|
141 | }
|
---|
142 |
|
---|
143 | @SuppressWarnings("unused")
|
---|
144 | @Test
|
---|
145 | public void persistentWithoutQuotesTest() throws URISyntaxException {
|
---|
146 | Parameters newparams = params.with("persistentstate",
|
---|
147 | "6bb5f909-0079-43ac-a8ac-a31794391074");
|
---|
148 | participants = createTeam(newparams, 1);
|
---|
149 |
|
---|
150 | new LearnSettings(participants, deadline);
|
---|
151 | }
|
---|
152 |
|
---|
153 | @SuppressWarnings("unused")
|
---|
154 | @Test(expected = IllegalArgumentException.class)
|
---|
155 | public void persistentExtraQuoteTest() throws URISyntaxException {
|
---|
156 | Parameters newparams = params.with("persistentstate",
|
---|
157 | "\"6bb5f909-0079-43ac-a8ac-a31794391074\"");
|
---|
158 | participants = createTeam(newparams, 1);
|
---|
159 |
|
---|
160 | new LearnSettings(participants, deadline);
|
---|
161 | }
|
---|
162 |
|
---|
163 | @SuppressWarnings("unused")
|
---|
164 | @Test(expected = IllegalArgumentException.class)
|
---|
165 | public void negotiationDataNotList() throws URISyntaxException {
|
---|
166 | Parameters newparams = params.with("negotiationdata",
|
---|
167 | "\"12b5f909-0079-43ac-a8ac-a31794391012\"");
|
---|
168 | participants = createTeam(newparams, 1);
|
---|
169 | new LearnSettings(participants, deadline);
|
---|
170 | }
|
---|
171 |
|
---|
172 | @SuppressWarnings("unused")
|
---|
173 | @Test(expected = IllegalArgumentException.class)
|
---|
174 | public void negotiationDataBadContents() throws URISyntaxException {
|
---|
175 | Parameters newparams = params.with("negotiationdata", "\"bad\"");
|
---|
176 | participants = createTeam(newparams, 1);
|
---|
177 | new LearnSettings(participants, deadline);
|
---|
178 | }
|
---|
179 |
|
---|
180 | @SuppressWarnings("unused")
|
---|
181 | @Test(expected = IllegalArgumentException.class)
|
---|
182 | public void negotiationDataNotString() throws URISyntaxException {
|
---|
183 | Parameters newparams = params.with("negotiationdata", 12);
|
---|
184 | participants = createTeam(newparams, 1);
|
---|
185 | new LearnSettings(participants, deadline);
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Test
|
---|
189 | public void getTeamsTest() {
|
---|
190 | assertEquals(participants, settings1.getTeams());
|
---|
191 | assertEquals((Integer) 1, settings1.getTeamSize());
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Test
|
---|
195 | public void getDeadlineTest() {
|
---|
196 | assertEquals(deadline, settings1.getDeadline());
|
---|
197 | }
|
---|
198 |
|
---|
199 | @Test
|
---|
200 | public void withTeamTest() {
|
---|
201 | SessionSettings newset = settings1.with(participants2.get(0));
|
---|
202 | assertEquals(2, newset.getTeams().size());
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Test
|
---|
206 | public void testDeserialize() throws IOException {
|
---|
207 | SessionSettings obj = jackson.readValue(serialized,
|
---|
208 | SessionSettings.class);
|
---|
209 | System.out.println(obj);
|
---|
210 | assertEquals(settings1, obj);
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Test
|
---|
214 | public void testSerialize()
|
---|
215 | throws JsonProcessingException, URISyntaxException {
|
---|
216 |
|
---|
217 | String string = jackson.writeValueAsString(settings1);
|
---|
218 | System.out.println(string);
|
---|
219 | assertEquals(serialized, string);
|
---|
220 | }
|
---|
221 |
|
---|
222 | }
|
---|