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