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