Springboot 2.0 upgrade
[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.junit.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
62 @RunWith(MockitoJUnitRunner.Silent.class)
63 public class AbstractServiceTaskProcessorTest extends MsoGroovyTest {
64
65         @Captor
66         ArgumentCaptor<ExecutionEntity> captor=  ArgumentCaptor.forClass(ExecutionEntity.class);
67
68         @Test
69         public void testCreateCallbackURL_Success() {
70                 ExecutionEntity mockExecution = setupMock()
71                 when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn('http://localhost:18080/mso/WorkflowMessage/')
72                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
73                 def endpoint = serviceTaskProcessor.createCallbackURL(mockExecution, 'testMessageType', 'testCorrelator')
74                 assertEquals('http://localhost:18080/mso/WorkflowMessage/testMessageType/testCorrelator', endpoint)
75         }
76
77         @Test
78         public void testCreateCallbackURL_NullEndpoint() {
79
80                 ExecutionEntity mockExecution = setupMock()
81                 try {
82
83                         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("")
84                         AbstractServiceTaskProcessorImpl serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
85                         def endpoint = serviceTaskProcessor.createCallbackURL(mockExecution, 'testMessageType', 'testCorrelator')
86                 }
87                 catch(Exception ex){
88                 }
89                 Mockito.verify(mockExecution,times(1)).setVariable(captor.capture(),captor.capture())
90                 WorkflowException workflowException = captor.getValue()
91                 Assert.assertEquals("mso:workflow:message:endpoint URN mapping is not set",workflowException.getErrorMessage())
92                 Assert.assertEquals(2000,workflowException.getErrorCode())
93         }
94
95         @Test
96         public void testCreateWorkflowMessageAdapterCallbackURL_Success() {
97                 ExecutionEntity mockExecution = setupMock()
98                 when(mockExecution.getVariable("mso.adapters.workflow.message.endpoint")).thenReturn('http://localhost:18080/workflows/messages/message/')
99
100                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
101                 def endpoint = serviceTaskProcessor.createWorkflowMessageAdapterCallbackURL(mockExecution, 'testMessageType', 'testCorrelator')
102                 assertEquals('http://localhost:18080/workflows/messages/message/testMessageType/testCorrelator', endpoint)
103         }
104
105         @Test
106         public void testCreateWorkflowMessageAdapterCallbackURL_NullEndpoint() {
107
108                 ExecutionEntity mockExecution = setupMock()
109                 try {
110
111                         when(mockExecution.getVariable("mso.adapters.workflow.message.endpoint")).thenReturn("")
112
113                         AbstractServiceTaskProcessorImpl serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
114                         def endpoint = serviceTaskProcessor.createWorkflowMessageAdapterCallbackURL(mockExecution, 'testMessageType', 'testCorrelator')
115                 }
116                 catch(Exception ex){
117                 }
118                 Mockito.verify(mockExecution,times(1)).setVariable(captor.capture(),captor.capture())
119                 WorkflowException workflowException = captor.getValue()
120                 Assert.assertEquals("mso:adapters:workflow:message:endpoint URN mapping is not set",workflowException.getErrorMessage())
121                 Assert.assertEquals(2000,workflowException.getErrorCode())
122         }
123
124         @Test
125         public void testSetRollbackEnabledNullOrEmptyDisableRollback() {
126                 ExecutionEntity mockExecution = setupMock()
127                 when(mockExecution.getVariable("prefix")).thenReturn('TEST_PREFIX')
128                 when(mockExecution.getVariable("disableRollback")).thenReturn(null)
129                 when(mockExecution.getVariable("mso.rollback")).thenReturn("")
130
131                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
132                 serviceTaskProcessor.setRollbackEnabled(mockExecution, "true")
133                 assertEquals(null, mockExecution.getVariable('TEST_PREFIXbackoutOnFailure'))
134         }
135
136         @Test
137         public void testSetRollbackEnabledDisableRollback() {
138                 ExecutionEntity mockExecution = setupMock()
139                 when(mockExecution.getVariable("prefix")).thenReturn('TEST_PREFIX')
140                 when(mockExecution.getVariable("disableRollback")).thenReturn(true)
141                 when(mockExecution.getVariable("mso.rollback")).thenReturn("true")
142
143                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
144                 serviceTaskProcessor.setRollbackEnabled(mockExecution, "true")
145                 verify(mockExecution).setVariable("TEST_PREFIXbackoutOnFailure",false)
146         }
147
148         @Test
149         public void testSetRollbackEnabledRollback() {
150                 ExecutionEntity mockExecution = setupMock()
151                 when(mockExecution.getVariable("prefix")).thenReturn('TEST_PREFIX')
152                 when(mockExecution.getVariable("disableRollback")).thenReturn(false)
153                 when(mockExecution.getVariable("mso.rollback")).thenReturn("true")
154
155                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
156                 serviceTaskProcessor.setRollbackEnabled(mockExecution, "true")
157                 verify(mockExecution).setVariable("TEST_PREFIXbackoutOnFailure",true)
158         }
159
160         @Test
161         public void testSetRollbackEnabledDefaultRollback() {
162                 ExecutionEntity mockExecution = setupMock()
163                 when(mockExecution.getVariable("prefix")).thenReturn('TEST_PREFIX')
164                 when(mockExecution.getVariable("disableRollback")).thenReturn("test")
165                 when(mockExecution.getVariable("mso.rollback")).thenReturn("true")
166
167                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
168                 serviceTaskProcessor.setRollbackEnabled(mockExecution, "true")
169                 verify(mockExecution).setVariable("TEST_PREFIXbackoutOnFailure",true)
170         }
171
172         @Test
173         public void testSetBasicDBAuthHeader_Success() {
174                 ExecutionEntity mockExecution = setupMock()
175                 when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn('5E12ACACBD552A415E081E29F2C4772F9835792A51C766CCFDD7433DB5220B59969CB2798C')
176                 when(mockExecution.getVariable("mso.msoKey")).thenReturn('07a7159d3bf51a0e53be7a8f89699be7')
177
178                 AbstractServiceTaskProcessorImpl  serviceTaskProcessor = new AbstractServiceTaskProcessorImpl();
179                 serviceTaskProcessor.setBasicDBAuthHeader(mockExecution, "true")
180                 verify(mockExecution).setVariable("BasicAuthHeaderValueDB",'Basic dGVzdDp0ZXN0')
181         }
182
183 }