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

Last change on this file since 43 was 39, checked in by wouter, 3 years ago

#3

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