source: boa/src/test/java/geniusweb/boa/BoaPartyTest.java@ 44

Last change on this file since 44 was 44, checked in by bart, 2 years ago

Added time-dependent parties for python and simpleRunner-GUI for java

File size: 6.8 KB
Line 
1package geniusweb.boa;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertTrue;
5import static org.mockito.ArgumentMatchers.any;
6import static org.mockito.ArgumentMatchers.eq;
7import static org.mockito.Mockito.mock;
8import static org.mockito.Mockito.spy;
9import static org.mockito.Mockito.times;
10import static org.mockito.Mockito.verify;
11
12import java.io.IOException;
13import java.net.URI;
14import java.net.URISyntaxException;
15import java.util.LinkedList;
16import java.util.List;
17import java.util.logging.Level;
18
19import org.junit.Test;
20
21import geniusweb.actions.Accept;
22import geniusweb.actions.Action;
23import geniusweb.actions.EndNegotiation;
24import geniusweb.actions.Offer;
25import geniusweb.actions.PartyId;
26import geniusweb.boa.acceptancestrategy.AcceptanceStrategy;
27import geniusweb.boa.biddingstrategy.BiddingStrategy;
28import geniusweb.connection.ConnectionEnd;
29import geniusweb.inform.ActionDone;
30import geniusweb.inform.Inform;
31import geniusweb.inform.Settings;
32import geniusweb.inform.YourTurn;
33import geniusweb.issuevalue.Bid;
34import geniusweb.issuevalue.DiscreteValue;
35import geniusweb.issuevalue.Domain;
36import geniusweb.opponentmodel.OpponentModel;
37import geniusweb.progress.Progress;
38import geniusweb.references.Parameters;
39import geniusweb.references.ProfileRef;
40import geniusweb.references.ProtocolRef;
41import geniusweb.references.Reference;
42import tudelft.utilities.listener.DefaultListenable;
43import tudelft.utilities.logging.ReportToLogger;
44import tudelft.utilities.logging.Reporter;
45
46public 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
153class asMock implements AcceptanceStrategy {
154
155 @Override
156 public Action filter(Action action, BoaState boaState) {
157 return action;
158 }
159
160}
161
162class bsMock implements BiddingStrategy {
163
164 @Override
165 public Action getAction(BoaState state) {
166 return BoaPartyTest.biddingstratOffer;
167 }
168
169}
170
171class 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 */
211class 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}
Note: See TracBrowser for help on using the repository browser.