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