1 | package geniusweb.boa;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 | import static org.mockito.ArgumentMatchers.any;
|
---|
6 | import static org.mockito.ArgumentMatchers.eq;
|
---|
7 | import static org.mockito.Mockito.mock;
|
---|
8 | import static org.mockito.Mockito.spy;
|
---|
9 | import static org.mockito.Mockito.times;
|
---|
10 | import static org.mockito.Mockito.verify;
|
---|
11 | import static org.mockito.Mockito.when;
|
---|
12 |
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.net.URI;
|
---|
15 | import java.net.URISyntaxException;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.logging.Level;
|
---|
19 |
|
---|
20 | import org.junit.Test;
|
---|
21 |
|
---|
22 | import geniusweb.actions.Accept;
|
---|
23 | import geniusweb.actions.Action;
|
---|
24 | import geniusweb.actions.EndNegotiation;
|
---|
25 | import geniusweb.actions.Offer;
|
---|
26 | import geniusweb.actions.PartyId;
|
---|
27 | import geniusweb.boa.acceptancestrategy.AcceptanceStrategy;
|
---|
28 | import geniusweb.boa.biddingstrategy.BiddingStrategy;
|
---|
29 | import geniusweb.connection.ConnectionEnd;
|
---|
30 | import geniusweb.inform.ActionDone;
|
---|
31 | import geniusweb.inform.Inform;
|
---|
32 | import geniusweb.inform.Settings;
|
---|
33 | import geniusweb.inform.YourTurn;
|
---|
34 | import geniusweb.issuevalue.Bid;
|
---|
35 | import geniusweb.issuevalue.DiscreteValue;
|
---|
36 | import geniusweb.issuevalue.Domain;
|
---|
37 | import geniusweb.opponentmodel.OpponentModel;
|
---|
38 | import geniusweb.progress.Progress;
|
---|
39 | import geniusweb.references.Parameters;
|
---|
40 | import geniusweb.references.ProfileRef;
|
---|
41 | import geniusweb.references.ProtocolRef;
|
---|
42 | import geniusweb.references.Reference;
|
---|
43 | import tudelft.utilities.listener.DefaultListenable;
|
---|
44 | import tudelft.utilities.logging.ReportToLogger;
|
---|
45 | import tudelft.utilities.logging.Reporter;
|
---|
46 |
|
---|
47 | public class BoaPartyTest {
|
---|
48 | private static final Bid otherBid = new Bid("issue",
|
---|
49 | new DiscreteValue("value2"));
|
---|
50 | private static final PartyId OTHER = new PartyId("other");
|
---|
51 | private static final Bid BID1 = new Bid("issue",
|
---|
52 | new DiscreteValue("value"));
|
---|
53 | private static final PartyId ME = new PartyId("me");
|
---|
54 | private TestConnection connection = new TestConnection();
|
---|
55 | private static final String PROFILE = "src/test/resources/testprofile.json";
|
---|
56 |
|
---|
57 | protected static final Offer biddingstratOffer = new Offer(ME, BID1);
|
---|
58 | private static final Offer othersOffer = new Offer(OTHER, otherBid);
|
---|
59 | private static final Accept accept = new Accept(ME, BID1);
|
---|
60 | private static final EndNegotiation endnego = new EndNegotiation(OTHER);
|
---|
61 |
|
---|
62 | @Test(expected = RuntimeException.class)
|
---|
63 | public void smokeTestMissingInit() {
|
---|
64 |
|
---|
65 | Reporter reporter = spy(new ReportToLogger("test"));
|
---|
66 | BoaParty boa = new BoaParty(reporter);
|
---|
67 | boa.connect(connection);
|
---|
68 | boa.notifyChange(mock(ActionDone.class));
|
---|
69 | verify(reporter, times(1)).log(Level.SEVERE, any());
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Test
|
---|
73 | public void settingsTest() throws URISyntaxException {
|
---|
74 | BoaState state = mock(BoaState.class);
|
---|
75 | BoaParty boa = new BoaParty() {
|
---|
76 | @Override
|
---|
77 | protected BoaState initialState() {
|
---|
78 | return state;
|
---|
79 | }
|
---|
80 | };
|
---|
81 | boa.connect(connection);
|
---|
82 | PartyId id = new PartyId("testparty");
|
---|
83 | ProfileRef profile = new ProfileRef(new URI("file:" + PROFILE));
|
---|
84 | ProtocolRef protocol = new ProtocolRef("SAOP");
|
---|
85 | Progress progress = mock(Progress.class);
|
---|
86 | Parameters parameters = new Parameters();
|
---|
87 | parameters = parameters.with("om", omMock.class.getCanonicalName());
|
---|
88 | parameters = parameters.with("bs", bsMock.class.getCanonicalName());
|
---|
89 | parameters = parameters.with("as", asMock.class.getCanonicalName());
|
---|
90 | Settings info = new Settings(id, profile, protocol, progress,
|
---|
91 | parameters);
|
---|
92 | boa.notifyChange(info);
|
---|
93 | // weird bug in Mockito? Work around with a weird cast...
|
---|
94 | verify(state).with(eq(info), eq(omMock.class));
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Test
|
---|
98 | public void youStartTestE2E() throws URISyntaxException {
|
---|
99 | BoaParty boa = getInitializedBoa();
|
---|
100 |
|
---|
101 | boa.notifyChange(new YourTurn());
|
---|
102 | assertEquals(1, connection.getActions().size());
|
---|
103 | System.out.println("party did action:" + connection.getActions());
|
---|
104 | // this should come out of bidding strat as there is no acceptable offer
|
---|
105 | assertEquals(biddingstratOffer, connection.getActions().get(0));
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Test
|
---|
109 | public void otherOfferstTestE2E() throws URISyntaxException {
|
---|
110 | BoaParty boa = getInitializedBoa();
|
---|
111 |
|
---|
112 | boa.notifyChange(new ActionDone(othersOffer));
|
---|
113 |
|
---|
114 | boa.notifyChange(new YourTurn());
|
---|
115 | assertEquals(1, connection.getActions().size());
|
---|
116 | System.out.println("party did action:" + connection.getActions());
|
---|
117 | // The mock offerstrat gives an offer.
|
---|
118 | // the mock acceptstrat just passes on the offer.
|
---|
119 | assertTrue(connection.getActions().get(0) instanceof Offer);
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Test
|
---|
123 | public void getLastBidTest() throws URISyntaxException {
|
---|
124 | BoaParty boa = getInitializedBoa();
|
---|
125 |
|
---|
126 | // just a potpourri of actions.
|
---|
127 | boa.notifyChange(new ActionDone(biddingstratOffer));
|
---|
128 | boa.notifyChange(new ActionDone(endnego));
|
---|
129 | boa.notifyChange(new ActionDone(othersOffer));
|
---|
130 | boa.notifyChange(new ActionDone(accept));
|
---|
131 | assertEquals(otherBid, boa.getState().getLastReceivedOffer().getBid());
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Test(expected = InstantiationFailedException.class)
|
---|
136 | public void loadClassSecurityTest() throws InstantiationFailedException {
|
---|
137 | BoaParty boa = new BoaParty();
|
---|
138 | Settings settings = mock(Settings.class);
|
---|
139 | Parameters parameters = new Parameters().with("om", "bla");
|
---|
140 | when(settings.getParameters()).thenReturn(parameters);
|
---|
141 | boa.getOpponentModel(settings);
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Test(expected = InstantiationFailedException.class)
|
---|
145 | public void loadClassSecurityTest2() throws InstantiationFailedException {
|
---|
146 | BoaParty boa = new BoaParty();
|
---|
147 | Settings settings = mock(Settings.class);
|
---|
148 | Parameters parameters = new Parameters().with("om",
|
---|
149 | "geniusweb.boa.acceptancestrategy.TimeDependentAcceptanceStrategy");
|
---|
150 | when(settings.getParameters()).thenReturn(parameters);
|
---|
151 | boa.getOpponentModel(settings);
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Test(expected = InstantiationFailedException.class)
|
---|
155 | public void loadClassSecurityTestCrashingClass()
|
---|
156 | throws InstantiationFailedException {
|
---|
157 | BoaParty boa = new BoaParty();
|
---|
158 | Settings settings = mock(Settings.class);
|
---|
159 | Parameters parameters = new Parameters().with("om",
|
---|
160 | "geniusweb.boa.CrashingClass");
|
---|
161 | when(settings.getParameters()).thenReturn(parameters);
|
---|
162 | boa.getOpponentModel(settings);
|
---|
163 | }
|
---|
164 |
|
---|
165 | private BoaParty getInitializedBoa() throws URISyntaxException {
|
---|
166 | BoaParty boa = new BoaParty();
|
---|
167 | boa.connect(connection);
|
---|
168 | PartyId id = new PartyId("testparty");
|
---|
169 | ProfileRef profile = new ProfileRef(new URI("file:" + PROFILE));
|
---|
170 | ProtocolRef protocol = new ProtocolRef("SAOP");
|
---|
171 | Progress progress = mock(Progress.class);
|
---|
172 | Parameters parameters = new Parameters();
|
---|
173 | parameters = parameters.with("om", omMock.class.getCanonicalName());
|
---|
174 | parameters = parameters.with("bs", bsMock.class.getCanonicalName());
|
---|
175 | parameters = parameters.with("as", asMock.class.getCanonicalName());
|
---|
176 | Settings info = new Settings(id, profile, protocol, progress,
|
---|
177 | parameters);
|
---|
178 | boa.notifyChange(info);
|
---|
179 | return boa;
|
---|
180 | }
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|
184 | class asMock implements AcceptanceStrategy {
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | public Action filter(Action action, BoaState boaState) {
|
---|
188 | return action;
|
---|
189 | }
|
---|
190 |
|
---|
191 | }
|
---|
192 |
|
---|
193 | class bsMock implements BiddingStrategy {
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | public Action getAction(BoaState state) {
|
---|
197 | return BoaPartyTest.biddingstratOffer;
|
---|
198 | }
|
---|
199 |
|
---|
200 | }
|
---|
201 |
|
---|
202 | class omMock implements OpponentModel {
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | public String getName() {
|
---|
206 | return null;
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public Domain getDomain() {
|
---|
211 | return null;
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public Bid getReservationBid() {
|
---|
216 | return null;
|
---|
217 | }
|
---|
218 |
|
---|
219 | @Override
|
---|
220 | public OpponentModel with(Domain domain, Bid resBid) {
|
---|
221 | return this;
|
---|
222 | }
|
---|
223 |
|
---|
224 | @Override
|
---|
225 | public OpponentModel with(Action action, Progress progress) {
|
---|
226 | return this;
|
---|
227 | }
|
---|
228 |
|
---|
229 | @Override
|
---|
230 | public OpponentModel with(Parameters parameters) {
|
---|
231 | return this; // ignore
|
---|
232 | }
|
---|
233 |
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * A "real" connection object, because the party is going to subscribe etc, and
|
---|
238 | * without a real connection we would have to do a lot of mocks that would make
|
---|
239 | * the test very hard to read.
|
---|
240 | *
|
---|
241 | */
|
---|
242 | class TestConnection extends DefaultListenable<Inform>
|
---|
243 | implements ConnectionEnd<Inform, Action> {
|
---|
244 | private List<Action> actions = new LinkedList<>();
|
---|
245 |
|
---|
246 | @Override
|
---|
247 | public void send(Action action) throws IOException {
|
---|
248 | actions.add(action);
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Override
|
---|
252 | public Reference getReference() {
|
---|
253 | return null;
|
---|
254 | }
|
---|
255 |
|
---|
256 | @Override
|
---|
257 | public URI getRemoteURI() {
|
---|
258 | return null;
|
---|
259 | }
|
---|
260 |
|
---|
261 | @Override
|
---|
262 | public void close() {
|
---|
263 |
|
---|
264 | }
|
---|
265 |
|
---|
266 | @Override
|
---|
267 | public Error getError() {
|
---|
268 | return null;
|
---|
269 | }
|
---|
270 |
|
---|
271 | public List<Action> getActions() {
|
---|
272 | return actions;
|
---|
273 | }
|
---|
274 |
|
---|
275 | }
|
---|