source: exampleparties/anac2019/winkyagent/src/test/java/geniusweb/exampleparties/anac2019/winkyagent/WinkyAgentTest.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

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