1 | package negotiator.session;
|
---|
2 |
|
---|
3 | import static org.mockito.Mockito.mock;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.net.MalformedURLException;
|
---|
7 | import java.net.URL;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.concurrent.ExecutionException;
|
---|
11 |
|
---|
12 | import org.junit.After;
|
---|
13 | import org.junit.Test;
|
---|
14 |
|
---|
15 | import agents.nastyagent.AddPersistentDataToStandard;
|
---|
16 | import agents.nastyagent.CheckStoredData;
|
---|
17 | import agents.nastyagent.RandomBid;
|
---|
18 | import agents.nastyagent.StoreAndRetrieve;
|
---|
19 | import agents.nastyagent.ThrowInChoose;
|
---|
20 | import agents.nastyagent.ThrowInConstructor;
|
---|
21 | import genius.core.AgentID;
|
---|
22 | import genius.core.Deadline;
|
---|
23 | import genius.core.DeadlineType;
|
---|
24 | import genius.core.events.BrokenPartyException;
|
---|
25 | import genius.core.events.NegotiationEvent;
|
---|
26 | import genius.core.events.SessionFailedEvent;
|
---|
27 | import genius.core.exceptions.InstantiateException;
|
---|
28 | import genius.core.exceptions.NegotiationPartyTimeoutException;
|
---|
29 | import genius.core.exceptions.NegotiatorException;
|
---|
30 | import genius.core.listener.Listener;
|
---|
31 | import genius.core.parties.NegotiationParty;
|
---|
32 | import genius.core.parties.NegotiationPartyInternal;
|
---|
33 | import genius.core.parties.SessionsInfo;
|
---|
34 | import genius.core.persistent.PersistentDataType;
|
---|
35 | import genius.core.protocol.StackedAlternatingOffersProtocol;
|
---|
36 | import genius.core.repository.DomainRepItem;
|
---|
37 | import genius.core.repository.PartyRepItem;
|
---|
38 | import genius.core.repository.ProfileRepItem;
|
---|
39 | import genius.core.session.ActionException;
|
---|
40 | import genius.core.session.ExecutorWithTimeout;
|
---|
41 | import genius.core.session.RepositoryException;
|
---|
42 | import genius.core.session.Session;
|
---|
43 | import genius.core.session.SessionConfiguration;
|
---|
44 | import genius.core.session.SessionManager;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Test if session manager correctly stores data
|
---|
48 | *
|
---|
49 | */
|
---|
50 | public class SessionStorageTest {
|
---|
51 |
|
---|
52 | private final static String RESOURCES = "file:src/test/resources/";
|
---|
53 |
|
---|
54 | private final String domain = RESOURCES + "partydomain/party_domain.xml";
|
---|
55 | private final String profile = RESOURCES + "partydomain/party1_utility.xml";
|
---|
56 | private static final Class<? extends NegotiationParty> OPPONENT = RandomBid.class;
|
---|
57 |
|
---|
58 | private DomainRepItem domainRepItem;
|
---|
59 | private ProfileRepItem profileRepItem;
|
---|
60 | private ExecutorWithTimeout executor = new ExecutorWithTimeout(3000);
|
---|
61 |
|
---|
62 | private Session session;
|
---|
63 | private SessionsInfo info;
|
---|
64 |
|
---|
65 | @After
|
---|
66 | public void after() {
|
---|
67 | info.close();
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Test
|
---|
71 | public void testWithStorageSerializableStorage() throws IOException, InstantiateException, BrokenPartyException {
|
---|
72 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(), PersistentDataType.SERIALIZABLE, true);
|
---|
73 | run(StoreAndRetrieve.class);
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Test(expected = BrokenPartyException.class)
|
---|
78 | public void testRunWithDisabledStorage() throws IOException, InstantiateException, BrokenPartyException {
|
---|
79 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(), PersistentDataType.DISABLED, true);
|
---|
80 | run(StoreAndRetrieve.class);
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Test(expected = BrokenPartyException.class)
|
---|
84 | public void testRunWithStandardStorage() throws IOException, InstantiateException, BrokenPartyException {
|
---|
85 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(), PersistentDataType.STANDARD, true);
|
---|
86 | run(StoreAndRetrieve.class);
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | public void checkStoreContents() throws IOException, InstantiateException, BrokenPartyException {
|
---|
91 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(), PersistentDataType.STANDARD, true);
|
---|
92 | run(CheckStoredData.class);
|
---|
93 |
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Test(expected = Exception.class) // unsupportedOperationException
|
---|
97 | public void tryChangeStorage() throws IOException, InstantiateException, BrokenPartyException {
|
---|
98 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(), PersistentDataType.STANDARD, true);
|
---|
99 | run(AddPersistentDataToStandard.class);
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Test(expected = InstantiateException.class)
|
---|
104 | public void firstPartyCrashDirectly() throws IOException, InstantiateException, BrokenPartyException {
|
---|
105 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(), PersistentDataType.STANDARD, true);
|
---|
106 | run(ThrowInConstructor.class);
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Test(expected = BrokenPartyException.class)
|
---|
110 | public void firstPartyCrashInReceive() throws IOException, InstantiateException, BrokenPartyException {
|
---|
111 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(), PersistentDataType.STANDARD, true);
|
---|
112 | run(ThrowInChoose.class);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Run session with an agent that tries to store some serializable object
|
---|
117 | * and that checks that the storage works
|
---|
118 | *
|
---|
119 | * @param partyClass
|
---|
120 | *
|
---|
121 | * @throws MalformedURLException
|
---|
122 | * @throws InstantiateException
|
---|
123 | * @throws BrokenPartyException
|
---|
124 | * @throws ActionException
|
---|
125 | * @throws InterruptedException
|
---|
126 | * @throws ExecutionException
|
---|
127 | * @throws NegotiationPartyTimeoutException
|
---|
128 | */
|
---|
129 | private void run(Class<? extends NegotiationParty> partyClass)
|
---|
130 | throws MalformedURLException, InstantiateException, BrokenPartyException {
|
---|
131 | session = new Session(new Deadline(180, DeadlineType.ROUND), info);
|
---|
132 | domainRepItem = new DomainRepItem(new URL(domain));
|
---|
133 | profileRepItem = new ProfileRepItem(new URL(profile), domainRepItem);
|
---|
134 | List<BrokenPartyException> errors = new ArrayList<>();
|
---|
135 |
|
---|
136 | List<NegotiationPartyInternal> theparties = generateParties(partyClass);
|
---|
137 | SessionManager sessionMgr = new SessionManager(mock(SessionConfiguration.class), theparties, session, executor);
|
---|
138 |
|
---|
139 | sessionMgr.addListener(new Listener<NegotiationEvent>() {
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | public void notifyChange(NegotiationEvent evt) {
|
---|
143 | if (evt instanceof SessionFailedEvent) {
|
---|
144 | errors.add(((SessionFailedEvent) evt).getException());
|
---|
145 | }
|
---|
146 | }
|
---|
147 | });
|
---|
148 | sessionMgr.runAndWait();
|
---|
149 | if (!errors.isEmpty()) {
|
---|
150 | throw errors.get(0);
|
---|
151 | }
|
---|
152 |
|
---|
153 | // run twice. 2nd time, it should check the data
|
---|
154 | theparties = generateParties(partyClass);
|
---|
155 | sessionMgr = new SessionManager(mock(SessionConfiguration.class), theparties, session, executor);
|
---|
156 |
|
---|
157 | sessionMgr.runAndWait();
|
---|
158 | if (!errors.isEmpty()) {
|
---|
159 | throw errors.get(0);
|
---|
160 | }
|
---|
161 |
|
---|
162 | }
|
---|
163 |
|
---|
164 | private List<NegotiationPartyInternal> generateParties(Class<? extends NegotiationParty> partyClass)
|
---|
165 | throws InstantiateException {
|
---|
166 | ArrayList<NegotiationPartyInternal> parties = new ArrayList<NegotiationPartyInternal>();
|
---|
167 | try {
|
---|
168 | parties.add(createParty(partyClass));
|
---|
169 | parties.add(createParty(OPPONENT));
|
---|
170 | } catch (MalformedURLException | IllegalAccessException | ClassNotFoundException | RepositoryException
|
---|
171 | | NegotiatorException | InstantiationException e) {
|
---|
172 | throw new InstantiateException("Failed to create party " + partyClass, e);
|
---|
173 | }
|
---|
174 | return parties;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Create a real party based on the class
|
---|
179 | *
|
---|
180 | * @param partyClass
|
---|
181 | * @return {@link NegotiationPartyInternal}
|
---|
182 | * @throws InstantiateException
|
---|
183 | * if party can't be instantiated
|
---|
184 | */
|
---|
185 | private NegotiationPartyInternal createParty(Class<? extends NegotiationParty> partyClass)
|
---|
186 | throws MalformedURLException, InstantiationException, IllegalAccessException, ClassNotFoundException,
|
---|
187 | RepositoryException, NegotiatorException, InstantiateException {
|
---|
188 | PartyRepItem partyRepItem = new PartyRepItem(partyClass.getCanonicalName());
|
---|
189 |
|
---|
190 | return new NegotiationPartyInternal(partyRepItem, profileRepItem, session, info, getAgentID(partyClass), null);
|
---|
191 | }
|
---|
192 |
|
---|
193 | private AgentID getAgentID(Class<? extends NegotiationParty> partyClass) {
|
---|
194 | return new AgentID(partyClass.getName());
|
---|
195 | }
|
---|
196 | }
|
---|