1 | import static org.junit.Assert.assertEquals;
|
---|
2 | import static org.junit.Assert.assertFalse;
|
---|
3 | import static org.junit.Assert.assertNotNull;
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 | import static org.mockito.Matchers.eq;
|
---|
6 | import static org.mockito.Mockito.mock;
|
---|
7 | import static org.mockito.Mockito.verify;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.net.URI;
|
---|
11 | import java.net.URISyntaxException;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.nio.file.Files;
|
---|
14 | import java.nio.file.Paths;
|
---|
15 | import java.util.HashMap;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.logging.Level;
|
---|
20 |
|
---|
21 | import org.junit.Before;
|
---|
22 | import org.junit.Ignore;
|
---|
23 | import org.junit.Test;
|
---|
24 |
|
---|
25 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
26 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
27 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
28 |
|
---|
29 | import geniusweb.actions.Accept;
|
---|
30 | import geniusweb.actions.Action;
|
---|
31 | import geniusweb.actions.ElicitComparison;
|
---|
32 | import geniusweb.actions.EndNegotiation;
|
---|
33 | import geniusweb.actions.Offer;
|
---|
34 | import geniusweb.actions.PartyId;
|
---|
35 | import geniusweb.connection.ConnectionEnd;
|
---|
36 | import geniusweb.inform.ActionDone;
|
---|
37 | import geniusweb.inform.Agreements;
|
---|
38 | import geniusweb.inform.Finished;
|
---|
39 | import geniusweb.inform.Inform;
|
---|
40 | import geniusweb.inform.Settings;
|
---|
41 | import geniusweb.inform.YourTurn;
|
---|
42 | import geniusweb.issuevalue.Bid;
|
---|
43 | import geniusweb.issuevalue.DiscreteValue;
|
---|
44 | import geniusweb.issuevalue.Value;
|
---|
45 | import geniusweb.party.Capabilities;
|
---|
46 | import geniusweb.profile.Profile;
|
---|
47 | import geniusweb.progress.ProgressRounds;
|
---|
48 | import geniusweb.references.Parameters;
|
---|
49 | import geniusweb.references.ProfileRef;
|
---|
50 | import geniusweb.references.ProtocolRef;
|
---|
51 | import geniusweb.references.Reference;
|
---|
52 | import tudelft.utilities.listener.DefaultListenable;
|
---|
53 | import tudelft.utilities.logging.Reporter;
|
---|
54 |
|
---|
55 | public 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 | */
|
---|
215 | class 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 | }
|
---|