source: anac2020/Angel/src/test/java/geniusweb/exampleparties/simpleshaop/AngelPartyTest.java@ 1

Last change on this file since 1 was 1, checked in by wouter, 4 years ago

#1910 added anac2020 parties

File size: 11.2 KB
Line 
1package geniusweb.exampleparties.simpleshaop;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.assertTrue;
7import static org.mockito.Matchers.eq;
8import static org.mockito.Mockito.mock;
9import static org.mockito.Mockito.verify;
10
11import java.io.IOException;
12import java.net.URI;
13import java.net.URISyntaxException;
14import java.nio.charset.StandardCharsets;
15import java.nio.file.Files;
16import java.nio.file.Paths;
17import java.util.HashMap;
18import java.util.LinkedList;
19import java.util.List;
20import java.util.Map;
21import java.util.logging.Level;
22import java.util.concurrent.TimeUnit;
23import java.util.Date;
24
25import org.junit.Before;
26import org.junit.Ignore;
27import org.junit.Test;
28
29import com.fasterxml.jackson.core.JsonParseException;
30import com.fasterxml.jackson.databind.JsonMappingException;
31import com.fasterxml.jackson.databind.ObjectMapper;
32
33import geniusweb.actions.Accept;
34import geniusweb.actions.Action;
35import geniusweb.actions.Comparison;
36import geniusweb.actions.ElicitComparison;
37import geniusweb.actions.EndNegotiation;
38import geniusweb.actions.Offer;
39import geniusweb.actions.PartyId;
40import geniusweb.connection.ConnectionEnd;
41import geniusweb.issuevalue.Bid;
42import geniusweb.issuevalue.DiscreteValue;
43import geniusweb.issuevalue.Value;
44import geniusweb.party.Capabilities;
45import geniusweb.party.inform.ActionDone;
46import geniusweb.party.inform.Finished;
47import geniusweb.party.inform.Inform;
48import geniusweb.party.inform.Settings;
49import geniusweb.party.inform.YourTurn;
50import geniusweb.profile.DefaultPartialOrdering;
51import geniusweb.profile.Profile;
52import geniusweb.progress.ProgressRounds;
53import geniusweb.references.Parameters;
54import geniusweb.references.ProfileRef;
55import geniusweb.references.ProtocolRef;
56import geniusweb.references.Reference;
57import tudelft.utilities.listener.DefaultListenable;
58import tudelft.utilities.logging.Reporter;
59
60public class AngelPartyTest {
61
62 private static final String SHAOP = "SHAOP";
63 private static final PartyId angel = new PartyId("Angel");
64 private static final PartyId otherparty = new PartyId("other");
65 private static final String PROFILE = "src/test/resources/testprofile.json";
66 private static final String PROFILE2 = "src/test/resources/testprofile2.json";
67 private static final String PROFILE3 = "src/test/resources/testprofile3.json";
68 private final static ObjectMapper jackson = new ObjectMapper();
69
70 private AngelParty party;
71 private AngelParty opp;
72 private final TestConnection connection = new TestConnection();
73 private final ProtocolRef protocol = mock(ProtocolRef.class);
74 private Integer T = 10;
75 private Integer t = 0;
76 private Date deadline = new Date();
77 //private final ProgressRounds progress = new ProgressRounds(T, t, deadline); // This was mock(ProgressRounds.class);
78 private final ProgressRounds progress = mock(ProgressRounds.class);
79 private Settings settings;
80 private Settings settings2;
81 private Profile profile;
82 private Profile profile2;
83 private Profile profile3;
84 private final Parameters parameters = new Parameters();
85
86 @Before
87 public void before() throws JsonParseException, JsonMappingException,
88 IOException, URISyntaxException {
89 party = new AngelParty();
90 opp = new AngelParty();
91 parameters.put("elicitationcost", 0.05);
92 settings = new Settings(new PartyId("party1"),
93 new ProfileRef(new URI("file:" + PROFILE3)), protocol, progress,
94 parameters);
95 settings2 = new Settings(new PartyId("party2"),
96 new ProfileRef(new URI("file:" + PROFILE2)), protocol, progress,
97 parameters);
98
99 String serialized = new String(Files.readAllBytes(Paths.get(PROFILE3)),
100 StandardCharsets.UTF_8);
101 profile3 = jackson.readValue(serialized, Profile.class);
102 serialized = new String(Files.readAllBytes(Paths.get(PROFILE2)), StandardCharsets.UTF_8);
103 profile2 = jackson.readValue(serialized, Profile.class);
104 }
105
106 @Test
107 public void smokeTest() {
108 }
109
110 @Test
111 public void getDescriptionTest() {
112 assertNotNull(party.getDescription());
113 }
114
115 @Test
116 public void getCapabilitiesTest() {
117 Capabilities capabilities = party.getCapabilities();
118 assertFalse("party does not define protocols",
119 capabilities.getBehaviours().isEmpty());
120 }
121
122 @Test
123 public void testInformConnection() {
124 // Good to go
125 party.connect(connection);
126 // agent should not start acting just after an inform
127 assertEquals(0, connection.getActions().size());
128 }
129
130 @Test
131 public void testInformSettings() {
132 // Good to go
133 party.connect(connection);
134 connection.notifyListeners(settings);
135 assertEquals(0, connection.getActions().size());
136 }
137
138 @Test
139 public void testInformAndConnection() {
140 // Good to go
141 party.connect(connection);
142 party.notifyChange(settings);
143 assertEquals(0, connection.getActions().size());
144 }
145
146 @Test
147 public void testOtherWalksAway() {
148 // Good to go
149 party.connect(connection);
150 party.notifyChange(settings);
151
152 party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
153
154 // party should not act at this point
155 assertEquals(0, connection.getActions().size());
156 }
157
158 @Test
159 // Good to go
160 public void testAgentHasFirstTurn() {
161 party.connect(connection);
162 party.notifyChange(settings);
163 party.notifyChange(new YourTurn());
164 assertEquals(1, connection.getActions().size());
165 assertTrue(connection.getActions().get(0) instanceof Offer);
166 }
167 @Ignore //This method for testing acceptance is from the simple shaop agent, it is not applicable to angel.
168 @Test
169 public void testAgentElicitsComparison() {
170 party.connect(connection);
171 party.notifyChange(settings);
172
173 Bid bid = makeBid("3", "3"); // not yet in profile
174 party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
175 party.notifyChange(new YourTurn());
176 assertEquals(1, connection.getActions().size());
177 assertTrue(connection.getActions().get(0) instanceof ElicitComparison);
178
179 }
180 @Ignore //This method for testing acceptance is from the simple shaop agent, it is not applicable to angel.
181 @Test
182 public void testAgentAccepts() {
183 // to make agent accept, the offered bid must score >0.9
184 // we would need to mock a lot t make that happen
185 // as we need to place >10 comparable bids for that.
186 party.connect(connection);
187 party.notifyChange(settings);
188
189 Bid bid = makeBid("1", "1");// best possible
190 party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
191 party.notifyChange(new YourTurn());
192 assertEquals(1, connection.getActions().size());
193 assertTrue(connection.getActions().get(0) instanceof Accept);
194
195 }
196
197 @Test
198 public void testAgentLogsFinal() {
199 // Good to go
200 // this log output is optional, this is to show how to check log
201 Reporter reporter = mock(Reporter.class);
202 party = new AngelParty(reporter);
203 party.connect(connection);
204 party.notifyChange(settings);
205 party.notifyChange(new Finished(null));
206
207 verify(reporter).log(eq(Level.INFO),
208 eq("Final ourcome:Finished[null]"));
209 }
210
211 @Test
212 public void testAgentsUpdatesProgress() {
213 // Good to go
214 party.connect(connection);
215 party.notifyChange(settings);
216 party.notifyChange(new ActionDone(new Offer(null, mock(Bid.class))));
217 party.notifyChange(new YourTurn());
218 verify(progress).advance();
219 }
220
221 @Test
222 public void testGetCapabilities() {
223 // Good to go
224 assertTrue(party.getCapabilities().getBehaviours().contains(SHAOP));
225 }
226
227 private Bid makeBid(String val1, String val2) {
228 Map<String, Value> map = new HashMap<>();
229 map.put("iss1", new DiscreteValue(val1));
230 map.put("iss2", new DiscreteValue(val2));
231 return new Bid(map);
232
233 }
234
235 private Bid getWorstBid() {
236 Map<String, Value> map = new HashMap<>();
237 map.put("iss1", new DiscreteValue("4"));
238 map.put("iss2", new DiscreteValue("4"));
239 return new Bid(map);
240 }
241
242 //***********************TESTING NEW THINGS*********************************
243 @Test
244 public void testMultiRounds() {
245 //System.out.println("\n\n\n\n\n");
246 //System.out.println("***********BEGIN NEGOTIATION**********");
247 party.connect(connection);
248 opp.connect(connection);
249 party.notifyChange(settings);
250 opp.notifyChange(settings2);
251 opp.notifyChange(new YourTurn());
252 AngelParty current;
253 for (int t=0; t<21; t++) {
254 int numActions = connection.getActions().size();
255 //System.out.println("\n***turn="+t+" numActions="+numActions+"***");
256 Action a = connection.getActions().get(numActions-1);
257 if (a instanceof Accept) {
258 party.notifyChange(new Finished(((Accept) a).getBid()));
259 opp.notifyChange(new Finished(((Accept) a).getBid()));
260 break;}
261 if ((t%2 == 0)) {
262 party.notifyChange(new ActionDone(a));
263 party.notifyChange(new YourTurn());
264 numActions = connection.getActions().size();
265 Action acted = connection.getActions().get(numActions-1);
266 while (acted instanceof ElicitComparison) {
267 Bid bid = ((ElicitComparison)acted).getBid();
268 List<Bid> options = ((ElicitComparison)acted).getOptions();
269 List<Bid> better = new LinkedList<Bid>();
270 List<Bid> worse = new LinkedList<Bid>();
271 DefaultPartialOrdering prof = (DefaultPartialOrdering) profile3;
272 for (Bid cbid: options) {
273 if (prof.isPreferredOrEqual(cbid, bid)) {better.add(cbid);}
274 else {worse.add(cbid);}
275 }
276 party.notifyChange(new ActionDone(new Comparison(angel, bid, better, worse)));
277 numActions = connection.getActions().size();
278 acted = connection.getActions().get(numActions-1);
279 }
280 }
281 else {
282 opp.notifyChange(new ActionDone(a));
283 opp.notifyChange(new YourTurn());
284 numActions = connection.getActions().size();
285 Action acted = connection.getActions().get(numActions-1);
286 while (acted instanceof ElicitComparison) {
287 Bid bid = ((ElicitComparison)acted).getBid();
288 List<Bid> options = ((ElicitComparison)acted).getOptions();
289 List<Bid> better = new LinkedList<Bid>();
290 List<Bid> worse = new LinkedList<Bid>();
291 DefaultPartialOrdering prof = (DefaultPartialOrdering) profile2;
292 for (Bid cbid: options) {
293 if (prof.isPreferredOrEqual(cbid, bid)) {better.add(cbid);}
294 else {worse.add(cbid);}
295 }
296 opp.notifyChange(new ActionDone(new Comparison(otherparty, bid, better, worse)));
297 numActions = connection.getActions().size();
298 acted = connection.getActions().get(numActions-1);
299 }
300 }
301 }
302
303 //System.out.println("***********END NEGOTIATION**********");
304 //System.out.println("\n\n\n\n\n");
305
306
307 }
308
309
310
311
312
313}
314
315/**
316 * A "real" connection object, because the party is going to subscribe etc, and
317 * without a real connection we would have to do a lot of mocks that would make
318 * the test very hard to read.
319 *
320 */
321class TestConnection extends DefaultListenable<Inform>
322 implements ConnectionEnd<Inform, Action> {
323 private List<Action> actions = new LinkedList<>();
324
325 @Override
326 public void send(Action action) throws IOException {
327 actions.add(action);
328 }
329
330 @Override
331 public Reference getReference() {
332 return null;
333 }
334
335 @Override
336 public URI getRemoteURI() {
337 return null;
338 }
339
340 @Override
341 public void close() {
342
343 }
344
345 @Override
346 public Error getError() {
347 return null;
348 }
349
350 public List<Action> getActions() {
351 return actions;
352 }
353
354}
Note: See TracBrowser for help on using the repository browser.