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