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