1 | package geniusweb.exampleparties.timedependentparty;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 | import static org.mockito.Matchers.eq;
|
---|
8 | import static org.mockito.Mockito.mock;
|
---|
9 | import static org.mockito.Mockito.verify;
|
---|
10 |
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.math.BigDecimal;
|
---|
13 | import java.net.URI;
|
---|
14 | import java.net.URISyntaxException;
|
---|
15 | import java.nio.charset.StandardCharsets;
|
---|
16 | import java.nio.file.Files;
|
---|
17 | import java.nio.file.Paths;
|
---|
18 | import java.util.LinkedList;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.logging.Level;
|
---|
21 |
|
---|
22 | import org.junit.Before;
|
---|
23 | import org.junit.Test;
|
---|
24 |
|
---|
25 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
26 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
27 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
28 |
|
---|
29 | import geniusweb.actions.Accept;
|
---|
30 | import geniusweb.actions.Action;
|
---|
31 | import geniusweb.actions.EndNegotiation;
|
---|
32 | import geniusweb.actions.Offer;
|
---|
33 | import geniusweb.actions.PartyId;
|
---|
34 | import geniusweb.bidspace.AllBidsList;
|
---|
35 | import geniusweb.connection.ConnectionEnd;
|
---|
36 | import geniusweb.issuevalue.Bid;
|
---|
37 | import geniusweb.party.Capabilities;
|
---|
38 | import geniusweb.party.inform.ActionDone;
|
---|
39 | import geniusweb.party.inform.Finished;
|
---|
40 | import geniusweb.party.inform.Inform;
|
---|
41 | import geniusweb.party.inform.Settings;
|
---|
42 | import geniusweb.party.inform.YourTurn;
|
---|
43 | import geniusweb.profile.Profile;
|
---|
44 | import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
|
---|
45 | import geniusweb.progress.ProgressRounds;
|
---|
46 | import geniusweb.references.Parameters;
|
---|
47 | import geniusweb.references.ProfileRef;
|
---|
48 | import geniusweb.references.ProtocolRef;
|
---|
49 | import geniusweb.references.Reference;
|
---|
50 | import tudelft.utilities.listener.DefaultListenable;
|
---|
51 | import tudelft.utilities.logging.Reporter;
|
---|
52 |
|
---|
53 | public class TimeDependentPartyTest {
|
---|
54 |
|
---|
55 | private static final String SAOP = "SAOP";
|
---|
56 | private static final PartyId otherparty = new PartyId("other");
|
---|
57 | private static final String PROFILE = "src/test/resources/testprofile.json";
|
---|
58 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
59 |
|
---|
60 | private TimeDependentParty party;
|
---|
61 | private TestConnection connection = new TestConnection();
|
---|
62 | private ProtocolRef protocol = mock(ProtocolRef.class);
|
---|
63 | private ProgressRounds progress = mock(ProgressRounds.class);
|
---|
64 | private Parameters parameters = new Parameters();
|
---|
65 | private Settings settings;
|
---|
66 | private LinearAdditiveUtilitySpace profile;
|
---|
67 |
|
---|
68 | @Before
|
---|
69 | public void before() throws JsonParseException, JsonMappingException,
|
---|
70 | IOException, URISyntaxException {
|
---|
71 | party = new TimeDependentParty() {
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public String getDescription() {
|
---|
75 | return "test";
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public double getE() {
|
---|
80 | return 2; // conceder-like
|
---|
81 | }
|
---|
82 | };
|
---|
83 | settings = new Settings(new PartyId("party1"),
|
---|
84 | new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
|
---|
85 | parameters);
|
---|
86 |
|
---|
87 | String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
|
---|
88 | StandardCharsets.UTF_8);
|
---|
89 | profile = (LinearAdditiveUtilitySpace) jackson.readValue(serialized,
|
---|
90 | Profile.class);
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Test
|
---|
95 | public void smokeTest() {
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Test
|
---|
99 | public void getDescriptionTest() {
|
---|
100 | assertNotNull(party.getDescription());
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Test
|
---|
104 | public void getCapabilitiesTest() {
|
---|
105 | Capabilities capabilities = party.getCapabilities();
|
---|
106 | assertFalse("party does not define protocols",
|
---|
107 | capabilities.getBehaviours().isEmpty());
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Test
|
---|
111 | public void testInformConnection() {
|
---|
112 | party.connect(connection);
|
---|
113 | // agent should not start acting just after an inform
|
---|
114 | assertEquals(0, connection.getActions().size());
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Test
|
---|
118 | public void testInformSettings() {
|
---|
119 | party.connect(connection);
|
---|
120 | connection.notifyListeners(settings);
|
---|
121 | assertEquals(0, connection.getActions().size());
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Test
|
---|
125 | public void testInformAndConnection() {
|
---|
126 | party.connect(connection);
|
---|
127 | party.notifyChange(settings);
|
---|
128 | assertEquals(0, connection.getActions().size());
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Test
|
---|
132 | public void testOtherWalksAway() {
|
---|
133 | party.connect(connection);
|
---|
134 | party.notifyChange(settings);
|
---|
135 |
|
---|
136 | party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
|
---|
137 |
|
---|
138 | // party should not act at this point
|
---|
139 | assertEquals(0, connection.getActions().size());
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Test
|
---|
143 | public void testAgentHasFirstTurn() {
|
---|
144 | party.connect(connection);
|
---|
145 | party.notifyChange(settings);
|
---|
146 |
|
---|
147 | party.notifyChange(new YourTurn());
|
---|
148 | assertEquals(1, connection.getActions().size());
|
---|
149 | assertTrue(connection.getActions().get(0) instanceof Offer);
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Test
|
---|
153 | public void testAgentAccepts() {
|
---|
154 | party.connect(connection);
|
---|
155 | party.notifyChange(settings);
|
---|
156 |
|
---|
157 | Bid bid = findBestBid();
|
---|
158 | party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
|
---|
159 | party.notifyChange(new YourTurn());
|
---|
160 | assertEquals(1, connection.getActions().size());
|
---|
161 | assertTrue(connection.getActions().get(0) instanceof Accept);
|
---|
162 |
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Test
|
---|
166 | public void testAgentLogsFinal() {
|
---|
167 | // this log output is optional, this is to show how to check log
|
---|
168 | Reporter reporter = mock(Reporter.class);
|
---|
169 | party = new TimeDependentParty(reporter) {
|
---|
170 |
|
---|
171 | @Override
|
---|
172 | public String getDescription() {
|
---|
173 | return "test";
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | public double getE() {
|
---|
178 | return 2; /// conceder like
|
---|
179 | }
|
---|
180 | };
|
---|
181 | party.connect(connection);
|
---|
182 | party.notifyChange(settings);
|
---|
183 | party.notifyChange(new Finished(null));
|
---|
184 |
|
---|
185 | verify(reporter).log(eq(Level.INFO),
|
---|
186 | eq("Final ourcome:Finished[null]"));
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Test
|
---|
190 | public void testAgentsUpdatesProgress() {
|
---|
191 | party.connect(connection);
|
---|
192 | party.notifyChange(settings);
|
---|
193 |
|
---|
194 | party.notifyChange(new YourTurn());
|
---|
195 | verify(progress).advance();
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Test
|
---|
199 | public void testGetCapabilities() {
|
---|
200 | assertTrue(party.getCapabilities().getBehaviours().contains(SAOP));
|
---|
201 | }
|
---|
202 |
|
---|
203 | private Bid findBestBid() {
|
---|
204 | BigDecimal bestvalue = BigDecimal.ZERO;
|
---|
205 | Bid best = null;
|
---|
206 | for (Bid bid : new AllBidsList(profile.getDomain())) {
|
---|
207 | BigDecimal util = profile.getUtility(bid);
|
---|
208 | if (util.compareTo(bestvalue) > 0) {
|
---|
209 | best = bid;
|
---|
210 | bestvalue = util;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | return best;
|
---|
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 | */
|
---|
224 | class 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 | }
|
---|