source: exampleparties/anac2019/agentgg/src/test/java/geniusweb/exampleparties/agentgg/AgentGGTest.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: 7.0 KB
Line 
1package geniusweb.exampleparties.agentgg;
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.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 AgentGGTest {
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 AgentGG 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.get(anyLong())).thenReturn(0.4);
74 when(progress.advance()).thenReturn(progress);
75 party = new AgentGG();
76 settings = new Settings(new PartyId("party1"),
77 new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
78 parameters);
79
80 String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
81 StandardCharsets.UTF_8);
82 profile = (PartialOrdering) jackson.readValue(serialized,
83 Profile.class);
84
85 }
86
87 @Test
88 public void smokeTest() {
89 }
90
91 @Test
92 public void testMinMaxRealistic() {
93 assertTrue(party.MIN_IMPORTANCE >= 0);
94 assertTrue(party.MIN_IMPORTANCE < 1);
95 assertTrue(party.MAX_IMPORTANCE >= 0);
96 assertTrue(party.MAX_IMPORTANCE < 1);
97 assertTrue(party.MIN_IMPORTANCE <= party.MAX_IMPORTANCE);
98 }
99
100 @Test
101 public void getDescriptionTest() {
102 assertNotNull(party.getDescription());
103 }
104
105 @Test
106 public void getCapabilitiesTest() {
107 Capabilities capabilities = party.getCapabilities();
108 assertFalse("party does not define protocols",
109 capabilities.getBehaviours().isEmpty());
110 }
111
112 @Test
113 public void testInformConnection() {
114 party.connect(connection);
115 // agent should not start acting just after an inform
116 assertEquals(0, connection.getActions().size());
117 }
118
119 @Test
120 public void testInformSettings() {
121 party.connect(connection);
122 connection.notifyListeners(settings);
123 assertEquals(0, connection.getActions().size());
124 }
125
126 @Test
127 public void testInformAndConnection() {
128 party.connect(connection);
129 party.notifyChange(settings);
130 assertEquals(0, connection.getActions().size());
131 }
132
133 @Test
134 public void testOtherWalksAway() {
135 party.connect(connection);
136 party.notifyChange(settings);
137
138 party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
139
140 // party should not act at this point
141 assertEquals(0, connection.getActions().size());
142 }
143
144 @Test
145 public void testAgentHasFirstTurn() {
146 party.connect(connection);
147 party.notifyChange(settings);
148
149 party.notifyChange(new YourTurn());
150 assertEquals(1, connection.getActions().size());
151 assertTrue(connection.getActions().get(0) instanceof Offer);
152 }
153
154 @Test
155 public void testAgentAccepts() {
156 party.connect(connection);
157 party.notifyChange(settings);
158
159 Bid bid = findGoodBid();
160
161 // agent generally does not accept in round 1. But it should accept
162 // soon. Also we have progress=40% so it's not a blank atart.
163 int round = 1;
164 boolean accept = false;
165 while (round < 10 && !accept) {
166 party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
167 party.notifyChange(new YourTurn());
168 assertEquals(round, connection.getActions().size());
169 accept = connection.getActions().get(round - 1) instanceof Accept;
170 round++;
171 }
172 assertTrue("Party did not accept good offer at all", accept);
173
174 }
175
176 @Test
177 public void testAgentLogsFinal() {
178 // this log output is optional, this is to show how to check log
179 Reporter reporter = mock(Reporter.class);
180 party = new AgentGG(reporter);
181 party.connect(connection);
182 party.notifyChange(settings);
183
184 Agreements agreements = mock(Agreements.class);
185 when(agreements.toString()).thenReturn("agree");
186 party.notifyChange(new Finished(agreements));
187
188 verify(reporter).log(eq(Level.INFO),
189 eq("Final ourcome:Finished[agree]"));
190 }
191
192 @Test
193 public void testAgentsUpdatesProgress() {
194 party.connect(connection);
195 party.notifyChange(settings);
196
197 party.notifyChange(new YourTurn());
198 verify(progress).advance();
199 }
200
201 @Test
202 public void testGetCapabilities() {
203 assertTrue(party.getCapabilities().getBehaviours().contains(SAOP));
204 }
205
206 private Bid findGoodBid() {
207 // assumes that profile is a DefaultPartial. The top 30% bids should be
208 // good.
209 if (!(profile instanceof DefaultPartialOrdering))
210 throw new IllegalStateException(
211 "Test can not be done: there is no good bid with utility>0.7");
212 List<Bid> bids = ((DefaultPartialOrdering) profile).getBids();
213 return bids.get(bids.size() - 2);
214 }
215
216}
217
218/**
219 * A "real" connection object, because the party is going to subscribe etc, and
220 * without a real connection we would have to do a lot of mocks that would make
221 * the test very hard to read.
222 *
223 */
224class TestConnection extends DefaultListenable<Inform>
225 implements ConnectionEnd<Inform, Action> {
226 private List<Action> actions = new LinkedList<>();
227
228 @Override
229 public void send(Action action) throws IOException {
230 actions.add(action);
231 }
232
233 @Override
234 public Reference getReference() {
235 return null;
236 }
237
238 @Override
239 public URI getRemoteURI() {
240 return null;
241 }
242
243 @Override
244 public void close() {
245
246 }
247
248 @Override
249 public Error getError() {
250 return null;
251 }
252
253 public List<Action> getActions() {
254 return actions;
255 }
256
257}
Note: See TracBrowser for help on using the repository browser.