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

Last change on this file since 595 was 595, checked in by wouter, 16 months ago

#197 working on translating @Test annotations.
added (expected-..) support.
Fixed poms that suddenly require https and /maven2

File size: 5.1 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 testNullConstructor() {
83 Map<String, Value> issuevalues = null;
84 Bid bid = new Bid(issuevalues);
85 }
86
87 @SuppressWarnings("unused")
88 @Test
89 public void bidTestSimple() {
90 Map<String, Value> issuevalues = new HashMap<>();
91 new Bid(issuevalues); // no issues, no values should be ok
92 }
93
94 @SuppressWarnings("unused")
95 @Test(expected = IllegalArgumentException.class)
96 public void bidTestNull() {
97 Map<String, Value> issuevalues = new HashMap<>();
98 issuevalues.put(ISSUE1, null);
99 new Bid(issuevalues); // smokes
100 }
101
102 @SuppressWarnings("unused")
103 @Test
104 public void bidTestOkIssueValue() {
105 Map<String, Value> issuevalues = new HashMap<>();
106 issuevalues.put(ISSUE1, mock(Value.class));
107
108 new Bid(issuevalues); // shouldn't smoke
109 }
110
111 @Test
112 public void testSerialize() throws JsonProcessingException {
113
114 ObjectMapper jackson = new ObjectMapper();
115 System.out.println(jackson.writeValueAsString(bid));
116 assertEquals(asString1, jackson.writeValueAsString(bid));
117
118 }
119
120 @Test
121 public void testDeserialize() 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 testMerge() {
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 testMergeIssuesOverlap() {
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 @SuppressWarnings("unused")
168 @Test(expected = NullPointerException.class)
169 public void smokeTestConstructor2Null1() {
170 new Bid(null, new DiscreteValue("dss"));
171 }
172
173 @SuppressWarnings("unused")
174 @Test(expected = NullPointerException.class)
175 public void smokeTestConstructor2Null2() {
176 new Bid("issue", null);
177 }
178
179}
Note: See TracBrowser for help on using the repository browser.