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.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
38
39
40
41
42
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
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
85
86
87 public void testGetComponentByCode() {
88
89 assertNull(componentService.getComponentByCode("blah", "blah"));
90
91
92 Component component = componentService.getComponentByCode("KR-WKFLW", "DocumentSearch");
93 assertNotNull(component);
94 assertTrue(component.isActive());
95 }
96
97 @Test
98
99
100
101
102 public void testGetAllComponentsByNamespaceCode() {
103
104 List<Component> components = componentService.getAllComponentsByNamespaceCode("blah");
105 assertNotNull(components);
106 assertEquals(0, components.size());
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 components = componentService.getAllComponentsByNamespaceCode("KR-NS");
124 assertEquals(7, components.size());
125
126 ComponentBo scheduleStepComponent = null;
127
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
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
153 assertEquals(6, numActive);
154 assertEquals(1, numInactive);
155 }
156
157 @Test
158
159
160
161
162 public void testGetActiveComponentsByNamespaceCode() {
163
164 List<Component> components = componentService.getActiveComponentsByNamespaceCode("blah");
165 assertNotNull(components);
166 assertEquals(0, components.size());
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 components = componentService.getActiveComponentsByNamespaceCode("KR-NS");
183 assertEquals(7, components.size());
184
185 ComponentBo scheduleStepComponent = null;
186
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
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
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
243 testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
244 assertEquals(4, testComponentSet.size());
245 for (Component component : testComponentSet) {
246
247 assertEquals(testComponentSetId, component.getComponentSetId());
248 }
249
250 List<Component> shuffledComponentSet = new ArrayList<Component>(testComponentSet);
251
252 Collections.shuffle(shuffledComponentSet);
253 componentService.publishDerivedComponents(testComponentSetId, shuffledComponentSet);
254
255
256 testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
257 assertEquals(4, testComponentSet.size());
258
259
260 List<Component> workflowComponentsNew = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
261 assertEquals(workflowComponents.size() + 1, workflowComponentsNew.size());
262
263
264 setToPublish = new ArrayList<Component>();
265 setToPublish.add(component2);
266 setToPublish.add(component3);
267 setToPublish.add(component4);
268 componentService.publishDerivedComponents(testComponentSetId, setToPublish);
269
270
271 testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
272 assertEquals(3, testComponentSet.size());
273
274
275 workflowComponentsNew = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
276 assertEquals(workflowComponents.size(), workflowComponentsNew.size());
277 }
278
279
280 }