1 | package geniusweb.protocol.tournament.allpermutationslearn;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.math.BigInteger;
|
---|
7 | import java.net.URISyntaxException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collections;
|
---|
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.DeadlineTime;
|
---|
19 | import geniusweb.protocol.NegoSettings;
|
---|
20 | import geniusweb.protocol.session.SessionSettings;
|
---|
21 | import geniusweb.protocol.session.learn.LearnSettings;
|
---|
22 | import geniusweb.protocol.session.saop.SAOPSettings;
|
---|
23 | import geniusweb.protocol.tournament.ProfileList;
|
---|
24 | import geniusweb.protocol.tournament.Team;
|
---|
25 | import geniusweb.protocol.tournament.TournamentSettings;
|
---|
26 | import geniusweb.references.Parameters;
|
---|
27 | import geniusweb.references.PartyRef;
|
---|
28 | import geniusweb.references.PartyWithParameters;
|
---|
29 | import geniusweb.references.ProfileRef;
|
---|
30 | import tudelft.utilities.immutablelist.ImmutableList;
|
---|
31 | import tudelft.utilities.junit.GeneralTests;
|
---|
32 | import tudelft.utilities.logging.ReportToLogger;
|
---|
33 |
|
---|
34 | public class AllPermutationsLearnSettingsTest
|
---|
35 | extends GeneralTests<AllPermutationsLearnSettings> {
|
---|
36 |
|
---|
37 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
38 |
|
---|
39 | private final String serialized = "{\"AllPermutationsLearnSettings\":{\"teams\":[{\"Team\":[{\"partyref\":\"party1\",\"parameters\":{\"persistentstate\":\"a48b8bc9-4275-4a4e-bee9-6c0a734dc99a\",\"negotiationdata\":[]}}]},{\"Team\":[{\"partyref\":\"party2\",\"parameters\":{\"persistentstate\":\"12b6af6c-b8a0-4731-8b37-0454cafeb7f4\",\"negotiationdata\":[]}}]}],\"profileslists\":[{\"ProfileList\":[\"profile1\"]},{\"ProfileList\":[\"profile2\"]},{\"ProfileList\":[\"profile3\"]}],\"reuseTeams\":false,\"teamsPerSession\":2,\"sessionsettings\":{\"SAOPSettings\":{\"participants\":[],\"deadline\":{\"DeadlineTime\":{\"durationms\":10000}}}},\"numberTournaments\":1,\"learnSettings\":{\"LearnSettings\":{\"participants\":[],\"deadline\":{\"DeadlineTime\":{\"durationms\":99000}}}}}}";
|
---|
40 | private AllPermutationsLearnSettings settings, settings1a, settings2,
|
---|
41 | settings3, settings4, settings5;
|
---|
42 |
|
---|
43 | // use directly because there is some hard coding currently.
|
---|
44 | private SAOPSettings saopsettings;
|
---|
45 |
|
---|
46 | private ProfileRef profile1, profile2, profile3;
|
---|
47 | private ProfileList profiles1, profiles2, profiles3;
|
---|
48 | private List<Team> parties, parties3;
|
---|
49 |
|
---|
50 | private LearnSettings learnsettings;
|
---|
51 |
|
---|
52 | private List<ProfileList> profiles;
|
---|
53 |
|
---|
54 | @Before
|
---|
55 | public void before() throws URISyntaxException {
|
---|
56 |
|
---|
57 | PartyRef partyref1 = new PartyRef("party1");
|
---|
58 | PartyRef partyref2 = new PartyRef("party2");
|
---|
59 | PartyRef partyref3 = new PartyRef("party3");
|
---|
60 |
|
---|
61 | Parameters params1 = new Parameters()
|
---|
62 | .with("persistentstate", "a48b8bc9-4275-4a4e-bee9-6c0a734dc99a")
|
---|
63 | .with("negotiationdata", Collections.emptyList());
|
---|
64 | Parameters params2 = new Parameters()
|
---|
65 | .with("persistentstate", "12b6af6c-b8a0-4731-8b37-0454cafeb7f4")
|
---|
66 | .with("negotiationdata", Collections.emptyList());
|
---|
67 | Parameters params3 = new Parameters()
|
---|
68 | .with("persistentstate", "2b9488ed-c06b-4027-826b-4528254558f1")
|
---|
69 | .with("negotiationdata", Collections.emptyList());
|
---|
70 |
|
---|
71 | PartyWithParameters pwithp1 = new PartyWithParameters(partyref1,
|
---|
72 | params1);
|
---|
73 | PartyWithParameters pwithp2 = new PartyWithParameters(partyref2,
|
---|
74 | params2);
|
---|
75 | PartyWithParameters pwithp3 = new PartyWithParameters(partyref3,
|
---|
76 | params3);
|
---|
77 | Team partywithparams1 = new Team(Arrays.asList(pwithp1));
|
---|
78 | Team partywithparams2 = new Team(Arrays.asList(pwithp2));
|
---|
79 | Team partywithparams3 = new Team(Arrays.asList(pwithp3));
|
---|
80 |
|
---|
81 | parties = Arrays.asList(partywithparams1, partywithparams2);
|
---|
82 | parties3 = Arrays.asList(partywithparams1, partywithparams2,
|
---|
83 | partywithparams3);
|
---|
84 | DeadlineTime deadline = new DeadlineTime(10000);
|
---|
85 |
|
---|
86 | saopsettings = new SAOPSettings(Collections.emptyList(), deadline);
|
---|
87 |
|
---|
88 | profile1 = new ProfileRef("profile1");
|
---|
89 | profile2 = new ProfileRef("profile2");
|
---|
90 | profile3 = new ProfileRef("profile3");
|
---|
91 | profiles1 = new ProfileList(Arrays.asList(profile1));
|
---|
92 | profiles2 = new ProfileList(Arrays.asList(profile2));
|
---|
93 | profiles3 = new ProfileList(Arrays.asList(profile3));
|
---|
94 |
|
---|
95 | learnsettings = new LearnSettings(Collections.emptyList(),
|
---|
96 | new DeadlineTime(99000));
|
---|
97 |
|
---|
98 | profiles = Arrays.asList(profiles1, profiles2, profiles3);
|
---|
99 |
|
---|
100 | settings = new AllPermutationsLearnSettings(parties, profiles, false, 2,
|
---|
101 | saopsettings, 1, learnsettings);
|
---|
102 |
|
---|
103 | settings1a = new AllPermutationsLearnSettings(parties, profiles, false,
|
---|
104 | 2, saopsettings, 1, learnsettings);
|
---|
105 |
|
---|
106 | settings2 = new AllPermutationsLearnSettings(
|
---|
107 | Arrays.asList(partywithparams1, partywithparams2,
|
---|
108 | partywithparams3),
|
---|
109 | profiles, false, 2, saopsettings, 1, learnsettings);
|
---|
110 |
|
---|
111 | settings3 = new AllPermutationsLearnSettings(parties, profiles, true, 2,
|
---|
112 | saopsettings, 1, learnsettings);
|
---|
113 |
|
---|
114 | settings4 = new AllPermutationsLearnSettings(parties,
|
---|
115 | Arrays.asList(profiles1, profiles2, profiles3, profiles1),
|
---|
116 | false, 2, saopsettings, 1, learnsettings);
|
---|
117 |
|
---|
118 | settings5 = new AllPermutationsLearnSettings(parties, profiles, false,
|
---|
119 | 2, saopsettings, 2, learnsettings);
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public List<List<AllPermutationsLearnSettings>> getGeneralTestData() {
|
---|
125 | return Arrays.asList(Arrays.asList(settings, settings1a),
|
---|
126 | Arrays.asList(settings2), Arrays.asList(settings3),
|
---|
127 | Arrays.asList(settings4), Arrays.asList(settings5));
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | public List<String> getGeneralTestStrings() {
|
---|
132 | return Arrays.asList(
|
---|
133 | "AllPermutationsLearnSettings.*party1.*party2.*,false,.*profile1.*profile2.*profile3.*,2,SAOPSettings.*1.*",
|
---|
134 | "AllPermutationsLearnSettings.*party1.*party2.*party3.*,false,.*profile1.*profile2.*profile3.*,2,SAOPSettings.*1.*",
|
---|
135 | "AllPermutationsLearnSettings.*party1.*party2.*,true,.*profile1.*profile2.*profile3.*,2,SAOPSettings.*1.*",
|
---|
136 | "AllPermutationsLearnSettings.*party1.*party2.*,false,.*profile1.*profile2.*profile3.*profile1.*,2,SAOPSettings.*1.*",
|
---|
137 | "AllPermutationsLearnSettings.*party1.*party2.*,false,.*profile1.*profile2.*profile3.*,2,SAOPSettings.*2.*");
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Test
|
---|
141 | public void getProtocolTest() {
|
---|
142 | assertEquals("APPLearn",
|
---|
143 | settings.getProtocol(new ReportToLogger("test")).getRef()
|
---|
144 | .getURI().toString());
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Test
|
---|
148 | public void testDeserialize() throws IOException {
|
---|
149 | TournamentSettings obj = (TournamentSettings) jackson
|
---|
150 | .readValue(serialized, NegoSettings.class);
|
---|
151 | System.out.println(obj);
|
---|
152 | assertEquals(settings, obj);
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Test
|
---|
156 | public void testSerialize()
|
---|
157 | throws JsonProcessingException, URISyntaxException {
|
---|
158 |
|
---|
159 | String string = jackson.writeValueAsString(settings);
|
---|
160 | System.out.println(string);
|
---|
161 | assertEquals(serialized, string);
|
---|
162 | }
|
---|
163 |
|
---|
164 | @SuppressWarnings("unused")
|
---|
165 | @Test(expected = IllegalArgumentException.class)
|
---|
166 | public void testNullParties() {
|
---|
167 | new AllPermutationsLearnSettings(null,
|
---|
168 | Arrays.asList(profiles1, profiles2, profiles3, profiles1), true,
|
---|
169 | 2, saopsettings, 1, learnsettings);
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 | @SuppressWarnings("unused")
|
---|
174 | @Test(expected = IllegalArgumentException.class)
|
---|
175 | public void testNoParties() {
|
---|
176 | new AllPermutationsLearnSettings(Collections.emptyList(),
|
---|
177 | Arrays.asList(profiles1, profiles2, profiles3, profiles1), true,
|
---|
178 | 2, saopsettings, 1, learnsettings);
|
---|
179 |
|
---|
180 | }
|
---|
181 |
|
---|
182 | @SuppressWarnings("unused")
|
---|
183 | @Test(expected = IllegalArgumentException.class)
|
---|
184 | public void testNullProfiles() {
|
---|
185 | new AllPermutationsLearnSettings(parties, null, true, 2, saopsettings,
|
---|
186 | 1, learnsettings);
|
---|
187 |
|
---|
188 | }
|
---|
189 |
|
---|
190 | @SuppressWarnings("unused")
|
---|
191 | @Test(expected = IllegalArgumentException.class)
|
---|
192 | public void testInsufficientProfiles() {
|
---|
193 | new AllPermutationsLearnSettings(parties,
|
---|
194 | Arrays.asList(profiles1, profiles2, profiles3, profiles1), true,
|
---|
195 | 4, saopsettings, 1, learnsettings);
|
---|
196 |
|
---|
197 | }
|
---|
198 |
|
---|
199 | @SuppressWarnings("unused")
|
---|
200 | @Test(expected = IllegalArgumentException.class)
|
---|
201 | public void testNullSessionSettings() {
|
---|
202 | new AllPermutationsLearnSettings(parties,
|
---|
203 | Arrays.asList(profiles1, profiles2, profiles3, profiles1), true,
|
---|
204 | 4, null, 1, learnsettings);
|
---|
205 |
|
---|
206 | }
|
---|
207 |
|
---|
208 | @SuppressWarnings("unused")
|
---|
209 | @Test(expected = IllegalArgumentException.class)
|
---|
210 | public void testOnePartyPerSession() {
|
---|
211 | new AllPermutationsLearnSettings(parties,
|
---|
212 | Arrays.asList(profiles1, profiles2, profiles3, profiles1), true,
|
---|
213 | 1, saopsettings, 1, learnsettings);
|
---|
214 |
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Test
|
---|
218 | public void getPermutationsTest() {
|
---|
219 | // #permutations + 1 training session
|
---|
220 | assertEquals(7, settings.permutations().size().intValue());
|
---|
221 | // 2* settings because 2 tournaments
|
---|
222 | assertEquals(14, settings5.permutations().size().intValue());
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Test the learn session
|
---|
227 | */
|
---|
228 | @Test
|
---|
229 | public void testGetlearnSessions() {
|
---|
230 | // particularly we want to test where numparties does not match
|
---|
231 | // numteamspersession
|
---|
232 | AllPermutationsLearnSettings settings3parties = new AllPermutationsLearnSettings(
|
---|
233 | parties3, profiles, false, 2, saopsettings, 2, learnsettings);
|
---|
234 | ImmutableList<SessionSettings> perms = settings3parties.permutations();
|
---|
235 | // 3 parties, 3 profiles, 2 parties per session, = 18 , + 1 learn, *2
|
---|
236 | // tournaments
|
---|
237 | assertEquals(38, perms.size().intValue());
|
---|
238 |
|
---|
239 | // get the last setting, which must be a LearnSetting.
|
---|
240 | LearnSettings learnsetting = (LearnSettings) perms
|
---|
241 | .get(BigInteger.valueOf(37));
|
---|
242 | assertEquals(3, learnsetting.getAllParties().size());
|
---|
243 | Parameters party1params = learnsetting.getAllParties().get(0).getParty()
|
---|
244 | .getParameters();
|
---|
245 | assertEquals("a48b8bc9-4275-4a4e-bee9-6c0a734dc99a",
|
---|
246 | party1params.get("persistentstate").toString());
|
---|
247 | List negodata = (List) party1params.get("negotiationdata");
|
---|
248 | assertEquals(12, negodata.size());
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Test
|
---|
252 | public void getMaxRuntimeTest() {
|
---|
253 | assertEquals((Double) (6d * (10 + 2) + 99d), settings.getMaxRunTime());
|
---|
254 | }
|
---|
255 |
|
---|
256 | }
|
---|