86a175d811f9f3adb041dfd5afd95498c3cccb06
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / AbstractServiceTaskProcessorTest.groovy
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * ONAP - SO 
4  * ================================================================================ 
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 
6  * ================================================================================ 
7  * Licensed under the Apache License, Version 2.0 (the "License"); 
8  * you may not use this file except in compliance with the License. 
9  * You may obtain a copy of the License at 
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0 
12  * 
13  * Unless required by applicable law or agreed to in writing, software 
14  * distributed under the License is distributed on an "AS IS" BASIS, 
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
16  * See the License for the specific language governing permissions and 
17  * limitations under the License. 
18  * ============LICENSE_END========================================================= 
19  */ 
20
21 package org.onap.so.bpmn.common.scripts
22
23 import org.camunda.bpm.engine.delegate.DelegateExecution
24 import org.junit.Assert
25 import org.mockito.ArgumentCaptor
26 import org.mockito.Captor
27 import org.mockito.Mockito
28 import org.onap.so.bpmn.core.WorkflowException;
29
30 import static org.junit.Assert.*;
31 import static org.mockito.Mockito.*
32
33 import org.onap.so.rest.HttpHeader
34 import org.mockito.MockitoAnnotations
35 import org.mockito.runners.MockitoJUnitRunner
36 import org.mockito.internal.debugging.MockitoDebuggerImpl
37 import org.junit.Before
38 import org.onap.so.bpmn.common.scripts.AaiUtil;
39 import org.junit.Rule;
40 import org.junit.Test
41 import org.junit.Ignore
42 import org.junit.runner.RunWith
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.camunda.bpm.engine.ProcessEngineServices
46 import org.camunda.bpm.engine.RepositoryService
47 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
48 import org.camunda.bpm.engine.impl.pvm.process.ProcessDefinitionImpl
49 import org.camunda.bpm.engine.repository.ProcessDefinition
50
51
52
53 class AbstractServiceTaskProcessorImpl extends  AbstractServiceTaskProcessor{
54
55         @Override
56         void preProcessRequest(DelegateExecution execution) {
57
58         }
59 }
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class AbstractServiceTaskProcessorTest extends MsoGroovyTest {
63
64         @Captor
65         ArgumentCaptor<ExecutionEntity> captor=  ArgumentCaptor.forClass(ExecutionEntity.class);
66
67         @Test
68         public void testCreateCallbackURL_Success() {
69                 ExecutionEntity mockExecution = setupMock()
70                 when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn('http://localhost:18080/mso/WorkflowMessage/')
71                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
72                 def endpoint = serviceTaskProcessor.createCallbackURL(mockExecution, 'testMessageType', 'testCorrelator')
73                 assertEquals('http://localhost:18080/mso/WorkflowMessage/testMessageType/testCorrelator', endpoint)
74         }
75
76         @Test
77         public void testCreateCallbackURL_NullEndpoint() {
78
79                 ExecutionEntity mockExecution = setupMock()
80                 try {
81
82                         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("")
83                         AbstractServiceTaskProcessorImpl serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
84                         def endpoint = serviceTaskProcessor.createCallbackURL(mockExecution, 'testMessageType', 'testCorrelator')
85                 }
86                 catch(Exception ex){
87                 }
88                 Mockito.verify(mockExecution,times(1)).setVariable(captor.capture(),captor.capture())
89                 WorkflowException workflowException = captor.getValue()
90                 Assert.assertEquals("mso:workflow:message:endpoint URN mapping is not set",workflowException.getErrorMessage())
91                 Assert.assertEquals(2000,workflowException.getErrorCode())
92         }
93
94         @Test
95         public void testCreateWorkflowMessageAdapterCallbackURL_Success() {
96                 ExecutionEntity mockExecution = setupMock()
97                 when(mockExecution.getVariable("mso.adapters.workflow.message.endpoint")).thenReturn('http://localhost:18080/workflows/messages/message/')
98
99                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
100                 def endpoint = serviceTaskProcessor.createWorkflowMessageAdapterCallbackURL(mockExecution, 'testMessageType', 'testCorrelator')
101                 assertEquals('http://localhost:18080/workflows/messages/message/testMessageType/testCorrelator', endpoint)
102         }
103
104         @Test
105         public void testCreateWorkflowMessageAdapterCallbackURL_NullEndpoint() {
106
107                 ExecutionEntity mockExecution = setupMock()
108                 try {
109
110                         when(mockExecution.getVariable("mso.adapters.workflow.message.endpoint")).thenReturn("")
111
112                         AbstractServiceTaskProcessorImpl serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
113                         def endpoint = serviceTaskProcessor.createWorkflowMessageAdapterCallbackURL(mockExecution, 'testMessageType', 'testCorrelator')
114                 }
115                 catch(Exception ex){
116                 }
117                 Mockito.verify(mockExecution,times(1)).setVariable(captor.capture(),captor.capture())
118                 WorkflowException workflowException = captor.getValue()
119                 Assert.assertEquals("mso:adapters:workflow:message:endpoint URN mapping is not set",workflowException.getErrorMessage())
120                 Assert.assertEquals(2000,workflowException.getErrorCode())
121         }
122
123         @Test
124         public void testSetRollbackEnabledNullOrEmptyDisableRollback() {
125                 ExecutionEntity mockExecution = setupMock()
126                 when(mockExecution.getVariable("prefix")).thenReturn('TEST_PREFIX')
127                 when(mockExecution.getVariable("disableRollback")).thenReturn(null)
128                 when(mockExecution.getVariable("mso.rollback")).thenReturn("")
129
130                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
131                 serviceTaskProcessor.setRollbackEnabled(mockExecution, "true")
132                 assertEquals(null, mockExecution.getVariable('TEST_PREFIXbackoutOnFailure'))
133         }
134
135         @Test
136         public void testSetRollbackEnabledDisableRollback() {
137                 ExecutionEntity mockExecution = setupMock()
138                 when(mockExecution.getVariable("prefix")).thenReturn('TEST_PREFIX')
139                 when(mockExecution.getVariable("disableRollback")).thenReturn(true)
140                 when(mockExecution.getVariable("mso.rollback")).thenReturn("true")
141
142                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
143                 serviceTaskProcessor.setRollbackEnabled(mockExecution, "true")
144                 verify(mockExecution).setVariable("TEST_PREFIXbackoutOnFailure",false)
145         }
146
147         @Test
148         public void testSetRollbackEnabledRollback() {
149                 ExecutionEntity mockExecution = setupMock()
150                 when(mockExecution.getVariable("prefix")).thenReturn('TEST_PREFIX')
151                 when(mockExecution.getVariable("disableRollback")).thenReturn(false)
152                 when(mockExecution.getVariable("mso.rollback")).thenReturn("true")
153
154                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
155                 serviceTaskProcessor.setRollbackEnabled(mockExecution, "true")
156                 verify(mockExecution).setVariable("TEST_PREFIXbackoutOnFailure",true)
157         }
158
159         @Test
160         public void testSetRollbackEnabledDefaultRollback() {
161                 ExecutionEntity mockExecution = setupMock()
162                 when(mockExecution.getVariable("prefix")).thenReturn('TEST_PREFIX')
163                 when(mockExecution.getVariable("disableRollback")).thenReturn("test")
164                 when(mockExecution.getVariable("mso.rollback")).thenReturn("true")
165
166                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
167                 serviceTaskProcessor.setRollbackEnabled(mockExecution, "true")
168                 verify(mockExecution).setVariable("TEST_PREFIXbackoutOnFailure",true)
169         }
170
171         @Test
172         public void testSetBasicDBAuthHeader_Success() {
173                 ExecutionEntity mockExecution = setupMock()
174                 when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn('9B2278E8B8E95F256A560719055F4DF3')
175                 when(mockExecution.getVariable("mso.msoKey")).thenReturn('aa3871669d893c7fb8abbcda31b88b4f')
176
177                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
178                 serviceTaskProcessor.setBasicDBAuthHeader(mockExecution, "true")
179                 verify(mockExecution).setVariable("BasicAuthHeaderValueDB",'Basic dXAyMTE4OnVwMjExOA==')
180         }
181
182 }