package geniusweb.issuevalue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; 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.eclipse.jdt.annotation.NonNull; 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 @NonNull String asString1 = "{\"issuevalues\":{\"issue3\":9012345678901234567,\"issue2\":1,\"issue1\":\"b\"}}"; // we use pattern (a|b|c){3} as the 3 issuevalues can be in any order // do not use decimal fraction, otherwise python will use scientific notation. private final @NonNull String asStringPattern = "\\{\"issuevalues\":\\{((\"issue1\":\"b\"|\"issue2\":1|\"issue3\":9012345678901234567).*){3}\\}\\}"; private final static @NonNull String ISSUE1 = "issue1"; private final static @NonNull Value VALUE1 = new DiscreteValue("value1"); private final static @NonNull String ISSUE2 = "issue2"; private final static @NonNull Value VALUE2 = new NumberValue("10"); private final static @NonNull Value VALUE3 = new NumberValue("1000"); private final static @NonNull Value VALUE3b = new NumberValue("1e+3"); private final static Bid bid, bid1, bidb, bidc, bidd, bidd2; static { final @NonNull Map<@NonNull String, @NonNull Value> issuevalues = new HashMap<>(); issuevalues.put(ISSUE1, new DiscreteValue("b")); issuevalues.put(ISSUE2, new NumberValue(BigDecimal.ONE)); issuevalues.put("issue3", new NumberValue(new BigDecimal("9012345678901234567"))); bid = new Bid(issuevalues); // different order but that shouldn't matter final @NonNull Map<@NonNull String, @NonNull Value> issuevalues1 = new HashMap<>(); issuevalues1.put("issue3", new NumberValue(new BigDecimal("9012345678901234567"))); issuevalues1.put(ISSUE2, new NumberValue(BigDecimal.ONE)); issuevalues1.put(ISSUE1, new DiscreteValue("b")); bid1 = new Bid(issuevalues1); final @NonNull Map<@NonNull String, @NonNull Value> 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. final @NonNull Map<@NonNull String, @NonNull Value> issuevaluesc = new HashMap<>(); issuevaluesc.put(ISSUE1, VALUE1); issuevaluesc.put(ISSUE2, VALUE2); bidc = new Bid(issuevaluesc); final @NonNull Map<@NonNull String, @NonNull Value> 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( // the .* accounts for whitespaces, quotes, column and equal sign. // variants of these occur between python and java due to differences // in hashmap.toString "Bid\\{((.*issue3.*9012345678901234567|.*issue2.*1|.*issue1.*b.*).*){3}\\}", "Bid\\{((.*issue2.*1|.*issue1.*b).*){2}\\}", "Bid\\{((.*issue2.*10|.*issue1.*value1).*){2}\\}", "Bid\\{.*issue1.*1.*\\}"); } @Test public void bidTestSimple() { final @NonNull Map<@NonNull String, @NonNull Value> issuevalues = new HashMap<>(); new Bid(issuevalues); // no issues, no values should be ok } //#PY #only works in java. @Test(expected = IllegalArgumentException.class) //#PY #only works in java. public void bidTestNull() { // willingly ignore @NonNull to test the cosntructor. Only works in java // because the Bid constructor is overloaded and plum will catch this // before our code can detect the problem. final Map issuevalues = new HashMap<>(); issuevalues.put(ISSUE1, null); new Bid(issuevalues); // smokes } @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 { final @NonNull ObjectMapper jackson = new ObjectMapper(); String res = jackson.writeValueAsString(bid); assertTrue("serialized result " + res + " does not match " + asStringPattern, res.matches(asStringPattern)); } @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")); } }