1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
35  
36  
37  
38  
39  
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  
54  
55  
56      public void testGetComponentByCode() {
57          
58          assertNull(componentService.getComponentByCode("blah", "blah"));
59  
60          
61          Component component = componentService.getComponentByCode("KR-WKFLW", "DocumentSearch");
62          assertNotNull(component);
63          assertTrue(component.isActive());
64      }
65  
66      @Test
67      
68  
69  
70  
71      public void testGetAllComponentsByNamespaceCode() {
72          
73          List<Component> components = componentService.getAllComponentsByNamespaceCode("blah");
74          assertNotNull(components);
75          assertEquals(0, components.size());
76  
77          
78          
79          
80          
81          
82          
83          
84          
85          
86          
87          
88          
89          
90          
91          components = componentService.getAllComponentsByNamespaceCode("KR-NS");
92          assertEquals(7, components.size());
93  
94          ComponentBo scheduleStepComponent = null;
95          
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         
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         
121         assertEquals(6, numActive);
122         assertEquals(1, numInactive);
123     }
124 
125     @Test
126     
127 
128 
129 
130     public void testGetActiveComponentsByNamespaceCode() {
131         
132         List<Component> components = componentService.getActiveComponentsByNamespaceCode("blah");
133         assertNotNull(components);
134         assertEquals(0, components.size());
135 
136         
137         
138         
139         
140         
141         
142         
143         
144         
145         
146         
147         
148         
149 
150         components = componentService.getActiveComponentsByNamespaceCode("KR-NS");
151         assertEquals(7, components.size());
152 
153         ComponentBo scheduleStepComponent = null;
154         
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         
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 
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         
211         testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
212         assertEquals(4, testComponentSet.size());
213         for (Component component : testComponentSet) {
214             
215             assertEquals(testComponentSetId, component.getComponentSetId());
216         }
217 
218         List<Component> shuffledComponentSet = new ArrayList<Component>(testComponentSet);
219         
220         Collections.shuffle(shuffledComponentSet);
221         componentService.publishDerivedComponents(testComponentSetId, shuffledComponentSet);
222 
223         
224         testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
225         assertEquals(4, testComponentSet.size());
226 
227         
228         List<Component> workflowComponentsNew = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
229         assertEquals(workflowComponents.size() + 1, workflowComponentsNew.size());
230 
231         
232         setToPublish = new ArrayList<Component>();
233         setToPublish.add(component2);
234         setToPublish.add(component3);
235         setToPublish.add(component4);
236         componentService.publishDerivedComponents(testComponentSetId, setToPublish);
237 
238         
239         testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
240         assertEquals(3, testComponentSet.size());
241 
242         
243         workflowComponentsNew = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
244         assertEquals(workflowComponents.size(), workflowComponentsNew.size());
245     }
246 
247 
248 }