1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.kuali.rice.krms.impl.repository.mock;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.LinkedHashSet;
25 import java.util.List;
26 import java.util.Set;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import static org.junit.Assert.*;
33 import org.kuali.rice.core.api.criteria.PredicateFactory;
34 import org.kuali.rice.core.api.criteria.QueryByCriteria;
35 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
36
37
38
39
40
41 public class CriteriaMatcherInMemoryTest {
42
43 public CriteriaMatcherInMemoryTest() {
44 }
45
46 @BeforeClass
47 public static void setUpClass() {
48 }
49
50 @AfterClass
51 public static void tearDownClass() {
52 }
53
54 @Before
55 public void setUp() {
56 }
57
58 @After
59 public void tearDown() {
60 }
61
62 private List<ContextDefinition> getTestContexts() {
63 List<ContextDefinition> contexts = new ArrayList<ContextDefinition>();
64 addContext(contexts, "ID-AA", "AA", "Automobile Association", "Consumer", "true", "Automobile Association towing");
65 addContext(contexts, "ID-CR", "CR", "Consumer Reports", "Consumer", "true", "Consumer reports magazine");
66 addContext(contexts, "ID-ARP", "ARP", "Assciation of Retired Persons", "Consumer", "false", null);
67 addContext(contexts, "ID-NRA", "NRA", "National Rifle Association", "Lobby", "true", "Guns and roses");
68 addContext(contexts, "ID-UFT", "UFT", "United Federation of Teachers", "Lobby", "true", "tenure for everyone");
69 addContext(contexts, "ID-AFL", "AFL", "AFL-CIO", "Union", "true", "Jimmy Hoffa may he rest in peace somewhere");
70 return contexts;
71 }
72
73 private void addContext(List<ContextDefinition> contexts,
74 String id,
75 String namespace,
76 String name,
77 String typeId,
78 String active,
79 String description) {
80 ContextDefinition.Builder info = ContextDefinition.Builder.create(namespace, name);
81 info.setId(id);
82 info.setTypeId(typeId);
83 info.setActive(Boolean.parseBoolean(active));
84 info.setDescription(description);
85 contexts.add(info.build());
86 }
87
88 @Test
89 public void testFindAll1() {
90 CriteriaMatcherInMemory<ContextDefinition> instance = new CriteriaMatcherInMemory<ContextDefinition>();
91 QueryByCriteria criteria = null;
92 instance.setCriteria(criteria);
93 Collection<ContextDefinition> result = instance.findMatching(getTestContexts());
94 Set<String> expectedIds = new LinkedHashSet<String>();
95 expectedIds.add("ID-AA");
96 expectedIds.add("ID-CR");
97 expectedIds.add("ID-ARP");
98 expectedIds.add("ID-NRA");
99 expectedIds.add("ID-UFT");
100 expectedIds.add("ID-AFL");
101 testIds(expectedIds, result);
102 }
103
104 @Test
105 public void testFindAll2() {
106 CriteriaMatcherInMemory<ContextDefinition> instance = new CriteriaMatcherInMemory<ContextDefinition>();
107 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
108 instance.setCriteria(criteria.build());
109 Collection<ContextDefinition> result = instance.findMatching(getTestContexts());
110 Set<String> expectedIds = new LinkedHashSet<String>();
111 expectedIds.add("ID-AA");
112 expectedIds.add("ID-CR");
113 expectedIds.add("ID-ARP");
114 expectedIds.add("ID-NRA");
115 expectedIds.add("ID-UFT");
116 expectedIds.add("ID-AFL");
117 testIds(expectedIds, result);
118 }
119
120 @Test
121 public void testFindEquals1() {
122 CriteriaMatcherInMemory<ContextDefinition> instance = new CriteriaMatcherInMemory<ContextDefinition>();
123 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
124 criteria.setPredicates(PredicateFactory.equal("typeId", "Consumer"));
125 instance.setCriteria(criteria.build());
126 Collection<ContextDefinition> result = instance.findMatching(getTestContexts());
127 Set<String> expectedIds = new LinkedHashSet<String>();
128 expectedIds.add("ID-AA");
129 expectedIds.add("ID-CR");
130 expectedIds.add("ID-ARP");
131
132
133
134 testIds(expectedIds, result);
135 }
136
137 @Test
138 public void testFindEquals2() {
139 CriteriaMatcherInMemory<ContextDefinition> instance = new CriteriaMatcherInMemory<ContextDefinition>();
140 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
141 criteria.setPredicates(PredicateFactory.equal("active", "true"));
142 instance.setCriteria(criteria.build());
143 Collection<ContextDefinition> result = instance.findMatching(getTestContexts());
144 Set<String> expectedIds = new LinkedHashSet<String>();
145 expectedIds.add("ID-AA");
146 expectedIds.add("ID-CR");
147
148 expectedIds.add("ID-NRA");
149 expectedIds.add("ID-UFT");
150 expectedIds.add("ID-AFL");
151 testIds(expectedIds, result);
152 }
153
154 @Test
155 public void testFindLike() {
156 CriteriaMatcherInMemory<ContextDefinition> instance = new CriteriaMatcherInMemory<ContextDefinition>();
157 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
158 criteria.setPredicates(PredicateFactory.like("name", "%Association%"));
159 instance.setCriteria(criteria.build());
160 Collection<ContextDefinition> result = instance.findMatching(getTestContexts());
161 Set<String> expectedIds = new LinkedHashSet<String>();
162 expectedIds.add("ID-AA");
163
164
165 expectedIds.add("ID-NRA");
166
167
168 testIds(expectedIds, result);
169 }
170
171 @Test
172 public void testFindDeepLike() {
173 CriteriaMatcherInMemory<ContextDefinition> instance = new CriteriaMatcherInMemory<ContextDefinition>();
174 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
175 criteria.setPredicates(PredicateFactory.like("description", "%magazine%"));
176 instance.setCriteria(criteria.build());
177 Collection<ContextDefinition> result = instance.findMatching(getTestContexts());
178 Set<String> expectedIds = new LinkedHashSet<String>();
179
180 expectedIds.add("ID-CR");
181
182
183
184
185 testIds(expectedIds, result);
186 }
187
188 @Test
189 public void testFindOr() {
190 CriteriaMatcherInMemory<ContextDefinition> instance = new CriteriaMatcherInMemory<ContextDefinition>();
191 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
192 criteria.setPredicates(PredicateFactory.or (
193 PredicateFactory.equal("typeId", "Lobby"),
194 PredicateFactory.equal("active", "false")));
195 instance.setCriteria(criteria.build());
196 Collection<ContextDefinition> result = instance.findMatching(getTestContexts());
197 Set<String> expectedIds = new LinkedHashSet<String>();
198
199
200 expectedIds.add("ID-ARP");
201 expectedIds.add("ID-NRA");
202 expectedIds.add("ID-UFT");
203
204 testIds(expectedIds, result);
205 }
206
207 @Test
208 public void testFindAnd() {
209 CriteriaMatcherInMemory<ContextDefinition> instance = new CriteriaMatcherInMemory<ContextDefinition>();
210 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
211 criteria.setPredicates(PredicateFactory.and (
212 PredicateFactory.equal("typeId", "Consumer"),
213 PredicateFactory.equal("active", "true")));
214 instance.setCriteria(criteria.build());
215 Collection<ContextDefinition> result = instance.findMatching(getTestContexts());
216 Set<String> expectedIds = new LinkedHashSet<String>();
217 expectedIds.add("ID-AA");
218 expectedIds.add("ID-CR");
219
220
221
222
223 testIds(expectedIds, result);
224 }
225
226 private void testIds(Set<String> expectedIds, Collection<ContextDefinition> result) {
227 for (ContextDefinition info : result) {
228 if (!expectedIds.remove(info.getId())) {
229 fail("Unexpectedly got " + info.getId());
230 }
231 }
232 if (!expectedIds.isEmpty()) {
233 fail("expected " + expectedIds.size() + " more");
234 }
235 }
236
237 @Test
238 public void testMatchesEqual() {
239 assertEquals(true, CriteriaMatcherInMemory.matchesEqual("a", "a"));
240 assertEquals(false, CriteriaMatcherInMemory.matchesEqual("a", "b"));
241 assertEquals(false, CriteriaMatcherInMemory.matchesEqual("b", "a"));
242 }
243
244 @Test
245 public void testMatchesLessThan() {
246 assertEquals(false, CriteriaMatcherInMemory.matchesLessThan("a", "a"));
247 assertEquals(true, CriteriaMatcherInMemory.matchesLessThan("a", "b"));
248 assertEquals(false, CriteriaMatcherInMemory.matchesLessThan("b", "a"));
249 }
250
251 @Test
252 public void testMatchesGreaterThan() {
253 assertEquals(false, CriteriaMatcherInMemory.matchesGreaterThan("a", "a"));
254 assertEquals(false, CriteriaMatcherInMemory.matchesGreaterThan("a", "b"));
255 assertEquals(true, CriteriaMatcherInMemory.matchesGreaterThan("b", "a"));
256 }
257
258 @Test
259 public void testMatchesLike() {
260 assertEquals(true, CriteriaMatcherInMemory.matchesLike("a", "a"));
261 assertEquals(false, CriteriaMatcherInMemory.matchesLike("a", "b"));
262 assertEquals(false, CriteriaMatcherInMemory.matchesLike("b", "a"));
263 assertEquals(true, CriteriaMatcherInMemory.matchesLike("b", "%"));
264 assertEquals(true, CriteriaMatcherInMemory.matchesLike("b", "b%"));
265 assertEquals(false, CriteriaMatcherInMemory.matchesLike("b", "a%"));
266 assertEquals(true, CriteriaMatcherInMemory.matchesLike("bbbbba", "%a%"));
267 }
268
269
270 @Test
271 public void testExtractValue() {
272
273 ContextDefinition org = this.getTestContexts().get(0);
274 assertEquals("ID-AA", CriteriaMatcherInMemory.extractValue("id", org));
275 assertEquals("AA", CriteriaMatcherInMemory.extractValue("namespace", org));
276 assertEquals("Automobile Association", CriteriaMatcherInMemory.extractValue("name", org));
277 assertEquals("Consumer", CriteriaMatcherInMemory.extractValue("typeId", org));
278 assertEquals("true", CriteriaMatcherInMemory.extractValue("active", org));
279 assertEquals("Automobile Association towing", CriteriaMatcherInMemory.extractValue("description", org));
280 }
281
282 }