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