View Javadoc
1   /*
2    * Copyright 2006-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.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.test.BaselineTestCase;
25  
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import static org.junit.Assert.*;
31  
32  /**
33   * An integration test which tests the reference implementation of the ComponentService
34   *
35   * TODO - For now this test is part of KRAD even though it should be part of the core (pending
36   * further modularity work)
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
41  public class DerivedComponentTest 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       * tests {@link org.kuali.rice.coreservice.api.component.ComponentService#getDerivedComponentSet(String)} and {@link org.kuali.rice.coreservice.api.component.ComponentService#publishDerivedComponents(String, java.util.List)}
54       */
55      public void testPublishComponents_and_getPublishedComponentSet() {
56  
57          String testComponentSetId = "testComponentSet";
58          String workflowNamespace = "KR-WKFLW";
59          String testNamespace1 = "TestNamespace1";
60          String testNamespace2 = "TestNamespace2";
61  
62          List<Component> testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
63          assertTrue("Initial testComponentSet should be empty", testComponentSet.isEmpty());
64          List<Component> workflowComponents = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
65  
66          assertTrue(componentService.getAllComponentsByNamespaceCode(testNamespace1).isEmpty());
67          assertTrue(componentService.getAllComponentsByNamespaceCode(testNamespace2).isEmpty());
68  
69          String customTestWorkflowComponent = "CustomTestWorkflowComponent";
70          Component component1 = Component.Builder.create(workflowNamespace, customTestWorkflowComponent, customTestWorkflowComponent).build();
71          String testNamespace1Component = "TestNamespace1Component";
72          Component component2 = Component.Builder.create(testNamespace1, testNamespace1Component, testNamespace1Component).build();
73          String testNamespace2Component1 = "TestNamespace2Component1";
74          Component component3 = Component.Builder.create(testNamespace2, testNamespace2Component1, testNamespace2Component1).build();
75          String testNamespace2Component2 = "TestNamespace2Component2";
76          Component component4 = Component.Builder.create(testNamespace2, testNamespace2Component2, testNamespace2Component2).build();
77  
78          List<Component> setToPublish = new ArrayList<Component>();
79          setToPublish.add(component1);
80          setToPublish.add(component2);
81          setToPublish.add(component3);
82          setToPublish.add(component4);
83  
84          componentService.publishDerivedComponents(testComponentSetId, setToPublish);
85  
86          // now if we fetch the component set it should be non-empty and should contain our 4 items
87          testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
88          assertEquals(4, testComponentSet.size());
89          for (Component component : testComponentSet) {
90              // ensure they all have the appropriate component set id
91              assertEquals(testComponentSetId, component.getComponentSetId());
92          }
93  
94          List<Component> shuffledComponentSet = new ArrayList<Component>(testComponentSet);
95          // now, do a slight shuffle of the list and republish...
96          Collections.shuffle(shuffledComponentSet);
97          componentService.publishDerivedComponents(testComponentSetId, shuffledComponentSet);
98  
99          // we should still have the same set
100         testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
101         assertEquals(4, testComponentSet.size());
102 
103         // refetch by workflow namespace, we should have an additional component now
104         List<Component> workflowComponentsNew = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
105         assertEquals(workflowComponents.size() + 1, workflowComponentsNew.size());
106 
107         // now republish our component set without the workflow namespace component
108         setToPublish = new ArrayList<Component>();
109         setToPublish.add(component2);
110         setToPublish.add(component3);
111         setToPublish.add(component4);
112         componentService.publishDerivedComponents(testComponentSetId, setToPublish);
113 
114         // we should have 3 components now
115         testComponentSet = componentService.getDerivedComponentSet(testComponentSetId);
116         assertEquals(3, testComponentSet.size());
117 
118         // and the workflow component should be gone
119         workflowComponentsNew = componentService.getAllComponentsByNamespaceCode(workflowNamespace);
120         assertEquals(workflowComponents.size(), workflowComponentsNew.size());
121     }
122 
123 
124 }