source: exampleparties/simpleshaop/src/test/java/geniusweb/exampleparties/simpleshaop/ShaopPartyTest.java@ 21

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

Version 1.5.

File size: 6.7 KB
Line 
1package geniusweb.exampleparties.simpleshaop;
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.Matchers.eq;
8import static org.mockito.Mockito.mock;
9import static org.mockito.Mockito.verify;
10
11import java.io.IOException;
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.HashMap;
18import java.util.LinkedList;
19import java.util.List;
20import java.util.Map;
21import java.util.logging.Level;
22
23import org.junit.Before;
24import org.junit.Ignore;
25import org.junit.Test;
26
27import com.fasterxml.jackson.core.JsonParseException;
28import com.fasterxml.jackson.databind.JsonMappingException;
29import com.fasterxml.jackson.databind.ObjectMapper;
30
31import geniusweb.actions.Accept;
32import geniusweb.actions.Action;
33import geniusweb.actions.ElicitComparison;
34import geniusweb.actions.EndNegotiation;
35import geniusweb.actions.Offer;
36import geniusweb.actions.PartyId;
37import geniusweb.connection.ConnectionEnd;
38import geniusweb.inform.ActionDone;
39import geniusweb.inform.Finished;
40import geniusweb.inform.Inform;
41import geniusweb.inform.Settings;
42import geniusweb.inform.YourTurn;
43import geniusweb.issuevalue.Bid;
44import geniusweb.issuevalue.DiscreteValue;
45import geniusweb.issuevalue.Value;
46import geniusweb.party.Capabilities;
47import geniusweb.profile.Profile;
48import geniusweb.progress.ProgressRounds;
49import geniusweb.references.Parameters;
50import geniusweb.references.ProfileRef;
51import geniusweb.references.ProtocolRef;
52import geniusweb.references.Reference;
53import tudelft.utilities.listener.DefaultListenable;
54import tudelft.utilities.logging.Reporter;
55
56public class ShaopPartyTest {
57
58 private static final String SHAOP = "SHAOP";
59 private static final PartyId otherparty = new PartyId("other");
60 private static final String PROFILE = "src/test/resources/testprofile.json";
61 private final static ObjectMapper jackson = new ObjectMapper();
62
63 private ShaopParty party;
64 private final TestConnection connection = new TestConnection();
65 private final ProtocolRef protocol = mock(ProtocolRef.class);
66 private final ProgressRounds progress = mock(ProgressRounds.class);
67 private Settings settings;
68 private Profile profile;
69 private final Parameters parameters = new Parameters();
70
71 @Before
72 public void before() throws JsonParseException, JsonMappingException,
73 IOException, URISyntaxException {
74 party = new ShaopParty();
75 settings = new Settings(new PartyId("party1"),
76 new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
77 parameters);
78
79 String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
80 StandardCharsets.UTF_8);
81 profile = jackson.readValue(serialized, Profile.class);
82
83 }
84
85 @Test
86 public void smokeTest() {
87 }
88
89 @Test
90 public void getDescriptionTest() {
91 assertNotNull(party.getDescription());
92 }
93
94 @Test
95 public void getCapabilitiesTest() {
96 Capabilities capabilities = party.getCapabilities();
97 assertFalse("party does not define protocols",
98 capabilities.getBehaviours().isEmpty());
99 }
100
101 @Test
102 public void testInformConnection() {
103 party.connect(connection);
104 // agent should not start acting just after an inform
105 assertEquals(0, connection.getActions().size());
106 }
107
108 @Test
109 public void testInformSettings() {
110 party.connect(connection);
111 connection.notifyListeners(settings);
112 assertEquals(0, connection.getActions().size());
113 }
114
115 @Test
116 public void testInformAndConnection() {
117 party.connect(connection);
118 party.notifyChange(settings);
119 assertEquals(0, connection.getActions().size());
120 }
121
122 @Test
123 public void testOtherWalksAway() {
124 party.connect(connection);
125 party.notifyChange(settings);
126
127 party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
128
129 // party should not act at this point
130 assertEquals(0, connection.getActions().size());
131 }
132
133 @Test
134 public void testAgentHasFirstTurn() {
135 party.connect(connection);
136 party.notifyChange(settings);
137
138 party.notifyChange(new YourTurn());
139 assertEquals(1, connection.getActions().size());
140 assertTrue(connection.getActions().get(0) instanceof Offer);
141 }
142
143 @Test
144 public void testAgentElicitsComparison() {
145 party.connect(connection);
146 party.notifyChange(settings);
147
148 Bid bid = makeBid("3", "3"); // not yet in profile
149 party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
150 party.notifyChange(new YourTurn());
151 assertEquals(1, connection.getActions().size());
152 assertTrue(connection.getActions().get(0) instanceof ElicitComparison);
153
154 }
155
156 @Ignore
157 @Test
158 public void testAgentAccepts() {
159 // to make agent accept, the offered bid must score >0.9
160 // we would need to mock a lot t make that happen
161 // as we need to place >10 comparable bids for that.
162 party.connect(connection);
163 party.notifyChange(settings);
164
165 Bid bid = makeBid("1", "1");// best pssible
166 party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
167 party.notifyChange(new YourTurn());
168 assertEquals(1, connection.getActions().size());
169 assertTrue(connection.getActions().get(0) instanceof Accept);
170
171 }
172
173 @Test
174 public void testAgentLogsFinal() {
175 // this log output is optional, this is to show how to check log
176 Reporter reporter = mock(Reporter.class);
177 party = new ShaopParty(reporter);
178 party.connect(connection);
179 party.notifyChange(settings);
180 party.notifyChange(new Finished(null));
181
182 verify(reporter).log(eq(Level.INFO),
183 eq("Final ourcome:Finished[null]"));
184 }
185
186 @Test
187 public void testAgentsUpdatesProgress() {
188 party.connect(connection);
189 party.notifyChange(settings);
190
191 party.notifyChange(new ActionDone(new Offer(null, mock(Bid.class))));
192
193 party.notifyChange(new YourTurn());
194 verify(progress).advance();
195 }
196
197 @Test
198 public void testGetCapabilities() {
199 assertTrue(party.getCapabilities().getBehaviours().contains(SHAOP));
200 }
201
202 private Bid makeBid(String val1, String val2) {
203 Map<String, Value> map = new HashMap<>();
204 map.put("iss1", new DiscreteValue(val1));
205 map.put("iss2", new DiscreteValue(val2));
206 return new Bid(map);
207
208 }
209
210}
211
212/**
213 * A "real" connection object, because the party is going to subscribe etc, and
214 * without a real connection we would have to do a lot of mocks that would make
215 * the test very hard to read.
216 *
217 */
218class TestConnection extends DefaultListenable<Inform>
219 implements ConnectionEnd<Inform, Action> {
220 private List<Action> actions = new LinkedList<>();
221
222 @Override
223 public void send(Action action) throws IOException {
224 actions.add(action);
225 }
226
227 @Override
228 public Reference getReference() {
229 return null;
230 }
231
232 @Override
233 public URI getRemoteURI() {
234 return null;
235 }
236
237 @Override
238 public void close() {
239
240 }
241
242 @Override
243 public Error getError() {
244 return null;
245 }
246
247 public List<Action> getActions() {
248 return actions;
249 }
250
251}
Note: See TracBrowser for help on using the repository browser.