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

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

#264 small fixes

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