package geniusweb.boa; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.LinkedList; import java.util.List; import org.junit.Test; import geniusweb.actions.Accept; import geniusweb.actions.Action; import geniusweb.actions.EndNegotiation; import geniusweb.actions.Offer; import geniusweb.actions.PartyId; import geniusweb.boa.acceptancestrategy.AcceptanceStrategy; import geniusweb.boa.biddingstrategy.BiddingStrategy; import geniusweb.connection.ConnectionEnd; import geniusweb.inform.ActionDone; import geniusweb.inform.Inform; import geniusweb.inform.Settings; import geniusweb.inform.YourTurn; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.DiscreteValue; import geniusweb.issuevalue.Domain; import geniusweb.opponentmodel.OpponentModel; import geniusweb.progress.Progress; import geniusweb.references.Parameters; import geniusweb.references.ProfileRef; import geniusweb.references.ProtocolRef; import geniusweb.references.Reference; import tudelft.utilities.listener.DefaultListenable; public class BoaPartyTest { private static final Bid otherBid = new Bid("issue", new DiscreteValue("value2")); private static final PartyId OTHER = new PartyId("other"); private static final Bid BID1 = new Bid("issue", new DiscreteValue("value")); private static final PartyId ME = new PartyId("me"); private TestConnection connection = new TestConnection(); private static final String PROFILE = "src/test/resources/testprofile.json"; protected static final Offer biddingstratOffer = new Offer(ME, BID1); private static final Offer othersOffer = new Offer(OTHER, otherBid); private static final Accept accept = new Accept(ME, BID1); private static final EndNegotiation endnego = new EndNegotiation(OTHER); @Test(expected = RuntimeException.class) public void smokeTestMissingInit() { BoaParty boa = new BoaParty(); boa.connect(connection); boa.notifyChange(mock(ActionDone.class)); } @Test public void settingsTest() throws URISyntaxException { BoaState state = mock(BoaState.class); BoaParty boa = new BoaParty() { @Override protected BoaState initialState() { return state; } }; boa.connect(connection); PartyId id = new PartyId("testparty"); ProfileRef profile = new ProfileRef(new URI("file:" + PROFILE)); ProtocolRef protocol = new ProtocolRef("SAOP"); Progress progress = mock(Progress.class); Parameters parameters = new Parameters(); parameters = parameters.with("om", omMock.class.getCanonicalName()); parameters = parameters.with("bs", bsMock.class.getCanonicalName()); parameters = parameters.with("as", asMock.class.getCanonicalName()); Settings info = new Settings(id, profile, protocol, progress, parameters); boa.notifyChange(info); // weird bug in Mockito? Work around with a weird cast... verify(state).with(eq(info), any(bsMock.class), any(asMock.class), eq(omMock.class)); } @Test public void youStartTestE2E() throws URISyntaxException { BoaParty boa = getInitializedBoa(); boa.notifyChange(new YourTurn()); assertEquals(1, connection.getActions().size()); System.out.println("party did action:" + connection.getActions()); // this should come out of bidding strat as there is no acceptable offer assertEquals(biddingstratOffer, connection.getActions().get(0)); } @Test public void otherOfferstTestE2E() throws URISyntaxException { BoaParty boa = getInitializedBoa(); boa.notifyChange(new ActionDone(othersOffer)); boa.notifyChange(new YourTurn()); assertEquals(1, connection.getActions().size()); System.out.println("party did action:" + connection.getActions()); // this should come out of bidding strat as there is no acceptable offer // Because asMock always accepts, this should accept. assertTrue(connection.getActions().get(0) instanceof Accept); } @Test public void getLastBidTest() throws URISyntaxException { BoaParty boa = getInitializedBoa(); // just a potpourri of actions. boa.notifyChange(new ActionDone(biddingstratOffer)); boa.notifyChange(new ActionDone(endnego)); boa.notifyChange(new ActionDone(othersOffer)); boa.notifyChange(new ActionDone(accept)); assertEquals(otherBid, boa.getLastBid()); } private BoaParty getInitializedBoa() throws URISyntaxException { BoaParty boa = new BoaParty(); boa.connect(connection); PartyId id = new PartyId("testparty"); ProfileRef profile = new ProfileRef(new URI("file:" + PROFILE)); ProtocolRef protocol = new ProtocolRef("SAOP"); Progress progress = mock(Progress.class); Parameters parameters = new Parameters(); parameters = parameters.with("om", omMock.class.getCanonicalName()); parameters = parameters.with("bs", bsMock.class.getCanonicalName()); parameters = parameters.with("as", asMock.class.getCanonicalName()); Settings info = new Settings(id, profile, protocol, progress, parameters); boa.notifyChange(info); return boa; } } class asMock implements AcceptanceStrategy { @Override public Boolean isAcceptable(Bid bid, BoaState negoState) { return true; } } class bsMock implements BiddingStrategy { @Override public Action getAction(BoaState state) { return BoaPartyTest.biddingstratOffer; } } class omMock implements OpponentModel { @Override public String getName() { // TODO Auto-generated method stub return null; } @Override public Domain getDomain() { // TODO Auto-generated method stub return null; } @Override public Bid getReservationBid() { // TODO Auto-generated method stub return null; } @Override public OpponentModel with(Domain domain, Bid resBid) { return this; } @Override public OpponentModel with(Action action, Progress progress) { return this; } } /** * A "real" connection object, because the party is going to subscribe etc, and * without a real connection we would have to do a lot of mocks that would make * the test very hard to read. * */ class TestConnection extends DefaultListenable implements ConnectionEnd { private List actions = new LinkedList<>(); @Override public void send(Action action) throws IOException { actions.add(action); } @Override public Reference getReference() { return null; } @Override public URI getRemoteURI() { return null; } @Override public void close() { } @Override public Error getError() { return null; } public List getActions() { return actions; } }