View Javadoc

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