package geniusweb.boa.acceptancestrategy; import static org.junit.Assert.assertFalse; 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.when; import java.math.BigDecimal; import org.junit.Before; import org.junit.Test; import geniusweb.actions.Accept; import geniusweb.actions.Offer; import geniusweb.actions.PartyId; import geniusweb.bidspace.BidsWithUtility; import geniusweb.bidspace.Interval; import geniusweb.boa.BoaState; import geniusweb.inform.Settings; import geniusweb.issuevalue.Bid; import geniusweb.profile.Profile; import geniusweb.profile.utilityspace.LinearAdditive; import geniusweb.progress.Progress; import geniusweb.progress.ProgressRounds; import geniusweb.references.Parameters; public class TimeDepAccStratTest { private static final Bid resbid = mock(Bid.class); private final Progress progress = mock(ProgressRounds.class); private final BidsWithUtility testbidspace = mock(BidsWithUtility.class); private final BoaState state = mock(BoaState.class); private final Bid bid1 = mock(Bid.class); private final Settings settings = mock(Settings.class); private final LinearAdditive linearprofile = mock(LinearAdditive.class); private PartyId party = new PartyId("someparty"); @Before public void before() { when(state.getSettings()).thenReturn(settings); when(state.getProfile()).thenReturn(linearprofile); when(state.getProgress()).thenReturn(progress); when(settings.getParameters()).thenReturn(new Parameters()); } @Test public void testRoundsProgressAtStart() { when(testbidspace.getRange()) .thenReturn(new Interval(BigDecimal.ZERO, BigDecimal.ONE)); TimeDependentAcceptanceStrategy ac = new TimeDependentAcceptanceStrategy() { @Override protected BidsWithUtility getBidspace(Profile profile) { return testbidspace; } }; // we use default e=1, k=0 which means p(0)=1 when(progress.get(any())).thenReturn(0.0d); when(linearprofile.getUtility(eq(bid1))) .thenReturn(BigDecimal.valueOf(0.4)); assertFalse(ac.filter(new Offer(party, bid1), state) instanceof Accept); } @Test public void testRoundsProgressAtStart1() { when(testbidspace.getRange()).thenReturn( new Interval(BigDecimal.ZERO, new BigDecimal("0.4"))); // the max util in this space is 0.4 TimeDependentAcceptanceStrategy ac = new TimeDependentAcceptanceStrategy() { @Override protected BidsWithUtility getBidspace(Profile profile) { return testbidspace; } }; // we use default e=1, k=0 which means p(0)=1 when(progress.get(any())).thenReturn(0.0d); when(linearprofile.getUtility(eq(bid1))) .thenReturn(new BigDecimal("0.4")); // now =max util when(state.getLastReceivedOffer()).thenReturn(new Offer(party, bid1)); assertTrue(ac.filter(new Offer(party, bid1), state) instanceof Accept); } @Test public void testRoundsProgressAtEnd() { when(testbidspace.getRange()) .thenReturn(new Interval(BigDecimal.ZERO, BigDecimal.ONE)); TimeDependentAcceptanceStrategy ac = new TimeDependentAcceptanceStrategy() { @Override protected BidsWithUtility getBidspace(Profile profile) { return testbidspace; } }; // we use default e=1, k=0 which means p(1)=0 when(progress.get(any())).thenReturn(1.0d); when(state.getLastReceivedOffer()).thenReturn(new Offer(party, bid1)); when(linearprofile.getUtility(eq(bid1))) .thenReturn(BigDecimal.valueOf(0.4)); when(state.getLastReceivedOffer()).thenReturn(new Offer(party, bid1)); assertTrue(ac.filter(new Offer(party, bid1), state) instanceof Accept); } @Test public void testRoundsProgressResValueIsUsed() { // resbid should raise the minimum at the end. // we check that at the end a bid with lower util than resbid is // not accepted. when(testbidspace.getRange()) .thenReturn(new Interval(BigDecimal.ZERO, BigDecimal.ONE)); TimeDependentAcceptanceStrategy ac = new TimeDependentAcceptanceStrategy() { @Override protected BidsWithUtility getBidspace(Profile profile) { return testbidspace; } }; // we use default e=1, k=0 which means p(1)=resValue. when(progress.get(any())).thenReturn(1.0d); when(linearprofile.getUtility(eq(bid1))) .thenReturn(BigDecimal.valueOf(0.4)); when(linearprofile.getReservationBid()).thenReturn(resbid); when(linearprofile.getUtility(eq(resbid))) .thenReturn(new BigDecimal("0.5")); // bid1 has util 0.4 which is below res value. when(state.getLastReceivedOffer()).thenReturn(new Offer(party, bid1)); assertFalse(ac.filter(new Offer(party, bid1), state) instanceof Accept); } }