1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37
38 public class RuleManagementContextDefinitionTest extends RuleManagementBaseTest {
39 @Override
40 @Before
41 public void setClassDiscriminator() {
42
43 CLASS_DISCRIMINATOR = "RMCDT";
44 }
45
46
47
48
49
50
51
52
53 @Test
54 public void testCreateContext() {
55
56 RuleManagementBaseTestObjectNames t0 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t0");
57
58
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
64 try {
65 ruleManagementService.createContext(contextDefinitionBuilder.build());
66 fail("Should have thrown IllegalStateException: the context to create already exists");
67 } catch (IllegalStateException e) {
68
69 }
70
71
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
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
83 }
84
85
86 try {
87 contextDefinitionBuilder = ContextDefinition.Builder.create(t0.namespaceName, null);
88 fail("Should have thrown IllegalArgumentException: name is blank");
89 } catch (IllegalArgumentException e) {
90
91 }
92 }
93
94
95
96
97
98
99
100
101 @Test
102 public void testFindCreateContext() {
103
104 RuleManagementBaseTestObjectNames t1 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t1");
105
106
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
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
117 contextDefinitionBuilder = ContextDefinition.Builder.create(t1.namespaceName, t1.context0_Name);
118 context = ruleManagementService.findCreateContext(contextDefinitionBuilder.build());
119
120
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
126 try {
127 ruleManagementService.findCreateContext(null);
128 fail("Should have thrown NullPointerException");
129 } catch (NullPointerException e) {
130
131 }
132 }
133
134
135
136
137
138
139
140
141 @Test
142 public void testUpdateContext() {
143
144 RuleManagementBaseTestObjectNames t2 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t2");
145
146
147 ContextDefinition context = buildTestContext(t2.object0);
148
149 context = ruleManagementService.getContext(context.getId());
150
151
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
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
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
175 try {
176 ruleManagementService.updateContext(null);
177 fail("Should have thrown IllegalArgumentException: context is null");
178 } catch (IllegalArgumentException e) {
179
180 }
181 }
182
183
184
185
186
187
188
189
190 @Test
191 public void testDeleteContext() {
192
193 RuleManagementBaseTestObjectNames t3 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t3");
194
195
196 ContextDefinition context = buildTestContext(t3.object0);
197
198 context = ruleManagementService.getContext(t3.context0_Id);
199 assertEquals("Unexpected contex name returned ",t3.context0_Name,context.getName());
200
201
202 try {
203 ruleManagementService.deleteContext(t3.context0_Id);
204 fail("Should have thrown RiceIllegalArgumentException: not implemented yet");
205 } catch (RiceIllegalArgumentException e) {
206
207 }
208
209
210
211
212 }
213
214
215
216
217
218
219
220
221 @Test
222 public void testGetContext() {
223
224 RuleManagementBaseTestObjectNames t4 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t4");
225
226
227 buildTestContext(t4.object0);
228
229
230 ContextDefinition context = ruleManagementService.getContext(t4.context0_Id);
231
232
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
243
244
245
246
247 @Test
248 public void testGetContextByNameAndNamespace() {
249
250 RuleManagementBaseTestObjectNames t5 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t5");
251
252
253 buildTestContext(t5.object0);
254
255
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
263 try {
264 ruleManagementService.getContextByNameAndNamespace(null,t5.namespaceName);
265 fail("Should have thrown IllegalArgumentException: name is null or blank");
266 } catch (IllegalArgumentException e) {
267
268 }
269
270
271 try {
272 ruleManagementService.getContextByNameAndNamespace(null,t5.namespaceName);
273 fail("Should have thrown IllegalArgumentException: namespace is null or blank");
274 } catch (IllegalArgumentException e) {
275
276 }
277
278
279 try {
280 ruleManagementService.getContextByNameAndNamespace(" ",t5.namespaceName);
281 fail("Should have thrown IllegalArgumentException: name is null or blank");
282 } catch (IllegalArgumentException e) {
283
284 }
285
286
287 try {
288 ruleManagementService.getContextByNameAndNamespace(t5.context0_Name," ");
289 fail("Should have thrown IllegalArgumentException: namespace is null or blank");
290 } catch (IllegalArgumentException e) {
291
292 }
293
294
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
302
303
304
305
306 @Test
307 public void testFindContextIds() {
308
309 RuleManagementBaseTestObjectNames t6 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t6");
310
311
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
334 }
335 }
336 }