1 | package geniusweb.exampleparties.randompartypy;
|
---|
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.Mockito.mock;
|
---|
8 | import static org.mockito.Mockito.verify;
|
---|
9 |
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.math.BigDecimal;
|
---|
12 | import java.net.URI;
|
---|
13 | import java.net.URISyntaxException;
|
---|
14 | import java.nio.charset.StandardCharsets;
|
---|
15 | import java.nio.file.Files;
|
---|
16 | import java.nio.file.Paths;
|
---|
17 | import java.util.LinkedList;
|
---|
18 | import java.util.List;
|
---|
19 |
|
---|
20 | import org.junit.Before;
|
---|
21 | import org.junit.Test;
|
---|
22 |
|
---|
23 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
24 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
25 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
26 |
|
---|
27 | import geniusweb.actions.Accept;
|
---|
28 | import geniusweb.actions.Action;
|
---|
29 | import geniusweb.actions.EndNegotiation;
|
---|
30 | import geniusweb.actions.Offer;
|
---|
31 | import geniusweb.actions.PartyId;
|
---|
32 | import geniusweb.bidspace.AllBidsList;
|
---|
33 | import geniusweb.connection.ConnectionEnd;
|
---|
34 | import geniusweb.inform.ActionDone;
|
---|
35 | import geniusweb.inform.Agreements;
|
---|
36 | import geniusweb.inform.Finished;
|
---|
37 | import geniusweb.inform.Inform;
|
---|
38 | import geniusweb.inform.Settings;
|
---|
39 | import geniusweb.inform.YourTurn;
|
---|
40 | import geniusweb.issuevalue.Bid;
|
---|
41 | import geniusweb.party.Capabilities;
|
---|
42 | import geniusweb.profile.Profile;
|
---|
43 | import geniusweb.profile.utilityspace.LinearAdditive;
|
---|
44 | import geniusweb.progress.ProgressRounds;
|
---|
45 | import geniusweb.pythonadapter.PythonPartyAdapter;
|
---|
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 |
|
---|
52 | public 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 | */
|
---|
203 | class 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 | }
|
---|