source: exampleparties/randomparty/src/test/java/geniusweb/exampleparties/randomparty/RandomPartyTest.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: 8.6 KB
Line 
1package geniusweb.exampleparties.randomparty;
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.times;
10import static org.mockito.Mockito.verify;
11import static org.mockito.Mockito.when;
12
13import java.io.IOException;
14import java.math.BigDecimal;
15import java.net.URI;
16import java.net.URISyntaxException;
17import java.nio.charset.StandardCharsets;
18import java.nio.file.Files;
19import java.nio.file.Paths;
20import java.util.Arrays;
21import java.util.Collections;
22import java.util.LinkedList;
23import java.util.List;
24import java.util.logging.Level;
25
26import org.junit.Before;
27import org.junit.Test;
28
29import com.fasterxml.jackson.core.JsonParseException;
30import com.fasterxml.jackson.databind.JsonMappingException;
31import com.fasterxml.jackson.databind.ObjectMapper;
32
33import geniusweb.actions.Accept;
34import geniusweb.actions.Action;
35import geniusweb.actions.EndNegotiation;
36import geniusweb.actions.Offer;
37import geniusweb.actions.PartyId;
38import geniusweb.actions.Votes;
39import geniusweb.actions.VotesWithValue;
40import geniusweb.bidspace.AllBidsList;
41import geniusweb.connection.ConnectionEnd;
42import geniusweb.inform.ActionDone;
43import geniusweb.inform.Agreements;
44import geniusweb.inform.Finished;
45import geniusweb.inform.Inform;
46import geniusweb.inform.OptIn;
47import geniusweb.inform.Settings;
48import geniusweb.inform.Voting;
49import geniusweb.inform.YourTurn;
50import geniusweb.issuevalue.Bid;
51import geniusweb.party.Capabilities;
52import geniusweb.profile.Profile;
53import geniusweb.profile.utilityspace.LinearAdditive;
54import geniusweb.progress.ProgressRounds;
55import geniusweb.references.Parameters;
56import geniusweb.references.ProfileRef;
57import geniusweb.references.ProtocolRef;
58import geniusweb.references.Reference;
59import tudelft.utilities.listener.DefaultListenable;
60import tudelft.utilities.logging.Reporter;
61
62public class RandomPartyTest {
63
64 private static final PartyId PARTY1 = new PartyId("party1");
65 private static final String SAOP = "SAOP";
66 private static final PartyId otherparty = new PartyId("other");
67 private static final String PROFILE = "src/test/resources/testprofile.json";
68 private final static ObjectMapper jackson = new ObjectMapper();
69
70 private RandomParty party;
71 private final TestConnection connection = new TestConnection();
72 private final ProtocolRef protocol = new ProtocolRef(SAOP);
73 private final ProtocolRef mopacProtocol = new ProtocolRef("MOPAC");
74 private final ProtocolRef mopac2Protocol = new ProtocolRef("MOPAC2");
75 private final ProgressRounds progress = mock(ProgressRounds.class);
76 private Settings settings, mopacSettings, mopac2Settings;
77 private LinearAdditive profile;
78 private final Parameters parameters = new Parameters();
79
80 @Before
81 public void before() throws JsonParseException, JsonMappingException,
82 IOException, URISyntaxException {
83 party = new RandomParty();
84 settings = new Settings(PARTY1,
85 new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
86 parameters);
87 mopacSettings = new Settings(PARTY1,
88 new ProfileRef(new URI("file:" + PROFILE)), mopacProtocol,
89 progress, parameters);
90 mopac2Settings = new Settings(PARTY1,
91 new ProfileRef(new URI("file:" + PROFILE)), mopac2Protocol,
92 progress, parameters);
93
94 String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
95 StandardCharsets.UTF_8);
96 profile = (LinearAdditive) jackson.readValue(serialized, Profile.class);
97
98 }
99
100 @Test
101 public void smokeTest() {
102 }
103
104 @Test
105 public void getDescriptionTest() {
106 assertNotNull(party.getDescription());
107 }
108
109 @Test
110 public void getCapabilitiesTest() {
111 Capabilities capabilities = party.getCapabilities();
112 assertFalse("party does not define protocols",
113 capabilities.getBehaviours().isEmpty());
114 }
115
116 @Test
117 public void testInformConnection() {
118 party.connect(connection);
119 // agent should not start acting just after an inform
120 assertEquals(0, connection.getActions().size());
121 }
122
123 @Test
124 public void testInformSettings() {
125 party.connect(connection);
126 connection.notifyListeners(settings);
127 assertEquals(0, connection.getActions().size());
128 }
129
130 @Test
131 public void testInformAndConnection() {
132 party.connect(connection);
133 party.notifyChange(settings);
134 assertEquals(0, connection.getActions().size());
135 }
136
137 @Test
138 public void testOtherWalksAway() {
139 party.connect(connection);
140 party.notifyChange(settings);
141
142 party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
143
144 // party should not act at this point
145 assertEquals(0, connection.getActions().size());
146 }
147
148 @Test
149 public void testAgentHasFirstTurn() {
150 party.connect(connection);
151 party.notifyChange(settings);
152
153 party.notifyChange(new YourTurn());
154 assertEquals(1, connection.getActions().size());
155 assertTrue(connection.getActions().get(0) instanceof Offer);
156 }
157
158 @Test
159 public void testAgentAccepts() {
160 party.connect(connection);
161 party.notifyChange(settings);
162
163 Bid bid = findGoodBid();
164 party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
165 party.notifyChange(new YourTurn());
166 assertEquals(1, connection.getActions().size());
167 assertTrue(connection.getActions().get(0) instanceof Accept);
168
169 }
170
171 @Test
172 public void testAgentLogsFinal() {
173 // this log output is optional, this is to show how to check log
174 Reporter reporter = mock(Reporter.class);
175 party = new RandomParty(reporter);
176 party.connect(connection);
177 party.notifyChange(settings);
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 testAgentsUpdatesSAOPProgress() {
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 testAgentsUpdatesMOPACProgress() {
197 party.connect(connection);
198 party.notifyChange(mopacSettings);
199 // in mopac, progress happens only after optin phase
200 party.notifyChange(new YourTurn());
201 verify(progress, times(0)).advance();
202 party.notifyChange(
203 new Voting(Collections.emptyList(), Collections.emptyMap()));
204 verify(progress, times(0)).advance();
205 party.notifyChange(new OptIn(Collections.emptyList()));
206 verify(progress, times(1)).advance();
207 }
208
209 @Test
210 public void testGetCapabilities() {
211 assertTrue(party.getCapabilities().getBehaviours().contains(SAOP));
212 }
213
214 private Bid findGoodBid() {
215 for (Bid bid : new AllBidsList(profile.getDomain())) {
216 if (profile.getUtility(bid)
217 .compareTo(BigDecimal.valueOf(0.7)) > 0) {
218 return bid;
219 }
220 }
221 throw new IllegalStateException(
222 "Test can not be done: there is no good bid with utility>0.7");
223 }
224
225 @Test
226 public void testVoting() throws URISyntaxException {
227 party.connect(connection);
228 party.notifyChange(mopacSettings);
229
230 Bid bid = findGoodBid();
231 Offer offer = new Offer(PARTY1, bid);
232 party.notifyChange(new Voting(Arrays.asList(offer),
233 Collections.singletonMap(PARTY1, 1)));
234 assertEquals(1, connection.getActions().size());
235 Action action = connection.getActions().get(0);
236 assertTrue(action instanceof Votes);
237 assertEquals(1, ((Votes) action).getVotes().size());
238 assertEquals(bid,
239 ((Votes) action).getVotes().iterator().next().getBid());
240 }
241
242 @Test
243 public void testVotingWithValue() throws URISyntaxException {
244 party.connect(connection);
245 party.notifyChange(mopac2Settings);
246
247 Bid bid = findGoodBid();
248 Offer offer = new Offer(PARTY1, bid);
249 party.notifyChange(new Voting(Arrays.asList(offer),
250 Collections.singletonMap(PARTY1, 1)));
251 assertEquals(1, connection.getActions().size());
252 Action action = connection.getActions().get(0);
253 assertTrue(action instanceof VotesWithValue);
254 assertEquals(1, ((VotesWithValue) action).getVotes().size());
255 assertEquals(bid, ((VotesWithValue) action).getVotes().iterator().next()
256 .getBid());
257 }
258}
259
260/**
261 * A "real" connection object, because the party is going to subscribe etc, and
262 * without a real connection we would have to do a lot of mocks that would make
263 * the test very hard to read.
264 *
265 */
266class TestConnection extends DefaultListenable<Inform>
267 implements ConnectionEnd<Inform, Action> {
268 private List<Action> actions = new LinkedList<>();
269
270 @Override
271 public void send(Action action) throws IOException {
272 actions.add(action);
273 }
274
275 @Override
276 public Reference getReference() {
277 return null;
278 }
279
280 @Override
281 public URI getRemoteURI() {
282 return null;
283 }
284
285 @Override
286 public void close() {
287
288 }
289
290 @Override
291 public Error getError() {
292 return null;
293 }
294
295 public List<Action> getActions() {
296 return actions;
297 }
298
299}
Note: See TracBrowser for help on using the repository browser.