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 org.apache.commons.lang.StringUtils;
20  import org.junit.Test;
21  
22  import org.kuali.rice.core.test.CORETestCase;
23  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
24  import org.kuali.rice.coreservice.api.component.Component;
25  import org.kuali.rice.coreservice.api.namespace.Namespace;
26  import org.kuali.rice.coreservice.api.parameter.Parameter;
27  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
28  import org.kuali.rice.coreservice.api.style.Style;
29  import org.kuali.rice.coreservice.impl.component.ComponentBo;
30  import org.kuali.rice.coreservice.impl.component.ComponentId;
31  import org.kuali.rice.coreservice.impl.component.ComponentSetBo;
32  import org.kuali.rice.coreservice.impl.component.DerivedComponentBo;
33  import org.kuali.rice.coreservice.impl.namespace.NamespaceBo;
34  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
35  import org.kuali.rice.coreservice.impl.parameter.ParameterTypeBo;
36  import org.kuali.rice.coreservice.impl.style.StyleBo;
37  import org.kuali.rice.krad.data.KradDataServiceLocator;
38  import org.kuali.rice.krad.data.PersistenceOption;
39  import org.kuali.rice.krad.service.KRADServiceLocator;
40  import org.kuali.rice.test.BaselineTestCase;
41  
42  import java.sql.Timestamp;
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  import static org.junit.Assert.*;
47  
48  /**
49   * Tests to confirm JPA mapping for the Core Service module data objects
50   */
51  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
52  public class CoreServiceJpaDataTest extends CORETestCase {
53      public static final String DERIVED_COMPONENT_SET_ID = "DD:TSTKR";
54  
55      public static final String NAMESPACE = "KR-TST";
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,"HELP");
112         assertTrue("ParameterTypeBo refetched after save", parameterTypeBoFetched != null &&
113                 StringUtils.equals(parameterTypeBoFetched.getName(),"Help"));
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() throws Exception{
160          setupNameSpaceBoDataObjectAndSave();
161 
162         Namespace namespace = CoreServiceApiServiceLocator.getNamespaceService().getNamespace(NAMESPACE);
163         assertTrue("getNamespace retrieved after save",namespace != null &&
164                         StringUtils.equals(namespace.getCode(),NAMESPACE));
165         List<Namespace> namespaceList = CoreServiceApiServiceLocator.getNamespaceService().
166                                             findAllNamespaces();
167         assertTrue("findAllNamespaces retrieved after save",namespaceList != null &&
168                 namespaceList.size() == 2);
169     }
170 
171     @Test
172     public void testStyleServiceImpl() throws Exception{
173         setupStyleBoDataObjectAndSave();
174 
175         List<String> styleNames = CoreServiceApiServiceLocator.getStyleService().getAllStyleNames();
176         assertTrue("getAllStyleNames retrieved correctly", styleNames != null && styleNames.size() == 1);
177 
178         Style style = CoreServiceApiServiceLocator.getStyleService().getStyle(STYLE_NAME);
179         assertTrue("getStyle retrieved correctly", style != null && StringUtils.equals(STYLE_NAME,style.getName()));
180 
181         StyleBo styleBo = new StyleBo();
182         styleBo.setActive(true);
183         styleBo.setId(STYLE_ID + "23");
184         styleBo.setName(STYLE_NAME + "_NEW");
185         styleBo.setXmlContent("<xml>something_new</xml>");
186         style = StyleBo.to(styleBo);
187         CoreServiceApiServiceLocator.getStyleService().saveStyle(style);
188         style = CoreServiceApiServiceLocator.getStyleService().getStyle(STYLE_NAME+"_NEW");
189         assertTrue("getStyle retrieved correctly", style != null && StringUtils.equals(STYLE_NAME+"_NEW",style.getName()));
190     }
191 
192     @Test
193     public void testParameterServiceImpl() throws Exception{
194         setupParameterTypeBoDataObjectAndSave();
195         setupParameterBoDataObjectAndSave();
196         ParameterKey parameterKey = ParameterKey.create(APP_ID, "TST_NM_SPACE", "TST", "TST_PARAM");
197 
198         Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().
199                 getParameter(parameterKey);
200         assertTrue("Parameter fetched correctly after save", parameter != null &&
201                 StringUtils.equals(parameter.getName(),"TST_PARAM"));
202         String value = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameterValueAsString(parameterKey);
203         assertTrue("Parameter value fetched correctly", StringUtils.equals(parameter.getValue(),value));
204         ParameterBo modifiedParam = ParameterBo.from(parameter);
205         modifiedParam.setValue("new value");
206         CoreServiceApiServiceLocator.getParameterRepositoryService().updateParameter(ParameterBo.to(modifiedParam));
207         value = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameterValueAsString(parameterKey);
208         assertTrue("Parameter value fetched correctly", StringUtils.equals(modifiedParam.getValue(),value));
209     }
210 
211     private void setupParameterBoDataObjectAndSave(){
212         NamespaceBo namespaceBo = new NamespaceBo();
213         namespaceBo.setActive(true);
214         namespaceBo.setApplicationId("RICE");
215         namespaceBo.setCode("TST_NM_SPACE");
216         namespaceBo.setName("Another Test Namespace");
217         KRADServiceLocator.getDataObjectService().save(namespaceBo, PersistenceOption.FLUSH);
218 
219         ParameterTypeBo parameterType = KradDataServiceLocator.getDataObjectService().find(ParameterTypeBo.class,"HELP");
220         assertTrue("Parameter type must be created first",parameterType != null);
221         ParameterBo parameterBo = new ParameterBo();
222         parameterBo.setApplicationId(APP_ID);
223         parameterBo.setValue("blah");
224         parameterBo.setDescription("descr");
225         parameterBo.setParameterTypeCode("HELP");
226         parameterBo.setParameterType(parameterType);
227         parameterBo.setComponentCode("TST");
228         parameterBo.setNamespaceCode("TST_NM_SPACE");
229         parameterBo.setEvaluationOperatorCode("A");
230         parameterBo.setName("TST_PARAM");
231         CoreServiceApiServiceLocator.getParameterRepositoryService().
232                     createParameter(ParameterBo.to(parameterBo));
233 
234     }
235 
236     private void setupStyleBoDataObjectAndSave(){
237         StyleBo styleBo = new StyleBo();
238         styleBo.setActive(true);
239         styleBo.setId("1234");
240         styleBo.setName(STYLE_NAME);
241         styleBo.setXmlContent("<xml>something</xml>");
242 
243         KRADServiceLocator.getDataObjectService().save(styleBo, PersistenceOption.FLUSH);
244 
245     }
246 
247     private void setupParameterTypeBoDataObjectAndSave(){
248         ParameterTypeBo parameterTypeBo = new ParameterTypeBo();
249         parameterTypeBo.setActive(true);
250         parameterTypeBo.setCode("HELP");
251         parameterTypeBo.setName("Help");
252         parameterTypeBo.setVersionNumber(null);
253 
254         KRADServiceLocator.getDataObjectService().save(parameterTypeBo, PersistenceOption.FLUSH);
255     }
256 
257     private void setupDerivedComponentBoDataObjectAndSave(){
258           DerivedComponentBo derivedComponentBo = new DerivedComponentBo();
259           derivedComponentBo.setCode("ComponentBo");
260           derivedComponentBo.setComponentSetId(DERIVED_COMPONENT_SET_ID);
261           derivedComponentBo.setName("Derived component");
262           derivedComponentBo.setNamespaceCode("KR-TST");
263 
264         KRADServiceLocator.getDataObjectService().save(derivedComponentBo, PersistenceOption.FLUSH);
265     }
266 
267     private void setupComponentSetBoDataObjectAndSave(){
268         ComponentSetBo componentSetBo = new ComponentSetBo();
269         componentSetBo.setChecksum("ASDFQWER");
270         componentSetBo.setComponentSetId("DD:RICETST");
271         componentSetBo.setLastUpdateTimestamp(new Timestamp(System.currentTimeMillis()));
272         componentSetBo.setVersionNumber(null);
273 
274         KRADServiceLocator.getDataObjectService().save(componentSetBo, PersistenceOption.FLUSH);
275     }
276 
277     private void setupComponentBoDataObjectAndSave(){
278         ComponentBo componentBo = new ComponentBo();
279         componentBo.setActive(true);
280         componentBo.setCode("All");
281         componentBo.setName("All");
282         componentBo.setNamespaceCode("KR-TST");
283         componentBo.setNamespace(KRADServiceLocator.getDataObjectService().find(NamespaceBo.class,"KR-TST"));
284         KRADServiceLocator.getDataObjectService().save(componentBo, PersistenceOption.FLUSH);
285     }
286 
287     private void setupNameSpaceBoDataObjectAndSave(){
288         NamespaceBo namespaceBo = new NamespaceBo();
289         namespaceBo.setActive(true);
290         namespaceBo.setApplicationId("RICE");
291         namespaceBo.setCode("KR-TST");
292         namespaceBo.setName("Kuali Rice Test Namespace");
293 
294         KRADServiceLocator.getDataObjectService().save(namespaceBo, PersistenceOption.FLUSH);
295 
296         namespaceBo = new NamespaceBo();
297         namespaceBo.setActive(true);
298         namespaceBo.setApplicationId("OTH");
299         namespaceBo.setCode("KR-OTH");
300         namespaceBo.setName("Kuali Other");
301 
302         KRADServiceLocator.getDataObjectService().save(namespaceBo, PersistenceOption.FLUSH);
303 
304     }
305 }