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  
17  package org.kuali.rice.core.data;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.sql.Timestamp;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.junit.Test;
30  import org.kuali.rice.core.test.CORETestCase;
31  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
32  import org.kuali.rice.coreservice.api.component.Component;
33  import org.kuali.rice.coreservice.api.namespace.Namespace;
34  import org.kuali.rice.coreservice.api.parameter.Parameter;
35  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
36  import org.kuali.rice.coreservice.api.style.Style;
37  import org.kuali.rice.coreservice.impl.component.ComponentBo;
38  import org.kuali.rice.coreservice.impl.component.ComponentId;
39  import org.kuali.rice.coreservice.impl.component.ComponentSetBo;
40  import org.kuali.rice.coreservice.impl.component.DerivedComponentBo;
41  import org.kuali.rice.coreservice.impl.namespace.NamespaceBo;
42  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
43  import org.kuali.rice.coreservice.impl.parameter.ParameterTypeBo;
44  import org.kuali.rice.coreservice.impl.style.StyleBo;
45  import org.kuali.rice.krad.data.PersistenceOption;
46  import org.kuali.rice.krad.service.KRADServiceLocator;
47  
48  /**
49   * Tests to confirm JPA mapping for the Core Service module data objects
50   */
51  public class CoreServiceJpaDataTest extends CORETestCase {
52      public static final String DERIVED_COMPONENT_SET_ID = "DD:TSTKR";
53  
54      public static final String NAMESPACE = "KR-TST";
55      public static final String NAMESPACE_PREFIX = "KR-";
56      public static final String STYLE_ID = "1234";
57      public static final String STYLE_NAME = "TestCSS";
58  
59      private static final String APP_ID = "KR-TST";
60  
61      @Test
62      public void testNameSpaceBoDataObject() throws Exception{
63          assertTrue("NameSpaceBo is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(NamespaceBo.class));
64          setupNameSpaceBoDataObjectAndSave();
65  
66          NamespaceBo namespaceBoFetched = KRADServiceLocator.getDataObjectService().find(NamespaceBo.class,"KR-TST");
67          assertTrue("Namespace BO refetched after save",
68                  namespaceBoFetched != null && StringUtils.equals(namespaceBoFetched.getName(),
69                          "Kuali Rice Test Namespace"));
70      }
71  
72      @Test
73      public void testComponentBoDataObject() throws Exception{
74          assertTrue("ComponentBo is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(ComponentBo.class));
75          setupNameSpaceBoDataObjectAndSave();
76          setupComponentBoDataObjectAndSave();
77  
78          ComponentBo componentBoFetched = KRADServiceLocator.getDataObjectService().find(ComponentBo.class,new ComponentId("KR-TST","All"));
79          assertTrue("ComponentBo refetched after save", componentBoFetched != null &&
80                  StringUtils.equals(componentBoFetched.getName(), "All"));
81          assertTrue("Campus Type Bo fetched from Campus BO relationship", componentBoFetched.getNamespace() != null
82                  && StringUtils.equals(componentBoFetched.getNamespace().getName(), "Kuali Rice Test Namespace"));
83  
84      }
85  
86      @Test
87      public void testComponentSetBoDataObject() throws Exception{
88          assertTrue("ComponentSetBo is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(ComponentSetBo.class));
89          setupComponentSetBoDataObjectAndSave();
90  
91          ComponentSetBo componentSetBo = KRADServiceLocator.getDataObjectService().find(ComponentSetBo.class,"DD:RICETST");
92          assertTrue("ComponentBo refetched after save", componentSetBo != null &&
93                  StringUtils.equals(componentSetBo.getChecksum(), "ASDFQWER"));
94      }
95  
96      @Test
97      public void testDerivedComponentBoDataObject() throws Exception{
98          assertTrue("DerivedComponentBo is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(DerivedComponentBo.class));
99          setupDerivedComponentBoDataObjectAndSave();
100 
101         DerivedComponentBo derivedComponentBoFetched = KRADServiceLocator.getDataObjectService().find(DerivedComponentBo.class,new ComponentId("KR-TST","ComponentBo"));
102         assertTrue("DerivedComponentBo refetched after save", derivedComponentBoFetched != null &&
103                 StringUtils.equals(derivedComponentBoFetched.getCode(), "ComponentBo"));
104     }
105 
106     @Test
107     public void testParameterTypeBoDataObject() throws Exception{
108         assertTrue("ParameterTypeBo is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(ParameterTypeBo.class));
109         setupParameterTypeBoDataObjectAndSave();
110 
111         ParameterTypeBo parameterTypeBoFetched = KRADServiceLocator.getDataObjectService().find(ParameterTypeBo.class,"PTBO");
112         assertTrue("ParameterTypeBo refetched after save", parameterTypeBoFetched != null &&
113                 StringUtils.equals(parameterTypeBoFetched.getName(),"Parameter Type"));
114     }
115 
116     @Test
117     public void testStyleBoDataObject() throws Exception{
118         assertTrue("StyleBO is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(StyleBo.class));
119         setupStyleBoDataObjectAndSave();
120 
121         StyleBo styleBo = KRADServiceLocator.getDataObjectService().find(StyleBo.class,"1234");
122         assertTrue("StyleBo refetched after save", styleBo != null &&
123                 StringUtils.equals(styleBo.getName(),"TestCSS"));
124     }
125 
126     @Test
127     public void testComponentServiceImpl() throws Exception{
128         setupNameSpaceBoDataObjectAndSave();
129         setupComponentBoDataObjectAndSave();
130         setupDerivedComponentBoDataObjectAndSave();
131 
132         Component component = CoreServiceApiServiceLocator.getComponentService().getComponentByCode("KR-TST","All");
133         assertTrue("ComponentBo refetched after save", component != null && StringUtils.equals(component.getCode(),
134                 "All"));
135 
136         List<Component> componentList = CoreServiceApiServiceLocator.getComponentService().
137                                     getAllComponentsByNamespaceCode("KR-TST");
138         assertTrue("getAllComponentsByNamespaceCode refetched after save", componentList != null && componentList.size() == 2);
139 
140         componentList = CoreServiceApiServiceLocator.getComponentService().
141                                     getActiveComponentsByNamespaceCode("KR-TST");
142         assertTrue("getActiveComponentsByNamespaceCode refetched after save", componentList != null && componentList.size() == 2);
143         componentList = CoreServiceApiServiceLocator.getComponentService().
144                             getDerivedComponentSet(DERIVED_COMPONENT_SET_ID);
145         assertTrue("getDerivedComponentSet refetched after save",componentList != null &&
146                             componentList.size() == 1);
147         DerivedComponentBo derivedComponentBo = DerivedComponentBo.from(componentList.get(0));
148         derivedComponentBo.setComponentSetId(null);
149         component = DerivedComponentBo.to(derivedComponentBo);
150         componentList = new ArrayList<Component>();
151         componentList.add(component);
152         CoreServiceApiServiceLocator.getComponentService().publishDerivedComponents("TEST",componentList);
153         componentList = CoreServiceApiServiceLocator.getComponentService().getDerivedComponentSet("TEST");
154         assertTrue("publishDerivedComponents corrected save",componentList != null &&
155                 componentList.size() == 1);
156     }
157 
158     @Test
159     public void testNamespaceServiceImpl_FindAllNamespaces() throws Exception{
160         setupNameSpaceBoDataObjectAndSave();
161 
162         List<Namespace> namespaceList = CoreServiceApiServiceLocator.getNamespaceService().
163                                             findAllNamespaces();
164         assertNotNull("findAllNamespaces returned null", namespaceList);
165         assertTrue( "findAllNamespaces did not return enough rows: " + namespaceList, namespaceList.size() >= 2 );
166     }
167 
168     @Test
169     public void testNamespaceServiceImpl_getNamespace() throws Exception{
170         setupNameSpaceBoDataObjectAndSave();
171 
172         Namespace namespace = CoreServiceApiServiceLocator.getNamespaceService().getNamespace(NAMESPACE);
173 
174         assertNotNull("getNamespace returned null", namespace);
175         assertEquals("getNamespace retrieved wrong namespace: " + namespace,NAMESPACE,namespace.getCode());
176 
177         namespace = CoreServiceApiServiceLocator.getNamespaceService().getNamespace(NAMESPACE_PREFIX+"*");
178         assertNull("getNamespace should have returned null for a search criteria of " + NAMESPACE_PREFIX + "*", namespace);
179     }
180 
181     @Test
182     public void testStyleServiceImpl() throws Exception{
183         setupStyleBoDataObjectAndSave();
184 
185         List<String> styleNames = CoreServiceApiServiceLocator.getStyleService().getAllStyleNames();
186         assertTrue("getAllStyleNames retrieved correctly", styleNames != null && styleNames.size() == 1);
187 
188         Style style = CoreServiceApiServiceLocator.getStyleService().getStyle(STYLE_NAME);
189         assertTrue("getStyle retrieved correctly", style != null && StringUtils.equals(STYLE_NAME,style.getName()));
190 
191         StyleBo styleBo = new StyleBo();
192         styleBo.setActive(true);
193         styleBo.setId(STYLE_ID + "23");
194         styleBo.setName(STYLE_NAME + "_NEW");
195         styleBo.setXmlContent("<xml>something_new</xml>");
196         style = StyleBo.to(styleBo);
197         CoreServiceApiServiceLocator.getStyleService().saveStyle(style);
198         style = CoreServiceApiServiceLocator.getStyleService().getStyle(STYLE_NAME+"_NEW");
199         assertTrue("getStyle retrieved correctly", style != null && StringUtils.equals(STYLE_NAME+"_NEW",style.getName()));
200     }
201 
202     @Test
203     public void testParameterServiceImpl() throws Exception{
204         setupParameterBoDataObjectAndSave();
205         ParameterKey parameterKey = ParameterKey.create(APP_ID, "TST_NM_SPACE", "TST", "TST_PARAM");
206 
207         Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().
208                 getParameter(parameterKey);
209         assertTrue("Parameter fetched correctly after save", parameter != null &&
210                 StringUtils.equals(parameter.getName(),"TST_PARAM"));
211         String value = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameterValueAsString(parameterKey);
212         assertTrue("Parameter value fetched correctly", StringUtils.equals(parameter.getValue(),value));
213         ParameterBo modifiedParam = ParameterBo.from(parameter);
214         modifiedParam.setValue("new value");
215         CoreServiceApiServiceLocator.getParameterRepositoryService().updateParameter(ParameterBo.to(modifiedParam));
216         value = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameterValueAsString(parameterKey);
217         assertTrue("Parameter value fetched correctly", StringUtils.equals(modifiedParam.getValue(),value));
218     }
219 
220     private void setupParameterBoDataObjectAndSave(){
221         NamespaceBo namespaceBo = new NamespaceBo();
222         namespaceBo.setActive(true);
223         namespaceBo.setApplicationId("RICE");
224         namespaceBo.setCode("TST_NM_SPACE");
225         namespaceBo.setName("Another Test Namespace");
226         namespaceBo = KRADServiceLocator.getDataObjectService().save(namespaceBo, PersistenceOption.FLUSH);
227 
228         ParameterTypeBo parameterTypeBo = new ParameterTypeBo();
229         parameterTypeBo.setActive(true);
230         parameterTypeBo.setCode("PT");
231         parameterTypeBo.setName("Another Test Parameter Type");
232         parameterTypeBo = KRADServiceLocator.getDataObjectService().save(parameterTypeBo, PersistenceOption.FLUSH);
233 
234         ParameterBo parameterBo = new ParameterBo();
235         parameterBo.setApplicationId(APP_ID);
236         parameterBo.setValue("blah");
237         parameterBo.setDescription("descr");
238         parameterBo.setParameterTypeCode("PT");
239         parameterBo.setParameterType(parameterTypeBo);
240         parameterBo.setComponentCode("TST");
241         parameterBo.setNamespaceCode("TST_NM_SPACE");
242         parameterBo.setNamespace(namespaceBo);
243         parameterBo.setEvaluationOperatorCode("A");
244         parameterBo.setName("TST_PARAM");
245         CoreServiceApiServiceLocator.getParameterRepositoryService().
246                     createParameter(ParameterBo.to(parameterBo));
247 
248     }
249 
250     private void setupStyleBoDataObjectAndSave(){
251         StyleBo styleBo = new StyleBo();
252         styleBo.setActive(true);
253         styleBo.setId("1234");
254         styleBo.setName(STYLE_NAME);
255         styleBo.setXmlContent("<xml>something</xml>");
256 
257         KRADServiceLocator.getDataObjectService().save(styleBo, PersistenceOption.FLUSH);
258 
259     }
260 
261     private void setupParameterTypeBoDataObjectAndSave(){
262         ParameterTypeBo parameterTypeBo = new ParameterTypeBo();
263         parameterTypeBo.setActive(true);
264         parameterTypeBo.setCode("PTBO");
265         parameterTypeBo.setName("Parameter Type");
266         parameterTypeBo.setVersionNumber(null);
267 
268         KRADServiceLocator.getDataObjectService().save(parameterTypeBo, PersistenceOption.FLUSH);
269     }
270 
271     private void setupDerivedComponentBoDataObjectAndSave(){
272           DerivedComponentBo derivedComponentBo = new DerivedComponentBo();
273           derivedComponentBo.setCode("ComponentBo");
274           derivedComponentBo.setComponentSetId(DERIVED_COMPONENT_SET_ID);
275           derivedComponentBo.setName("Derived component");
276           derivedComponentBo.setNamespaceCode("KR-TST");
277 
278         KRADServiceLocator.getDataObjectService().save(derivedComponentBo, PersistenceOption.FLUSH);
279     }
280 
281     private void setupComponentSetBoDataObjectAndSave(){
282         ComponentSetBo componentSetBo = new ComponentSetBo();
283         componentSetBo.setChecksum("ASDFQWER");
284         componentSetBo.setComponentSetId("DD:RICETST");
285         componentSetBo.setLastUpdateTimestamp(new Timestamp(System.currentTimeMillis()));
286         componentSetBo.setVersionNumber(null);
287 
288         KRADServiceLocator.getDataObjectService().save(componentSetBo, PersistenceOption.FLUSH);
289     }
290 
291     private void setupComponentBoDataObjectAndSave(){
292         ComponentBo componentBo = new ComponentBo();
293         componentBo.setActive(true);
294         componentBo.setCode("All");
295         componentBo.setName("All");
296         componentBo.setNamespaceCode("KR-TST");
297         componentBo.setNamespace(KRADServiceLocator.getDataObjectService().find(NamespaceBo.class,"KR-TST"));
298         KRADServiceLocator.getDataObjectService().save(componentBo, PersistenceOption.FLUSH);
299     }
300 
301     private void setupNameSpaceBoDataObjectAndSave(){
302         NamespaceBo namespaceBo = new NamespaceBo();
303         namespaceBo.setActive(true);
304         namespaceBo.setApplicationId("RICE");
305         namespaceBo.setCode("KR-TST");
306         namespaceBo.setName("Kuali Rice Test Namespace");
307 
308         KRADServiceLocator.getDataObjectService().save(namespaceBo, PersistenceOption.FLUSH);
309 
310         namespaceBo = new NamespaceBo();
311         namespaceBo.setActive(true);
312         namespaceBo.setApplicationId("OTH");
313         namespaceBo.setCode("KR-OTH");
314         namespaceBo.setName("Kuali Other");
315 
316         KRADServiceLocator.getDataObjectService().save(namespaceBo, PersistenceOption.FLUSH);
317 
318     }
319 }