source: anac2020/Anaconda/src/test/java/geniusweb/exampleparties/anaconda/AnacondaTest.java@ 1

Last change on this file since 1 was 1, checked in by wouter, 4 years ago

#1910 added anac2020 parties

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