forked from kiegroup/drools-wb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NewScenarioSimulationHandlerTest.java
190 lines (169 loc) · 7.82 KB
/
NewScenarioSimulationHandlerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.workbench.screens.scenariosimulation.client.handlers;
import com.google.gwtmockito.GwtMockitoTestRunner;
import org.drools.scenariosimulation.api.model.ScenarioSimulationModel;
import org.drools.workbench.screens.scenariosimulation.client.editor.ScenarioSimulationEditorPresenter;
import org.drools.workbench.screens.scenariosimulation.client.type.ScenarioSimulationResourceType;
import org.drools.workbench.screens.scenariosimulation.service.ScenarioSimulationService;
import org.guvnor.common.services.project.model.Package;
import org.jboss.errai.security.shared.api.identity.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kie.workbench.common.widgets.client.handlers.NewResourcePresenter;
import org.kie.workbench.common.widgets.client.resources.i18n.CommonConstants;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.uberfire.backend.vfs.Path;
import org.uberfire.client.mvp.PlaceManager;
import org.uberfire.ext.widgets.common.client.common.BusyIndicatorView;
import org.uberfire.mocks.CallerMock;
import org.uberfire.mocks.EventSourceMock;
import org.uberfire.rpc.SessionInfo;
import org.uberfire.security.ResourceAction;
import org.uberfire.security.ResourceRef;
import org.uberfire.security.authz.AuthorizationManager;
import org.uberfire.workbench.events.NotificationEvent;
import org.uberfire.workbench.model.ActivityResourceType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(GwtMockitoTestRunner.class)
public class NewScenarioSimulationHandlerTest extends AbstractNewScenarioTest {
@Mock
private BusyIndicatorView busyIndicatorViewMock;
@Mock
private ScenarioSimulationService scenarioSimulationServiceMock;
@Mock
private ScenarioSimulationResourceType resourceTypeMock;
@Mock
private EventSourceMock notificationEventMock;
@Mock
private EventSourceMock newResourceSuccessEventMock;
@Mock
private PlaceManager placeManagerMock;
@Mock
private AuthorizationManager authorizationManagerMock;
@Mock
private SessionInfo sessionInfoMock;
@Mock
private User userMock;
@Captor
private ArgumentCaptor<ResourceRef> refArgumentCaptor;
@Mock
private TitledAttachmentFileWidget uploadWidgetMock;
@Mock
private SourceTypeSelector sourceTypeSelectorMock;
private NewScenarioSimulationHandler handler;
private CallerMock<ScenarioSimulationService> scenarioSimulationServiceCallerMock;
@Before
public void setup() throws Exception {
super.setup();
scenarioSimulationServiceCallerMock = new CallerMock<>(scenarioSimulationServiceMock);
handler = spy(new NewScenarioSimulationHandler(resourceTypeMock,
busyIndicatorViewMock,
notificationEventMock,
newResourceSuccessEventMock,
placeManagerMock,
scenarioSimulationServiceCallerMock,
authorizationManagerMock,
sessionInfoMock,
scenarioSimulationDropdownMock) {
{
this.uploadWidget = uploadWidgetMock;
this.sourceTypeSelector = sourceTypeSelectorMock;
}
});
when(sessionInfoMock.getIdentity()).thenReturn(userMock);
}
@Test
public void createValidDMO() {
createCommon(ScenarioSimulationModel.Type.RULE, true, true);
}
@Test
public void createInvalidDMN() {
createCommon(ScenarioSimulationModel.Type.DMN, false, false);
}
@Test
public void createValidDMN() {
createCommon(ScenarioSimulationModel.Type.DMN, true, true);
}
@Test
public void checkCanCreateWhenFeatureDisabled() {
when(authorizationManagerMock.authorize(any(ResourceRef.class),
eq(ResourceAction.READ),
eq(userMock))).thenReturn(false);
assertFalse(handler.canCreate());
assertResourceRef();
}
@Test
public void checkCanCreateWhenFeatureEnabled() {
when(authorizationManagerMock.authorize(any(ResourceRef.class),
eq(ResourceAction.READ),
eq(userMock))).thenReturn(true);
assertTrue(handler.canCreate());
assertResourceRef();
}
@Test
public void checkRightResourceType() throws Exception {
doReturn(true).when(sourceTypeSelectorMock).validate();
when(sourceTypeSelectorMock.getSelectedType()).thenReturn(ScenarioSimulationModel.Type.RULE);
handler.create(new Package(),
"newfile.scesim",
mock(NewResourcePresenter.class));
verify(busyIndicatorViewMock).showBusyIndicator("Saving");
verify(busyIndicatorViewMock).hideBusyIndicator();
verify(notificationEventMock).fire(any(NotificationEvent.class));
verify(newResourceSuccessEventMock).fire(any(NewResourcePresenter.class));
verify(placeManagerMock).goTo(any(Path.class));
}
@Test
public void getCommandMethod() {
NewResourcePresenter newResourcePresenterMock = mock(NewResourcePresenter.class);
handler.getCommandMethod(newResourcePresenterMock);
verify(uploadWidgetMock, times(1)).clearStatus();
verify(newResourcePresenterMock, times(1)).show(any());
}
private void createCommon(ScenarioSimulationModel.Type type, boolean validate, boolean called) {
doReturn(validate).when(sourceTypeSelectorMock).validate();
when(sourceTypeSelectorMock.getSelectedType()).thenReturn(type);
handler.create(mock(Package.class), "BASEFILENAME", mock(NewResourcePresenter.class));
if (called) {
verify(busyIndicatorViewMock, times(1)).showBusyIndicator(eq(CommonConstants.INSTANCE.Saving()));
verify(scenarioSimulationServiceMock, times(1)).create(any(), any(), any(), any(), any(), isNull(String.class));
}
}
private void assertResourceRef() {
verify(authorizationManagerMock).authorize(refArgumentCaptor.capture(),
eq(ResourceAction.READ),
eq(userMock));
assertEquals(ScenarioSimulationEditorPresenter.IDENTIFIER,
refArgumentCaptor.getValue().getIdentifier());
assertEquals(ActivityResourceType.EDITOR,
refArgumentCaptor.getValue().getResourceType());
}
}