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