View Javadoc

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