1 | package geniusweb.issuevalue;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 | import static org.mockito.Mockito.mock;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.math.BigDecimal;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import org.eclipse.jdt.annotation.NonNull;
|
---|
15 | import org.junit.Test;
|
---|
16 |
|
---|
17 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
18 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
19 |
|
---|
20 | import tudelft.utilities.junit.GeneralTests;
|
---|
21 |
|
---|
22 | public class BidTest extends GeneralTests<Bid> {
|
---|
23 |
|
---|
24 | private final @NonNull String asString1 = "{\"issuevalues\":{\"issue3\":9012345678901234567,\"issue2\":1,\"issue1\":\"b\"}}";
|
---|
25 | // we use pattern (a|b|c){3} as the 3 issuevalues can be in any order
|
---|
26 | // do not use decimal fraction, otherwise python will use scientific notation.
|
---|
27 | private final @NonNull String asStringPattern = "\\{\"issuevalues\":\\{((\"issue1\":\"b\"|\"issue2\":1|\"issue3\":9012345678901234567).*){3}\\}\\}";
|
---|
28 | private final static @NonNull String ISSUE1 = "issue1";
|
---|
29 | private final static @NonNull Value VALUE1 = new DiscreteValue("value1");
|
---|
30 | private final static @NonNull String ISSUE2 = "issue2";
|
---|
31 | private final static @NonNull Value VALUE2 = new NumberValue("10");
|
---|
32 | private final static @NonNull Value VALUE3 = new NumberValue("1000");
|
---|
33 | private final static @NonNull Value VALUE3b = new NumberValue("1e+3");
|
---|
34 |
|
---|
35 | private final static Bid bid, bid1, bidb, bidc, bidd, bidd2;
|
---|
36 | static {
|
---|
37 | final @NonNull Map<@NonNull String, @NonNull Value> issuevalues = new HashMap<>();
|
---|
38 | issuevalues.put(ISSUE1, new DiscreteValue("b"));
|
---|
39 | issuevalues.put(ISSUE2, new NumberValue(BigDecimal.ONE));
|
---|
40 | issuevalues.put("issue3",
|
---|
41 | new NumberValue(new BigDecimal("9012345678901234567")));
|
---|
42 | bid = new Bid(issuevalues);
|
---|
43 |
|
---|
44 | // different order but that shouldn't matter
|
---|
45 | final @NonNull Map<@NonNull String, @NonNull Value> issuevalues1 = new HashMap<>();
|
---|
46 | issuevalues1.put("issue3",
|
---|
47 | new NumberValue(new BigDecimal("9012345678901234567")));
|
---|
48 | issuevalues1.put(ISSUE2, new NumberValue(BigDecimal.ONE));
|
---|
49 | issuevalues1.put(ISSUE1, new DiscreteValue("b"));
|
---|
50 | bid1 = new Bid(issuevalues1);
|
---|
51 |
|
---|
52 | final @NonNull Map<@NonNull String, @NonNull Value> issuevaluesb = new HashMap<>();
|
---|
53 | issuevaluesb.put(ISSUE1, new DiscreteValue("b"));
|
---|
54 | issuevaluesb.put(ISSUE2, new NumberValue(BigDecimal.ONE));
|
---|
55 | bidb = new Bid(issuevaluesb);
|
---|
56 |
|
---|
57 | // bidc and bidd have values swapped, so different issuevalues.
|
---|
58 | final @NonNull Map<@NonNull String, @NonNull Value> issuevaluesc = new HashMap<>();
|
---|
59 | issuevaluesc.put(ISSUE1, VALUE1);
|
---|
60 | issuevaluesc.put(ISSUE2, VALUE2);
|
---|
61 | bidc = new Bid(issuevaluesc);
|
---|
62 |
|
---|
63 | final @NonNull Map<@NonNull String, @NonNull Value> issuevaluesd = new HashMap<>();
|
---|
64 | issuevaluesd.put(ISSUE1, VALUE3);
|
---|
65 | bidd = new Bid(issuevaluesd);
|
---|
66 | issuevaluesd.put(ISSUE1, VALUE3b);
|
---|
67 | bidd2 = new Bid(issuevaluesd);
|
---|
68 |
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public List<List<Bid>> getGeneralTestData() {
|
---|
73 | return Arrays.asList(Arrays.asList(bid, bid1), Arrays.asList(bidb),
|
---|
74 | Arrays.asList(bidc), Arrays.asList(bidd, bidd2));
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public List<String> getGeneralTestStrings() {
|
---|
79 | return Arrays.asList(
|
---|
80 | "Bid\\{issue3=9012345678901234567, issue2=1, issue1=\"b\"\\}",
|
---|
81 | "Bid\\{issue2=1, issue1=\"b\"\\}",
|
---|
82 | "Bid\\{issue2=10, issue1=\"value1\"\\}",
|
---|
83 | "Bid\\{issue1=1...\\}");
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Test
|
---|
87 | public void bidTestSimple() {
|
---|
88 | final @NonNull Map<@NonNull String, @NonNull Value> issuevalues = new HashMap<>();
|
---|
89 | new Bid(issuevalues); // no issues, no values should be ok
|
---|
90 | }
|
---|
91 |
|
---|
92 | //#PY #only works in java.
|
---|
93 | @Test(expected = IllegalArgumentException.class)
|
---|
94 | //#PY #only works in java.
|
---|
95 | public void bidTestNull() {
|
---|
96 | // willingly ignore @NonNull to test the cosntructor. Only works in java
|
---|
97 | // because the Bid constructor is overloaded and plum will catch this
|
---|
98 | // before our code can detect the problem.
|
---|
99 | final Map<String, Value> issuevalues = new HashMap<>();
|
---|
100 | issuevalues.put(ISSUE1, null);
|
---|
101 | new Bid(issuevalues); // smokes
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Test
|
---|
105 | public void bidTestOkIssueValue() {
|
---|
106 | Map<String, Value> issuevalues = new HashMap<>();
|
---|
107 | issuevalues.put(ISSUE1, mock(Value.class));
|
---|
108 |
|
---|
109 | new Bid(issuevalues); // shouldn't smoke
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Test
|
---|
113 | public void serializeTest() throws JsonProcessingException {
|
---|
114 | final @NonNull ObjectMapper jackson = new ObjectMapper();
|
---|
115 | String res = jackson.writeValueAsString(bid);
|
---|
116 | assertTrue("serialized result " + res + " does not match "
|
---|
117 | + asStringPattern, res.matches(asStringPattern));
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Test
|
---|
121 | public void deserializeTest() throws IOException {
|
---|
122 |
|
---|
123 | ObjectMapper jackson = new ObjectMapper();
|
---|
124 | Bid read = jackson.readValue(asString1, Bid.class);
|
---|
125 | assertEquals(bid, read);
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Test
|
---|
129 | public void mergeTest() {
|
---|
130 | Map<String, Value> issuevalues = new HashMap<>();
|
---|
131 | issuevalues.put(ISSUE1, VALUE1);
|
---|
132 | Bid partial1 = new Bid(issuevalues);
|
---|
133 |
|
---|
134 | issuevalues = new HashMap<>();
|
---|
135 | issuevalues.put(ISSUE2, VALUE2);
|
---|
136 | Bid partial2 = new Bid(issuevalues);
|
---|
137 |
|
---|
138 | Bid mergedbid = partial1.merge(partial2);
|
---|
139 | assertEquals(bidc, mergedbid);
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Test(expected = IllegalArgumentException.class)
|
---|
143 | public void mergeIssuesOverlapTest() {
|
---|
144 | Map<String, Value> issuevalues = new HashMap<>();
|
---|
145 | issuevalues.put(ISSUE1, VALUE1);
|
---|
146 | Bid partial1 = new Bid(issuevalues);
|
---|
147 |
|
---|
148 | issuevalues = new HashMap<>();
|
---|
149 | issuevalues.put(ISSUE1, VALUE2);
|
---|
150 | Bid partial2 = new Bid(issuevalues);
|
---|
151 |
|
---|
152 | partial1.merge(partial2);
|
---|
153 | }
|
---|
154 |
|
---|
155 | @SuppressWarnings("unused")
|
---|
156 | @Test
|
---|
157 | public void smokeTestConstructor2() {
|
---|
158 | new Bid("issue", new DiscreteValue("dss"));
|
---|
159 | }
|
---|
160 |
|
---|
161 | @SuppressWarnings("unused")
|
---|
162 | @Test
|
---|
163 | public void smokeTestConstructor2b() {
|
---|
164 | new Bid("issue", new NumberValue("0.5"));
|
---|
165 | }
|
---|
166 |
|
---|
167 | }
|
---|