View Javadoc

1   /**
2    * Copyright 2005-2014 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  package org.kuali.rice.krms.impl.repository;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.kuali.rice.krad.service.BusinessObjectService;
21  import org.kuali.rice.krms.api.repository.NaturalLanguageTemplateGenTest;
22  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
23  import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
24  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
25  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
26  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
27  
28  import java.util.Map;
29  
30  import static org.mockito.Mockito.mock;
31  
32  /**
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   * 
35   */
36  public final class NaturalLanguageTemplateBoServiceImplGenTest {
37  
38      NaturalLanguageTemplateBoServiceImpl naturalLanguageTemplateBoServiceImpl;
39      NaturalLanguageTemplate naturalLanguageTemplate;
40      KrmsAttributeDefinitionService krmsAttributeDefinitionService;
41  
42      NaturalLanguageTemplate getNaturalLanguageTemplate() {
43          return naturalLanguageTemplate;
44      }
45  
46      public void setNaturalLanguageTemplateBoServiceImpl(NaturalLanguageTemplateBoServiceImpl impl) {
47          this.naturalLanguageTemplateBoServiceImpl = impl;
48      }
49  
50      public void setKrmsAttributeDefinitionService(KrmsAttributeDefinitionService impl) {
51          krmsAttributeDefinitionService = impl;
52      }
53  
54      public static org.kuali.rice.krms.impl.repository.NaturalLanguageTemplateBoServiceImplGenTest create(
55              NaturalLanguageTemplateBoServiceImpl nlTemplateBoService,
56              KrmsAttributeDefinitionService attributeDefService) {
57  
58          org.kuali.rice.krms.impl.repository.NaturalLanguageTemplateBoServiceImplGenTest test =
59                  new org.kuali.rice.krms.impl.repository.NaturalLanguageTemplateBoServiceImplGenTest();
60          test.setKrmsAttributeDefinitionService(attributeDefService);
61          test.setNaturalLanguageTemplateBoServiceImpl(nlTemplateBoService);
62          return test;
63      }
64  
65      @Before
66      public void setUp() {
67          naturalLanguageTemplateBoServiceImpl = new NaturalLanguageTemplateBoServiceImpl();
68          KrmsAttributeDefinitionService mockAttributeService = mock(KrmsAttributeDefinitionService.class);
69          NaturalLanguageTemplateBo.setAttributeDefinitionService(mockAttributeService);
70          KrmsTypeRepositoryService mockTypeRepositoryService = mock(KrmsTypeRepositoryService.class);
71          NaturalLanguageTemplateBo.setTypeRepositoryService(mockTypeRepositoryService);
72          naturalLanguageTemplateBoServiceImpl.setAttributeDefinitionService(mockAttributeService);
73          naturalLanguageTemplateBoServiceImpl.setBusinessObjectService(mock(BusinessObjectService.class));
74      }
75  
76      @Test(expected = java.lang.IllegalArgumentException.class)
77      public void test_getNaturalLanguageTemplatesByAttributes_null_fail() {
78          naturalLanguageTemplateBoServiceImpl.findNaturalLanguageTemplatesByAttributes(null);
79      }
80  
81      @Test(expected = java.lang.IllegalArgumentException.class)
82      public void test_getNaturalLanguageTemplatesByLanguageCode_null_fail() {
83          naturalLanguageTemplateBoServiceImpl.findNaturalLanguageTemplatesByLanguageCode(null);
84      }
85  
86      @Test(expected = java.lang.IllegalArgumentException.class)
87      public void test_getNaturalLanguageTemplatesByNaturalLanguageUsage_null_fail() {
88          naturalLanguageTemplateBoServiceImpl.findNaturalLanguageTemplatesByNaturalLanguageUsage(null);
89      }
90  
91      @Test(expected = java.lang.IllegalArgumentException.class)
92      public void test_getNaturalLanguageTemplatesByType_null_fail() {
93          naturalLanguageTemplateBoServiceImpl.findNaturalLanguageTemplatesByType(null);
94      }
95  
96      @Test(expected = java.lang.IllegalArgumentException.class)
97      public void test_getNaturalLanguageTemplatesByTemplate_null_fail() {
98          naturalLanguageTemplateBoServiceImpl.findNaturalLanguageTemplatesByTemplate(null);
99      }
100 
101     @Test
102     public void test_from_null_yields_null() {
103         assert(naturalLanguageTemplateBoServiceImpl.from(null) == null);
104     }
105 
106     @Test
107     public void test_from() {
108         NaturalLanguageTemplate def = NaturalLanguageTemplateGenTest.buildFullNaturalLanguageTemplate();
109         NaturalLanguageTemplateBo naturalLanguageTemplateBo = naturalLanguageTemplateBoServiceImpl.from(def);
110         assert(naturalLanguageTemplateBo.getLanguageCode().equals(def.getLanguageCode()));
111         assert(naturalLanguageTemplateBo.getNaturalLanguageUsageId().equals(def.getNaturalLanguageUsageId()));
112         assert(naturalLanguageTemplateBo.getTypeId().equals(def.getTypeId()));
113         assert(naturalLanguageTemplateBo.getTemplate().equals(def.getTemplate()));
114         assert(naturalLanguageTemplateBo.getId().equals(def.getId()));
115     }
116 
117     @Test
118     public void test_to() {
119         NaturalLanguageTemplate def = NaturalLanguageTemplateGenTest.buildFullNaturalLanguageTemplate();
120         NaturalLanguageTemplateBo naturalLanguageTemplateBo = naturalLanguageTemplateBoServiceImpl.from(def);
121         NaturalLanguageTemplate def2 = NaturalLanguageTemplateBo.to(naturalLanguageTemplateBo);
122         assert(def.equals(def2));
123     }
124 
125     @Test
126     public void test_createNaturalLanguageTemplate() {
127         NaturalLanguageTemplate def = NaturalLanguageTemplateGenTest.buildFullNaturalLanguageTemplate();
128         naturalLanguageTemplate = naturalLanguageTemplateBoServiceImpl.createNaturalLanguageTemplate(def);
129     }
130 
131     @Test(expected = java.lang.IllegalArgumentException.class)
132     public void test_createNaturalLanguageTemplate_null_fail() {
133         naturalLanguageTemplateBoServiceImpl.createNaturalLanguageTemplate(null);
134     }
135 
136     @Test(expected = java.lang.IllegalArgumentException.class)
137     public void test_updateNaturalLanguageTemplate_null_fail() {
138         naturalLanguageTemplateBoServiceImpl.updateNaturalLanguageTemplate(null);
139     }
140 
141     @Test(expected = java.lang.IllegalArgumentException.class)
142     public void test_deleteNaturalLanguageTemplate_null_fail() {
143         naturalLanguageTemplateBoServiceImpl.deleteNaturalLanguageTemplate(null);
144     }
145 
146     void createNaturalLanguageTemplate(NaturalLanguageUsage naturalLanguageUsage, KrmsTypeDefinition type) {
147         NaturalLanguageTemplate def = NaturalLanguageTemplateGenTest.buildFullNaturalLanguageTemplate(naturalLanguageUsage, type);
148 
149         for (Map.Entry<String, String> attributeEntry : def.getAttributes().entrySet()) {
150             // check for template attribute definition, create if not there
151             KrmsAttributeDefinition attrDef = krmsAttributeDefinitionService.getAttributeDefinitionByNameAndNamespace(
152                     attributeEntry.getKey(), type.getNamespace());
153 
154           // rebuild attributes in all cases until Constraint Error found and corrected
155           //  if (null == attrDef) {
156                 KrmsAttributeDefinition.Builder attrDefBuilder =
157                         KrmsAttributeDefinition.Builder.create(null, attributeEntry.getKey(), type.getNamespace());
158                 krmsAttributeDefinitionService.createAttributeDefinition(attrDefBuilder.build());
159           //  }
160         }
161 
162         naturalLanguageTemplate = naturalLanguageTemplateBoServiceImpl.createNaturalLanguageTemplate(def);
163     }
164 
165 }