package geniusweb.bidspace.pareto; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import org.junit.Before; import org.junit.Test; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.DiscreteValue; import geniusweb.issuevalue.DiscreteValueSet; import geniusweb.issuevalue.Domain; import geniusweb.issuevalue.NumberValue; import geniusweb.issuevalue.NumberValueSet; import geniusweb.issuevalue.Value; import geniusweb.issuevalue.ValueSet; import geniusweb.profile.PartialOrdering; import geniusweb.profile.Profile; public class GenericParetoTest { private static final DiscreteValue I1V2 = new DiscreteValue("i1v2"); private static final DiscreteValue I1V1 = new DiscreteValue("i1v1"); private static final NumberValue I2V1 = new NumberValue("2.00"); private static final NumberValue I2V2 = new NumberValue("2.45"); private static final NumberValue I2V3 = new NumberValue("2.90"); private static final String DOMAINNAME = "testdomain"; private static final String ISSUE1 = "issue1"; private static final String ISSUE2 = "issue2"; private static final Map issues = new HashMap<>(); private static ValueSet values1; private static ValueSet values2; private static final BigDecimal TWO = new BigDecimal("2"); private static final BigDecimal THREE = new BigDecimal("3"); private static Domain domain; private static Bid bid1, bid2, bid3; private PartialOrdering profile1, profile2, profile3; private GenericPareto pareto; @Before public void before() { Collection discretevalues1 = new LinkedList<>(); discretevalues1.add(I1V1); discretevalues1.add(I1V2); values1 = new DiscreteValueSet(discretevalues1); issues.put(ISSUE1, values1); values2 = new NumberValueSet(TWO, THREE, new BigDecimal("0.45")); issues.put(ISSUE2, values2); domain = new Domain(DOMAINNAME, issues); Map issuevalues = new HashMap<>(); issuevalues.put(ISSUE1, I1V1); issuevalues.put(ISSUE2, I2V1); bid1 = new Bid(issuevalues); issuevalues.put(ISSUE1, I1V1); issuevalues.put(ISSUE2, I2V2); bid2 = new Bid(issuevalues); issuevalues.put(ISSUE1, I1V2); issuevalues.put(ISSUE2, I2V1); bid3 = new Bid(issuevalues); profile1 = mock(PartialOrdering.class); profile2 = mock(PartialOrdering.class); profile3 = mock(PartialOrdering.class); when(profile1.getDomain()).thenReturn(domain); when(profile2.getDomain()).thenReturn(domain); when(profile3.getDomain()).thenReturn(domain); pareto = new GenericPareto(Arrays.asList(profile1, profile2)); } @Test public void genericParetoTest() { // fefault: there is no preference at all, all isPreferredOrEqual // returns false and we can't remove pareto points. List profiles = Arrays.asList(profile1, profile2); GenericPareto pareto = new GenericPareto(profiles); Collection points = pareto.getPoints(); assertEquals(6, points.size()); } @Test public void genericParetoTest1() { // both prefer bid1 over any other bid. when(profile1.isPreferredOrEqual(eq(bid1), any(Bid.class))) .thenReturn(true); when(profile2.isPreferredOrEqual(eq(bid1), any(Bid.class))) .thenReturn(true); Collection points = pareto.getPoints(); assertEquals(1, points.size()); assertEquals(bid1, points.iterator().next()); } @Test public void genericParetoTest2() { // both prefer bid2 over any other bid. when(profile1.isPreferredOrEqual(eq(bid2), any(Bid.class))) .thenReturn(true); when(profile2.isPreferredOrEqual(eq(bid2), any(Bid.class))) .thenReturn(true); Collection points = pareto.getPoints(); // since both prefer bid2, bid is pareto point. assertEquals(1, points.size()); assertEquals(bid2, points.iterator().next()); } @Test public void genericParetoTest3() { // profile1 prefers bid1, profile2 prefers bid2. // now neither bid1 nor bid2 are dominating each other // and nor does bid1 or bid2 dominate anything else. when(profile1.isPreferredOrEqual(eq(bid1), any(Bid.class))) .thenReturn(true); when(profile2.isPreferredOrEqual(eq(bid2), any(Bid.class))) .thenReturn(true); Collection points = pareto.getPoints(); // since both prefer bid2, bid is pareto point. assertEquals(6, points.size()); } @Test public void genericParetoTest4() { // profile1 prefers bid1, profile2 prefers bid2. // but they both hate bid3. when(profile1.isPreferredOrEqual(eq(bid1), any(Bid.class))) .thenReturn(true); when(profile1.isPreferredOrEqual(any(Bid.class), eq(bid3))) .thenReturn(true); when(profile2.isPreferredOrEqual(any(Bid.class), eq(bid3))) .thenReturn(true); when(profile2.isPreferredOrEqual(eq(bid2), any(Bid.class))) .thenReturn(true); Collection points = pareto.getPoints(); // both hate bid3 but don't agree on bid1/2. bid3 is ruled out. assertEquals(5, points.size()); } @Test(expected = IllegalArgumentException.class) public void testNullNotOk() { new GenericPareto(Arrays.asList(profile1, null, profile1)); } @Test public void testImmutable() { List list = new ArrayList<>( Arrays.asList(profile1, profile2)); GenericPareto pareto = new GenericPareto(list); // if we modify the list, the pareto should not change list.add(profile3); assertEquals(2, pareto.getProfiles().size()); } @Test(expected = UnsupportedOperationException.class) public void testGetParetoAndAdd() { List list = pareto.getProfiles(); list.add(profile3); } @Test(expected = UnsupportedOperationException.class) public void testGetPointsAndAdd() { Set list = pareto.getPoints(); list.add(bid1); } }