1 | package geniusweb.exampleparties.anaconda;
|
---|
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.anyLong;
|
---|
8 | import static org.mockito.Matchers.eq;
|
---|
9 | import static org.mockito.Mockito.mock;
|
---|
10 | import static org.mockito.Mockito.verify;
|
---|
11 | import static org.mockito.Mockito.when;
|
---|
12 |
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.net.URI;
|
---|
15 | import java.net.URISyntaxException;
|
---|
16 | import java.nio.charset.StandardCharsets;
|
---|
17 | import java.nio.file.Files;
|
---|
18 | import java.nio.file.Paths;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.logging.Level;
|
---|
22 |
|
---|
23 | import org.junit.Before;
|
---|
24 | import org.junit.Test;
|
---|
25 |
|
---|
26 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
27 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
28 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
29 |
|
---|
30 | import geniusweb.actions.Accept;
|
---|
31 | import geniusweb.actions.Action;
|
---|
32 | import geniusweb.actions.EndNegotiation;
|
---|
33 | import geniusweb.actions.Offer;
|
---|
34 | import geniusweb.actions.PartyId;
|
---|
35 | import geniusweb.connection.ConnectionEnd;
|
---|
36 | import geniusweb.inform.ActionDone;
|
---|
37 | import geniusweb.inform.Agreements;
|
---|
38 | import geniusweb.inform.Finished;
|
---|
39 | import geniusweb.inform.Inform;
|
---|
40 | import geniusweb.inform.Settings;
|
---|
41 | import geniusweb.inform.YourTurn;
|
---|
42 | import geniusweb.issuevalue.Bid;
|
---|
43 | import geniusweb.party.Capabilities;
|
---|
44 | import geniusweb.profile.DefaultPartialOrdering;
|
---|
45 | import geniusweb.profile.PartialOrdering;
|
---|
46 | import geniusweb.profile.Profile;
|
---|
47 | import geniusweb.progress.ProgressRounds;
|
---|
48 | import geniusweb.references.Parameters;
|
---|
49 | import geniusweb.references.ProfileRef;
|
---|
50 | import geniusweb.references.ProtocolRef;
|
---|
51 | import geniusweb.references.Reference;
|
---|
52 | import tudelft.utilities.listener.DefaultListenable;
|
---|
53 | import tudelft.utilities.logging.Reporter;
|
---|
54 |
|
---|
55 | public class AnacondaTest {
|
---|
56 |
|
---|
57 | private static final String SHAOP = "SHAOP";
|
---|
58 | private static final PartyId otherparty = new PartyId("other");
|
---|
59 | private static final String PROFILE = "src/test/resources/jobs1partial20.json";
|
---|
60 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
61 |
|
---|
62 | private Anaconda party;
|
---|
63 | private final TestConnection connection = new TestConnection();
|
---|
64 | private final ProtocolRef protocol = mock(ProtocolRef.class);
|
---|
65 | private ProgressRounds progress = mock(ProgressRounds.class);
|
---|
66 | private Settings settings;
|
---|
67 | private PartialOrdering profile;
|
---|
68 | private final Parameters parameters = new Parameters();
|
---|
69 |
|
---|
70 | @Before
|
---|
71 | public void before() throws JsonParseException, JsonMappingException, IOException, URISyntaxException {
|
---|
72 | when(progress.get(anyLong())).thenReturn(0.4);
|
---|
73 | when(progress.advance()).thenReturn(progress);
|
---|
74 | party = new Anaconda();
|
---|
75 | settings = new Settings(new PartyId("party1"), new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
|
---|
76 | parameters);
|
---|
77 |
|
---|
78 | String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)), StandardCharsets.UTF_8);
|
---|
79 | profile = (PartialOrdering) 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", capabilities.getBehaviours().isEmpty());
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Test
|
---|
99 | public void testInformConnection() {
|
---|
100 | party.connect(connection);
|
---|
101 | // agent should not start acting just after an inform
|
---|
102 | assertEquals(0, connection.getActions().size());
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Test
|
---|
106 | public void testInformSettings() {
|
---|
107 | party.connect(connection);
|
---|
108 | connection.notifyListeners(settings);
|
---|
109 | assertEquals(0, connection.getActions().size());
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Test
|
---|
113 | public void testInformAndConnection() {
|
---|
114 | party.connect(connection);
|
---|
115 | party.notifyChange(settings);
|
---|
116 | assertEquals(0, connection.getActions().size());
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Test
|
---|
120 | public void testOtherWalksAway() {
|
---|
121 | party.connect(connection);
|
---|
122 | party.notifyChange(settings);
|
---|
123 |
|
---|
124 | party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
|
---|
125 |
|
---|
126 | // party should not act at this point
|
---|
127 | assertEquals(0, connection.getActions().size());
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Test
|
---|
131 | public void testAgentHasFirstTurn() {
|
---|
132 | party.connect(connection);
|
---|
133 | party.notifyChange(settings);
|
---|
134 |
|
---|
135 | party.notifyChange(new YourTurn());
|
---|
136 | assertEquals(1, connection.getActions().size());
|
---|
137 | assertTrue(connection.getActions().get(0) instanceof Offer);
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Test
|
---|
141 | public void testAgentAccepts() {
|
---|
142 | party.connect(connection);
|
---|
143 | party.notifyChange(settings);
|
---|
144 |
|
---|
145 | Bid bid = findGoodBid();
|
---|
146 |
|
---|
147 | // agent generally does not accept in round 1. But it should accept
|
---|
148 | // soon. Also we have progress=40% so it's not a blank atart.
|
---|
149 | int round = 1;
|
---|
150 | boolean accept = false;
|
---|
151 | while (round < 10 && !accept) {
|
---|
152 | party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
|
---|
153 | party.notifyChange(new YourTurn());
|
---|
154 | assertEquals(round, connection.getActions().size());
|
---|
155 | accept = connection.getActions().get(round - 1) instanceof Accept;
|
---|
156 | round++;
|
---|
157 | }
|
---|
158 | assertTrue("Party did not accept good offer at all", accept);
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Test
|
---|
163 | public void testAgentLogsFinal() {
|
---|
164 | // this log output is optional, this is to show how to check log
|
---|
165 | Reporter reporter = mock(Reporter.class);
|
---|
166 | party = new Anaconda(reporter);
|
---|
167 | party.connect(connection);
|
---|
168 | party.notifyChange(settings);
|
---|
169 | party.notifyChange(new Finished(new Agreements()));
|
---|
170 |
|
---|
171 | verify(reporter).log(eq(Level.INFO), eq("Final ourcome:Finished[Agreements{}]"));
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Test
|
---|
175 | public void testAgentsUpdatesProgress() {
|
---|
176 | party.connect(connection);
|
---|
177 | party.notifyChange(settings);
|
---|
178 |
|
---|
179 | party.notifyChange(new YourTurn());
|
---|
180 | verify(progress).advance();
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Test
|
---|
184 | public void testGetCapabilities() {
|
---|
185 | assertTrue(party.getCapabilities().getBehaviours().contains(SHAOP));
|
---|
186 | }
|
---|
187 |
|
---|
188 | private Bid findGoodBid() {
|
---|
189 | // assumes that profile is a DefaultPartial. The top 30% bids should be
|
---|
190 | // good.
|
---|
191 | if (!(profile instanceof DefaultPartialOrdering))
|
---|
192 | throw new IllegalStateException("Test can not be done: there is no good bid with utility>0.7");
|
---|
193 | List<Bid> bids = ((DefaultPartialOrdering) profile).getBids();
|
---|
194 | return bids.get(bids.size() - 2);
|
---|
195 | }
|
---|
196 |
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * A "real" connection object, because the party is going to subscribe etc, and
|
---|
201 | * without a real connection we would have to do a lot of mocks that would make
|
---|
202 | * the test very hard to read.
|
---|
203 | *
|
---|
204 | */
|
---|
205 | class TestConnection extends DefaultListenable<Inform> implements ConnectionEnd<Inform, Action> {
|
---|
206 | private List<Action> actions = new LinkedList<>();
|
---|
207 |
|
---|
208 | @Override
|
---|
209 | public void send(Action action) throws IOException {
|
---|
210 | actions.add(action);
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Override
|
---|
214 | public Reference getReference() {
|
---|
215 | return null;
|
---|
216 | }
|
---|
217 |
|
---|
218 | @Override
|
---|
219 | public URI getRemoteURI() {
|
---|
220 | return null;
|
---|
221 | }
|
---|
222 |
|
---|
223 | @Override
|
---|
224 | public void close() {
|
---|
225 |
|
---|
226 | }
|
---|
227 |
|
---|
228 | @Override
|
---|
229 | public Error getError() {
|
---|
230 | return null;
|
---|
231 | }
|
---|
232 |
|
---|
233 | public List<Action> getActions() {
|
---|
234 | return actions;
|
---|
235 | }
|
---|
236 |
|
---|
237 | }
|
---|