View Javadoc

1   /*
2    * Copyright 2006-2013 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.kuali.rice.krad.util.ObjectUtils;
21  import org.kuali.rice.krms.api.repository.action.ActionDefinition;
22  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
23  import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
24  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
25  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
26  import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
27  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
28  import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
29  import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
30  import org.kuali.rice.krms.api.repository.proposition.PropositionType;
31  import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding;
32  import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
33  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
34  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
35  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
36  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
37  import org.kuali.rice.krms.impl.repository.NaturalLanguageTemplateBoServiceImpl;
38  import org.kuali.rice.krms.impl.repository.NaturalLanguageTemplateIntegrationGenTest;
39  import org.kuali.rice.krms.impl.repository.RuleManagementServiceImpl;
40  import org.kuali.rice.test.BaselineTestCase;
41  
42  import java.util.ArrayList;
43  import java.util.HashMap;
44  import java.util.List;
45  import java.util.Map;
46  
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertNull;
49  import static org.junit.Assert.assertTrue;
50  
51  /**
52   * Base test case and methods for testing RuleManagementServiceImpl
53   */
54  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
55  public class RuleManagementBaseTest extends AbstractAgendaBoTest{
56  
57      //public static final String TEST_PREFIX = "RuleManagementIT";
58      RuleManagementServiceImpl ruleManagementServiceImpl;
59  
60      protected String CLASS_DISCRIMINATOR = "0";
61  
62      @Override
63      @Before
64      public void setup() {
65          ruleManagementServiceImpl = new RuleManagementServiceImpl();
66          NaturalLanguageTemplateBoServiceImpl naturalLanguageTemplateBoServiceImpl = new NaturalLanguageTemplateBoServiceImpl();
67          naturalLanguageTemplateBoServiceImpl.setNaturalLanguageTemplater(
68                  NaturalLanguageTemplateIntegrationGenTest.newStringReplaceTemplater());
69          ruleManagementServiceImpl.setNaturalLanguageTemplateBoService(naturalLanguageTemplateBoServiceImpl);
70          ruleManagementServiceImpl.setBusinessObjectService(getBoService()); // Business Object Service gets set to other Services
71          termBoService = KrmsRepositoryServiceLocator.getTermBoService();
72  
73          contextRepository = KrmsRepositoryServiceLocator.getContextBoService();
74          krmsTypeRepository = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
75          ruleBoService = KrmsRepositoryServiceLocator.getRuleBoService();
76          agendaBoService = KrmsRepositoryServiceLocator.getAgendaBoService();
77          actionBoService = KrmsRepositoryServiceLocator.getBean("actionBoService");
78          functionBoService = KrmsRepositoryServiceLocator.getBean("functionRepositoryService");
79          krmsAttributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
80      }
81  
82      /**
83       * setClassDiscriminator needs to be run before tests to ensure uniqueness of test objects within a class
84       *
85       *  Should be overridden by each class which extends this class
86       *
87       *  Test object naming is comprised of class, test and object uniqueness discriminators.
88       *  The Class Discriminator needs to be set before any tests.
89       *  The Test Discriminator needs to be set at the start of each test. ex: setTestObjectNames(testDiscriminator)
90       *  The Object Discriminators are Sequential (object0, OBJECT1 ...)
91       */
92      @Before
93      public void setClassDiscriminator() {
94          // set a unique discriminator for test objects of this class should be uniquely set by each extending class
95          CLASS_DISCRIMINATOR = "BaseTest";
96      }
97  
98      protected RuleDefinition createTestRule(String namespace, String ruleId) {
99          return ruleManagementServiceImpl.createRule(newTestRuleDefinition(namespace, ruleId));
100     }
101 
102     protected RuleDefinition newTestRuleDefinition(String namespace, String objectDiscriminator) {
103         String ruleId = "RuleId" + objectDiscriminator;
104         PropositionDefinition prop = createTestPropositionForRule(objectDiscriminator);
105         PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(prop);
106         RuleDefinition.Builder builder = RuleDefinition.Builder.create(ruleId, "RuleName" + objectDiscriminator, namespace, null, prop.getId());
107         builder.setProposition(propBuilder);
108 
109         return builder.build();
110     }
111 
112     protected AgendaItemDefinition newTestAgendaItemDefinition(String id, String agendaId, String ruleId) {
113         AgendaItemDefinition.Builder agendaItemDefinitionBuilder = AgendaItemDefinition.Builder.create(id, agendaId);
114         agendaItemDefinitionBuilder.setRuleId(ruleId);
115 
116         return agendaItemDefinitionBuilder.build();
117     }
118 
119     protected ActionDefinition createTestActions(String actionId, String actionName, String actionDescr, int actionSequence, String ruleId, String namespace) {
120         KrmsTypeDefinition krmsTypeDefinition = createKrmsActionTypeDefinition(namespace);
121 
122         // create rule if it does not exist
123         if(ruleManagementServiceImpl.getRule(ruleId) == null) {
124             RuleDefinition ruleDefinition = createTestRule(namespace, ruleId);
125             ruleId = ruleDefinition.getId();
126         }
127 
128         ActionDefinition actionDefinition = ActionDefinition.Builder.create(actionId,actionName,
129                 NAMESPACE1,krmsTypeDefinition.getId(),ruleId,actionSequence).build();
130 
131         assertNull("action[" + actionId + "] should not already be in database:", ruleManagementServiceImpl.getAction(actionId ));
132 
133         actionDefinition =  ruleManagementServiceImpl.createAction(actionDefinition);
134         ActionDefinition returnActionDefinition = ruleManagementServiceImpl.getAction(actionDefinition.getId());
135         ActionDefinition.Builder builder = ActionDefinition.Builder.create(returnActionDefinition);
136         builder.setDescription(actionDescr);
137         ruleManagementServiceImpl.updateAction(builder.build());
138 
139         return ruleManagementServiceImpl.getAction(actionDefinition.getId());
140     }
141 
142     protected PropositionDefinition createTestPropositionForRule(String objectDiscriminator) {
143         KrmsTypeDefinition krmsTypeDefinition = createKrmsActionTypeDefinition("Namespace" + objectDiscriminator);
144         String ruleId = "RuleId" + objectDiscriminator;
145         String propId = "P" + objectDiscriminator;
146 
147         return createTestSimpleProposition("Namespace" + objectDiscriminator, propId, "TSI_" + propId, "ABC", "=", "java.lang.String", ruleId,
148                 "TSI_" + propId + "_Descr");
149     }
150 
151     protected PropositionDefinition createTestSimpleProposition(String namespace, String propId, String termSpecId, String propConstant, String propOperator, String termSpecType, String ruleId, String termSpecDescr){
152         TermSpecificationDefinition termSpecificationDefinition = createTestTermSpecification(termSpecId, termSpecId, namespace, termSpecType, termSpecDescr);
153         KrmsTypeDefinition krmsTypeDefinition = createKrmsTypeDefinition(namespace, termSpecId, "testTypeService");
154 
155         List<PropositionParameter.Builder> propParam =  new ArrayList<PropositionParameter.Builder>();
156         propParam.add(PropositionParameter.Builder.create(propId + "_0", "unused_notnull", termSpecId, PropositionParameterType.TERM.getCode(), 0));
157         propParam.add(PropositionParameter.Builder.create(propId + "_1", "unused_notnull", propConstant, PropositionParameterType.CONSTANT.getCode(), 1));
158         propParam.add(PropositionParameter.Builder.create(propId + "_2", "unused_notnull", propOperator, PropositionParameterType.OPERATOR.getCode(), 2));
159         PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(null, PropositionType.SIMPLE.getCode(), ruleId, krmsTypeDefinition.getId(), propParam);
160         propBuilder.setDescription(propId + "_simple_proposition");
161 
162         return ruleManagementServiceImpl.createProposition(propBuilder.build());
163     }
164 
165     protected TermSpecificationDefinition createTestTermSpecification(String termSpecId, String termSpecName, String namespace, String type, String termSpecDescr){
166         TermSpecificationDefinition termSpecificationDefinition = termBoService.getTermSpecificationByNameAndNamespace(termSpecName,namespace);
167 
168         if(termSpecificationDefinition != null) {
169 
170             return termSpecificationDefinition;
171         }
172 
173         TermSpecificationDefinition.Builder termSpecificationDefinitionBuilder =
174                 TermSpecificationDefinition.Builder.create(null, termSpecName, namespace, type);
175         termSpecificationDefinitionBuilder.setDescription(termSpecDescr);
176         termSpecificationDefinition = termSpecificationDefinitionBuilder.build();
177 
178         return termBoService.createTermSpecification(termSpecificationDefinition);
179     }
180 
181     protected KrmsTypeDefinition createKrmsTypeDefinition(String nameSpace, String typeName, String serviceName) {
182         KrmsTypeDefinition krmsTypeDefinition =  krmsTypeRepository.getTypeByName(nameSpace, typeName);
183 
184         if (krmsTypeDefinition == null) {
185             KrmsTypeDefinition.Builder krmsTypeDefnBuilder = KrmsTypeDefinition.Builder.create(typeName, nameSpace);
186             krmsTypeDefnBuilder.setServiceName(serviceName);
187             krmsTypeDefinition = krmsTypeRepository.createKrmsType(krmsTypeDefnBuilder.build());
188         }
189 
190         return krmsTypeDefinition;
191     }
192 
193     // Context ("ContextId4000", "Namespace4000", "ContextName4000")
194     // Agenda  ("AgendaId4000", "AgendaName4000")
195     //    AgendaItem ("AI4000")
196     //        Rule ( TEST_PREFIX + "RuleId4000"
197     protected AgendaDefinition.Builder buildAgenda(String objectDiscriminator) {
198         String namespace =  "Namespace" + objectDiscriminator;
199         // create a context
200         ContextDefinition.Builder contextDefinitionBuilder = ContextDefinition.Builder.create(
201                 namespace, "ContextName" + objectDiscriminator);
202         contextDefinitionBuilder.setId("ContextId" + objectDiscriminator);
203         ContextDefinition contextDefinition = contextDefinitionBuilder.build();
204         contextDefinition = ruleManagementServiceImpl.createContext(contextDefinition );
205 
206         // create an agenda ( a agendaItem cannot be created without an existing agenda.
207         AgendaDefinition.Builder agendaBuilder = AgendaDefinition.Builder.create(
208                 "AgendaId" + objectDiscriminator, "AgendaName" + objectDiscriminator, null, "ContextId" + objectDiscriminator);
209         AgendaDefinition agenda = agendaBuilder.build();
210         agenda = ruleManagementServiceImpl.createAgenda(agenda);
211 
212         // create a rule
213         RuleDefinition rule = ruleManagementServiceImpl.createRule(newTestRuleDefinition(namespace, objectDiscriminator));
214 
215         // create a agendaItem using the context agenda and rule above
216         AgendaItemDefinition agendaItem = newTestAgendaItemDefinition("AI" + objectDiscriminator, agenda.getId(), rule.getId());
217         agendaItem = ruleManagementServiceImpl.createAgendaItem(agendaItem);
218 
219         agendaBuilder = AgendaDefinition.Builder.create(ruleManagementServiceImpl.getAgenda("AgendaId" + objectDiscriminator));
220         String defaultAgendaItemId = agendaBuilder.getFirstItemId();
221         agendaBuilder.setFirstItemId("AI" + objectDiscriminator);
222         ruleManagementServiceImpl.updateAgenda(agendaBuilder.build());
223 
224         // clean up default agendaItem created by createAgenda
225         ruleManagementServiceImpl.deleteAgendaItem(defaultAgendaItemId);
226 
227         return AgendaDefinition.Builder.create(ruleManagementServiceImpl.getAgenda(agenda.getId()));
228     }
229 
230     protected AgendaDefinition.Builder buildComplexAgenda(RuleManagementBaseTestObjectNames names) {
231         String namespace = "Namespace" + names.discriminator;
232         return buildComplexAgenda(namespace,"AGENDA",names);
233     }
234 
235     /**
236      *   buildComplexAgenda builds a "complex" agenda object as shown here.
237      *
238      *           // agenda
239                  //   agendaItem0 ( rule0)
240                  //       WhenTrue   agendaItem1( rule1 )
241                  //       WhenFalse  agendaItem2( rule2 )
242                  //       Always     agendaItem3( rule3 )
243                  //   agendaItem1 ( rule1 )
244                  //       Always     agendaItem5
245                  //   agendaItem2 ( rule2 )
246                  //       WhenFalse  agendaItem4
247                  //       Always     agendaItem6
248                  //   agendaItem3 ( rule3 )
249                  //   agendaItem4 ( rule4 )
250                  //   agendaItem5 ( rule5 )
251                  //   agendaItem6 ( rule6 )
252      *
253      * @param namespace of the KRMS Agenda to be created
254      * @param namespaceType of the namepace passed (Namespace will be created if it does not exist.)
255      * @param {@link RuleManagementBaseTestObjectNames}  Unique discriminator names to base created Agenda upon
256      * @return {@link AgendaDefinition.Builder} populated from the built agenda
257      */
258     protected AgendaDefinition.Builder buildComplexAgenda(String namespace, String namespaceType, RuleManagementBaseTestObjectNames names) {
259         // create a context
260         ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create(
261                 namespace, names.contextName);
262         contextBuilder.setId(names.contextId);
263         ContextDefinition context = contextBuilder.build();
264         ruleManagementServiceImpl.createContext(context );
265 
266         // create krms type AGENDA
267         createKrmsTypeDefinition(namespace, namespaceType, null);
268 
269         // create a agenda
270         AgendaDefinition.Builder agendaBuilder = AgendaDefinition.Builder.create(
271                 names.agenda_Id, names.agenda_Name, namespaceType, names.contextId);
272         AgendaDefinition agenda = agendaBuilder.build();
273         agenda = ruleManagementServiceImpl.createAgenda(agenda);
274 
275         // create rules
276         RuleDefinition rule0 = ruleManagementServiceImpl.createRule(newTestRuleDefinition(namespace, names.object0));
277         RuleDefinition rule1 = ruleManagementServiceImpl.createRule(newTestRuleDefinition(namespace, names.object1));
278         RuleDefinition rule2 = ruleManagementServiceImpl.createRule(newTestRuleDefinition(namespace, names.object2));
279         RuleDefinition rule3 = ruleManagementServiceImpl.createRule(newTestRuleDefinition(namespace, names.object3));
280         RuleDefinition rule4 = ruleManagementServiceImpl.createRule(newTestRuleDefinition(namespace, names.object4));
281         RuleDefinition rule5 = ruleManagementServiceImpl.createRule(newTestRuleDefinition(namespace, names.object5));
282         RuleDefinition rule6 = ruleManagementServiceImpl.createRule(newTestRuleDefinition(namespace, names.object6));
283 
284         // create agendaItems
285         AgendaItemDefinition agendaItemOBJECT6 = newTestAgendaItemDefinition(names.agendaItem_6_Id, agenda.getId(), rule6.getId());
286         AgendaItemDefinition.Builder itemBuilderOBJECT6 = AgendaItemDefinition.Builder.create(agendaItemOBJECT6);
287         itemBuilderOBJECT6 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.createAgendaItem(itemBuilderOBJECT6.build()));
288 
289         AgendaItemDefinition agendaItemOBJECT5 = newTestAgendaItemDefinition(names.agendaItem_5_Id, agenda.getId(), rule5.getId());
290         AgendaItemDefinition.Builder itemBuilderOBJECT5 = AgendaItemDefinition.Builder.create(agendaItemOBJECT5);
291         itemBuilderOBJECT5 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.createAgendaItem(itemBuilderOBJECT5.build()));
292 
293         AgendaItemDefinition agendaItemOBJECT4 = newTestAgendaItemDefinition(names.agendaItem_4_Id, agenda.getId(), rule4.getId());
294         AgendaItemDefinition.Builder itemBuilderOBJECT4 = AgendaItemDefinition.Builder.create(agendaItemOBJECT4);
295         itemBuilderOBJECT4 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.createAgendaItem(
296                 itemBuilderOBJECT4.build()));
297 
298         AgendaItemDefinition agendaItemOBJECT3 = newTestAgendaItemDefinition(names.agendaItem_3_Id, agenda.getId(), rule3.getId());
299         AgendaItemDefinition.Builder itemBuilderOBJECT3 = AgendaItemDefinition.Builder.create(agendaItemOBJECT3);
300         itemBuilderOBJECT3 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.createAgendaItem(
301                 itemBuilderOBJECT3.build()));
302 
303         AgendaItemDefinition agendaItemOBJECT2 = newTestAgendaItemDefinition(names.agendaItem_2_Id, agenda.getId(), rule2.getId());
304         AgendaItemDefinition.Builder itemBuilderOBJECT2 = AgendaItemDefinition.Builder.create(agendaItemOBJECT2);
305         itemBuilderOBJECT4 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.getAgendaItem(itemBuilderOBJECT4.getId()));
306         itemBuilderOBJECT2.setWhenFalse(itemBuilderOBJECT4);
307         itemBuilderOBJECT2.setWhenFalseId(itemBuilderOBJECT4.getId());
308         itemBuilderOBJECT6 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.getAgendaItem(itemBuilderOBJECT6.getId()));
309         itemBuilderOBJECT2.setAlways(itemBuilderOBJECT6);
310         itemBuilderOBJECT2.setAlwaysId(itemBuilderOBJECT6.getId());
311         itemBuilderOBJECT2 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.createAgendaItem(
312                 itemBuilderOBJECT2.build()));
313 
314         AgendaItemDefinition agendaItemOBJECT1 = newTestAgendaItemDefinition(names.agendaItem_1_Id, agenda.getId(), rule1.getId());
315         AgendaItemDefinition.Builder itemBuilderOBJECT1 = AgendaItemDefinition.Builder.create(agendaItemOBJECT1);
316         itemBuilderOBJECT5 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.getAgendaItem(itemBuilderOBJECT5.getId()));
317         itemBuilderOBJECT1.setAlways(itemBuilderOBJECT5);
318         itemBuilderOBJECT1.setAlwaysId(itemBuilderOBJECT5.getId());
319         itemBuilderOBJECT1 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.createAgendaItem(
320                 itemBuilderOBJECT1.build()));
321 
322         AgendaItemDefinition agendaItemOBJECT0 = newTestAgendaItemDefinition(names.agendaItem_0_Id, agenda.getId(), rule0.getId());
323         AgendaItemDefinition.Builder itemBuilderOBJECT0 = AgendaItemDefinition.Builder.create(agendaItemOBJECT0);
324         itemBuilderOBJECT1 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.getAgendaItem(itemBuilderOBJECT1.getId()));
325         itemBuilderOBJECT0.setWhenTrue(itemBuilderOBJECT1);
326         itemBuilderOBJECT0.setWhenTrueId(itemBuilderOBJECT1.getId());
327         itemBuilderOBJECT2 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.getAgendaItem(itemBuilderOBJECT2.getId()));
328         itemBuilderOBJECT0.setWhenFalse(itemBuilderOBJECT2);
329         itemBuilderOBJECT0.setWhenFalseId(itemBuilderOBJECT2.getId());
330         itemBuilderOBJECT3 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.getAgendaItem(itemBuilderOBJECT3.getId()));
331         itemBuilderOBJECT0.setAlways(itemBuilderOBJECT3);
332         itemBuilderOBJECT0.setAlwaysId(itemBuilderOBJECT3.getId());
333         itemBuilderOBJECT0 = AgendaItemDefinition.Builder.create(ruleManagementServiceImpl.createAgendaItem(itemBuilderOBJECT0.build()));
334 
335         agendaBuilder = AgendaDefinition.Builder.create(ruleManagementServiceImpl.getAgenda(agenda.getId()));
336         String defaultAgendaItemId = agendaBuilder.getFirstItemId();
337         agendaBuilder.setFirstItemId(itemBuilderOBJECT0.getId());
338         ruleManagementServiceImpl.updateAgenda(agendaBuilder.build());
339         ruleManagementServiceImpl.deleteAgendaItem(defaultAgendaItemId);
340         List<AgendaItemDefinition> agendaItems = ruleManagementServiceImpl.getAgendaItemsByContext(names.contextId);
341 
342         assertEquals("Invalid number of agendaItems created", 7, agendaItems.size());
343 
344         return AgendaDefinition.Builder.create(ruleManagementServiceImpl.getAgenda(agenda.getId()));
345     }
346 
347 
348     protected ReferenceObjectBinding.Builder buildReferenceObjectBinding(String objectDiscriminator) {
349         String namespace = "Namespace" + objectDiscriminator;
350         AgendaDefinition.Builder agendaBuilder = buildAgenda(objectDiscriminator);
351         // create krms type for AgendaOBJECT
352         KrmsTypeDefinition krmsTypeDefinition = createKrmsTypeDefinition(namespace, "AgendaType" + objectDiscriminator, null);
353 
354         return buildReferenceObjectBinding("ParkingPolicies", agendaBuilder.getId(), krmsTypeDefinition.getId(),
355                 namespace, "PA" + objectDiscriminator, "ParkingAffiliationType", true);
356     }
357 
358 
359     protected ReferenceObjectBinding.Builder buildReferenceObjectBinding(String collectionName, String krmsObjectId, String krmsDiscriminatorType,
360             String namespace, String referenceObjectId, String referenceDiscriminatorType, boolean active) {
361 
362         ReferenceObjectBinding.Builder refObjBindingBuilder = ReferenceObjectBinding.Builder.
363                 create(krmsDiscriminatorType, krmsObjectId, namespace, referenceDiscriminatorType,   referenceObjectId);
364         refObjBindingBuilder.setCollectionName(collectionName);
365         refObjBindingBuilder.setActive(active);
366 
367         return  ReferenceObjectBinding.Builder.create(ruleManagementServiceImpl.createReferenceObjectBinding(
368                 refObjBindingBuilder.build()));
369     }
370 
371     /**
372      * Used to build a NaturalLanguageTemplate in the database for testing.
373      *
374      * @param namespace of the KRMS Type which may be created
375      * @param langCode  The two character code of applicable language
376      * @param nlObjectName  Seed value to create unique Template and Usage records
377      * @param template The text of the template for the Template record
378      *
379      * @return {@link NaturalLanguageTemplate} for the created Template
380      */
381     protected NaturalLanguageTemplate buildTestNaturalLanguageTemplate(String namespace, String langCode, String nlObjectName, String template) {
382         KrmsTypeDefinition  krmsType = createKrmsTypeDefinition(namespace, nlObjectName, null);
383 
384         // create NaturalLanguageUsage if it does not exist
385         if (ObjectUtils.isNull(ruleManagementServiceImpl.getNaturalLanguageUsage("krms.nl." + nlObjectName))) {
386             NaturalLanguageUsage.Builder naturalLanguageUsageBuilder =  NaturalLanguageUsage.Builder.create(nlObjectName,namespace );
387             naturalLanguageUsageBuilder.setId("krms.nl." + nlObjectName);
388             naturalLanguageUsageBuilder.setDescription("Description-" + nlObjectName);
389             naturalLanguageUsageBuilder.setActive(true);
390             NaturalLanguageUsage naturalLangumageUsage =  ruleManagementServiceImpl.createNaturalLanguageUsage(naturalLanguageUsageBuilder.build());
391         }
392 
393         // create  NaturalLanguageTemplate attributes if they don't exist
394         createTestKrmsAttribute("TemplateAttribute1", "TemplateAttributeName1", namespace);
395         createTestKrmsAttribute("TemplateAttribute2", "TemplateAttributeName2", namespace);
396         Map<String, String> nlAttributes = new HashMap<String, String>();
397         nlAttributes.put("TemplateAttributeName1","value1");
398         nlAttributes.put("TemplateAttributeName2","value2");
399 
400 
401         // create NaturalLanguageTemplate
402         String naturalLanguageUsageId = "krms.nl." + nlObjectName;
403         String typeId = krmsType.getId();
404         NaturalLanguageTemplate.Builder naturalLanguageTemplateBuilder = NaturalLanguageTemplate.Builder.create(langCode,naturalLanguageUsageId,template,typeId);
405         naturalLanguageTemplateBuilder.setActive(true);
406         naturalLanguageTemplateBuilder.setId(langCode + "-" + nlObjectName);
407         naturalLanguageTemplateBuilder.setAttributes(nlAttributes);
408 
409         return ruleManagementServiceImpl.createNaturalLanguageTemplate(naturalLanguageTemplateBuilder.build());
410     }
411 
412     protected KrmsAttributeDefinition createTestKrmsAttribute(String id, String name, String namespace) {
413         // if the AttributeDefinition does not exist, create it
414         if (ObjectUtils.isNull(krmsAttributeDefinitionService.getAttributeDefinitionById(id))) {
415             KrmsAttributeDefinition.Builder krmsAttributeDefinitionBuilder = KrmsAttributeDefinition.Builder.create(id, name, namespace);
416             KrmsAttributeDefinition  krmsAttributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(krmsAttributeDefinitionBuilder.build());
417         }
418 
419         return krmsAttributeDefinitionService.getAttributeDefinitionById(id);
420     }
421 
422     protected NaturalLanguageUsage buildTestNaturalLanguageUsage(String namespace, String nlUsageName ) {
423         KrmsTypeDefinition  krmsType = createKrmsTypeDefinition(namespace, nlUsageName, null);
424 
425         // create NaturalLanguageUsage
426         NaturalLanguageUsage.Builder naturalLanguageUsageBuilder =  NaturalLanguageUsage.Builder.create(nlUsageName,namespace );
427         naturalLanguageUsageBuilder.setId("krms.nl." + nlUsageName);
428         naturalLanguageUsageBuilder.setDescription("Description-" + nlUsageName);
429         naturalLanguageUsageBuilder.setActive(true);
430 
431         return  ruleManagementServiceImpl.createNaturalLanguageUsage(naturalLanguageUsageBuilder.build());
432     }
433 
434     protected ContextDefinition buildTestContext(String objectDiscriminator) {
435         String namespace =  "Namespace" + objectDiscriminator;
436         // create a context
437         ContextDefinition.Builder contextDefinitionBuilder = ContextDefinition.Builder.create(
438                 namespace, "ContextName" + objectDiscriminator);
439         contextDefinitionBuilder.setId("ContextId" + objectDiscriminator);
440         ContextDefinition contextDefinition = contextDefinitionBuilder.build();
441 
442         return ruleManagementServiceImpl.createContext(contextDefinition );
443     }
444 }