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