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