1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
79
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
89
90 ActionListCustomizationMediatorImpl.ActionListCustomizationHandlerServiceChooser
91 mockActionListCustomizationHandlerServiceChooser =
92 mock(ActionListCustomizationMediatorImpl.ActionListCustomizationHandlerServiceChooser.class);
93
94 when(mockActionListCustomizationHandlerServiceChooser.getByApplicationId("foo")).thenReturn(
95 fooActionListCustomizationHandlerService);
96 when(mockActionListCustomizationHandlerServiceChooser.getByApplicationId(null)).thenReturn(
97 defaultActionListCustomizationHandlerService);
98
99
100
101
102
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
128 mediator.setDocumentTypeService(mockDocumentTypeService);
129 mediator.setActionListCustomizationHandlerServiceChooser(mockActionListCustomizationHandlerServiceChooser);
130 }
131
132 @Test public void testApplicationServiceMediation() {
133
134
135
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
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 }