source: exampleparties/timedependentparty/src/test/java/geniusweb/exampleparties/timedependentparty/TimeDependentPartyTest.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: 6.9 KB
Line 
1package geniusweb.exampleparties.timedependentparty;
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.ArgumentMatchers.any;
8import static org.mockito.ArgumentMatchers.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.math.BigDecimal;
15import java.net.URI;
16import java.net.URISyntaxException;
17import java.nio.charset.StandardCharsets;
18import java.nio.file.Files;
19import java.nio.file.Paths;
20import java.util.logging.Level;
21
22import org.junit.Before;
23import org.junit.Test;
24
25import com.fasterxml.jackson.core.JsonParseException;
26import com.fasterxml.jackson.databind.JsonMappingException;
27import com.fasterxml.jackson.databind.ObjectMapper;
28
29import geniusweb.actions.Accept;
30import geniusweb.actions.EndNegotiation;
31import geniusweb.actions.Offer;
32import geniusweb.actions.PartyId;
33import geniusweb.bidspace.AllBidsList;
34import geniusweb.inform.ActionDone;
35import geniusweb.inform.Agreements;
36import geniusweb.inform.Finished;
37import geniusweb.inform.Settings;
38import geniusweb.inform.YourTurn;
39import geniusweb.issuevalue.Bid;
40import geniusweb.party.Capabilities;
41import geniusweb.profile.Profile;
42import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
43import geniusweb.progress.ProgressRounds;
44import geniusweb.references.Parameters;
45import geniusweb.references.ProfileRef;
46import geniusweb.references.ProtocolRef;
47import tudelft.utilities.logging.Reporter;
48
49public 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}
Note: See TracBrowser for help on using the repository browser.