source: exampleparties/simpleboa/src/test/java/geniusweb/exampleparties/simpleboa/SimpleBoaTest.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 6.3 KB
Line 
1package geniusweb.exampleparties.simpleboa;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.assertTrue;
7import static org.mockito.Matchers.any;
8import static org.mockito.Matchers.eq;
9import static org.mockito.Mockito.mock;
10import static org.mockito.Mockito.verify;
11import static org.mockito.Mockito.when;
12
13import java.io.IOException;
14import java.math.BigDecimal;
15import java.net.URI;
16import java.net.URISyntaxException;
17import java.nio.charset.StandardCharsets;
18import java.nio.file.Files;
19import java.nio.file.Paths;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.logging.Level;
23
24import org.junit.Before;
25import org.junit.Test;
26
27import com.fasterxml.jackson.core.JsonParseException;
28import com.fasterxml.jackson.databind.JsonMappingException;
29import com.fasterxml.jackson.databind.ObjectMapper;
30
31import geniusweb.actions.Accept;
32import geniusweb.actions.Action;
33import geniusweb.actions.EndNegotiation;
34import geniusweb.actions.Offer;
35import geniusweb.actions.PartyId;
36import geniusweb.bidspace.AllBidsList;
37import geniusweb.connection.ConnectionEnd;
38import geniusweb.inform.ActionDone;
39import geniusweb.inform.Agreements;
40import geniusweb.inform.Finished;
41import geniusweb.inform.Inform;
42import geniusweb.inform.Settings;
43import geniusweb.inform.YourTurn;
44import geniusweb.issuevalue.Bid;
45import geniusweb.party.Capabilities;
46import geniusweb.profile.Profile;
47import geniusweb.profile.utilityspace.LinearAdditive;
48import geniusweb.progress.ProgressRounds;
49import geniusweb.references.Parameters;
50import geniusweb.references.ProfileRef;
51import geniusweb.references.ProtocolRef;
52import geniusweb.references.Reference;
53import tudelft.utilities.listener.DefaultListenable;
54import tudelft.utilities.logging.Reporter;
55
56public 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 */
200class 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}
Note: See TracBrowser for help on using the repository browser.