View Javadoc
1   /**
2    * Copyright 2005-2014 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.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   * An integration test which tests the reference implementation of the ComponentService.
39   *
40   * TODO - for now this test is part of KRAD even though it should be part of the core (pending further modularity work)
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * Ensure that an existing {@link Component} can be fetched by the {link DataObjectService}.
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          // get a component we know exists
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       * Ensure that {@link ComponentService#getComponentByCode(String, String)} returns a null {@link Component} when no
78       * records exist by that namespace and code and a not-null {@link Component} when a record exists by that namespace
79       * and code.
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          // get a component we know does not exist
89          Component nonExistingComponent = componentService.getComponentByCode("blah", "blah");
90          assertNull("Component was not null", nonExistingComponent);
91  
92          // get a component we know exists
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       * Ensure that {@link ComponentService#getAllComponentsByNamespaceCode(String)} returns a null {@link Component}
100      * when no records exist by that namespace and a not-null {@link Component} when a record exists by that namespace.
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         // get components we know do not exist
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         // get components we know exist
117         List<Component> existingComponents = componentService.getAllComponentsByNamespaceCode("KR-TST");
118         assertEquals("Wrong number of components were found", 2, existingComponents.size());
119 
120         // all components should be active
121         for (Component existingComponent : existingComponents) {
122             assertTrue("Component should have been active: " + existingComponent, existingComponent.isActive());
123         }
124 
125         // inactivate last component
126         ComponentBo lastComponent = ComponentBo.from(existingComponents.get(existingComponents.size() - 1));
127         lastComponent.setActive(false);
128         dataObjectService.save(lastComponent);
129 
130         // get components we know exist
131         List<Component> activeOrInactiveComponents = componentService.getAllComponentsByNamespaceCode("KR-TST");
132         assertEquals("Wrong number of components were found", 2, activeOrInactiveComponents.size());
133 
134         // count active and inactive components
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         // should be 1 active and 1 inactive component
146         assertEquals("Wrong number of components were active", 1, numActive);
147         assertEquals("Wrong number of components were inactive", 1, numInactive);
148     }
149 
150     /**
151      * Ensure that {@link ComponentService#getAllComponentsByNamespaceCode(String)} returns a null {@link Component}
152      * when no active records exist by that namespace and a not-null {@link Component} when an active record exists by
153      * that namespace.
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         // get components we know do not exist
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         // get components we know exist
170         List<Component> existingComponents = componentService.getActiveComponentsByNamespaceCode("KR-TST");
171         assertEquals("Wrong number of components were found", 2, existingComponents.size());
172 
173         // all components should be active
174         for (Component existingComponent : existingComponents) {
175             assertTrue("Component should have been active: " + existingComponent, existingComponent.isActive());
176         }
177 
178         // inactivate last component
179         ComponentBo lastComponent = ComponentBo.from(existingComponents.get(existingComponents.size() - 1));
180         lastComponent.setActive(false);
181         dataObjectService.save(lastComponent);
182 
183         // get components we know exist
184         List<Component> activeComponents = componentService.getActiveComponentsByNamespaceCode("KR-TST");
185         assertEquals("Wrong number of components were found", 1, activeComponents.size());
186 
187         // all components should be active
188         for (Component activeComponent : activeComponents) {
189             assertTrue("Component should have been active: " + activeComponent, activeComponent.isActive());
190         }
191     }
192 }