1 | package negotiator.session;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.mockito.Mockito.mock;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.net.MalformedURLException;
|
---|
8 | import java.net.URL;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import org.junit.After;
|
---|
15 | import org.junit.Before;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.junit.runner.RunWith;
|
---|
18 | import org.junit.runners.Parameterized;
|
---|
19 | import org.junit.runners.Parameterized.Parameters;
|
---|
20 |
|
---|
21 | import agents.nastyagent.BadBid;
|
---|
22 | import agents.nastyagent.BadSuperInit;
|
---|
23 | import agents.nastyagent.NastyAgent;
|
---|
24 | import agents.nastyagent.NearlyOutOfMem;
|
---|
25 | import agents.nastyagent.NonsenseActionInChoose;
|
---|
26 | import agents.nastyagent.NullBid;
|
---|
27 | import agents.nastyagent.OnlyBestBid;
|
---|
28 | import agents.nastyagent.OutOfMem;
|
---|
29 | import agents.nastyagent.SleepInChoose;
|
---|
30 | import agents.nastyagent.SleepInNegoEnd;
|
---|
31 | import agents.nastyagent.SleepInReceiveMessage;
|
---|
32 | import agents.nastyagent.StoreNull;
|
---|
33 | import agents.nastyagent.StoreUnserializableThing;
|
---|
34 | import agents.nastyagent.ThrowInChoose;
|
---|
35 | import agents.nastyagent.ThrowInConstructor;
|
---|
36 | import agents.nastyagent.ThrowInNegoEnd;
|
---|
37 | import agents.nastyagent.ThrowInReceiveMessage;
|
---|
38 | import agents.nastyagent.UnknownValue;
|
---|
39 | import genius.core.AgentID;
|
---|
40 | import genius.core.Deadline;
|
---|
41 | import genius.core.DeadlineType;
|
---|
42 | import genius.core.events.BrokenPartyException;
|
---|
43 | import genius.core.events.NegotiationEvent;
|
---|
44 | import genius.core.events.SessionFailedEvent;
|
---|
45 | import genius.core.exceptions.InstantiateException;
|
---|
46 | import genius.core.exceptions.NegotiatorException;
|
---|
47 | import genius.core.listener.Listener;
|
---|
48 | import genius.core.parties.NegotiationParty;
|
---|
49 | import genius.core.parties.NegotiationPartyInternal;
|
---|
50 | import genius.core.parties.SessionsInfo;
|
---|
51 | import genius.core.persistent.PersistentDataType;
|
---|
52 | import genius.core.protocol.MultilateralProtocol;
|
---|
53 | import genius.core.protocol.StackedAlternatingOffersProtocol;
|
---|
54 | import genius.core.repository.DomainRepItem;
|
---|
55 | import genius.core.repository.PartyRepItem;
|
---|
56 | import genius.core.repository.ProfileRepItem;
|
---|
57 | import genius.core.session.ExecutorWithTimeout;
|
---|
58 | import genius.core.session.RepositoryException;
|
---|
59 | import genius.core.session.Session;
|
---|
60 | import genius.core.session.SessionConfiguration;
|
---|
61 | import 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)
|
---|
71 | public 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 | }
|
---|