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

Last change on this file since 50 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 7.1 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() 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}
Note: See TracBrowser for help on using the repository browser.