source: src/test/java/negotiator/session/SessionManagerTest.java

Last change on this file was 78, checked in by Wouter Pasman, 6 years ago

#30 use UncertainAdditiveUtilitySpace.

File size: 9.6 KB
Line 
1package negotiator.session;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Mockito.mock;
5
6import java.io.IOException;
7import java.net.MalformedURLException;
8import java.net.URL;
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.List;
13
14import org.junit.After;
15import org.junit.Before;
16import org.junit.Test;
17import org.junit.runner.RunWith;
18import org.junit.runners.Parameterized;
19import org.junit.runners.Parameterized.Parameters;
20
21import agents.nastyagent.BadBid;
22import agents.nastyagent.BadSuperInit;
23import agents.nastyagent.NastyAgent;
24import agents.nastyagent.NearlyOutOfMem;
25import agents.nastyagent.NonsenseActionInChoose;
26import agents.nastyagent.NullBid;
27import agents.nastyagent.OnlyBestBid;
28import agents.nastyagent.OutOfMem;
29import agents.nastyagent.SleepInChoose;
30import agents.nastyagent.SleepInNegoEnd;
31import agents.nastyagent.SleepInReceiveMessage;
32import agents.nastyagent.StoreNull;
33import agents.nastyagent.StoreUnserializableThing;
34import agents.nastyagent.ThrowInChoose;
35import agents.nastyagent.ThrowInConstructor;
36import agents.nastyagent.ThrowInNegoEnd;
37import agents.nastyagent.ThrowInReceiveMessage;
38import agents.nastyagent.UnknownValue;
39import genius.core.AgentID;
40import genius.core.Deadline;
41import genius.core.DeadlineType;
42import genius.core.events.BrokenPartyException;
43import genius.core.events.NegotiationEvent;
44import genius.core.events.SessionFailedEvent;
45import genius.core.exceptions.InstantiateException;
46import genius.core.exceptions.NegotiatorException;
47import genius.core.listener.Listener;
48import genius.core.parties.NegotiationParty;
49import genius.core.parties.NegotiationPartyInternal;
50import genius.core.parties.SessionsInfo;
51import genius.core.persistent.PersistentDataType;
52import genius.core.protocol.MultilateralProtocol;
53import genius.core.protocol.StackedAlternatingOffersProtocol;
54import genius.core.repository.DomainRepItem;
55import genius.core.repository.PartyRepItem;
56import genius.core.repository.ProfileRepItem;
57import genius.core.session.ExecutorWithTimeout;
58import genius.core.session.RepositoryException;
59import genius.core.session.Session;
60import genius.core.session.SessionConfiguration;
61import genius.core.session.SessionManager;
62
63/**
64 * Semi-end-to-end test. We use real {@link NegotiationParty} to test the
65 * {@link SessionManager}.
66 *
67 * @author W.Pasman
68 *
69 */
70@RunWith(Parameterized.class)
71public class SessionManagerTest {
72 private static final Class<? extends NegotiationParty> OPPONENT = OnlyBestBid.class;
73
74 @Parameters
75 public static Collection<Object[]> data() {
76 return Arrays.asList(new Object[][] { { OPPONENT, null },
77 { BadBid.class, BrokenPartyException.class },
78 { BadSuperInit.class, InstantiateException.class },
79 { NearlyOutOfMem.class, BrokenPartyException.class },
80 { NonsenseActionInChoose.class, BrokenPartyException.class },
81 { NullBid.class, BrokenPartyException.class },
82 { OutOfMem.class, BrokenPartyException.class },
83 { SleepInChoose.class, BrokenPartyException.class },
84 // This one seems to hang the system!
85 // { SleepInConstructor.class,
86 // NullPointerException.class },
87 // { SleepInInit.class, NullPointerException.class },
88 { SleepInReceiveMessage.class, BrokenPartyException.class },
89 { ThrowInChoose.class, BrokenPartyException.class },
90 { ThrowInConstructor.class, InstantiateException.class },
91 { ThrowInReceiveMessage.class, BrokenPartyException.class },
92 { UnknownValue.class, null },
93
94 { SleepInNegoEnd.class, null }, { ThrowInNegoEnd.class, null },
95 { StoreNull.class, null },
96 { StoreUnserializableThing.class, null },
97
98 });
99 }
100
101 final String RESOURCES = "file:src/test/resources/";
102 final String domain = RESOURCES + "partydomain/party_domain.xml";
103 final String profile = RESOURCES + "partydomain/party1_utility.xml";
104 private DomainRepItem domainRepItem;
105 private Session session;
106 private MultilateralProtocol protocol = new StackedAlternatingOffersProtocol();
107 private ExecutorWithTimeout executor = new ExecutorWithTimeout(3000);
108 private ProfileRepItem profileRepItem;
109
110 private Class<? extends NegotiationParty> partyClass;
111 private Class<? extends Exception> expectedExceptionClass;
112 private Exception actualException;
113 private SessionsInfo info;
114
115 @Before
116 public void before() throws IOException {
117 this.info = new SessionsInfo(null, PersistentDataType.DISABLED, true);
118 }
119
120 @After
121 public void after() {
122 info.close();
123 }
124
125 /**
126 * Test if running the given partyClass against {@link OnlyBestBid} gives us
127 * the correct behaviour.
128 *
129 * @param partyClass
130 * a class extending {@link NegotiationParty} that is to be used
131 * as first agent in a nego.
132 * @param eClass
133 * the class of the expected {@link Exception}
134 * @param n
135 * @throws IOException
136 */
137 public SessionManagerTest(Class<? extends NegotiationParty> partyClass,
138 Class<? extends Exception> eClass) throws IOException {
139 this.partyClass = partyClass;
140 this.expectedExceptionClass = eClass;
141 System.out.println("Running " + partyClass);
142 }
143
144 @Test
145 public void testRun() throws IOException {
146 session = new Session(new Deadline(180, DeadlineType.ROUND),
147 new SessionsInfo(protocol, PersistentDataType.DISABLED, true));
148 domainRepItem = new DomainRepItem(new URL(domain));
149 profileRepItem = new ProfileRepItem(new URL(profile), domainRepItem);
150
151 try {
152 List<NegotiationPartyInternal> theparties = generateParties(
153 partyClass);
154 SessionManager sessionMgr = new SessionManager(
155 mock(SessionConfiguration.class), theparties, session,
156 executor);
157 sessionMgr.addListener(new Listener<NegotiationEvent>() {
158
159 @Override
160 public void notifyChange(NegotiationEvent evt) {
161 if (evt instanceof SessionFailedEvent) {
162 actualException = ((SessionFailedEvent) evt)
163 .getException();
164 }
165 }
166 });
167 sessionMgr.runAndWait();
168
169 } catch (Exception e) {
170 actualException = e;
171 }
172 checkOutcome();
173 }
174
175 /**
176 * Check if always
177 * {@link NegotiationParty#negotiationEnded(genius.core.Bid)} is called
178 * after the nego (if the party was generated ok to start with).
179 *
180 * @throws IOException
181 *
182 * @throws InstantiateException
183 * @throws MalformedURLException
184 *
185 */
186 @Test
187 public void testEndingProperly() throws IOException {
188 List<NegotiationPartyInternal> theparties = new ArrayList<>();
189 try {
190 session = new Session(new Deadline(180, DeadlineType.ROUND),
191 new SessionsInfo(protocol, PersistentDataType.DISABLED,
192 true));
193 domainRepItem = new DomainRepItem(new URL(domain));
194 profileRepItem = new ProfileRepItem(new URL(profile),
195 domainRepItem);
196 theparties = generateParties(partyClass);
197 } catch (MalformedURLException | InstantiateException e) {
198 return; // crashes here are checked in #testRun
199 }
200
201 // if we get here, session starts. It should then always END properly
202 SessionManager sessionMgr = new SessionManager(
203 mock(SessionConfiguration.class), theparties, session,
204 executor);
205 try {
206 sessionMgr.run();
207 } catch (RuntimeException e) {
208 // if it errs is not relevant in this test. What matters is if
209 // negotioationEnded is called.
210 }
211 checkEndedProperly(theparties);
212 }
213
214 /**
215 * Check if all parties received the negotiationEnded call
216 *
217 * @param theparties
218 */
219 private void checkEndedProperly(List<NegotiationPartyInternal> theparties) {
220 for (NegotiationPartyInternal party : theparties) {
221 if (!((NastyAgent) (party.getParty())).isEnded()) {
222 throw new IllegalStateException("Agent " + party
223 + " did not receive final negotiationEnded call");
224 }
225 }
226 }
227
228 private void checkOutcome() {
229 boolean ok;
230 Class<? extends Exception> actualExClass = actualException != null
231 ? actualException.getClass() : null;
232 if (expectedExceptionClass != null) {
233 ok = expectedExceptionClass.equals(actualExClass);
234 } else {
235 ok = null == actualException;
236 }
237 if (!ok) {
238 System.err.println("Failure running " + partyClass + ": expected "
239 + string(expectedExceptionClass) + " but got "
240 + string(actualExClass));
241 if (actualException != null) {
242 actualException.printStackTrace();
243 }
244 assertEquals(expectedExceptionClass, actualExClass);
245 }
246 }
247
248 String string(Class<? extends Exception> eClass) {
249 return eClass == null ? "No exception" : eClass.toString();
250 }
251
252 private List<NegotiationPartyInternal> generateParties(
253 Class<? extends NegotiationParty> partyClass)
254 throws InstantiateException {
255 ArrayList<NegotiationPartyInternal> parties = new ArrayList<NegotiationPartyInternal>();
256 try {
257 parties.add(createParty(partyClass));
258 parties.add(createParty(OPPONENT));
259 } catch (MalformedURLException | IllegalAccessException
260 | ClassNotFoundException | RepositoryException
261 | NegotiatorException | InstantiationException e) {
262 throw new InstantiateException(
263 "Failed to create party " + partyClass, e);
264 }
265 return parties;
266 }
267
268 /**
269 * Create a real party based on the class
270 *
271 * @param partyClass
272 * @return {@link NegotiationPartyInternal}
273 * @throws InstantiateException
274 * if party can't be instantiated
275 */
276 private NegotiationPartyInternal createParty(
277 Class<? extends NegotiationParty> partyClass)
278 throws MalformedURLException, InstantiationException,
279 IllegalAccessException, ClassNotFoundException, RepositoryException,
280 NegotiatorException, InstantiateException {
281 PartyRepItem partyRepItem = new PartyRepItem(
282 partyClass.getCanonicalName());
283
284 return new NegotiationPartyInternal(partyRepItem, profileRepItem,
285 session, info, getAgentID(partyClass));
286 }
287
288 private AgentID getAgentID(Class<? extends NegotiationParty> partyClass) {
289 return new AgentID(partyClass.getName());
290 }
291}
Note: See TracBrowser for help on using the repository browser.