[1] | 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
|
---|
[78] | 71 | public void testWithStorageSerializableStorage()
|
---|
| 72 | throws IOException, InstantiateException, BrokenPartyException {
|
---|
| 73 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(),
|
---|
| 74 | PersistentDataType.SERIALIZABLE, true);
|
---|
[1] | 75 | run(StoreAndRetrieve.class);
|
---|
| 76 |
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | @Test(expected = BrokenPartyException.class)
|
---|
[78] | 80 | public void testRunWithDisabledStorage()
|
---|
| 81 | throws IOException, InstantiateException, BrokenPartyException {
|
---|
| 82 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(),
|
---|
| 83 | PersistentDataType.DISABLED, true);
|
---|
[1] | 84 | run(StoreAndRetrieve.class);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | @Test(expected = BrokenPartyException.class)
|
---|
[78] | 88 | public void testRunWithStandardStorage()
|
---|
| 89 | throws IOException, InstantiateException, BrokenPartyException {
|
---|
| 90 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(),
|
---|
| 91 | PersistentDataType.STANDARD, true);
|
---|
[1] | 92 | run(StoreAndRetrieve.class);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @Test
|
---|
[78] | 96 | public void checkStoreContents()
|
---|
| 97 | throws IOException, InstantiateException, BrokenPartyException {
|
---|
| 98 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(),
|
---|
| 99 | PersistentDataType.STANDARD, true);
|
---|
[1] | 100 | run(CheckStoredData.class);
|
---|
| 101 |
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | @Test(expected = Exception.class) // unsupportedOperationException
|
---|
[78] | 105 | public void tryChangeStorage()
|
---|
| 106 | throws IOException, InstantiateException, BrokenPartyException {
|
---|
| 107 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(),
|
---|
| 108 | PersistentDataType.STANDARD, true);
|
---|
[1] | 109 | run(AddPersistentDataToStandard.class);
|
---|
| 110 |
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | @Test(expected = InstantiateException.class)
|
---|
[78] | 114 | public void firstPartyCrashDirectly()
|
---|
| 115 | throws IOException, InstantiateException, BrokenPartyException {
|
---|
| 116 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(),
|
---|
| 117 | PersistentDataType.STANDARD, true);
|
---|
[1] | 118 | run(ThrowInConstructor.class);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | @Test(expected = BrokenPartyException.class)
|
---|
[78] | 122 | public void firstPartyCrashInReceive()
|
---|
| 123 | throws IOException, InstantiateException, BrokenPartyException {
|
---|
| 124 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(),
|
---|
| 125 | PersistentDataType.STANDARD, true);
|
---|
[1] | 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)
|
---|
[78] | 144 | throws MalformedURLException, InstantiateException,
|
---|
| 145 | BrokenPartyException {
|
---|
[1] | 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);
|
---|
[78] | 152 | SessionManager sessionMgr = new SessionManager(
|
---|
| 153 | mock(SessionConfiguration.class), theparties, session,
|
---|
| 154 | executor);
|
---|
[1] | 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);
|
---|
[78] | 172 | sessionMgr = new SessionManager(mock(SessionConfiguration.class),
|
---|
| 173 | theparties, session, executor);
|
---|
[1] | 174 |
|
---|
| 175 | sessionMgr.runAndWait();
|
---|
| 176 | if (!errors.isEmpty()) {
|
---|
| 177 | throw errors.get(0);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[78] | 182 | private List<NegotiationPartyInternal> generateParties(
|
---|
| 183 | Class<? extends NegotiationParty> partyClass)
|
---|
[1] | 184 | throws InstantiateException {
|
---|
| 185 | ArrayList<NegotiationPartyInternal> parties = new ArrayList<NegotiationPartyInternal>();
|
---|
| 186 | try {
|
---|
| 187 | parties.add(createParty(partyClass));
|
---|
| 188 | parties.add(createParty(OPPONENT));
|
---|
[78] | 189 | } catch (MalformedURLException | IllegalAccessException
|
---|
| 190 | | ClassNotFoundException | RepositoryException
|
---|
[1] | 191 | | NegotiatorException | InstantiationException e) {
|
---|
[78] | 192 | throw new InstantiateException(
|
---|
| 193 | "Failed to create party " + partyClass, e);
|
---|
[1] | 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 | */
|
---|
[78] | 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());
|
---|
[1] | 213 |
|
---|
[78] | 214 | return new NegotiationPartyInternal(partyRepItem, profileRepItem,
|
---|
| 215 | session, info, getAgentID(partyClass));
|
---|
[1] | 216 | }
|
---|
| 217 |
|
---|
| 218 | private AgentID getAgentID(Class<? extends NegotiationParty> partyClass) {
|
---|
| 219 | return new AgentID(partyClass.getName());
|
---|
| 220 | }
|
---|
| 221 | }
|
---|