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

Last change on this file since 874 was 863, checked in by wouter, 7 months ago

#264 ALL geniusweb tests now OK! That includes junit tests that are translated and then run in python

File size: 5.7 KB
Line 
1package geniusweb.issuevalue;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertTrue;
5import static org.mockito.Mockito.mock;
6
7import java.io.IOException;
8import java.math.BigDecimal;
9import java.util.Arrays;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Map;
13
14import org.eclipse.jdt.annotation.NonNull;
15import org.junit.Test;
16
17import com.fasterxml.jackson.core.JsonProcessingException;
18import com.fasterxml.jackson.databind.ObjectMapper;
19
20import tudelft.utilities.junit.GeneralTests;
21
22public 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 // the .* accounts for whitespaces, quotes, column and equal sign.
81 // variants of these occur between python and java due to differences
82 // in hashmap.toString
83 "Bid\\{((.*issue3.*9012345678901234567|.*issue2.*1|.*issue1.*b.*).*){3}\\}",
84 "Bid\\{((.*issue2.*1|.*issue1.*b).*){2}\\}",
85 "Bid\\{((.*issue2.*10|.*issue1.*value1).*){2}\\}",
86 "Bid\\{.*issue1.*1.*\\}");
87 }
88
89 @Test
90 public void bidTestSimple() {
91 final @NonNull Map<@NonNull String, @NonNull Value> issuevalues = new HashMap<>();
92 new Bid(issuevalues); // no issues, no values should be ok
93 }
94
95 //#PY #only works in java.
96 @Test(expected = IllegalArgumentException.class)
97 //#PY #only works in java.
98 public void bidTestNull() {
99 // willingly ignore @NonNull to test the cosntructor. Only works in java
100 // because the Bid constructor is overloaded and plum will catch this
101 // before our code can detect the problem.
102 final Map<String, Value> issuevalues = new HashMap<>();
103 issuevalues.put(ISSUE1, null);
104 new Bid(issuevalues); // smokes
105 }
106
107 @Test
108 public void bidTestOkIssueValue() {
109 Map<String, Value> issuevalues = new HashMap<>();
110 issuevalues.put(ISSUE1, mock(Value.class));
111
112 new Bid(issuevalues); // shouldn't smoke
113 }
114
115 @Test
116 public void serializeTest() throws JsonProcessingException {
117 final @NonNull ObjectMapper jackson = new ObjectMapper();
118 String res = jackson.writeValueAsString(bid);
119 assertTrue("serialized result " + res + " does not match "
120 + asStringPattern, res.matches(asStringPattern));
121 }
122
123 @Test
124 public void deserializeTest() throws IOException {
125
126 ObjectMapper jackson = new ObjectMapper();
127 Bid read = jackson.readValue(asString1, Bid.class);
128 assertEquals(bid, read);
129 }
130
131 @Test
132 public void mergeTest() {
133 Map<String, Value> issuevalues = new HashMap<>();
134 issuevalues.put(ISSUE1, VALUE1);
135 Bid partial1 = new Bid(issuevalues);
136
137 issuevalues = new HashMap<>();
138 issuevalues.put(ISSUE2, VALUE2);
139 Bid partial2 = new Bid(issuevalues);
140
141 Bid mergedbid = partial1.merge(partial2);
142 assertEquals(bidc, mergedbid);
143 }
144
145 @Test(expected = IllegalArgumentException.class)
146 public void mergeIssuesOverlapTest() {
147 Map<String, Value> issuevalues = new HashMap<>();
148 issuevalues.put(ISSUE1, VALUE1);
149 Bid partial1 = new Bid(issuevalues);
150
151 issuevalues = new HashMap<>();
152 issuevalues.put(ISSUE1, VALUE2);
153 Bid partial2 = new Bid(issuevalues);
154
155 partial1.merge(partial2);
156 }
157
158 @SuppressWarnings("unused")
159 @Test
160 public void smokeTestConstructor2() {
161 new Bid("issue", new DiscreteValue("dss"));
162 }
163
164 @SuppressWarnings("unused")
165 @Test
166 public void smokeTestConstructor2b() {
167 new Bid("issue", new NumberValue("0.5"));
168 }
169
170}
Note: See TracBrowser for help on using the repository browser.