source: java2python/geniuswebtranslator/geniuswebtest/geniusweb/issuevalue/DomainTest.java@ 829

Last change on this file since 829 was 786, checked in by wouter, 9 months ago

#276 introduced Optional for any variable. You can explicitly say it will be not null using @Nonnull (javax.annotations). Added a few in a test to check it. Many places will need fixing/cleaning up

File size: 6.6 KB
Line 
1package geniusweb.issuevalue;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNull;
5import static org.junit.Assert.assertThat;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.when;
8
9import java.io.IOException;
10import java.math.BigDecimal;
11import java.util.Arrays;
12import java.util.Collection;
13import java.util.Collections;
14import java.util.HashMap;
15import java.util.HashSet;
16import java.util.LinkedList;
17import java.util.List;
18import java.util.Map;
19
20import org.junit.Test;
21
22import com.fasterxml.jackson.core.JsonProcessingException;
23import com.fasterxml.jackson.databind.ObjectMapper;
24
25import tudelft.utilities.junit.GeneralTests;
26
27public class DomainTest extends GeneralTests<Domain> {
28 private static final BigDecimal TWO = new BigDecimal("2");
29 private static final BigDecimal THREE = new BigDecimal("3");
30
31 private static final DiscreteValue I1V2 = new DiscreteValue("i1v2");
32 private static final DiscreteValue I1V1 = new DiscreteValue("i1v1");
33 private static final NumberValue I2V1 = new NumberValue(TWO);
34 private static final String DOMAINNAME = "testdomain";
35 private static final String ISSUE1 = "issue1";
36 private static final String ISSUE2 = "issue2";
37 private static final String ISSUE3 = "issue3";
38 private static final ObjectMapper jackson = new ObjectMapper();
39 private static final String domainstring = "{\"name\":\"testdomain\",\"issuesValues\":{\"issue2\":{\"range\":{\"low\":2,\"high\":3,\"step\":0.22}},\"issue1\":{\"values\":[\"i1v1\",\"i1v2\"]}}}";
40
41 private static final String jobsstring = "{\"name\":\"jobs\","
42 + "\"issuesValues\":{"
43 + "\"lease car\":{\"values\":[\"yes\",\"no\"]},"
44 + "\"permanent contract\":{\"values\":[\"yes\",\"no\"]},"
45 + "\"career development opportunities\":{\"values\":[\"low\",\"medium\",\"high\"]},"
46 + "\"fte\":{\"values\":[\"0.6\",\"0.8\",\"1.0\"]},"
47 + "\"salary\":{\"values\":[\"2000\",\"2500\",\"3000\",\"3500\",\"4000\"]},"
48 + "\"work from home\":{\"values\":[\"0\",\"1\",\"2\"]}}}";
49
50 private static final Map<String, ValueSet> issues = new HashMap<>();
51 private static final ValueSet values1;
52 private static final ValueSet values2;
53 private static final Domain domain, domain1, domain2, domain3;
54
55 static {
56 Collection<DiscreteValue> discretevalues1 = new LinkedList<>();
57 discretevalues1.add(I1V1);
58 discretevalues1.add(I1V2);
59 values1 = new DiscreteValueSet(discretevalues1);
60 issues.put(ISSUE1, values1);
61
62 values2 = new NumberValueSet(TWO, THREE, new BigDecimal("0.22"));
63 issues.put(ISSUE2, values2);
64
65 domain = new Domain(DOMAINNAME, issues);
66 domain1 = new Domain(DOMAINNAME, issues);
67 domain2 = new Domain(DOMAINNAME + "1", issues);
68
69 Map<String, ValueSet> issues1 = new HashMap<>();
70 issues1.put(ISSUE1, values1);
71 issues1.put(ISSUE2, new NumberValueSet(BigDecimal.ONE, TWO,
72 new BigDecimal("0.22")));
73 domain3 = new Domain(DOMAINNAME, issues1);
74
75 }
76
77 @Override
78 public List<List<Domain>> getGeneralTestData() {
79 return Arrays.asList(Arrays.asList(domain, domain1),
80 Arrays.asList(domain2), Arrays.asList(domain3));
81 }
82
83 @Override
84 public List<String> getGeneralTestStrings() {
85 return Arrays.asList("Domain.*testdomain.*", "Domain.*testdomain1.*",
86 "Domain.*testdomain.*");
87 }
88
89 @Test
90 public void SerializeTest() throws JsonProcessingException {
91 //System.out.println(jackson.writeValueAsString(domain));
92 assertEquals(domainstring, jackson.writeValueAsString(domain));
93
94 }
95
96 @Test
97 public void DeSerializeTest() throws IOException {
98 //System.out.println(domainstring);
99 assertEquals(domain, jackson.readValue(domainstring, Domain.class));
100
101 }
102
103 @Test
104 public void DeSerializeJobsTest() throws IOException {
105 //System.out.println(jobsstring);
106 Domain jobsdomain = jackson.readValue(jobsstring, Domain.class);
107 assertEquals(new HashSet<>(Arrays.asList("lease car",
108 "permanent contract", "career development opportunities", "fte",
109 "salary", "work from home")), jobsdomain.getIssuesValues());
110
111 }
112
113 @Test
114 public void getIssuesTest() {
115 assertEquals(issues.keySet(), domain.getIssuesValues());
116 }
117
118 @Test
119 public void getValuesTest() {
120 assertEquals(values1, domain.getValues(ISSUE1));
121 assertEquals(values2, domain.getValues(ISSUE2));
122 }
123
124 @Test
125 public void getNameTest() {
126 assertEquals(DOMAINNAME, domain.getName());
127 }
128
129 @Test
130 public void testIsCompleteNoIssuesDefined() {
131 Bid bid = mock(Bid.class);
132 when(bid.getIssues()).thenReturn(Collections.emptySet());
133 assertThat(domain.isComplete(bid), new PatternMatch(
134 "Issues in bid .* do not match issues in domain .*"));
135 }
136
137 @Test
138 public void testIsComplete() {
139 Bid bid = mock(Bid.class);
140 when(bid.getIssues()).thenReturn(issues.keySet());
141 when(bid.getValue(ISSUE1)).thenReturn(I1V2);
142 when(bid.getValue(ISSUE2)).thenReturn(new NumberValue(TWO));
143 assertEquals(null, domain.isComplete(bid));
144 }
145
146 @Test
147 public void testIsCompleteWrongNumberValue() {
148 Bid bid = mock(Bid.class);
149 when(bid.getIssues()).thenReturn(issues.keySet());
150 when(bid.getValue(ISSUE1)).thenReturn(I1V2);
151 // THREE is NOT a possible value in the NumberValueSet.
152 when(bid.getValue(ISSUE2)).thenReturn(new NumberValue(THREE));
153 assertThat(domain.isComplete(bid),
154 new PatternMatch("issue 'issue2' in bid has illegal value 3"));
155 }
156
157 @Test
158 public void testIsCompleteExtraIssue() {
159 Bid bid = mock(Bid.class);
160 HashMap extraissues = new HashMap<>(issues);
161 extraissues.put(ISSUE3, values1);
162 when(bid.getIssues()).thenReturn(extraissues.keySet());
163 when(bid.getValue(ISSUE1)).thenReturn(I1V2);
164 // THREE is NOT a possible value in the NumberValueSet.
165 when(bid.getValue(ISSUE2)).thenReturn(new NumberValue(THREE));
166 assertThat(domain.isComplete(bid), new PatternMatch(
167 "Issues in bid .* do not match issues in domain .*"));
168 }
169
170 @Test(expected = IllegalArgumentException.class)
171 public void badName1Test() {
172 new Domain("($@#", issues);
173 }
174
175 @Test(expected = IllegalArgumentException.class)
176 public void emptyNameTest() {
177 new Domain("", issues);
178 }
179
180 @Test
181 public void testIsFittingPartialBid() {
182 Bid bid = new Bid(ISSUE1, I1V1);
183 assertNull(domain.isFitting(bid));
184 }
185
186 @Test
187 public void testIsFittingWrongTypeBid() {
188 Bid bid = new Bid(ISSUE1, I2V1);
189 assertThat(domain.isFitting(bid),
190 new PatternMatch("issue 'issue1' in bid has illegal value 2"));
191 }
192
193 @Test
194 public void testIsFittingNonsenseValue() {
195 Bid bid = new Bid(ISSUE1, new DiscreteValue("nonsense"));
196 assertThat(domain.isFitting(bid), new PatternMatch(
197 "issue 'issue1' in bid has illegal value \"nonsense\""));
198 }
199
200 @Test
201 public void testIsFittingNonsenseIssue() {
202 Bid bid = new Bid("nonsense", I1V1);
203 assertThat(domain.isFitting(bid), new PatternMatch(
204 "bid .* refers to non-domain issue 'nonsense'"));
205 }
206}
Note: See TracBrowser for help on using the repository browser.