View Javadoc

1   /*
2    * Copyright 2006-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.kuali.rice.krms.test;
18  
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.kuali.rice.core.api.criteria.QueryByCriteria;
22  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
23  import org.kuali.rice.krad.criteria.CriteriaLookupDaoProxy;
24  import org.kuali.rice.krad.criteria.CriteriaLookupServiceImpl;
25  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
26  
27  import java.util.Arrays;
28  import java.util.List;
29  
30  import static org.junit.Assert.*;
31  import static org.kuali.rice.core.api.criteria.PredicateFactory.in;
32  
33  /**
34   *   RuleManagementContextDefinitionTest is to test the methods of ruleManagementServiceImpl relating to ContextDefinitions
35   *
36   *   Each test focuses on one of the methods.
37   */
38  public class RuleManagementContextDefinitionTest  extends RuleManagementBaseTest {
39      @Override
40      @Before
41      public void setClassDiscriminator() {
42          // set a unique discriminator for test objects of this class
43          CLASS_DISCRIMINATOR = "RMCDT";
44      }
45  
46  
47      /**
48       *  Test testCreateContext()
49       *
50       *  This test focuses specifically on the RuleManagementServiceImpl
51       *      .createContext(ContextDefinition contextDefinition) method
52       */
53      @Test
54      public void testCreateContext() {
55          // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
56          RuleManagementBaseTestObjectNames t0 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t0");
57  
58          // create a context
59          ContextDefinition.Builder contextDefinitionBuilder = ContextDefinition.Builder.create(t0.namespaceName, t0.context0_Name);
60          contextDefinitionBuilder.setId(t0.context0_Id);
61          ruleManagementService.createContext(contextDefinitionBuilder.build());
62  
63          // try to create context that already exists
64          try {
65              ruleManagementService.createContext(contextDefinitionBuilder.build());
66              fail("Should have thrown IllegalStateException: the context to create already exists");
67          } catch (IllegalStateException e) {
68              // throws IllegalStateException: the context to create already exists
69          }
70  
71          // verify created context
72          ContextDefinition context = ruleManagementService.getContext(t0.context0_Id);
73          assertEquals("Unexpected namespace on created context",t0.namespaceName,context.getNamespace());
74          assertEquals("Unexpected name on created context",t0.context0_Name,context.getName());
75          assertEquals("Unexpected context id on returned context",t0.context0_Id,context.getId());
76  
77          // build context with null Namespace
78          try {
79              contextDefinitionBuilder = ContextDefinition.Builder.create(null, t0.context1_Name);
80              fail("Should have thrown IllegalArgumentException: namespace is blank");
81          } catch (IllegalArgumentException e) {
82              // throws IllegalArgumentException: namespace is blank
83          }
84  
85          // build context with null Name
86          try {
87              contextDefinitionBuilder = ContextDefinition.Builder.create(t0.namespaceName, null);
88              fail("Should have thrown IllegalArgumentException: name is blank");
89          } catch (IllegalArgumentException e) {
90              // throws IllegalArgumentException: name is blank
91          }
92      }
93  
94  
95      /**
96       *  Test testFindCreateContext()
97       *
98       *  This test focuses specifically on the RuleManagementServiceImpl
99       *      .findCreateContext(ContextDefinition contextDefinition) method
100      */
101     @Test
102     public void testFindCreateContext() {
103         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
104         RuleManagementBaseTestObjectNames t1 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t1");
105 
106         // findCreateContext a context which does not already exist
107         ContextDefinition.Builder contextDefinitionBuilder = ContextDefinition.Builder.create(t1.namespaceName, t1.context0_Name);
108         contextDefinitionBuilder.setId(t1.context0_Id);
109         ContextDefinition context = ruleManagementService.findCreateContext(contextDefinitionBuilder.build());
110 
111         // verify created context
112         assertEquals("Unexpected namespace on created context",t1.namespaceName,context.getNamespace());
113         assertEquals("Unexpected name on created context",t1.context0_Name,context.getName());
114         assertEquals("Unexpected context id on returned context",t1.context0_Id,context.getId());
115 
116         // try to findCreate context that already exists
117         contextDefinitionBuilder = ContextDefinition.Builder.create(t1.namespaceName, t1.context0_Name);
118         context = ruleManagementService.findCreateContext(contextDefinitionBuilder.build());
119 
120         // re-verify created context - id should be from original create
121         assertEquals("Unexpected context id on returned context",t1.context0_Id,context.getId());
122         assertEquals("Unexpected namespace on created context",t1.namespaceName,context.getNamespace());
123         assertEquals("Unexpected name on created context",t1.context0_Name,context.getName());
124 
125         // test findCreate with null ContextDefinition
126         try {
127             ruleManagementService.findCreateContext(null);
128             fail("Should have thrown NullPointerException");
129         } catch (NullPointerException e) {
130             // throws NullPointerException
131         }
132     }
133 
134 
135     /**
136      *  Test testUpdateContext()
137      *
138      *  This test focuses specifically on the RuleManagementServiceImpl
139      *      .updateContext(ContextDefinition contextDefinition) method
140      */
141     @Test
142     public void testUpdateContext() {
143         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
144         RuleManagementBaseTestObjectNames t2 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t2");
145 
146         // build test Context
147         ContextDefinition context = buildTestContext(t2.object0);
148         // make sure it is cached to test update CacheEvict
149         context = ruleManagementService.getContext(context.getId());
150 
151         // verify created context
152         assertEquals("Unexpected namespace on created context",t2.namespaceName,context.getNamespace());
153         assertEquals("Unexpected name on created context",t2.context0_Name,context.getName());
154         assertEquals("Unexpected context id on returned context",t2.context0_Id,context.getId());
155         assertNull("Context Description not yet set, should have been null",context.getDescription());
156         assertEquals("Unexpected context active state",true,context.isActive());
157 
158         // update the context's namespace, name, description and set inactive
159         ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create(context);
160         contextBuilder.setNamespace(t2.namespaceName + "Changed");
161         contextBuilder.setName(t2.context0_Name + "Changed");
162         contextBuilder.setDescription(t2.context0_Descr + "Changed");
163         contextBuilder.setActive(false);
164         ruleManagementService.updateContext(contextBuilder.build());
165 
166         context = ruleManagementService.getContext(t2.context0_Id);
167         // verify updated context
168         assertEquals("Unexpected namespace on created context",t2.namespaceName + "Changed",context.getNamespace());
169         assertEquals("Unexpected name on created context",t2.context0_Name + "Changed",context.getName());
170         assertEquals("Unexpected context id on returned context",t2.context0_Id,context.getId());
171         assertEquals("Unexpected context description on returned context",t2.context0_Descr + "Changed",context.getDescription());
172         assertEquals("Unexpected contex active state",false,context.isActive());
173 
174         // try update on null Content
175         try {
176             ruleManagementService.updateContext(null);
177             fail("Should have thrown IllegalArgumentException: context is null");
178         } catch (IllegalArgumentException e) {
179             // throws IllegalArgumentException: context is null
180         }
181     }
182 
183 
184     /**
185      *  Test testDeleteContext()
186      *
187      *  This test focuses specifically on the RuleManagementServiceImpl
188      *      .deleteContext(String contextId) method
189      */
190     @Test
191     public void testDeleteContext() {
192         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
193         RuleManagementBaseTestObjectNames t3 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t3");
194 
195         // build test Context
196         ContextDefinition context = buildTestContext(t3.object0);
197         // proof text context exists
198         context = ruleManagementService.getContext(t3.context0_Id);
199         assertEquals("Unexpected contex name returned ",t3.context0_Name,context.getName());
200 
201         // delete Context
202         try {
203             ruleManagementService.deleteContext(t3.context0_Id);
204             fail("Should have thrown RiceIllegalArgumentException: not implemented yet");
205         } catch (RiceIllegalArgumentException e) {
206             // throws RiceIllegalArgumentException: not implemented yet
207         }
208 
209         // proof text context deleted  (uncomment when implemented
210         // context = ruleManagementServiceImpl.getContext(t3.context0_Id);
211         // assertEquals("Unexpected contex name returned ",t3.context0_Name,context.getName());
212     }
213 
214 
215     /**
216      *  Test testGetContext()
217      *
218      *  This test focuses specifically on the RuleManagementServiceImpl
219      *      .getContext(String contextId) method
220      */
221     @Test
222     public void testGetContext() {
223         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
224         RuleManagementBaseTestObjectNames t4 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t4");
225 
226         // build test Context
227         buildTestContext(t4.object0);
228 
229         // read context
230         ContextDefinition context = ruleManagementService.getContext(t4.context0_Id);
231 
232         //proof context was read
233         assertEquals("Unexpected contex name returned ",t4.context0_Name,context.getName());
234 
235         assertNull("Should be null", ruleManagementService.getContext(null));
236         assertNull("Should be null", ruleManagementService.getContext("   "));
237         assertNull("Should be null", ruleManagementService.getContext("badValue"));
238     }
239 
240 
241     /**
242      *  Test testGetContextByNameAndNamespace()
243      *
244      *  This test focuses specifically on the RuleManagementServiceImpl
245      *      .getContextByNameAndNamespace(String name, String namespace) method
246      */
247     @Test
248     public void testGetContextByNameAndNamespace() {
249         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
250         RuleManagementBaseTestObjectNames t5 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t5");
251 
252         // build test Context
253         buildTestContext(t5.object0);
254 
255         // read context  ByNameAndNamespace
256         ContextDefinition context = ruleManagementService.getContextByNameAndNamespace(t5.context0_Name,
257                 t5.namespaceName);
258 
259         assertEquals("Unexpected namespace on created context",t5.namespaceName,context.getNamespace());
260         assertEquals("Unexpected name on created context",t5.context0_Name,context.getName());
261 
262         // test call with null name
263         try {
264             ruleManagementService.getContextByNameAndNamespace(null,t5.namespaceName);
265             fail("Should have thrown IllegalArgumentException: name is null or blank");
266         } catch (IllegalArgumentException e) {
267             // throws IllegalArgumentException: name is null or blank
268         }
269 
270         // test call with null namespace
271         try {
272             ruleManagementService.getContextByNameAndNamespace(null,t5.namespaceName);
273             fail("Should have thrown IllegalArgumentException: namespace is null or blank");
274         } catch (IllegalArgumentException e) {
275             // throws IllegalArgumentException: namespace is null or blank
276         }
277 
278         // test call with blank name
279         try {
280             ruleManagementService.getContextByNameAndNamespace("  ",t5.namespaceName);
281             fail("Should have thrown IllegalArgumentException: name is null or blank");
282         } catch (IllegalArgumentException e) {
283             // throws IllegalArgumentException: name is null or blank
284         }
285 
286         // test call with null namespace
287         try {
288             ruleManagementService.getContextByNameAndNamespace(t5.context0_Name,"  ");
289             fail("Should have thrown IllegalArgumentException: namespace is null or blank");
290         } catch (IllegalArgumentException e) {
291             // throws IllegalArgumentException: namespace is null or blank
292         }
293 
294         // test get with values for non-existent name and namespace
295         assertNull("Should be null", ruleManagementService.getContextByNameAndNamespace("badValue", t5.namespaceName));
296         assertNull("Should be null", ruleManagementService.getContextByNameAndNamespace(t5.context0_Name, "badValue"));
297     }
298 
299 
300     /**
301      *  Test testFindContextIds()
302      *
303      *  This test focuses specifically on the RuleManagementServiceImpl
304      *      .findContextIds(QueryByCriteria queryByCriteria) method
305      */
306     @Test
307     public void testFindContextIds() {
308         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
309         RuleManagementBaseTestObjectNames t6 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t6");
310 
311         // build four test Contexts
312         buildTestContext(t6.object0);
313         buildTestContext(t6.object1);
314         buildTestContext(t6.object2);
315         buildTestContext(t6.object3);
316         List<String> contextIds = Arrays.asList(t6.context0_Id,t6.context1_Id,t6.context2_Id,t6.context3_Id);
317 
318         QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
319 
320         builder.setPredicates(in("id", contextIds.toArray(new String[]{})));
321 
322         List<String> foundIds = ruleManagementService.findContextIds(builder.build());
323         assertEquals("Should of found 4 contexts",4,foundIds.size());
324 
325         for (String contactId : contextIds) {
326             assertTrue("Should have only these ids",foundIds.contains(contactId));
327         }
328 
329         try {
330             ruleManagementService.findContextIds(null);
331             fail("Should have thrown IllegalArgumentException: criteria is null");
332         } catch (IllegalArgumentException e) {
333             // throws IllegalArgumentException: criteria is null
334         }
335     }
336 }