1 | package geniusweb.exampleparties.simpleboa;
|
---|
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.any;
|
---|
8 | import static org.mockito.Matchers.eq;
|
---|
9 | import static org.mockito.Mockito.mock;
|
---|
10 | import static org.mockito.Mockito.verify;
|
---|
11 | import static org.mockito.Mockito.when;
|
---|
12 |
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.math.BigDecimal;
|
---|
15 | import java.net.URI;
|
---|
16 | import java.net.URISyntaxException;
|
---|
17 | import java.nio.charset.StandardCharsets;
|
---|
18 | import java.nio.file.Files;
|
---|
19 | import java.nio.file.Paths;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.logging.Level;
|
---|
23 |
|
---|
24 | import org.junit.Before;
|
---|
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.bidspace.AllBidsList;
|
---|
37 | import geniusweb.connection.ConnectionEnd;
|
---|
38 | import geniusweb.inform.ActionDone;
|
---|
39 | import geniusweb.inform.Agreements;
|
---|
40 | import geniusweb.inform.Finished;
|
---|
41 | import geniusweb.inform.Inform;
|
---|
42 | import geniusweb.inform.Settings;
|
---|
43 | import geniusweb.inform.YourTurn;
|
---|
44 | import geniusweb.issuevalue.Bid;
|
---|
45 | import geniusweb.party.Capabilities;
|
---|
46 | import geniusweb.profile.Profile;
|
---|
47 | import geniusweb.profile.utilityspace.LinearAdditive;
|
---|
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 SimpleBoaTest {
|
---|
57 |
|
---|
58 | private static final String SAOP = "SAOP";
|
---|
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 SimpleBoa 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 LinearAdditive profile;
|
---|
69 | private final Parameters parameters = new Parameters();
|
---|
70 |
|
---|
71 | @Before
|
---|
72 | public void before() throws JsonParseException, JsonMappingException,
|
---|
73 | IOException, URISyntaxException {
|
---|
74 | party = new SimpleBoa();
|
---|
75 | settings = new Settings(new PartyId("party1"),
|
---|
76 | new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
|
---|
77 | parameters);
|
---|
78 |
|
---|
79 | String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
|
---|
80 | StandardCharsets.UTF_8);
|
---|
81 | profile = (LinearAdditive) jackson.readValue(serialized, Profile.class);
|
---|
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 testAgentHasFirstTurn() {
|
---|
135 | party.connect(connection);
|
---|
136 | party.notifyChange(settings);
|
---|
137 |
|
---|
138 | party.notifyChange(new YourTurn());
|
---|
139 | assertEquals(1, connection.getActions().size());
|
---|
140 | assertTrue(connection.getActions().get(0) instanceof Offer);
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Test
|
---|
144 | public void testAgentAccepts() {
|
---|
145 | // at very start, only perfect bid is accepted.
|
---|
146 | // so set time at 10% where good bids are also accepted.
|
---|
147 | when(progress.get(any())).thenReturn(0.1);
|
---|
148 |
|
---|
149 | party.connect(connection);
|
---|
150 | party.notifyChange(settings);
|
---|
151 |
|
---|
152 | Bid bid = findGoodBid();
|
---|
153 | party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
|
---|
154 | party.notifyChange(new YourTurn());
|
---|
155 | assertEquals(1, connection.getActions().size());
|
---|
156 | assertTrue(connection.getActions().get(0) instanceof Accept);
|
---|
157 |
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Test
|
---|
161 | public void testAgentLogsFinal() {
|
---|
162 | // this log output is optional, this is to show how to check log
|
---|
163 | Reporter reporter = mock(Reporter.class);
|
---|
164 | party = new SimpleBoa(reporter);
|
---|
165 | party.connect(connection);
|
---|
166 | party.notifyChange(settings);
|
---|
167 |
|
---|
168 | Agreements agreements = mock(Agreements.class);
|
---|
169 | when(agreements.toString()).thenReturn("agree");
|
---|
170 | party.notifyChange(new Finished(agreements));
|
---|
171 |
|
---|
172 | verify(reporter).log(eq(Level.INFO),
|
---|
173 | eq("Final ourcome:Finished[agree]"));
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Test
|
---|
177 | public void testGetCapabilities() {
|
---|
178 | assertTrue(party.getCapabilities().getBehaviours().contains(SAOP));
|
---|
179 | }
|
---|
180 |
|
---|
181 | private Bid findGoodBid() {
|
---|
182 | for (Bid bid : new AllBidsList(profile.getDomain())) {
|
---|
183 | if (profile.getUtility(bid)
|
---|
184 | .compareTo(BigDecimal.valueOf(0.7)) > 0) {
|
---|
185 | return bid;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | throw new IllegalStateException(
|
---|
189 | "Test can not be done: there is no good bid with utility>0.7");
|
---|
190 | }
|
---|
191 |
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * A "real" connection object, because the party is going to subscribe etc, and
|
---|
196 | * without a real connection we would have to do a lot of mocks that would make
|
---|
197 | * the test very hard to read.
|
---|
198 | *
|
---|
199 | */
|
---|
200 | class TestConnection extends DefaultListenable<Inform>
|
---|
201 | implements ConnectionEnd<Inform, Action> {
|
---|
202 | private List<Action> actions = new LinkedList<>();
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | public void send(Action action) throws IOException {
|
---|
206 | actions.add(action);
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public Reference getReference() {
|
---|
211 | return null;
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public URI getRemoteURI() {
|
---|
216 | return null;
|
---|
217 | }
|
---|
218 |
|
---|
219 | @Override
|
---|
220 | public void close() {
|
---|
221 |
|
---|
222 | }
|
---|
223 |
|
---|
224 | @Override
|
---|
225 | public Error getError() {
|
---|
226 | return null;
|
---|
227 | }
|
---|
228 |
|
---|
229 | public List<Action> getActions() {
|
---|
230 | return actions;
|
---|
231 | }
|
---|
232 |
|
---|
233 | }
|
---|