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

Last change on this file since 749 was 744, checked in by wouter, 12 months ago

#254 PyProgram now throws TranslationException. Ignore broken test in tudunit-t

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