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