source: java2python/geniuswebtranslator/geniuswebtest/geniusweb/issuevalue/BidTest.java@ 805

Last change on this file since 805 was 804, checked in by wouter, 7 months ago

#278 added NonNull annotation in many places in the geniusweb code

File size: 5.3 KB
Line 
1package geniusweb.issuevalue;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Mockito.mock;
5
6import java.io.IOException;
7import java.math.BigDecimal;
8import java.util.Arrays;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12
13import org.junit.Test;
14
15import com.fasterxml.jackson.core.JsonProcessingException;
16import com.fasterxml.jackson.databind.ObjectMapper;
17
18import tudelft.utilities.junit.GeneralTests;
19
20public class BidTest extends GeneralTests<Bid> {
21
22 private final String asString1 = "{\"issuevalues\":{\"issue3\":9012345678901234567.89,\"issue2\":1,\"issue1\":\"b\"}}";
23 private final static String ISSUE1 = "issue1";
24 private final static Value VALUE1 = new DiscreteValue("value1");
25 private final static String ISSUE2 = "issue2";
26 private final static Value VALUE2 = new NumberValue("10");
27 private final static Value VALUE3 = new NumberValue("1000");
28 private final static Value VALUE3b = new NumberValue("1e+3");
29
30 private final static Bid bid, bid1, bidb, bidc, bidd, bidd2;
31 static {
32 Map<String, Value> issuevalues = new HashMap<>();
33 issuevalues.put(ISSUE1, new DiscreteValue("b"));
34 issuevalues.put(ISSUE2, new NumberValue(BigDecimal.ONE));
35 issuevalues.put("issue3",
36 new NumberValue(new BigDecimal("9012345678901234567.89")));
37 bid = new Bid(issuevalues);
38
39 // different order but that shouldn't matter
40 Map<String, Value> issuevalues1 = new HashMap<>();
41 issuevalues1.put("issue3",
42 new NumberValue(new BigDecimal("9012345678901234567.89")));
43 issuevalues1.put(ISSUE2, new NumberValue(BigDecimal.ONE));
44 issuevalues1.put(ISSUE1, new DiscreteValue("b"));
45 bid1 = new Bid(issuevalues1);
46
47 Map<String, Value> issuevaluesb = new HashMap<>();
48 issuevaluesb.put(ISSUE1, new DiscreteValue("b"));
49 issuevaluesb.put(ISSUE2, new NumberValue(BigDecimal.ONE));
50 bidb = new Bid(issuevaluesb);
51
52 // bidc and bidd have values swapped, so different issuevalues.
53 Map<String, Value> issuevaluesc = new HashMap<>();
54 issuevaluesc.put(ISSUE1, VALUE1);
55 issuevaluesc.put(ISSUE2, VALUE2);
56 bidc = new Bid(issuevaluesc);
57
58 Map<String, Value> issuevaluesd = new HashMap<>();
59 issuevaluesd.put(ISSUE1, VALUE3);
60 bidd = new Bid(issuevaluesd);
61 issuevaluesd.put(ISSUE1, VALUE3b);
62 bidd2 = new Bid(issuevaluesd);
63
64 }
65
66 @Override
67 public List<List<Bid>> getGeneralTestData() {
68 return Arrays.asList(Arrays.asList(bid, bid1), Arrays.asList(bidb),
69 Arrays.asList(bidc), Arrays.asList(bidd, bidd2));
70 }
71
72 @Override
73 public List<String> getGeneralTestStrings() {
74 return Arrays.asList(
75 "Bid\\{issue3=9012345678901234567.89, issue2=1, issue1=\"b\"\\}",
76 "Bid\\{issue2=1, issue1=\"b\"\\}",
77 "Bid\\{issue2=10, issue1=\"value1\"\\}",
78 "Bid\\{issue1=1...\\}");
79 }
80
81 @Test(expected = NullPointerException.class)
82 public void NullConstructorTest() {
83 Bid bid = new Bid((Map<String, Value>) Nul());
84 }
85
86 @SuppressWarnings("unused")
87 @Test
88 public void bidTestSimple() {
89 Map<String, Value> issuevalues = new HashMap<>();
90 new Bid(issuevalues); // no issues, no values should be ok
91 }
92
93 @SuppressWarnings("unused")
94 @Test(expected = IllegalArgumentException.class)
95 public void bidTestNull() {
96 Map<String, Value> issuevalues = new HashMap<>();
97 issuevalues.put(ISSUE1, null);
98 new Bid(issuevalues); // smokes
99 }
100
101 @SuppressWarnings("unused")
102 @Test
103 public void bidTestOkIssueValue() {
104 Map<String, Value> issuevalues = new HashMap<>();
105 issuevalues.put(ISSUE1, mock(Value.class));
106
107 new Bid(issuevalues); // shouldn't smoke
108 }
109
110 @Test
111 public void serializeTest() throws JsonProcessingException {
112
113 ObjectMapper jackson = new ObjectMapper();
114 //System.out.println(jackson.writeValueAsString(bid));
115 assertEquals(asString1, jackson.writeValueAsString(bid));
116
117 }
118
119 @Test
120 public void deserializeTest() throws IOException {
121
122 ObjectMapper jackson = new ObjectMapper();
123 Bid read = jackson.readValue(asString1, Bid.class);
124 assertEquals(bid, read);
125 }
126
127 @Test
128 public void mergeTest() {
129 Map<String, Value> issuevalues = new HashMap<>();
130 issuevalues.put(ISSUE1, VALUE1);
131 Bid partial1 = new Bid(issuevalues);
132
133 issuevalues = new HashMap<>();
134 issuevalues.put(ISSUE2, VALUE2);
135 Bid partial2 = new Bid(issuevalues);
136
137 Bid mergedbid = partial1.merge(partial2);
138 assertEquals(bidc, mergedbid);
139 }
140
141 @Test(expected = IllegalArgumentException.class)
142 public void mergeIssuesOverlapTest() {
143 Map<String, Value> issuevalues = new HashMap<>();
144 issuevalues.put(ISSUE1, VALUE1);
145 Bid partial1 = new Bid(issuevalues);
146
147 issuevalues = new HashMap<>();
148 issuevalues.put(ISSUE1, VALUE2);
149 Bid partial2 = new Bid(issuevalues);
150
151 partial1.merge(partial2);
152 }
153
154 @SuppressWarnings("unused")
155 @Test
156 public void smokeTestConstructor2() {
157 new Bid("issue", new DiscreteValue("dss"));
158 }
159
160 @SuppressWarnings("unused")
161 @Test
162 public void smokeTestConstructor2b() {
163 new Bid("issue", new NumberValue("0.5"));
164 }
165
166 @SuppressWarnings("unused")
167 @Test(expected = NullPointerException.class)
168 public void smokeTestConstructor2Null1() {
169 new Bid((String) Nul(), new DiscreteValue("dss"));
170 }
171
172 @SuppressWarnings("unused")
173 @Test(expected = NullPointerException.class)
174 public void smokeTestConstructor2Null2() {
175 new Bid("issue", (Value) Nul());
176 }
177
178 /**
179 * @return null , hack to fool the code analyzer into accepting null values
180 * at illegal places.
181 */
182 private Object Nul() {
183 return null;
184 }
185}
Note: See TracBrowser for help on using the repository browser.