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