source: exampleparties/randompartypy/src/test/java/geniusweb/exampleparties/randompartypy/RandomPartyTest.java@ 21

Last change on this file since 21 was 21, checked in by bart, 4 years ago

Version 1.5.

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