package geniusweb.issuevalue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; import java.math.BigDecimal; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import tudelft.utilities.junit.GeneralTests; public class DomainTest extends GeneralTests { private static final BigDecimal TWO = new BigDecimal("2"); private static final BigDecimal THREE = new BigDecimal("3"); private static final DiscreteValue I1V2 = new DiscreteValue("i1v2"); private static final DiscreteValue I1V1 = new DiscreteValue("i1v1"); private static final NumberValue I2V1 = new NumberValue(TWO); private static final String DOMAINNAME = "testdomain"; private static final String ISSUE1 = "issue1"; private static final String ISSUE2 = "issue2"; private static final String ISSUE3 = "issue3"; private static final ObjectMapper jackson = new ObjectMapper(); private static final String domainstring = "{\"name\":\"testdomain\",\"issuesValues\":{\"issue2\":{\"range\":{\"low\":2,\"high\":3,\"step\":0.22}},\"issue1\":{\"values\":[\"i1v1\",\"i1v2\"]}}}"; private static final String jobsstring = "{\"name\":\"jobs\"," + "\"issuesValues\":{" + "\"lease car\":{\"values\":[\"yes\",\"no\"]}," + "\"permanent contract\":{\"values\":[\"yes\",\"no\"]}," + "\"career development opportunities\":{\"values\":[\"low\",\"medium\",\"high\"]}," + "\"fte\":{\"values\":[\"0.6\",\"0.8\",\"1.0\"]}," + "\"salary\":{\"values\":[\"2000\",\"2500\",\"3000\",\"3500\",\"4000\"]}," + "\"work from home\":{\"values\":[\"0\",\"1\",\"2\"]}}}"; private static final Map issues = new HashMap<>(); private static final ValueSet values1; private static final ValueSet values2; private static final Domain domain, domain1, domain2, domain3; static { 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.22")); issues.put(ISSUE2, values2); domain = new Domain(DOMAINNAME, issues); domain1 = new Domain(DOMAINNAME, issues); domain2 = new Domain(DOMAINNAME + "1", issues); Map issues1 = new HashMap<>(); issues1.put(ISSUE1, values1); issues1.put(ISSUE2, new NumberValueSet(BigDecimal.ONE, TWO, new BigDecimal("0.22"))); domain3 = new Domain(DOMAINNAME, issues1); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(domain, domain1), Arrays.asList(domain2), Arrays.asList(domain3)); } @Override public List getGeneralTestStrings() { return Arrays.asList("Domain.*testdomain.*", "Domain.*testdomain1.*", "Domain.*testdomain.*"); } @Test public void SerializeTest() throws JsonProcessingException { //System.out.println(jackson.writeValueAsString(domain)); assertEquals(domainstring, jackson.writeValueAsString(domain)); } @Test public void DeSerializeTest() throws IOException { //System.out.println(domainstring); assertEquals(domain, jackson.readValue(domainstring, Domain.class)); } @Test public void DeSerializeJobsTest() throws IOException { //System.out.println(jobsstring); Domain jobsdomain = jackson.readValue(jobsstring, Domain.class); assertEquals(new HashSet<>(Arrays.asList("lease car", "permanent contract", "career development opportunities", "fte", "salary", "work from home")), jobsdomain.getIssuesValues()); } @Test public void getIssuesTest() { assertEquals(issues.keySet(), domain.getIssuesValues()); } @Test public void getValuesTest() { assertEquals(values1, domain.getValues(ISSUE1)); assertEquals(values2, domain.getValues(ISSUE2)); } @Test public void getNameTest() { assertEquals(DOMAINNAME, domain.getName()); } @Test public void testIsCompleteNoIssuesDefined() { Bid bid = mock(Bid.class); when(bid.getIssues()).thenReturn(Collections.emptySet()); assertThat(domain.isComplete(bid), new PatternMatch( "Issues in bid .* do not match issues in domain .*")); } @Test public void testIsComplete() { Bid bid = mock(Bid.class); when(bid.getIssues()).thenReturn(issues.keySet()); when(bid.getValue(ISSUE1)).thenReturn(I1V2); when(bid.getValue(ISSUE2)).thenReturn(new NumberValue(TWO)); assertEquals(null, domain.isComplete(bid)); } @Test public void testIsCompleteWrongNumberValue() { Bid bid = mock(Bid.class); when(bid.getIssues()).thenReturn(issues.keySet()); when(bid.getValue(ISSUE1)).thenReturn(I1V2); // THREE is NOT a possible value in the NumberValueSet. when(bid.getValue(ISSUE2)).thenReturn(new NumberValue(THREE)); assertThat(domain.isComplete(bid), new PatternMatch("issue 'issue2' in bid has illegal value 3")); } @Test public void testIsCompleteExtraIssue() { Bid bid = mock(Bid.class); HashMap extraissues = new HashMap<>(issues); extraissues.put(ISSUE3, values1); when(bid.getIssues()).thenReturn(extraissues.keySet()); when(bid.getValue(ISSUE1)).thenReturn(I1V2); // THREE is NOT a possible value in the NumberValueSet. when(bid.getValue(ISSUE2)).thenReturn(new NumberValue(THREE)); assertThat(domain.isComplete(bid), new PatternMatch( "Issues in bid .* do not match issues in domain .*")); } @Test(expected = IllegalArgumentException.class) public void badName1Test() { new Domain("($@#", issues); } @Test(expected = IllegalArgumentException.class) public void emptyNameTest() { new Domain("", issues); } @Test public void testIsFittingPartialBid() { Bid bid = new Bid(ISSUE1, I1V1); assertNull(domain.isFitting(bid)); } @Test public void testIsFittingWrongTypeBid() { Bid bid = new Bid(ISSUE1, I2V1); assertThat(domain.isFitting(bid), new PatternMatch("issue 'issue1' in bid has illegal value 2")); } @Test public void testIsFittingNonsenseValue() { Bid bid = new Bid(ISSUE1, new DiscreteValue("nonsense")); assertThat(domain.isFitting(bid), new PatternMatch( "issue 'issue1' in bid has illegal value \"nonsense\"")); } @Test public void testIsFittingNonsenseIssue() { Bid bid = new Bid("nonsense", I1V1); assertThat(domain.isFitting(bid), new PatternMatch( "bid .* refers to non-domain issue 'nonsense'")); } }