View Javadoc
1   /**
2    * Copyright 2005-2016 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.kew.impl.actionlist;
17  
18  import org.joda.time.DateTime;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
22  import org.kuali.rice.kew.actionlist.DefaultCustomActionListAttribute;
23  import org.kuali.rice.kew.api.action.ActionItem;
24  import org.kuali.rice.kew.api.action.ActionItemCustomization;
25  import org.kuali.rice.kew.api.action.ActionSet;
26  import org.kuali.rice.kew.api.actionlist.DisplayParameters;
27  import org.kuali.rice.kew.doctype.bo.DocumentType;
28  import org.kuali.rice.kew.doctype.service.DocumentTypeService;
29  import org.kuali.rice.kew.framework.actionlist.ActionListCustomizationHandlerService;
30  import org.kuali.rice.kew.rule.bo.RuleAttribute;
31  
32  import java.util.ArrayList;
33  import java.util.Collections;
34  import java.util.Map;
35  
36  import static org.junit.Assert.*;
37  import static org.mockito.Mockito.*;
38  
39  /**
40   * Integration test for {@link ActionListCustomizationMediatorImpl}
41   */
42  public class ActionListCustomizationMediatorImplTest {
43  
44      private ActionListCustomizationMediatorImpl mediator = new ActionListCustomizationMediatorImpl();
45  
46      private final String FOO_ACTION_ITEM_ID = "123";
47  
48      private final ActionItemCustomization fooResult =
49              ActionItemCustomization.Builder.create(
50                      FOO_ACTION_ITEM_ID,
51                      ActionSet.Builder.create().build(),
52                      DisplayParameters.Builder.create(Integer.valueOf(10)).build()
53              ).build();
54  
55      private final String DEFAULT_ACTION_ITEM_ID = "987";
56  
57      private final ActionItemCustomization defaultResult;
58      {
59          // set up this custom result
60          DefaultCustomActionListAttribute defaultAttribute = new DefaultCustomActionListAttribute();
61          ActionSet legalActions = null;
62          DisplayParameters displayParameters = null;
63  
64          try {
65              legalActions = defaultAttribute.getLegalActions(null, null);
66              displayParameters = defaultAttribute.getDocHandlerDisplayParameters(null, null);
67          } catch (Exception e) {
68              throw new RuntimeException(e);
69          }
70  
71          defaultResult = ActionItemCustomization.Builder.create(DEFAULT_ACTION_ITEM_ID, legalActions, displayParameters).build();
72      }
73  
74      @Before
75      public void setup() throws Exception {
76  
77          /**
78           * Mock up our ActionListCustomizationHandlerServices, one for the "foo" applicationId, and one for the null
79           * applicationId (the default).  These pass back specific results for easy test verification.
80           */
81  
82          ActionListCustomizationHandlerService fooActionListCustomizationHandlerService = mock(ActionListCustomizationHandlerService.class);
83          when(fooActionListCustomizationHandlerService.customizeActionList(anyString(), anyList())).thenReturn(Collections.singletonList(fooResult));
84  
85          ActionListCustomizationHandlerService defaultActionListCustomizationHandlerService = mock(ActionListCustomizationHandlerService.class);
86          when(defaultActionListCustomizationHandlerService.customizeActionList(anyString(), anyList())).thenReturn(Collections.singletonList(defaultResult));
87  
88          // Mock up this internal class in the ActionListCustomizationMediatorImpl so that we don't try to use the
89          // GRL and end up with a configuration nightmare
90          ActionListCustomizationMediatorImpl.ActionListCustomizationHandlerServiceChooser
91                  mockActionListCustomizationHandlerServiceChooser =
92                  mock(ActionListCustomizationMediatorImpl.ActionListCustomizationHandlerServiceChooser.class);
93          // set it up to choose the correct mock implementation based on application ID
94          when(mockActionListCustomizationHandlerServiceChooser.getByApplicationId("foo")).thenReturn(
95                  fooActionListCustomizationHandlerService);
96          when(mockActionListCustomizationHandlerServiceChooser.getByApplicationId(null)).thenReturn(
97                  defaultActionListCustomizationHandlerService);
98  
99          /**
100          * Mock up our DocumentTypeService.  Setting it up to handle two doctypes: FooDocType and BarDocType, where
101          * FooDocType's custom action list rule attribute has an applicationId of "foo", and BarDocType's has an
102          * applicationId of null.
103          */
104 
105         DocumentTypeService mockDocumentTypeService = mock(DocumentTypeService.class);
106 
107         DocumentType fooDocType = mock(DocumentType.class);
108         {
109             RuleAttribute ruleAttr = new RuleAttribute();
110             ruleAttr.setApplicationId("foo");
111 
112             when(fooDocType.getCustomActionListRuleAttribute()).thenReturn(ruleAttr);
113         }
114 
115         when(mockDocumentTypeService.findByName("FooDocType")).thenReturn(fooDocType);
116 
117         DocumentType barDocType = mock(DocumentType.class);
118         {
119             RuleAttribute ruleAttr = new RuleAttribute();
120             ruleAttr.setApplicationId(null);
121 
122             when(barDocType.getCustomActionListRuleAttribute()).thenReturn(ruleAttr);
123         }
124 
125         when(mockDocumentTypeService.findByName("BarDocType")).thenReturn(barDocType);
126 
127         // set our mocks on the mediator
128         mediator.setDocumentTypeService(mockDocumentTypeService);
129         mediator.setActionListCustomizationHandlerServiceChooser(mockActionListCustomizationHandlerServiceChooser);
130     }
131 
132     @Test public void testApplicationServiceMediation() {
133 
134         //
135         // Create our test ActionItems to customize
136         //
137 
138         ActionItem.Builder fooActionItemBuilder = ActionItem.Builder.create(
139                 "321", "A", "234", DateTime.now(), "Foo Doc Label", "http://asdf.com/", "FooDocType", "345", "gilesp"
140         );
141         fooActionItemBuilder.setId(FOO_ACTION_ITEM_ID);
142 
143         ActionItem.Builder barActionItemBuilder = ActionItem.Builder.create(
144                 "322", "A", "235", DateTime.now(), "Bar Doc Label", "http://asdf.com/", "BarDocType", "346", "gilesp"
145         );
146         fooActionItemBuilder.setId(DEFAULT_ACTION_ITEM_ID);
147 
148         ArrayList<ActionItem> actionItems = new ArrayList<ActionItem>();
149         actionItems.add(fooActionItemBuilder.build());
150         actionItems.add(barActionItemBuilder.build());
151 
152         Map<String, ActionItemCustomization> customizations = mediator.getActionListCustomizations("gilesp", actionItems);
153 
154         assertTrue(customizations.size() == 2);
155         // this proves that the appropriate services were called by the mediator
156         assertEquals(fooResult, customizations.get(fooResult.getActionItemId()));
157         assertEquals(defaultResult, customizations.get(defaultResult.getActionItemId()));
158     }
159 
160     @Test(expected = RiceIllegalArgumentException.class) public void testNullPrincipalId() {
161         mediator.getActionListCustomizations(null, Collections.<ActionItem>emptyList());
162     }
163 
164     @Test(expected = RiceIllegalArgumentException.class) public void testEmptyPrincipalId() {
165         mediator.getActionListCustomizations("", Collections.<ActionItem>emptyList());
166     }
167 
168     @Test public void testEmptyActionItemList() {
169         Map<String, ActionItemCustomization> customizations = mediator.getActionListCustomizations("gilesp", Collections.<ActionItem>emptyList());
170         assertTrue(customizations.size() == 0);
171     }
172 
173 }