View Javadoc

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