1 | package geniusweb.issuevalue;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertNull;
|
---|
5 | import static org.junit.Assert.assertThat;
|
---|
6 | import static org.mockito.Mockito.mock;
|
---|
7 | import static org.mockito.Mockito.when;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.math.BigDecimal;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Map;
|
---|
19 |
|
---|
20 | import org.junit.Test;
|
---|
21 |
|
---|
22 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
23 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
24 |
|
---|
25 | import tudelft.utilities.junit.GeneralTests;
|
---|
26 |
|
---|
27 | public 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(
|
---|
108 | new HashSet<>(Arrays.asList("lease car", "permanent contract",
|
---|
109 | "career development opportunities", "fte", "salary",
|
---|
110 | "work from home")),
|
---|
111 | jobsdomain.getIssuesValues().keySet());
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Test
|
---|
115 | public void getIssuesTest() {
|
---|
116 | assertEquals(issues, domain.getIssuesValues());
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Test
|
---|
120 | public void getValuesTest() {
|
---|
121 | assertEquals(values1, domain.getValues(ISSUE1));
|
---|
122 | assertEquals(values2, domain.getValues(ISSUE2));
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Test
|
---|
126 | public void getNameTest() {
|
---|
127 | assertEquals(DOMAINNAME, domain.getName());
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Test
|
---|
131 | public void testIsCompleteNoIssuesDefined() {
|
---|
132 | Bid bid = mock(Bid.class);
|
---|
133 | when(bid.getIssues()).thenReturn(Collections.emptySet());
|
---|
134 | assertThat(domain.isComplete(bid), new PatternMatch(
|
---|
135 | "Issues in bid .* do not match issues in domain .*"));
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Test
|
---|
139 | public void testIsComplete() {
|
---|
140 | Bid bid = mock(Bid.class);
|
---|
141 | when(bid.getIssues()).thenReturn(issues.keySet());
|
---|
142 | when(bid.getValue(ISSUE1)).thenReturn(I1V2);
|
---|
143 | when(bid.getValue(ISSUE2)).thenReturn(new NumberValue(TWO));
|
---|
144 | assertEquals(null, domain.isComplete(bid));
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Test
|
---|
148 | public void testIsCompleteWrongNumberValue() {
|
---|
149 | Bid bid = mock(Bid.class);
|
---|
150 | when(bid.getIssues()).thenReturn(issues.keySet());
|
---|
151 | when(bid.getValue(ISSUE1)).thenReturn(I1V2);
|
---|
152 | // THREE is NOT a possible value in the NumberValueSet.
|
---|
153 | when(bid.getValue(ISSUE2)).thenReturn(new NumberValue(THREE));
|
---|
154 | assertThat(domain.isComplete(bid),
|
---|
155 | new PatternMatch("issue 'issue2' in bid has illegal value 3"));
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Test
|
---|
159 | public void testIsCompleteExtraIssue() {
|
---|
160 | Bid bid = mock(Bid.class);
|
---|
161 | HashMap extraissues = new HashMap<>(issues);
|
---|
162 | extraissues.put(ISSUE3, values1);
|
---|
163 | when(bid.getIssues()).thenReturn(extraissues.keySet());
|
---|
164 | when(bid.getValue(ISSUE1)).thenReturn(I1V2);
|
---|
165 | // THREE is NOT a possible value in the NumberValueSet.
|
---|
166 | when(bid.getValue(ISSUE2)).thenReturn(new NumberValue(THREE));
|
---|
167 | assertThat(domain.isComplete(bid), new PatternMatch(
|
---|
168 | "Issues in bid .* do not match issues in domain .*"));
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Test(expected = IllegalArgumentException.class)
|
---|
172 | public void badName1Test() {
|
---|
173 | new Domain("($@#", issues);
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Test(expected = IllegalArgumentException.class)
|
---|
177 | public void emptyNameTest() {
|
---|
178 | new Domain("", issues);
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Test
|
---|
182 | public void testIsFittingPartialBid() {
|
---|
183 | Bid bid = new Bid(ISSUE1, I1V1);
|
---|
184 | assertNull(domain.isFitting(bid));
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Test
|
---|
188 | public void testIsFittingWrongTypeBid() {
|
---|
189 | Bid bid = new Bid(ISSUE1, I2V1);
|
---|
190 | assertThat(domain.isFitting(bid),
|
---|
191 | new PatternMatch("issue 'issue1' in bid has illegal value 2"));
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Test
|
---|
195 | public void testIsFittingNonsenseValue() {
|
---|
196 | Bid bid = new Bid(ISSUE1, new DiscreteValue("nonsense"));
|
---|
197 | assertThat(domain.isFitting(bid), new PatternMatch(
|
---|
198 | "issue 'issue1' in bid has illegal value \"nonsense\""));
|
---|
199 | }
|
---|
200 |
|
---|
201 | @Test
|
---|
202 | public void testIsFittingNonsenseIssue() {
|
---|
203 | Bid bid = new Bid("nonsense", I1V1);
|
---|
204 | assertThat(domain.isFitting(bid), new PatternMatch(
|
---|
205 | "bid .* refers to non-domain issue 'nonsense'"));
|
---|
206 | }
|
---|
207 | }
|
---|