source: anac2020/agentKT/src/test/java/geniusweb/exampleparties/simpleshaop/ShaopPartyTest.java

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

#3 agentkt updated

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