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

Last change on this file since 889 was 863, checked in by wouter, 5 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
RevLine 
[595]1package geniusweb.issuevalue;
2
3import static org.junit.Assert.assertEquals;
[861]4import static org.junit.Assert.assertTrue;
[595]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
[846]14import org.eclipse.jdt.annotation.NonNull;
[595]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
[862]24 private final @NonNull String asString1 = "{\"issuevalues\":{\"issue3\":9012345678901234567,\"issue2\":1,\"issue1\":\"b\"}}";
[861]25 // we use pattern (a|b|c){3} as the 3 issuevalues can be in any order
[862]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}\\}\\}";
[846]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");
[595]34
35 private final static Bid bid, bid1, bidb, bidc, bidd, bidd2;
36 static {
[846]37 final @NonNull Map<@NonNull String, @NonNull Value> issuevalues = new HashMap<>();
[595]38 issuevalues.put(ISSUE1, new DiscreteValue("b"));
39 issuevalues.put(ISSUE2, new NumberValue(BigDecimal.ONE));
40 issuevalues.put("issue3",
[862]41 new NumberValue(new BigDecimal("9012345678901234567")));
[595]42 bid = new Bid(issuevalues);
43
44 // different order but that shouldn't matter
[846]45 final @NonNull Map<@NonNull String, @NonNull Value> issuevalues1 = new HashMap<>();
[595]46 issuevalues1.put("issue3",
[862]47 new NumberValue(new BigDecimal("9012345678901234567")));
[595]48 issuevalues1.put(ISSUE2, new NumberValue(BigDecimal.ONE));
49 issuevalues1.put(ISSUE1, new DiscreteValue("b"));
50 bid1 = new Bid(issuevalues1);
51
[846]52 final @NonNull Map<@NonNull String, @NonNull Value> issuevaluesb = new HashMap<>();
[595]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.
[846]58 final @NonNull Map<@NonNull String, @NonNull Value> issuevaluesc = new HashMap<>();
[595]59 issuevaluesc.put(ISSUE1, VALUE1);
60 issuevaluesc.put(ISSUE2, VALUE2);
61 bidc = new Bid(issuevaluesc);
62
[846]63 final @NonNull Map<@NonNull String, @NonNull Value> issuevaluesd = new HashMap<>();
[595]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(
[863]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.*\\}");
[595]87 }
88
89 @Test
90 public void bidTestSimple() {
[846]91 final @NonNull Map<@NonNull String, @NonNull Value> issuevalues = new HashMap<>();
[595]92 new Bid(issuevalues); // no issues, no values should be ok
93 }
94
[846]95 //#PY #only works in java.
[595]96 @Test(expected = IllegalArgumentException.class)
[846]97 //#PY #only works in java.
[595]98 public void bidTestNull() {
[846]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<>();
[595]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
[732]116 public void serializeTest() throws JsonProcessingException {
[846]117 final @NonNull ObjectMapper jackson = new ObjectMapper();
[862]118 String res = jackson.writeValueAsString(bid);
119 assertTrue("serialized result " + res + " does not match "
120 + asStringPattern, res.matches(asStringPattern));
[595]121 }
122
123 @Test
[732]124 public void deserializeTest() throws IOException {
[595]125
126 ObjectMapper jackson = new ObjectMapper();
127 Bid read = jackson.readValue(asString1, Bid.class);
128 assertEquals(bid, read);
129 }
130
131 @Test
[732]132 public void mergeTest() {
[595]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)
[732]146 public void mergeIssuesOverlapTest() {
[595]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.