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

Last change on this file was 28, checked in by wouter, 3 years ago

#3

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