package geniusweb.issuevalue; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import java.io.IOException; import java.math.BigDecimal; import java.util.Arrays; import java.util.HashMap; 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 BidTest extends GeneralTests { private final String asString1 = "{\"issuevalues\":{\"issue3\":9012345678901234567.89,\"issue2\":1,\"issue1\":\"b\"}}"; private final static String ISSUE1 = "issue1"; private final static Value VALUE1 = new DiscreteValue("value1"); private final static String ISSUE2 = "issue2"; private final static Value VALUE2 = new NumberValue("10"); private final static Value VALUE3 = new NumberValue("1000"); private final static Value VALUE3b = new NumberValue("1e+3"); private final static Bid bid, bid1, bidb, bidc, bidd, bidd2; static { Map issuevalues = new HashMap<>(); issuevalues.put(ISSUE1, new DiscreteValue("b")); issuevalues.put(ISSUE2, new NumberValue(BigDecimal.ONE)); issuevalues.put("issue3", new NumberValue(new BigDecimal("9012345678901234567.89"))); bid = new Bid(issuevalues); // different order but that shouldn't matter Map issuevalues1 = new HashMap<>(); issuevalues1.put("issue3", new NumberValue(new BigDecimal("9012345678901234567.89"))); issuevalues1.put(ISSUE2, new NumberValue(BigDecimal.ONE)); issuevalues1.put(ISSUE1, new DiscreteValue("b")); bid1 = new Bid(issuevalues1); Map issuevaluesb = new HashMap<>(); issuevaluesb.put(ISSUE1, new DiscreteValue("b")); issuevaluesb.put(ISSUE2, new NumberValue(BigDecimal.ONE)); bidb = new Bid(issuevaluesb); // bidc and bidd have values swapped, so different issuevalues. Map issuevaluesc = new HashMap<>(); issuevaluesc.put(ISSUE1, VALUE1); issuevaluesc.put(ISSUE2, VALUE2); bidc = new Bid(issuevaluesc); Map issuevaluesd = new HashMap<>(); issuevaluesd.put(ISSUE1, VALUE3); bidd = new Bid(issuevaluesd); issuevaluesd.put(ISSUE1, VALUE3b); bidd2 = new Bid(issuevaluesd); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(bid, bid1), Arrays.asList(bidb), Arrays.asList(bidc), Arrays.asList(bidd, bidd2)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "Bid\\{issue3=9012345678901234567.89, issue2=1, issue1=\"b\"\\}", "Bid\\{issue2=1, issue1=\"b\"\\}", "Bid\\{issue2=10, issue1=\"value1\"\\}", "Bid\\{issue1=1...\\}"); } @Test(expected = NullPointerException.class) public void NullConstructorTest() { Map issuevalues = null; Bid bid = new Bid(issuevalues); } @SuppressWarnings("unused") @Test public void bidTestSimple() { Map issuevalues = new HashMap<>(); new Bid(issuevalues); // no issues, no values should be ok } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void bidTestNull() { Map issuevalues = new HashMap<>(); issuevalues.put(ISSUE1, null); new Bid(issuevalues); // smokes } @SuppressWarnings("unused") @Test public void bidTestOkIssueValue() { Map issuevalues = new HashMap<>(); issuevalues.put(ISSUE1, mock(Value.class)); new Bid(issuevalues); // shouldn't smoke } @Test public void serializeTest() throws JsonProcessingException { ObjectMapper jackson = new ObjectMapper(); //System.out.println(jackson.writeValueAsString(bid)); assertEquals(asString1, jackson.writeValueAsString(bid)); } @Test public void deserializeTest() throws IOException { ObjectMapper jackson = new ObjectMapper(); Bid read = jackson.readValue(asString1, Bid.class); assertEquals(bid, read); } @Test public void mergeTest() { Map issuevalues = new HashMap<>(); issuevalues.put(ISSUE1, VALUE1); Bid partial1 = new Bid(issuevalues); issuevalues = new HashMap<>(); issuevalues.put(ISSUE2, VALUE2); Bid partial2 = new Bid(issuevalues); Bid mergedbid = partial1.merge(partial2); assertEquals(bidc, mergedbid); } @Test(expected = IllegalArgumentException.class) public void mergeIssuesOverlapTest() { Map issuevalues = new HashMap<>(); issuevalues.put(ISSUE1, VALUE1); Bid partial1 = new Bid(issuevalues); issuevalues = new HashMap<>(); issuevalues.put(ISSUE1, VALUE2); Bid partial2 = new Bid(issuevalues); partial1.merge(partial2); } @SuppressWarnings("unused") @Test public void smokeTestConstructor2() { new Bid("issue", new DiscreteValue("dss")); } @SuppressWarnings("unused") @Test public void smokeTestConstructor2b() { new Bid("issue", new NumberValue("0.5")); } @SuppressWarnings("unused") @Test(expected = NullPointerException.class) public void smokeTestConstructor2Null1() { new Bid(null, new DiscreteValue("dss")); } @SuppressWarnings("unused") @Test(expected = NullPointerException.class) public void smokeTestConstructor2Null2() { new Bid("issue", null); } }