[1] | 1 | package shineagent;
|
---|
| 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;
|
---|
| 17 | import java.util.HashMap;
|
---|
| 18 | import java.util.LinkedList;
|
---|
| 19 | import java.util.List;
|
---|
| 20 | import java.util.Map;
|
---|
| 21 | import java.util.logging.Level;
|
---|
| 22 |
|
---|
| 23 | import org.junit.Before;
|
---|
| 24 | import org.junit.Ignore;
|
---|
| 25 | import org.junit.Test;
|
---|
| 26 |
|
---|
| 27 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
| 28 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
| 29 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 30 |
|
---|
| 31 | import geniusweb.actions.Accept;
|
---|
| 32 | import geniusweb.actions.Action;
|
---|
| 33 | import geniusweb.actions.ElicitComparison;
|
---|
| 34 | import geniusweb.actions.EndNegotiation;
|
---|
| 35 | import geniusweb.actions.Offer;
|
---|
| 36 | import geniusweb.actions.PartyId;
|
---|
| 37 | import geniusweb.connection.ConnectionEnd;
|
---|
| 38 | import geniusweb.issuevalue.Bid;
|
---|
| 39 | import geniusweb.issuevalue.DiscreteValue;
|
---|
| 40 | import geniusweb.issuevalue.Value;
|
---|
| 41 | import geniusweb.party.Capabilities;
|
---|
| 42 | import geniusweb.party.inform.ActionDone;
|
---|
| 43 | import geniusweb.party.inform.Finished;
|
---|
| 44 | import geniusweb.party.inform.Inform;
|
---|
| 45 | import geniusweb.party.inform.Settings;
|
---|
| 46 | import geniusweb.party.inform.YourTurn;
|
---|
| 47 | import geniusweb.profile.Profile;
|
---|
| 48 | import geniusweb.progress.ProgressRounds;
|
---|
| 49 | import geniusweb.references.Parameters;
|
---|
| 50 | import geniusweb.references.ProfileRef;
|
---|
| 51 | import geniusweb.references.ProtocolRef;
|
---|
| 52 | import geniusweb.references.Reference;
|
---|
| 53 | import shineagent.ShineParty;
|
---|
| 54 | import tudelft.utilities.listener.DefaultListenable;
|
---|
| 55 | import tudelft.utilities.logging.Reporter;
|
---|
| 56 |
|
---|
| 57 | public class ShinePartyTest {
|
---|
| 58 |
|
---|
| 59 | private static final String SHAOP = "SHAOP";
|
---|
| 60 | private static final PartyId otherparty = new PartyId("other");
|
---|
| 61 | private static final String PROFILE = "src/test/resources/testprofile.json";
|
---|
| 62 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
| 63 |
|
---|
| 64 | private ShineParty party;
|
---|
| 65 | private final TestConnection connection = new TestConnection();
|
---|
| 66 | private final ProtocolRef protocol = mock(ProtocolRef.class);
|
---|
| 67 | private final ProgressRounds progress = mock(ProgressRounds.class);
|
---|
| 68 | private Settings settings;
|
---|
| 69 | private Profile profile;
|
---|
| 70 | private final Parameters parameters = new Parameters();
|
---|
| 71 |
|
---|
| 72 | @Before
|
---|
| 73 | public void before() throws JsonParseException, JsonMappingException,
|
---|
| 74 | IOException, URISyntaxException {
|
---|
| 75 | party = new ShineParty();
|
---|
| 76 | settings = new Settings(new PartyId("party1"),
|
---|
| 77 | new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
|
---|
| 78 | parameters);
|
---|
| 79 |
|
---|
| 80 | String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
|
---|
| 81 | StandardCharsets.UTF_8);
|
---|
| 82 | profile = jackson.readValue(serialized, Profile.class);
|
---|
| 83 |
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | @Test
|
---|
| 87 | public void smokeTest() {
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | @Test
|
---|
| 91 | public void getDescriptionTest() {
|
---|
| 92 | assertNotNull(party.getDescription());
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @Test
|
---|
| 96 | public void getCapabilitiesTest() {
|
---|
| 97 | Capabilities capabilities = party.getCapabilities();
|
---|
| 98 | assertFalse("party does not define protocols",
|
---|
| 99 | capabilities.getBehaviours().isEmpty());
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | @Test
|
---|
| 103 | public void testInformConnection() {
|
---|
| 104 | party.connect(connection);
|
---|
| 105 | // agent should not start acting just after an inform
|
---|
| 106 | assertEquals(0, connection.getActions().size());
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | @Test
|
---|
| 110 | public void testInformSettings() {
|
---|
| 111 | party.connect(connection);
|
---|
| 112 | connection.notifyListeners(settings);
|
---|
| 113 | assertEquals(0, connection.getActions().size());
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | @Test
|
---|
| 117 | public void testInformAndConnection() {
|
---|
| 118 | party.connect(connection);
|
---|
| 119 | party.notifyChange(settings);
|
---|
| 120 | assertEquals(0, connection.getActions().size());
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | @Test
|
---|
| 124 | public void testOtherWalksAway() {
|
---|
| 125 | party.connect(connection);
|
---|
| 126 | party.notifyChange(settings);
|
---|
| 127 |
|
---|
| 128 | party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
|
---|
| 129 |
|
---|
| 130 | // party should not act at this point
|
---|
| 131 | assertEquals(0, connection.getActions().size());
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | @Test
|
---|
| 135 | public void testAgentHasFirstTurn() {
|
---|
| 136 | party.connect(connection);
|
---|
| 137 | party.notifyChange(settings);
|
---|
| 138 |
|
---|
| 139 | party.notifyChange(new YourTurn());
|
---|
| 140 | assertEquals(1, connection.getActions().size());
|
---|
| 141 | assertTrue(connection.getActions().get(0) instanceof Offer);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | @Test
|
---|
| 145 | public void testAgentElicitsComparison() {
|
---|
| 146 | party.connect(connection);
|
---|
| 147 | party.notifyChange(settings);
|
---|
| 148 |
|
---|
| 149 | Bid bid = makeBid("3", "3"); // not yet in profile
|
---|
| 150 | party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
|
---|
| 151 | party.notifyChange(new YourTurn());
|
---|
| 152 | assertEquals(1, connection.getActions().size());
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | @Ignore
|
---|
| 156 | @Test
|
---|
| 157 | public void testAgentAccepts() {
|
---|
| 158 | // to make agent accept, the offered bid must score >0.9
|
---|
| 159 | // we would need to mock a lot t make that happen
|
---|
| 160 | // as we need to place >10 comparable bids for that.
|
---|
| 161 | party.connect(connection);
|
---|
| 162 | party.notifyChange(settings);
|
---|
| 163 |
|
---|
| 164 | Bid bid = makeBid("1", "1");// best pssible
|
---|
| 165 | party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
|
---|
| 166 | party.notifyChange(new YourTurn());
|
---|
| 167 | assertEquals(1, connection.getActions().size());
|
---|
| 168 | assertTrue(connection.getActions().get(0) instanceof Accept);
|
---|
| 169 |
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | @Test
|
---|
| 173 | public void testAgentLogsFinal() {
|
---|
| 174 | // this log output is optional, this is to show how to check log
|
---|
| 175 | Reporter reporter = mock(Reporter.class);
|
---|
| 176 | party = new ShineParty(reporter);
|
---|
| 177 | party.connect(connection);
|
---|
| 178 | party.notifyChange(settings);
|
---|
| 179 | party.notifyChange(new Finished(null));
|
---|
| 180 |
|
---|
| 181 | verify(reporter).log(eq(Level.INFO),
|
---|
| 182 | eq("Final ourcome:Finished[null]"));
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | @Test
|
---|
| 186 | public void testAgentsUpdatesProgress() {
|
---|
| 187 | party.connect(connection);
|
---|
| 188 | party.notifyChange(settings);
|
---|
| 189 |
|
---|
| 190 | party.notifyChange(new ActionDone(new Offer(null, mock(Bid.class))));
|
---|
| 191 |
|
---|
| 192 | party.notifyChange(new YourTurn());
|
---|
| 193 | verify(progress).advance();
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | @Test
|
---|
| 197 | public void testGetCapabilities() {
|
---|
| 198 | assertTrue(party.getCapabilities().getBehaviours().contains(SHAOP));
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | private Bid makeBid(String val1, String val2) {
|
---|
| 202 | Map<String, Value> map = new HashMap<>();
|
---|
| 203 | map.put("iss1", new DiscreteValue(val1));
|
---|
| 204 | map.put("iss2", new DiscreteValue(val2));
|
---|
| 205 | return new Bid(map);
|
---|
| 206 |
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /**
|
---|
| 212 | * A "real" connection object, because the party is going to subscribe etc, and
|
---|
| 213 | * without a real connection we would have to do a lot of mocks that would make
|
---|
| 214 | * the test very hard to read.
|
---|
| 215 | *
|
---|
| 216 | */
|
---|
| 217 | class TestConnection extends DefaultListenable<Inform>
|
---|
| 218 | implements ConnectionEnd<Inform, Action> {
|
---|
| 219 | private List<Action> actions = new LinkedList<>();
|
---|
| 220 |
|
---|
| 221 | @Override
|
---|
| 222 | public void send(Action action) throws IOException {
|
---|
| 223 | actions.add(action);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | @Override
|
---|
| 227 | public Reference getReference() {
|
---|
| 228 | return null;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | @Override
|
---|
| 232 | public URI getRemoteURI() {
|
---|
| 233 | return null;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | @Override
|
---|
| 237 | public void close() {
|
---|
| 238 |
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | @Override
|
---|
| 242 | public Error getError() {
|
---|
| 243 | return null;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | public List<Action> getActions() {
|
---|
| 247 | return actions;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | }
|
---|