source: exampleparties/anac2019/agentgg/src/test/java/geniusweb/exampleparties/agentgg/AgentGGTest.java@ 21

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

Version 1.5.

File size: 6.9 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.Finished;
38import geniusweb.inform.Inform;
39import geniusweb.inform.Settings;
40import geniusweb.inform.YourTurn;
41import geniusweb.issuevalue.Bid;
42import geniusweb.party.Capabilities;
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 AgentGGTest {
55
56 private static final String SAOP = "SAOP";
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 AgentGG 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 AgentGG();
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 testMinMaxRealistic() {
92 assertTrue(party.MIN_IMPORTANCE >= 0);
93 assertTrue(party.MIN_IMPORTANCE < 1);
94 assertTrue(party.MAX_IMPORTANCE >= 0);
95 assertTrue(party.MAX_IMPORTANCE < 1);
96 assertTrue(party.MIN_IMPORTANCE <= party.MAX_IMPORTANCE);
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
160 // agent generally does not accept in round 1. But it should accept
161 // soon. Also we have progress=40% so it's not a blank atart.
162 int round = 1;
163 boolean accept = false;
164 while (round < 10 && !accept) {
165 party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
166 party.notifyChange(new YourTurn());
167 assertEquals(round, connection.getActions().size());
168 accept = connection.getActions().get(round - 1) instanceof Accept;
169 round++;
170 }
171 assertTrue("Party did not accept good offer at all", accept);
172
173 }
174
175 @Test
176 public void testAgentLogsFinal() {
177 // this log output is optional, this is to show how to check log
178 Reporter reporter = mock(Reporter.class);
179 party = new AgentGG(reporter);
180 party.connect(connection);
181 party.notifyChange(settings);
182 party.notifyChange(new Finished(null));
183
184 verify(reporter).log(eq(Level.INFO),
185 eq("Final ourcome:Finished[null]"));
186 }
187
188 @Test
189 public void testAgentsUpdatesProgress() {
190 party.connect(connection);
191 party.notifyChange(settings);
192
193 party.notifyChange(new YourTurn());
194 verify(progress).advance();
195 }
196
197 @Test
198 public void testGetCapabilities() {
199 assertTrue(party.getCapabilities().getBehaviours().contains(SAOP));
200 }
201
202 private Bid findGoodBid() {
203 // assumes that profile is a DefaultPartial. The top 30% bids should be
204 // good.
205 if (!(profile instanceof DefaultPartialOrdering))
206 throw new IllegalStateException(
207 "Test can not be done: there is no good bid with utility>0.7");
208 List<Bid> bids = ((DefaultPartialOrdering) profile).getBids();
209 return bids.get(bids.size() - 2);
210 }
211
212}
213
214/**
215 * A "real" connection object, because the party is going to subscribe etc, and
216 * without a real connection we would have to do a lot of mocks that would make
217 * the test very hard to read.
218 *
219 */
220class TestConnection extends DefaultListenable<Inform>
221 implements ConnectionEnd<Inform, Action> {
222 private List<Action> actions = new LinkedList<>();
223
224 @Override
225 public void send(Action action) throws IOException {
226 actions.add(action);
227 }
228
229 @Override
230 public Reference getReference() {
231 return null;
232 }
233
234 @Override
235 public URI getRemoteURI() {
236 return null;
237 }
238
239 @Override
240 public void close() {
241
242 }
243
244 @Override
245 public Error getError() {
246 return null;
247 }
248
249 public List<Action> getActions() {
250 return actions;
251 }
252
253}
Note: See TracBrowser for help on using the repository browser.