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.core.impl.component;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.kuali.rice.core.api.criteria.QueryByCriteria;
22  import org.kuali.rice.core.api.criteria.QueryResults;
23  import org.kuali.rice.core.test.CORETestCase;
24  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
25  import org.kuali.rice.coreservice.api.component.Component;
26  import org.kuali.rice.coreservice.api.component.ComponentService;
27  import org.kuali.rice.coreservice.impl.component.ComponentBo;
28  import org.kuali.rice.krad.service.KRADServiceLocator;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.List;
33  
34  import static org.junit.Assert.*;
35  
36  /**
37   * An integration test which tests the reference implementation of the ComponentService
38   *
39   * TODO - for now this test is part of KRAD even though it should be part of the core (pending
40   * further modularity work)
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  
45  public class ComponentServiceTest extends CORETestCase {
46  
47      private ComponentService componentService;
48  
49      private static boolean suiteLoaded;
50  
51      @Before
52      public void setUp() throws Exception {
53  
54          if (!suiteLoaded) {
55              try {
56                  super.loadSuiteTestData();
57                  suiteLoaded = true;
58              } catch (Exception e) {
59                  // ignore
60              }
61          }
62          super.setUp();
63      }
64  
65      @Before
66      public void establishComponentService() {
67          componentService = CoreServiceApiServiceLocator.getComponentService();
68          assertNotNull("Failed to locate ComponentService", componentService);
69      }
70  
71  
72      @Test
73      public void testGetComponentByDataObjectService() {
74          QueryByCriteria.Builder qbc = QueryByCriteria.Builder.forAttribute("namespace.code", "KR-NS");
75          QueryResults<ComponentBo> results = KRADServiceLocator.getDataObjectService().findMatching(ComponentBo.class, qbc.build());
76  
77          assertNotNull(results);
78          assertTrue(CollectionUtils.isNotEmpty(results.getResults()));
79      }
80  
81  
82      @Test
83      /**
84       * tests {@link ComponentService#getComponentByCode(String, String)} for a component that does not exist
85       * and for a component that exists
86       */
87      public void testGetComponentByCode() {
88          // get a component we know does not exist
89          assertNull(componentService.getComponentByCode("blah", "blah"));
90  
91          // get a component which we know exists
92          Component component = componentService.getComponentByCode("KR-WKFLW", "DocumentSearch");
93          assertNotNull(component);
94          assertTrue(component.isActive());
95      }
96  
97      @Test
98      /**
99       * tests {@link ComponentService#getAllComponentsByNamespaceCode(String)} by a component namespace that does not exist and
100      * by a component namespace that does exist
101      */
102     public void testGetAllComponentsByNamespaceCode() {
103         // get by a component namespace we know does not exist
104         List<Component> components = componentService.getAllComponentsByNamespaceCode("blah");
105         assertNotNull(components);
106         assertEquals(0, components.size());
107 
108         // now fetch all components for a namespace which we know has more than 1,
109         // we should have 7 components under the "KR-NS" namespace code in our default test data set as follows:
110         // +----------+-----------------------------+
111         // | NMSPC_CD | CMPNT_CD                    |
112         // +----------+-----------------------------+
113         // | KR-NS    | All                         |
114         // | KR-NS    | Batch                       |
115         // | KR-NS    | Document                    |
116         // | KR-NS    | Lookup                      |
117         // | KR-NS    | PurgePendingAttachmentsStep |
118         // | KR-NS    | PurgeSessionDocumentsStep   |
119         // | KR-NS    | ScheduleStep                |
120         // +----------+-----------------------------+
121 
122 
123         components = componentService.getAllComponentsByNamespaceCode("KR-NS");
124         assertEquals(7, components.size());
125 
126         ComponentBo scheduleStepComponent = null;
127         // all should be active
128         for (Component component : components) {
129             assertTrue("Component should have been active: " + component, component.isActive());
130             if (component.getCode().equals("ScheduleStep")) {
131                 scheduleStepComponent = ComponentBo.from(component);
132             }
133         }
134         assertNotNull("Failed to locate schedule step component", scheduleStepComponent);
135 
136         // inactivate schedule step component
137         scheduleStepComponent.setActive(false);
138         KRADServiceLocator.getDataObjectService().save(scheduleStepComponent);
139 
140         components = componentService.getAllComponentsByNamespaceCode("KR-NS");
141         assertEquals(7, components.size());
142         int numActive = 0;
143         int numInactive = 0;
144         for (Component component : components) {
145             if (component.isActive()) {
146                 numActive++;
147             } else {
148                 numInactive++;
149             }
150         }
151 
152         // should be 6 active, 1 inactive
153         assertEquals(6, numActive);
154         assertEquals(1, numInactive);
155     }
156 
157     @Test
158     /**
159      * tests that {@link ComponentService#getActiveComponentsByNamespaceCode(String)} returns all active components
160      * for the given name space code
161      */
162     public void testGetActiveComponentsByNamespaceCode() {
163         // get by a component namespace we know does not exist
164         List<Component> components = componentService.getActiveComponentsByNamespaceCode("blah");
165         assertNotNull(components);
166         assertEquals(0, components.size());
167 
168         // now fetch all components for a namespace which we know has more than 1,
169         // we should have 7 components under the "KR-NS" namespace code in our default test data set as follows:
170         // +----------+-----------------------------+
171         // | NMSPC_CD | CMPNT_CD                    |
172         // +----------+-----------------------------+
173         // | KR-NS    | All                         |
174         // | KR-NS    | Batch                       |
175         // | KR-NS    | Document                    |
176         // | KR-NS    | Lookup                      |
177         // | KR-NS    | PurgePendingAttachmentsStep |
178         // | KR-NS    | PurgeSessionDocumentsStep   |
179         // | KR-NS    | ScheduleStep                |
180         // +----------+-----------------------------+
181 
182         components = componentService.getActiveComponentsByNamespaceCode("KR-NS");
183         assertEquals(7, components.size());
184 
185         ComponentBo scheduleStepComponent = null;
186         // all should be active
187         for (Component component : components) {
188             assertTrue("Component should have been active: " + component, component.isActive());
189             if (component.getCode().equals("ScheduleStep")) {
190                 scheduleStepComponent = ComponentBo.from(component);
191             }
192         }
193         assertNotNull("Failed to locate schedule step component", scheduleStepComponent);
194 
195         // inactivate schedule step component
196         scheduleStepComponent.setActive(false);
197         KRADServiceLocator.getDataObjectService().save(scheduleStepComponent);
198 
199         components = componentService.getActiveComponentsByNamespaceCode("KR-NS");
200         assertEquals(6, components.size());
201         for (Component component : components) {
202             assertTrue("Component should have been active: " + component, component.isActive());
203         }
204     }
205 
206     @Test
207     /**
208      * tests {@link ComponentService#getDerivedComponentSet(String)} and {@link ComponentService#publishDerivedComponents(String, java.util.List)}
209      */
210     public void testPublishComponents_and_getPublishedComponentSet() {
211 
212         String testComponentSetId = "testComponentSet";
213         String workflowNamespace = "KR-WKFLW";
214         String testNamespace1 = "TestNamespace1";
215         String testNamespace2 = "TestNamespace2";
216 
217         List<Component> testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
218         assertTrue("Initial testComponentSet should be empty", testComponentSet.isEmpty());
219         List<Component> workflowComponents = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
220         assertFalse("There should be some components for the " + workflowNamespace + " namespace", workflowComponents.isEmpty());
221 
222         assertTrue(componentService.getAllComponentsByNamespaceCode(testNamespace1).isEmpty());
223         assertTrue(componentService.getAllComponentsByNamespaceCode(testNamespace2).isEmpty());
224 
225         String customTestWorkflowComponent = "CustomTestWorkflowComponent";
226         Component component1 = Component.Builder.create(workflowNamespace, customTestWorkflowComponent, customTestWorkflowComponent).build();
227         String testNamespace1Component = "TestNamespace1Component";
228         Component component2 = Component.Builder.create(testNamespace1, testNamespace1Component, testNamespace1Component).build();
229         String testNamespace2Component1 = "TestNamespace2Component1";
230         Component component3 = Component.Builder.create(testNamespace2, testNamespace2Component1, testNamespace2Component1).build();
231         String testNamespace2Component2 = "TestNamespace2Component2";
232         Component component4 = Component.Builder.create(testNamespace2, testNamespace2Component2, testNamespace2Component2).build();
233 
234         List<Component> setToPublish = new ArrayList<Component>();
235         setToPublish.add(component1);
236         setToPublish.add(component2);
237         setToPublish.add(component3);
238         setToPublish.add(component4);
239 
240         componentService.publishDerivedComponents(testComponentSetId, setToPublish);
241 
242         // now if we fetch the component set it should be non-empty and should contain our 4 items
243         testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
244         assertEquals(4, testComponentSet.size());
245         for (Component component : testComponentSet) {
246             // ensure they all have the appropriate component set id
247             assertEquals(testComponentSetId, component.getComponentSetId());
248         }
249 
250         List<Component> shuffledComponentSet = new ArrayList<Component>(testComponentSet);
251         // now, do a slight shuffle of the list and republish...
252         Collections.shuffle(shuffledComponentSet);
253         componentService.publishDerivedComponents(testComponentSetId, shuffledComponentSet);
254 
255         // we should still have the same set
256         testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
257         assertEquals(4, testComponentSet.size());
258 
259         // refetch by workflow namespace, we should have an additional component now
260         List<Component> workflowComponentsNew = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
261         assertEquals(workflowComponents.size() + 1, workflowComponentsNew.size());
262 
263         // now republish our component set without the workflow namespace component
264         setToPublish = new ArrayList<Component>();
265         setToPublish.add(component2);
266         setToPublish.add(component3);
267         setToPublish.add(component4);
268         componentService.publishDerivedComponents(testComponentSetId, setToPublish);
269 
270         // we should have 3 components now
271         testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
272         assertEquals(3, testComponentSet.size());
273 
274         // and the workflow component should be gone
275         workflowComponentsNew = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
276         assertEquals(workflowComponents.size(), workflowComponentsNew.size());
277     }
278 
279 
280 }