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

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

#30 use UncertainAdditiveUtilitySpace.

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