source: anac2020/HammingAgent/src/test/java/HammingAgentTest.java@ 14

Last change on this file since 14 was 1, checked in by wouter, 4 years ago

#1910 added anac2020 parties

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