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

Last change on this file since 846 was 846, checked in by wouter, 5 months ago

#264 small fixes

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