source: exampleparties/randomparty/src/test/java/geniusweb/exampleparties/randomparty/RandomPartyTest.java@ 28

Last change on this file since 28 was 28, checked in by bart, 4 years ago

minor fixes to improve extendability

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