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.ArgumentMatchers.any;
|
---|
8 | import static org.mockito.ArgumentMatchers.eq;
|
---|
9 | import static org.mockito.Mockito.mock;
|
---|
10 | import static org.mockito.Mockito.verify;
|
---|
11 | import static org.mockito.Mockito.when;
|
---|
12 |
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.math.BigDecimal;
|
---|
15 | import java.net.URI;
|
---|
16 | import java.net.URISyntaxException;
|
---|
17 | import java.nio.charset.StandardCharsets;
|
---|
18 | import java.nio.file.Files;
|
---|
19 | import java.nio.file.Paths;
|
---|
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.EndNegotiation;
|
---|
31 | import geniusweb.actions.Offer;
|
---|
32 | import geniusweb.actions.PartyId;
|
---|
33 | import geniusweb.bidspace.AllBidsList;
|
---|
34 | import geniusweb.inform.ActionDone;
|
---|
35 | import geniusweb.inform.Agreements;
|
---|
36 | import geniusweb.inform.Finished;
|
---|
37 | import geniusweb.inform.Settings;
|
---|
38 | import geniusweb.inform.YourTurn;
|
---|
39 | import geniusweb.issuevalue.Bid;
|
---|
40 | import geniusweb.party.Capabilities;
|
---|
41 | import geniusweb.profile.Profile;
|
---|
42 | import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
|
---|
43 | import geniusweb.progress.ProgressRounds;
|
---|
44 | import geniusweb.references.Parameters;
|
---|
45 | import geniusweb.references.ProfileRef;
|
---|
46 | import geniusweb.references.ProtocolRef;
|
---|
47 | import tudelft.utilities.logging.Reporter;
|
---|
48 |
|
---|
49 | public class TimeDependentPartyTest {
|
---|
50 |
|
---|
51 | private static final String SAOP = "SAOP";
|
---|
52 | private static final PartyId otherparty = new PartyId("other");
|
---|
53 | private static final String PROFILE = "src/test/resources/testprofile.json";
|
---|
54 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
55 |
|
---|
56 | private TimeDependentParty party;
|
---|
57 | private TestConnection connection = new TestConnection();
|
---|
58 | private ProtocolRef protocol = new ProtocolRef("SAOP");
|
---|
59 | private ProgressRounds progress = mock(ProgressRounds.class);
|
---|
60 | private Parameters parameters = new Parameters();
|
---|
61 | private Settings settings;
|
---|
62 | private LinearAdditiveUtilitySpace profile;
|
---|
63 |
|
---|
64 | @Before
|
---|
65 | public void before() throws JsonParseException, JsonMappingException,
|
---|
66 | IOException, URISyntaxException {
|
---|
67 | party = new TimeDependentParty() {
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public String getDescription() {
|
---|
71 | return "test";
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public double getE() {
|
---|
76 | return 2; // conceder-like
|
---|
77 | }
|
---|
78 | };
|
---|
79 | when(progress.get(any())).thenReturn(0d);
|
---|
80 | settings = new Settings(new PartyId("party1"),
|
---|
81 | new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
|
---|
82 | parameters);
|
---|
83 |
|
---|
84 | String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
|
---|
85 | StandardCharsets.UTF_8);
|
---|
86 | profile = (LinearAdditiveUtilitySpace) jackson.readValue(serialized,
|
---|
87 | Profile.class);
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Test
|
---|
92 | public void smokeTest() {
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Test
|
---|
96 | public void getDescriptionTest() {
|
---|
97 | assertNotNull(party.getDescription());
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Test
|
---|
101 | public void getCapabilitiesTest() {
|
---|
102 | Capabilities capabilities = party.getCapabilities();
|
---|
103 | assertFalse("party does not define protocols",
|
---|
104 | capabilities.getBehaviours().isEmpty());
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Test
|
---|
108 | public void testInformConnection() {
|
---|
109 | party.connect(connection);
|
---|
110 | // Party should not start acting just after an inform
|
---|
111 | assertEquals(0, connection.getActions().size());
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Test
|
---|
115 | public void testInformSettings() {
|
---|
116 | party.connect(connection);
|
---|
117 | connection.notifyListeners(settings);
|
---|
118 | assertEquals(0, connection.getActions().size());
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Test
|
---|
122 | public void testInformAndConnection() {
|
---|
123 | party.connect(connection);
|
---|
124 | party.notifyChange(settings);
|
---|
125 | assertEquals(0, connection.getActions().size());
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Test
|
---|
129 | public void testOtherWalksAway() {
|
---|
130 | party.connect(connection);
|
---|
131 | party.notifyChange(settings);
|
---|
132 |
|
---|
133 | party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
|
---|
134 |
|
---|
135 | // party should not act at this point
|
---|
136 | assertEquals(0, connection.getActions().size());
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Test
|
---|
140 | public void testPartyHasFirstTurn() {
|
---|
141 | party.connect(connection);
|
---|
142 | party.notifyChange(settings);
|
---|
143 |
|
---|
144 | party.notifyChange(new YourTurn());
|
---|
145 | assertEquals(1, connection.getActions().size());
|
---|
146 | assertTrue(connection.getActions().get(0) instanceof Offer);
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Test
|
---|
150 | public void testPartyAccepts() {
|
---|
151 | party.connect(connection);
|
---|
152 | party.notifyChange(settings);
|
---|
153 |
|
---|
154 | Bid bid = findBestBid();
|
---|
155 | party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
|
---|
156 | party.notifyChange(new YourTurn());
|
---|
157 | assertEquals(1, connection.getActions().size());
|
---|
158 | assertTrue(connection.getActions().get(0) instanceof Accept);
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Test
|
---|
163 | public void testPartyLogsFinal() {
|
---|
164 | // this log output is optional, this is to show how to check log
|
---|
165 | Reporter reporter = mock(Reporter.class);
|
---|
166 | party = new TimeDependentParty(reporter) {
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public String getDescription() {
|
---|
170 | return "test";
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | public double getE() {
|
---|
175 | return 2; /// conceder like
|
---|
176 | }
|
---|
177 | };
|
---|
178 | party.connect(connection);
|
---|
179 | party.notifyChange(settings);
|
---|
180 | Agreements agreements = mock(Agreements.class);
|
---|
181 | when(agreements.toString()).thenReturn("agree");
|
---|
182 |
|
---|
183 | party.notifyChange(new Finished(agreements));
|
---|
184 |
|
---|
185 | verify(reporter).log(eq(Level.INFO),
|
---|
186 | eq("Final ourcome:Finished[agree]"));
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Test
|
---|
190 | public void testPartysUpdatesProgress() {
|
---|
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 | @Test
|
---|
204 | public void testUtilityTarget() {
|
---|
205 | TimeDependentParty tdp = new TimeDependentParty();
|
---|
206 | BigDecimal N02 = new BigDecimal("0.2");
|
---|
207 | BigDecimal N043 = new BigDecimal("0.42521212");
|
---|
208 | BigDecimal goal = tdp.getUtilityGoal(0.1, 1.2, N02, N043);
|
---|
209 | assertTrue(goal.compareTo(N02) > 0);
|
---|
210 | assertTrue(goal.compareTo(N043) < 0);
|
---|
211 | }
|
---|
212 |
|
---|
213 | private Bid findBestBid() {
|
---|
214 | BigDecimal bestvalue = BigDecimal.ZERO;
|
---|
215 | Bid best = null;
|
---|
216 | for (Bid bid : new AllBidsList(profile.getDomain())) {
|
---|
217 | BigDecimal util = profile.getUtility(bid);
|
---|
218 | if (util.compareTo(bestvalue) > 0) {
|
---|
219 | best = bid;
|
---|
220 | bestvalue = util;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | return best;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Check that delay indeed waits
|
---|
228 | *
|
---|
229 | * @throws URISyntaxException
|
---|
230 | */
|
---|
231 | @Test
|
---|
232 | public void testPartyAcceptsWithDelay() throws URISyntaxException {
|
---|
233 | party.connect(connection);
|
---|
234 |
|
---|
235 | Settings settingsdelay = new Settings(new PartyId("party1"),
|
---|
236 | new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
|
---|
237 | parameters.with("delay", 2));
|
---|
238 |
|
---|
239 | party.notifyChange(settingsdelay);
|
---|
240 |
|
---|
241 | Bid bid = findBestBid();
|
---|
242 | party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
|
---|
243 | long start = System.currentTimeMillis();
|
---|
244 | party.notifyChange(new YourTurn());
|
---|
245 | long end = System.currentTimeMillis();
|
---|
246 | assertEquals(1, connection.getActions().size());
|
---|
247 | assertTrue(connection.getActions().get(0) instanceof Accept);
|
---|
248 |
|
---|
249 | long dt = end - start;
|
---|
250 | assertTrue(dt >= 1000);
|
---|
251 | assertTrue(dt <= 3000);
|
---|
252 |
|
---|
253 | }
|
---|
254 |
|
---|
255 | }
|
---|