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.api.namespace.Namespace;
28 import org.kuali.rice.coreservice.impl.component.ComponentBo;
29 import org.kuali.rice.coreservice.impl.namespace.NamespaceBo;
30 import org.kuali.rice.krad.data.DataObjectService;
31 import org.kuali.rice.krad.service.KRADServiceLocator;
32
33 import java.util.List;
34
35 import static org.junit.Assert.*;
36
37
38
39
40
41
42
43
44
45 public class ComponentServiceTest extends CORETestCase {
46
47 private ComponentService componentService;
48 private DataObjectService dataObjectService;
49
50 @Before
51 public void establishServices() {
52 componentService = CoreServiceApiServiceLocator.getComponentService();
53 assertNotNull("Failed to locate ComponentService", componentService);
54
55 dataObjectService = KRADServiceLocator.getDataObjectService();
56 assertNotNull("Failed to locate DataObjectService", dataObjectService);
57 }
58
59
60
61
62 @Test
63 public void testGetComponentByDataObjectService() {
64 NamespaceBo namespace = NamespaceBo.from(Namespace.Builder.create("KR-TST").build());
65 dataObjectService.save(namespace);
66 ComponentBo component = ComponentBo.from(Component.Builder.create("KR-TST", "TST-DOS", "Test Data Object Service").build());
67 dataObjectService.save(component);
68
69
70 QueryByCriteria.Builder query = QueryByCriteria.Builder.forAttribute("namespace.code", "KR-TST");
71 QueryResults<ComponentBo> results = dataObjectService.findMatching(ComponentBo.class, query.build());
72 assertNotNull("Results were null", results);
73 assertTrue("Results were empty", CollectionUtils.isNotEmpty(results.getResults()));
74 }
75
76
77
78
79
80
81 @Test
82 public void testGetComponentByCode() {
83 NamespaceBo namespace = NamespaceBo.from(Namespace.Builder.create("KR-TST").build());
84 dataObjectService.save(namespace);
85 ComponentBo component = ComponentBo.from(Component.Builder.create("KR-TST", "TST-CD", "Test Code").build());
86 dataObjectService.save(component);
87
88
89 Component nonExistingComponent = componentService.getComponentByCode("blah", "blah");
90 assertNull("Component was not null", nonExistingComponent);
91
92
93 Component existingComponent = componentService.getComponentByCode("KR-TST", "TST-CD");
94 assertNotNull("Component was null", existingComponent);
95 assertTrue("Component was not active", existingComponent.isActive());
96 }
97
98
99
100
101
102 @Test
103 public void testGetAllComponentsByNamespaceCode() {
104 NamespaceBo namespace = NamespaceBo.from(Namespace.Builder.create("KR-TST").build());
105 dataObjectService.save(namespace);
106 ComponentBo component1 = ComponentBo.from(Component.Builder.create("KR-TST", "TST-NMSPC1", "Test Namespace 1").build());
107 dataObjectService.save(component1);
108 ComponentBo component2 = ComponentBo.from(Component.Builder.create("KR-TST", "TST-NMSPC2", "Test Namespace 2").build());
109 dataObjectService.save(component2);
110
111
112 List<Component> nonExistingComponents = componentService.getAllComponentsByNamespaceCode("blah");
113 assertNotNull("Components were null", nonExistingComponents);
114 assertEquals("Wrong number of components were found", 0, nonExistingComponents.size());
115
116
117 List<Component> existingComponents = componentService.getAllComponentsByNamespaceCode("KR-TST");
118 assertEquals("Wrong number of components were found", 2, existingComponents.size());
119
120
121 for (Component existingComponent : existingComponents) {
122 assertTrue("Component should have been active: " + existingComponent, existingComponent.isActive());
123 }
124
125
126 ComponentBo lastComponent = ComponentBo.from(existingComponents.get(existingComponents.size() - 1));
127 lastComponent.setActive(false);
128 dataObjectService.save(lastComponent);
129
130
131 List<Component> activeOrInactiveComponents = componentService.getAllComponentsByNamespaceCode("KR-TST");
132 assertEquals("Wrong number of components were found", 2, activeOrInactiveComponents.size());
133
134
135 int numActive = 0;
136 int numInactive = 0;
137 for (Component activeOrInactiveComponent : activeOrInactiveComponents) {
138 if (activeOrInactiveComponent.isActive()) {
139 numActive++;
140 } else {
141 numInactive++;
142 }
143 }
144
145
146 assertEquals("Wrong number of components were active", 1, numActive);
147 assertEquals("Wrong number of components were inactive", 1, numInactive);
148 }
149
150
151
152
153
154
155 @Test
156 public void testGetActiveComponentsByNamespaceCode() {
157 NamespaceBo namespace = NamespaceBo.from(Namespace.Builder.create("KR-TST").build());
158 dataObjectService.save(namespace);
159 ComponentBo component1 = ComponentBo.from(Component.Builder.create("KR-TST", "TST-ACTV1", "Test Active 1").build());
160 dataObjectService.save(component1);
161 ComponentBo component2 = ComponentBo.from(Component.Builder.create("KR-TST", "TST-ACTV2", "Test Active 2").build());
162 dataObjectService.save(component2);
163
164
165 List<Component> nonExistingComponents = componentService.getActiveComponentsByNamespaceCode("blah");
166 assertNotNull("Components were null", nonExistingComponents);
167 assertEquals("Wrong number of components were found", 0, nonExistingComponents.size());
168
169
170 List<Component> existingComponents = componentService.getActiveComponentsByNamespaceCode("KR-TST");
171 assertEquals("Wrong number of components were found", 2, existingComponents.size());
172
173
174 for (Component existingComponent : existingComponents) {
175 assertTrue("Component should have been active: " + existingComponent, existingComponent.isActive());
176 }
177
178
179 ComponentBo lastComponent = ComponentBo.from(existingComponents.get(existingComponents.size() - 1));
180 lastComponent.setActive(false);
181 dataObjectService.save(lastComponent);
182
183
184 List<Component> activeComponents = componentService.getActiveComponentsByNamespaceCode("KR-TST");
185 assertEquals("Wrong number of components were found", 1, activeComponents.size());
186
187
188 for (Component activeComponent : activeComponents) {
189 assertTrue("Component should have been active: " + activeComponent, activeComponent.isActive());
190 }
191 }
192 }