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.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
35
36
37
38
39
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
54 assertNull(componentService.getComponentByCode("blah", "blah"));
55
56
57 Component component = componentService.getComponentByCode("KR-WKFLW", "DocumentSearch");
58 assertNotNull(component);
59 assertTrue(component.isActive());
60 }
61
62 @Test
63 public void testGetAllComponentsByNamespaceCode() {
64
65 List<Component> components = componentService.getAllComponentsByNamespaceCode("blah");
66 assertNotNull(components);
67 assertEquals(0, components.size());
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 components = componentService.getAllComponentsByNamespaceCode("KR-NS");
84 assertEquals(7, components.size());
85
86 ComponentBo scheduleStepComponent = null;
87
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
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
113 assertEquals(6, numActive);
114 assertEquals(1, numInactive);
115 }
116
117 @Test
118 public void testGetActiveComponentsByNamespaceCode() {
119
120 List<Component> components = componentService.getActiveComponentsByNamespaceCode("blah");
121 assertNotNull(components);
122 assertEquals(0, components.size());
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 components = componentService.getActiveComponentsByNamespaceCode("KR-NS");
139 assertEquals(7, components.size());
140
141 ComponentBo scheduleStepComponent = null;
142
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
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
196 testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
197 assertEquals(4, testComponentSet.size());
198 for (Component component : testComponentSet) {
199
200 assertEquals(testComponentSetId, component.getComponentSetId());
201 }
202
203 List<Component> shuffledComponentSet = new ArrayList<Component>(testComponentSet);
204
205 Collections.shuffle(shuffledComponentSet);
206 componentService.publishDerivedComponents(testComponentSetId, shuffledComponentSet);
207
208
209 testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
210 assertEquals(4, testComponentSet.size());
211
212
213 List<Component> workflowComponentsNew = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
214 assertEquals(workflowComponents.size() + 1, workflowComponentsNew.size());
215
216
217 setToPublish = new ArrayList<Component>();
218 setToPublish.add(component2);
219 setToPublish.add(component3);
220 setToPublish.add(component4);
221 componentService.publishDerivedComponents(testComponentSetId, setToPublish);
222
223
224 testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
225 assertEquals(3, testComponentSet.size());
226
227
228 workflowComponentsNew = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
229 assertEquals(workflowComponents.size(), workflowComponentsNew.size());
230 }
231
232
233 }