source: anac2020/ShineAgent/src/test/java/shineagent/ShinePartyTest.java@ 1

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

#1910 added anac2020 parties

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