source: exampleparties/randompartypy/src/test/java/geniusweb/exampleparties/randompartypy/RandomPartyTest.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.1 KB
Line 
1package geniusweb.exampleparties.randompartypy;
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.Mockito.mock;
8import static org.mockito.Mockito.verify;
9
10import java.io.IOException;
11import java.math.BigDecimal;
12import java.net.URI;
13import java.net.URISyntaxException;
14import java.nio.charset.StandardCharsets;
15import java.nio.file.Files;
16import java.nio.file.Paths;
17import java.util.LinkedList;
18import java.util.List;
19
20import org.junit.Before;
21import org.junit.Test;
22
23import com.fasterxml.jackson.core.JsonParseException;
24import com.fasterxml.jackson.databind.JsonMappingException;
25import com.fasterxml.jackson.databind.ObjectMapper;
26
27import geniusweb.actions.Accept;
28import geniusweb.actions.Action;
29import geniusweb.actions.EndNegotiation;
30import geniusweb.actions.Offer;
31import geniusweb.actions.PartyId;
32import geniusweb.bidspace.AllBidsList;
33import geniusweb.connection.ConnectionEnd;
34import geniusweb.inform.ActionDone;
35import geniusweb.inform.Agreements;
36import geniusweb.inform.Finished;
37import geniusweb.inform.Inform;
38import geniusweb.inform.Settings;
39import geniusweb.inform.YourTurn;
40import geniusweb.issuevalue.Bid;
41import geniusweb.party.Capabilities;
42import geniusweb.profile.Profile;
43import geniusweb.profile.utilityspace.LinearAdditive;
44import geniusweb.progress.ProgressRounds;
45import geniusweb.pythonadapter.PythonPartyAdapter;
46import geniusweb.references.Parameters;
47import geniusweb.references.ProfileRef;
48import geniusweb.references.ProtocolRef;
49import geniusweb.references.Reference;
50import tudelft.utilities.listener.DefaultListenable;
51
52public class RandomPartyTest {
53
54 private static final String PROFILE = "src/test/resources/testprofile.json";
55 private static final String SAOP = "SAOP";
56 private static final PartyId otherparty = new PartyId("other");
57 private final static ObjectMapper jackson = new ObjectMapper();
58
59 private PythonPartyAdapter party;
60 private TestConnection connection = new TestConnection();
61 private final ProtocolRef protocol = mock(ProtocolRef.class);
62 private final ProgressRounds progress = mock(ProgressRounds.class);
63 private Settings settings;
64 private final Parameters parameters = new Parameters();
65
66 private LinearAdditive profile; // the real profile
67
68 @Before
69 public void before() throws JsonParseException, JsonMappingException,
70 IOException, URISyntaxException {
71 party = new PythonPartyAdapter() {
72
73 @Override
74 public String getPythonClass() {
75 return "RandomParty";
76 }
77
78 };
79 settings = new Settings(new PartyId("party1"),
80 new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
81 parameters);
82 String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
83 StandardCharsets.UTF_8);
84 profile = (LinearAdditive) jackson.readValue(serialized, Profile.class);
85
86 }
87
88 @Test
89 public void smokeTest() {
90 }
91
92 @Test
93 public void getDescriptionTest() {
94 assertNotNull(party.getDescription());
95 }
96
97 @Test
98 public void getCapabilitiesTest() {
99 Capabilities capabilities = party.getCapabilities();
100 assertFalse("party does not define protocols",
101 capabilities.getBehaviours().isEmpty());
102 }
103
104 @Test
105 public void testInformConnection() {
106 party.connect(connection);
107 // agent should not start acting just after an inform
108 assertEquals(0, connection.getActions().size());
109 }
110
111 @Test
112 public void testInformSettings() {
113 party.connect(connection);
114 connection.notifyListeners(settings);
115 assertEquals(0, connection.getActions().size());
116 }
117
118 @Test
119 public void testInformAndConnection() {
120 party.connect(connection);
121 party.notifyChange(settings);
122 assertEquals(0, connection.getActions().size());
123 }
124
125 @Test
126 public void testOtherWalksAway() {
127 party.connect(connection);
128 party.notifyChange(settings);
129
130 party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
131
132 // party should not act at this point
133 assertEquals(0, connection.getActions().size());
134 }
135
136 @Test
137 public void testFinished() {
138 party.connect(connection);
139 party.notifyChange(settings);
140
141 party.notifyChange(new Finished(new Agreements()));
142
143 // party should not act at this point
144 assertEquals(0, connection.getActions().size());
145 }
146
147 @Test
148 public void testAgentHasFirstTurn() {
149 party.connect(connection);
150 party.notifyChange(settings);
151
152 party.notifyChange(new YourTurn());
153 assertEquals(1, connection.getActions().size());
154 assertTrue(connection.getActions().get(0) instanceof Offer);
155 }
156
157 @Test
158 public void testAgentAccepts() {
159 party.connect(connection);
160 party.notifyChange(settings);
161
162 Bid bid = findGoodBid();
163 party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
164 party.notifyChange(new YourTurn());
165 assertEquals(1, connection.getActions().size());
166 assertTrue(connection.getActions().get(0) instanceof Accept);
167
168 }
169
170 @Test
171 public void testAgentsUpdatesProgress() {
172 party.connect(connection);
173 party.notifyChange(settings);
174
175 party.notifyChange(new YourTurn());
176 verify(progress).advance();
177 }
178
179 @Test
180 public void testGetCapabilities() {
181 assertTrue(party.getCapabilities().getBehaviours().contains(SAOP));
182 }
183
184 private Bid findGoodBid() {
185 for (Bid bid : new AllBidsList(profile.getDomain())) {
186 if (profile.getUtility(bid)
187 .compareTo(BigDecimal.valueOf(0.7)) > 0) {
188 return bid;
189 }
190 }
191 throw new IllegalStateException(
192 "Test can not be done: there is no good bid with utility>0.7");
193 }
194
195}
196
197/**
198 * A "real" connection object, because the party is going to subscribe etc, and
199 * without a real connection we would have to do a lot of mocks that would make
200 * the test very hard to read.
201 *
202 */
203class TestConnection extends DefaultListenable<Inform>
204 implements ConnectionEnd<Inform, Action> {
205 private List<Action> actions = new LinkedList<>();
206
207 @Override
208 public void send(Action action) throws IOException {
209 actions.add(action);
210 }
211
212 @Override
213 public Reference getReference() {
214 return null;
215 }
216
217 @Override
218 public URI getRemoteURI() {
219 return null;
220 }
221
222 @Override
223 public void close() {
224
225 }
226
227 @Override
228 public Error getError() {
229 return null;
230 }
231
232 public List<Action> getActions() {
233 return actions;
234 }
235
236}
Note: See TracBrowser for help on using the repository browser.