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.apache.commons.collections.CollectionUtils;
20 import org.junit.Before;
21 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22 import org.kuali.rice.krad.data.DataObjectService;
23 import org.kuali.rice.krad.util.ObjectUtils;
24 import org.kuali.rice.krms.api.repository.LogicalOperator;
25 import org.kuali.rice.krms.api.repository.RuleManagementService;
26 import org.kuali.rice.krms.api.repository.action.ActionDefinition;
27 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
28 import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
29 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
30 import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
31 import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
32 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
33 import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
34 import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
35 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
36 import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding;
37 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
38 import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
39 import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
40 import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
41 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
42 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
43 import org.kuali.rice.krms.impl.repository.ActionBoService;
44 import org.kuali.rice.krms.impl.repository.AgendaBoService;
45 import org.kuali.rice.krms.impl.repository.ContextBoService;
46 import org.kuali.rice.krms.impl.repository.FunctionBoServiceImpl;
47 import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService;
48 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
49 import org.kuali.rice.krms.impl.repository.RuleBoService;
50 import org.kuali.rice.krms.impl.repository.TermBoService;
51 import org.kuali.rice.test.BaselineTestCase;
52
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.HashMap;
56 import java.util.List;
57 import java.util.Map;
58
59 import static org.junit.Assert.assertEquals;
60
61
62
63
64 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
65 public abstract class RuleManagementBaseTest extends KRMSTestCase {
66
67 protected RuleManagementService ruleManagementService;
68 protected TermBoService termBoService;
69 protected ContextBoService contextRepository;
70 protected KrmsTypeRepositoryService krmsTypeRepository;
71 protected RuleBoService ruleBoService;
72 protected AgendaBoService agendaBoService;
73 protected ActionBoService actionBoService;
74 protected FunctionBoServiceImpl functionBoService;
75 protected KrmsAttributeDefinitionService krmsAttributeDefinitionService;
76 protected DataObjectService dataObjectService;
77
78 protected String CLASS_DISCRIMINATOR;
79
80 private static String lastTestClass = null;
81
82 @Before
83 public void setup() {
84
85 TestActionTypeService.resetActionsFired();
86
87 ruleManagementService = KrmsRepositoryServiceLocator.getService("ruleManagementService");
88 termBoService = KrmsRepositoryServiceLocator.getTermBoService();
89 contextRepository = KrmsRepositoryServiceLocator.getContextBoService();
90 krmsTypeRepository = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
91 ruleBoService = KrmsRepositoryServiceLocator.getRuleBoService();
92 agendaBoService = KrmsRepositoryServiceLocator.getAgendaBoService();
93 actionBoService = KrmsRepositoryServiceLocator.getBean("actionBoService");
94 functionBoService = KrmsRepositoryServiceLocator.getBean("functionRepositoryService");
95 krmsAttributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
96 dataObjectService = (DataObjectService)GlobalResourceLoader.getService("dataObjectService");
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111 @Before
112 public void setClassDiscriminator() {
113
114 CLASS_DISCRIMINATOR = "BaseTest";
115 }
116
117
118
119
120
121
122
123 @Override
124 protected List<String> getPerTestTablesNotToClear() {
125 List<String> tablesNotToClear = super.getPerTestTablesNotToClear();
126
127
128 if (getClass().getName().equals(lastTestClass)) {
129 tablesNotToClear.add("KRMS_.*");
130 }
131 lastTestClass = getClass().getName();
132
133 return tablesNotToClear;
134 }
135
136
137
138
139
140
141
142
143
144 protected RuleDefinition buildTestRuleDefinition(String namespace, String objectDiscriminator) {
145 PropositionDefinition propositionDefinition = createTestPropositionForRule(objectDiscriminator);
146
147 String ruleId = "RuleId" + objectDiscriminator;
148 String name = "RuleName" + objectDiscriminator;
149 String propId = propositionDefinition.getId();
150 RuleDefinition.Builder ruleDefinitionBuilder = RuleDefinition.Builder.create(ruleId, name, namespace, null, propId);
151 ruleDefinitionBuilder.setProposition(PropositionDefinition.Builder.create(propositionDefinition));
152
153 String id = ruleManagementService.createRule(ruleDefinitionBuilder.build()).getId();
154 RuleDefinition ruleDefinition = ruleManagementService.getRule(id);
155
156 return ruleDefinition;
157 }
158
159
160
161
162
163
164
165
166
167
168 protected AgendaItemDefinition buildTestAgendaItemDefinition(String agendaItemId, String agendaId, String ruleId) {
169 AgendaItemDefinition.Builder agendaItemDefinitionBuilder = AgendaItemDefinition.Builder.create(agendaItemId,
170 agendaId);
171 agendaItemDefinitionBuilder.setRuleId(ruleId);
172
173 AgendaItemDefinition agendaItemDefinition =
174 ruleManagementService.createAgendaItem(agendaItemDefinitionBuilder.build());
175
176 return agendaItemDefinition;
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191 protected ActionDefinition buildTestActionDefinition(String actionId, String actionName, String actionDescr, int actionSequence, String ruleId, String namespace) {
192 return buildTestActionDefinition(actionId, actionName, actionDescr, actionSequence, ruleId, namespace, new HashMap<String, String>());
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 protected ActionDefinition buildTestActionDefinition(String actionId, String actionName, String actionDescr, int actionSequence, String ruleId, String namespace, Map<String, String> attributes) {
209 KrmsTypeDefinition krmsTypeDefinition = createKrmsActionTypeDefinition(namespace);
210
211 ActionDefinition.Builder actionDefinitionBuilder = ActionDefinition.Builder.create(actionId, actionName,
212 namespace, krmsTypeDefinition.getId(), ruleId, actionSequence);
213 actionDefinitionBuilder.setDescription(actionDescr);
214 actionDefinitionBuilder.setAttributes(attributes);
215 String id = ruleManagementService.createAction(actionDefinitionBuilder.build()).getId();
216 ActionDefinition actionDefinition = ruleManagementService.getAction(id);
217
218 return actionDefinition;
219 }
220
221
222
223
224
225
226
227
228
229 protected PropositionDefinition createTestPropositionForRule(String objectDiscriminator) {
230 createKrmsActionTypeDefinition("Namespace" + objectDiscriminator);
231
232 String namespace = "Namespace" + objectDiscriminator;
233 String propId = "P" + objectDiscriminator;
234 String termSpecId = "TSI_" + propId;
235 String ruleId = "RuleId" + objectDiscriminator;
236 String termSpecDescr = "TSI_" + propId + "_Descr";
237
238 return createTestSimpleProposition(namespace, propId, termSpecId, "ABC", "=", "java.lang.String", ruleId, termSpecDescr);
239 }
240
241
242
243
244
245
246
247
248
249
250 protected PropositionDefinition createTestPropositionForTranslation(String objectDiscriminator, String namespace, String typeName) {
251 createKrmsTypeDefinition("TypeId" + objectDiscriminator, namespace, typeName, null);
252
253 String ruleId = "RuleId" + objectDiscriminator;
254 String propId = "P" + objectDiscriminator;
255
256 return createTestSimpleProposition(namespace, propId, typeName, "ABC", "=", "java.lang.String", ruleId,
257 "TSI_" + propId + "_Descr");
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 protected PropositionDefinition createTestSimpleProposition(String namespace, String propId, String termSpecId, String propConstant, String propOperator, String termSpecType, String ruleId, String termSpecDescr){
275 createTestTermSpecification(termSpecId, termSpecId, namespace, termSpecType, termSpecDescr);
276 KrmsTypeDefinition krmsTypeDefinition = createKrmsTypeDefinition(null, namespace, termSpecId, "testTypeService");
277
278 PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(null,
279 PropositionType.SIMPLE.getCode(), ruleId, krmsTypeDefinition.getId(), Collections.<PropositionParameter.Builder>emptyList());
280 propBuilder.setDescription(propId + "_simple_proposition");
281
282 PropositionDefinition propositionDefinition = ruleManagementService.createProposition(propBuilder.build());
283
284 List<PropositionParameter.Builder> propParam = new ArrayList<PropositionParameter.Builder>();
285 propParam.add(PropositionParameter.Builder.create(propId + "_0", propositionDefinition.getId(), termSpecId,
286 PropositionParameterType.TERM.getCode(), 0));
287 propParam.add(PropositionParameter.Builder.create(propId + "_1", propositionDefinition.getId(), propConstant,
288 PropositionParameterType.CONSTANT.getCode(), 1));
289 propParam.add(PropositionParameter.Builder.create(propId + "_2", propositionDefinition.getId(), propOperator,
290 PropositionParameterType.OPERATOR.getCode(), 2));
291
292 propBuilder = PropositionDefinition.Builder.create(propositionDefinition);
293 propBuilder.setParameters(propParam);
294
295 ruleManagementService.updateProposition(propBuilder.build());
296
297 propositionDefinition = ruleManagementService.getProposition(propositionDefinition.getId());
298
299 return propositionDefinition;
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313 protected TermSpecificationDefinition createTestTermSpecification(String termSpecId, String termSpecName, String namespace, String type, String termSpecDescr){
314 TermSpecificationDefinition termSpecificationDefinition = termBoService.getTermSpecificationByNameAndNamespace(
315 termSpecName, namespace);
316
317 if (termSpecificationDefinition == null) {
318 TermSpecificationDefinition.Builder termSpecificationDefinitionBuilder =
319 TermSpecificationDefinition.Builder.create(null, termSpecName, namespace, type);
320 termSpecificationDefinitionBuilder.setDescription(termSpecDescr);
321 String id = termBoService.createTermSpecification(termSpecificationDefinitionBuilder.build()).getId();
322 termSpecificationDefinition = termBoService.getTermSpecificationById(id);
323 }
324
325 return termSpecificationDefinition;
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339 protected KrmsTypeDefinition createKrmsTypeDefinition(String typeId, String nameSpace, String typeName, String serviceName, List<KrmsTypeAttribute.Builder> typeAttributes) {
340 KrmsTypeDefinition krmsTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, typeName);
341
342 if (krmsTypeDefinition == null) {
343 KrmsTypeDefinition.Builder krmsTypeDefnBuilder = KrmsTypeDefinition.Builder.create(typeName, nameSpace);
344
345 if (!CollectionUtils.isEmpty(typeAttributes)) {
346 krmsTypeDefnBuilder.setAttributes(typeAttributes);
347 }
348
349 krmsTypeDefnBuilder.setId(typeId);
350 krmsTypeDefnBuilder.setServiceName(serviceName);
351 String id = krmsTypeRepository.createKrmsType(krmsTypeDefnBuilder.build()).getId();
352 krmsTypeDefinition = krmsTypeRepository.getTypeById(id);
353 }
354
355 return krmsTypeDefinition;
356 }
357
358
359
360
361
362
363
364
365
366
367
368 protected KrmsTypeDefinition createKrmsTypeDefinition(String typeId, String nameSpace, String typeName, String serviceName) {
369 return createKrmsTypeDefinition(typeId, nameSpace, typeName, serviceName, null);
370 }
371
372
373
374
375
376 protected AgendaDefinition createTestAgenda(String objectDiscriminator) {
377 return createTestAgenda(objectDiscriminator, false);
378 }
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 protected AgendaDefinition createTestAgenda(String objectDiscriminator, boolean createAttributes) {
395 String namespace = "Namespace" + objectDiscriminator;
396 String typeId = "TypeId" + objectDiscriminator;
397 String typeName = "TypeName" + objectDiscriminator;
398 String agendaId = "AgendaId" + objectDiscriminator;
399 String agendaName = "AgendaName" + objectDiscriminator;
400
401 List<KrmsTypeAttribute.Builder> typeAttrs = Collections.emptyList();
402
403 String attrDefName = "AttrName" + objectDiscriminator;
404 String attrValue = "AttrVal" + objectDiscriminator;
405
406 if (createAttributes) {
407
408 String attrDefId = "KRTEST" + objectDiscriminator;
409 String attrNamespace = "Namespace" + objectDiscriminator;
410
411 KrmsAttributeDefinition attrDef = createTestKrmsAttribute(attrDefId, attrDefName, attrNamespace);
412
413 typeAttrs = Collections.singletonList(KrmsTypeAttribute.Builder.create(null, attrDef.getId(), 1));
414 }
415
416
417 KrmsTypeDefinition krmsTypeDefinition = createKrmsTypeDefinition(typeId, namespace, typeName, null, typeAttrs);
418
419
420 ContextDefinition contextDefinition = buildTestContext(objectDiscriminator);
421
422
423 AgendaDefinition.Builder agendaBuilder = AgendaDefinition.Builder.create(agendaId, agendaName,
424 krmsTypeDefinition.getId(), contextDefinition.getId());
425
426 if (createAttributes) {
427 agendaBuilder.setAttributes(Collections.singletonMap(attrDefName, attrValue));
428 }
429
430 String id = ruleManagementService.createAgenda(agendaBuilder.build()).getId();
431 AgendaDefinition agendaDefinition = ruleManagementService.getAgenda(id);
432
433 return agendaDefinition;
434 }
435
436 protected AgendaDefinition.Builder buildComplexAgenda(RuleManagementBaseTestObjectNames names) {
437 String namespace = "Namespace" + names.discriminator;
438 return createComplexAgenda(namespace, "AGENDA", names);
439 }
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469 protected AgendaDefinition.Builder createComplexAgenda(String namespace, String namespaceType,
470 RuleManagementBaseTestObjectNames names) {
471
472 ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create(
473 namespace, names.contextName);
474 contextBuilder.setId(names.contextId);
475 ContextDefinition context = contextBuilder.build();
476 ruleManagementService.createContext(context );
477
478
479 createKrmsTypeDefinition(null, namespace, namespaceType, null);
480
481
482 AgendaDefinition.Builder agendaBuilder = AgendaDefinition.Builder.create(
483 names.agenda_Id, names.agenda_Name, namespaceType, names.contextId);
484 AgendaDefinition agenda = agendaBuilder.build();
485 agenda = ruleManagementService.createAgenda(agenda);
486
487
488 RuleDefinition rule0 = buildTestRuleDefinition(namespace, names.object0);
489 RuleDefinition rule1 = buildTestRuleDefinition(namespace, names.object1);
490 RuleDefinition rule2 = buildTestRuleDefinition(namespace, names.object2);
491 RuleDefinition rule3 = buildTestRuleDefinition(namespace, names.object3);
492 RuleDefinition rule4 = buildTestRuleDefinition(namespace, names.object4);
493 RuleDefinition rule5 = buildTestRuleDefinition(namespace, names.object5);
494 RuleDefinition rule6 = buildTestRuleDefinition(namespace, names.object6);
495 RuleDefinition rule7 = buildTestRuleDefinition(namespace, names.object7);
496
497
498 AgendaItemDefinition agendaItemOBJECT7 = buildTestAgendaItemDefinition(names.agendaItem_7_Id, agenda.getId(), rule7.getId());
499
500 AgendaItemDefinition agendaItemOBJECT6 = buildTestAgendaItemDefinition(names.agendaItem_6_Id, agenda.getId(),
501 rule6.getId());
502
503 AgendaItemDefinition agendaItemOBJECT5 = buildTestAgendaItemDefinition(names.agendaItem_5_Id, agenda.getId(),
504 rule5.getId());
505
506 AgendaItemDefinition agendaItemOBJECT4 = buildTestAgendaItemDefinition(names.agendaItem_4_Id, agenda.getId(),
507 rule4.getId());
508
509 AgendaItemDefinition agendaItemOBJECT3 = buildTestAgendaItemDefinition(names.agendaItem_3_Id, agenda.getId(),
510 rule3.getId());
511
512 AgendaItemDefinition agendaItemOBJECT2 = buildTestAgendaItemDefinition(names.agendaItem_2_Id, agenda.getId(),
513 rule2.getId());
514
515 AgendaItemDefinition.Builder itemBuilderOBJECT2 = AgendaItemDefinition.Builder.create(agendaItemOBJECT2);
516 AgendaItemDefinition.Builder itemBuilderOBJECT4 = AgendaItemDefinition.Builder.create(agendaItemOBJECT4);
517 AgendaItemDefinition.Builder itemBuilderOBJECT7 = AgendaItemDefinition.Builder.create(agendaItemOBJECT7);
518
519 itemBuilderOBJECT2.setWhenTrue(itemBuilderOBJECT7);
520 itemBuilderOBJECT2.setWhenTrueId(itemBuilderOBJECT7.getId());
521 itemBuilderOBJECT2.setWhenFalse(itemBuilderOBJECT4);
522 itemBuilderOBJECT2.setWhenFalseId(itemBuilderOBJECT4.getId());
523 AgendaItemDefinition.Builder itemBuilderOBJECT6 = AgendaItemDefinition.Builder.create(agendaItemOBJECT6);
524 itemBuilderOBJECT2.setAlways(itemBuilderOBJECT6);
525 itemBuilderOBJECT2.setAlwaysId(itemBuilderOBJECT6.getId());
526
527
528 ruleManagementService.updateAgendaItem(itemBuilderOBJECT2.build());
529 itemBuilderOBJECT2 =
530 AgendaItemDefinition.Builder.create(ruleManagementService.getAgendaItem(itemBuilderOBJECT2.getId()));
531
532 AgendaItemDefinition agendaItemOBJECT1 = buildTestAgendaItemDefinition(names.agendaItem_1_Id, agenda.getId(),
533 rule1.getId());
534 AgendaItemDefinition.Builder itemBuilderOBJECT1 = AgendaItemDefinition.Builder.create(agendaItemOBJECT1);
535 AgendaItemDefinition.Builder itemBuilderOBJECT5 = AgendaItemDefinition.Builder.create(agendaItemOBJECT5);
536 itemBuilderOBJECT1.setAlways(itemBuilderOBJECT5);
537 itemBuilderOBJECT1.setAlwaysId(itemBuilderOBJECT5.getId());
538 itemBuilderOBJECT1 = AgendaItemDefinition.Builder.create(ruleManagementService.createAgendaItem(
539 itemBuilderOBJECT1.build()));
540
541 AgendaItemDefinition agendaItemOBJECT0 = buildTestAgendaItemDefinition(names.agendaItem_0_Id, agenda.getId(),
542 rule0.getId());
543 AgendaItemDefinition.Builder itemBuilderOBJECT0 = AgendaItemDefinition.Builder.create(agendaItemOBJECT0);
544
545 itemBuilderOBJECT1 = AgendaItemDefinition.Builder.create(ruleManagementService.getAgendaItem(itemBuilderOBJECT1.getId()));
546 itemBuilderOBJECT0.setWhenTrue(itemBuilderOBJECT1);
547 itemBuilderOBJECT0.setWhenTrueId(itemBuilderOBJECT1.getId());
548
549 itemBuilderOBJECT2 = AgendaItemDefinition.Builder.create(ruleManagementService.getAgendaItem(itemBuilderOBJECT2.getId()));
550 itemBuilderOBJECT0.setWhenFalse(itemBuilderOBJECT2);
551 itemBuilderOBJECT0.setWhenFalseId(itemBuilderOBJECT2.getId());
552
553 AgendaItemDefinition.Builder itemBuilderOBJECT3 = AgendaItemDefinition.Builder.create(agendaItemOBJECT3);
554 itemBuilderOBJECT0.setAlways(itemBuilderOBJECT3);
555 itemBuilderOBJECT0.setAlwaysId(itemBuilderOBJECT3.getId());
556 itemBuilderOBJECT0 = AgendaItemDefinition.Builder.create(ruleManagementService.createAgendaItem(itemBuilderOBJECT0.build()));
557
558 agendaBuilder = AgendaDefinition.Builder.create(ruleManagementService.getAgenda(agenda.getId()));
559 agendaBuilder.setFirstItemId(itemBuilderOBJECT0.getId());
560
561
562 createTestKrmsAttribute(names.actionAttribute0, names.actionAttribute0_Key, names.namespaceName);
563 createTestKrmsAttribute(names.actionAttribute1, names.actionAttribute1_Key, names.namespaceName);
564
565
566 Map<String, String> actionAttributesOBJECT7 = new HashMap<String, String>();
567 actionAttributesOBJECT7.put(names.actionAttribute_Key, names.actionAttribute_Value);
568
569
570
571 buildTestActionDefinition(names.action_Id, names.action_Name, names.action_Descr, 1, rule7.getId(),
572 names.namespaceName, actionAttributesOBJECT7);
573
574
575 ruleManagementService.updateAgenda(agendaBuilder.build());
576 List<AgendaItemDefinition> agendaItems = ruleManagementService.getAgendaItemsByContext(names.contextId);
577
578
579
580 assertEquals("Invalid number of agendaItems created", 8, agendaItems.size());
581
582 return AgendaDefinition.Builder.create(ruleManagementService.getAgenda(agenda.getId()));
583 }
584
585
586
587
588
589
590
591
592 protected ReferenceObjectBinding.Builder createTestReferenceObjectBinding(String objectDiscriminator) {
593 String namespace = "Namespace" + objectDiscriminator;
594 AgendaDefinition agendaDefinition = createTestAgenda(objectDiscriminator);
595
596 KrmsTypeDefinition krmsTypeDefinition = createKrmsTypeDefinition(null, namespace, "AgendaType" + objectDiscriminator, null);
597
598 return createReferenceObjectBinding("ParkingPolicies", agendaDefinition.getId(), krmsTypeDefinition.getId(),
599 namespace, "PA" + objectDiscriminator, "ParkingAffiliationType", true);
600 }
601
602
603
604
605
606
607
608
609
610
611
612
613
614 protected ReferenceObjectBinding.Builder createReferenceObjectBinding(String collectionName, String krmsObjectId,
615 String krmsDiscriminatorType, String namespace, String referenceObjectId, String referenceDiscriminatorType,
616 boolean active) {
617
618 ReferenceObjectBinding.Builder refObjBindingBuilder = ReferenceObjectBinding.Builder.
619 create(krmsDiscriminatorType, krmsObjectId, namespace, referenceDiscriminatorType, referenceObjectId);
620 refObjBindingBuilder.setCollectionName(collectionName);
621 refObjBindingBuilder.setActive(active);
622
623 return ReferenceObjectBinding.Builder.create(ruleManagementService.createReferenceObjectBinding(
624 refObjBindingBuilder.build()));
625 }
626
627
628
629
630
631
632
633
634
635
636
637 protected NaturalLanguageTemplate createTestNaturalLanguageTemplate(String namespace, String langCode,
638 String nlObjectName, String template, boolean createIdFromLangCodeAndNlObjectName) {
639 return createTestNaturalLanguageTemplate(namespace, langCode,
640 nlObjectName, template, "krms.nl." + nlObjectName, createIdFromLangCodeAndNlObjectName);
641 }
642
643
644
645
646
647
648
649
650
651
652
653
654 protected NaturalLanguageTemplate createTestNaturalLanguageTemplate(String namespace, String langCode,
655 String nlObjectName, String template, String nlUsage, boolean createIdFromLangCodeAndNlObjectName) {
656 KrmsTypeDefinition krmsType = createKrmsTypeDefinition(null, namespace, nlObjectName, null);
657
658
659 if (ObjectUtils.isNull(ruleManagementService.getNaturalLanguageUsage(nlUsage))) {
660 NaturalLanguageUsage.Builder naturalLanguageUsageBuilder = NaturalLanguageUsage.Builder.create(nlObjectName,namespace );
661 naturalLanguageUsageBuilder.setId(nlUsage);
662 naturalLanguageUsageBuilder.setDescription("Description-" + nlObjectName);
663 naturalLanguageUsageBuilder.setActive(true);
664 NaturalLanguageUsage naturalLangumageUsage = ruleManagementService.createNaturalLanguageUsage(naturalLanguageUsageBuilder.build());
665 }
666
667
668 createTestKrmsAttribute(langCode + "_" + nlObjectName + "Attribute1", nlObjectName + "TemplateAttributeName1", namespace);
669 createTestKrmsAttribute(langCode + "_" + nlObjectName + "Attribute2", nlObjectName + "TemplateAttributeName2", namespace);
670 Map<String, String> nlAttributes = new HashMap<String, String>();
671 nlAttributes.put( nlObjectName + "TemplateAttributeName1","value1");
672 nlAttributes.put( nlObjectName + "TemplateAttributeName2","value2");
673
674
675 String naturalLanguageUsageId = nlUsage;
676 String typeId = krmsType.getId();
677 NaturalLanguageTemplate.Builder naturalLanguageTemplateBuilder = NaturalLanguageTemplate.Builder.create(
678 langCode, naturalLanguageUsageId, template, typeId);
679 naturalLanguageTemplateBuilder.setActive(true);
680 if (createIdFromLangCodeAndNlObjectName) {
681 naturalLanguageTemplateBuilder.setId(langCode + "-" + nlObjectName);
682 }
683 naturalLanguageTemplateBuilder.setAttributes(nlAttributes);
684
685 return ruleManagementService.createNaturalLanguageTemplate(naturalLanguageTemplateBuilder.build());
686 }
687
688
689
690
691
692
693
694
695
696
697 protected KrmsAttributeDefinition createTestKrmsAttribute(String id, String name, String namespace) {
698
699
700 if (ObjectUtils.isNull(krmsAttributeDefinitionService.getAttributeDefinitionById(id))) {
701 KrmsAttributeDefinition.Builder krmsAttributeDefinitionBuilder = KrmsAttributeDefinition.Builder.create(id, name, namespace);
702 KrmsAttributeDefinition krmsAttributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(krmsAttributeDefinitionBuilder.build());
703 }
704
705 return krmsAttributeDefinitionService.getAttributeDefinitionById(id);
706 }
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725 protected NaturalLanguageUsage buildTestNaturalLanguageUsage(String namespace, String nlUsageName ) {
726 KrmsTypeDefinition krmsType = createKrmsTypeDefinition(null, namespace, nlUsageName, null);
727
728
729 NaturalLanguageUsage.Builder naturalLanguageUsageBuilder = NaturalLanguageUsage.Builder.create(nlUsageName,namespace );
730 naturalLanguageUsageBuilder.setId("krms.nl." + nlUsageName);
731 naturalLanguageUsageBuilder.setDescription("Description-" + nlUsageName);
732 naturalLanguageUsageBuilder.setActive(true);
733
734 return ruleManagementService.createNaturalLanguageUsage(naturalLanguageUsageBuilder.build());
735 }
736
737
738
739
740
741
742
743
744
745
746
747 protected ContextDefinition buildTestContext(String objectDiscriminator) {
748 String namespace = "Namespace" + objectDiscriminator;
749 String contextId = "ContextId" + objectDiscriminator;
750 String contextName = "ContextName" + objectDiscriminator;
751
752 ContextDefinition.Builder contextDefinitionBuilder = ContextDefinition.Builder.create(namespace, contextName);
753 contextDefinitionBuilder.setId(contextId);
754 String id = ruleManagementService.createContext(contextDefinitionBuilder.build()).getId();
755 ContextDefinition contextDefinition = ruleManagementService.getContext(id);
756
757 return contextDefinition;
758 }
759
760
761
762
763
764
765
766
767
768
769
770
771
772 protected PropositionDefinition createTestCompoundProposition(RuleManagementBaseTestObjectNames t0){
773
774 PropositionDefinition propS2 = createTestSimpleProposition(
775 t0.namespaceName, "S2", "Occasion", "Conference", "=", "java.lang.String", t0.rule_0_Id, "Special Event");
776 PropositionDefinition.Builder propBuilderS2 = PropositionDefinition.Builder.create(propS2);
777
778
779 PropositionDefinition propS1 = createTestSimpleProposition(
780 t0.namespaceName, "S1", "Account", "54321", "=", "java.lang.String", t0.rule_0_Id, "Charged To Account");
781 PropositionDefinition.Builder propBuilderS1 = PropositionDefinition.Builder.create(propS1);
782
783
784 KrmsTypeDefinition krmsTypeDefinition = createKrmsTypeDefinition(null, t0.namespaceName, "proposition", "testTypeService");
785 PropositionDefinition.Builder propBuilderC1 = PropositionDefinition.Builder.create(
786 null,PropositionType.COMPOUND.getCode(),t0.rule_0_Id,null,new ArrayList<PropositionParameter.Builder>());
787 propBuilderC1.compoundOpCode(LogicalOperator.AND.getCode());
788 List<PropositionDefinition.Builder> compoundComponentsC1 = new ArrayList<PropositionDefinition.Builder>();
789 compoundComponentsC1.add(propBuilderS1);
790 compoundComponentsC1.add(propBuilderS2);
791 propBuilderC1.setCompoundComponents(compoundComponentsC1);
792 propBuilderC1.setDescription("C1_compound_proposition");
793 propBuilderC1.setTypeId(krmsTypeDefinition.getId());
794
795 return ruleManagementService.createProposition(propBuilderC1.build());
796 }
797
798 protected KrmsTypeDefinition createKrmsActionTypeDefinition(String nameSpace) {
799 String ACTION_TYPE_NAME = "KrmsActionResolverType";
800 KrmsTypeDefinition krmsActionTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, ACTION_TYPE_NAME);
801
802 if (krmsActionTypeDefinition == null) {
803 KrmsTypeDefinition.Builder krmsActionTypeDefnBuilder = KrmsTypeDefinition.Builder.create(ACTION_TYPE_NAME, nameSpace);
804 krmsActionTypeDefnBuilder.setServiceName("testActionTypeService");
805 krmsActionTypeDefinition = krmsTypeRepository.createKrmsType(krmsActionTypeDefnBuilder.build());
806 }
807
808 return krmsActionTypeDefinition;
809 }
810 }