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

Last change on this file was 42, checked in by wouter, 3 years ago

#3

File size: 6.6 KB
Line 
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.EndNegotiation;
34import geniusweb.actions.Offer;
35import geniusweb.actions.PartyId;
36import geniusweb.connection.ConnectionEnd;
37import geniusweb.inform.ActionDone;
38import geniusweb.inform.Agreements;
39import geniusweb.inform.Finished;
40import geniusweb.inform.Inform;
41import geniusweb.inform.Settings;
42import geniusweb.inform.YourTurn;
43import geniusweb.issuevalue.Bid;
44import geniusweb.issuevalue.DiscreteValue;
45import geniusweb.issuevalue.Value;
46import geniusweb.party.Capabilities;
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 tudelft.utilities.listener.DefaultListenable;
54import tudelft.utilities.logging.Reporter;
55
56public 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 */
211class 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}
Note: See TracBrowser for help on using the repository browser.