051107bb137eea14098861fb4bc885dc0be00823
[so.git] / bpmn / mso-infrastructure-bpmn / src / test / java / org / onap / so / bpmn / common / workflow / service / CallbackHandlerServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 Nokia.
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.workflow.service;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
32 import org.camunda.bpm.engine.RuntimeService;
33 import org.camunda.bpm.engine.impl.ExecutionQueryImpl;
34 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
35 import org.camunda.bpm.engine.runtime.Execution;
36 import org.camunda.bpm.engine.runtime.ExecutionQuery;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.so.bpmn.common.workflow.service.CallbackHandlerService.CallbackError;
40 import org.onap.so.bpmn.common.workflow.service.CallbackHandlerService.CallbackResult;
41 import org.onap.so.bpmn.common.workflow.service.CallbackHandlerService.CallbackSuccess;
42 import org.onap.so.bpmn.core.UrnPropertiesReader;
43 import org.springframework.core.env.Environment;
44
45 public class CallbackHandlerServiceTest {
46
47     private static final String METHOD_NAME = "testMethod";
48     private static final String MESSAGE = "testMessage";
49     private static final String EVENT_NAME = "eventNameTest";
50     private static final String MESSAGE_VARIABLE = "messageVarTest";
51     private static final String CORRELATION_VARIABLE = "corrVarTest";
52     private static final String CORRELATION_VALUE = "corrValueTest";
53     private static final String LOG_MARKER = "markerTest";
54
55     private RuntimeService runtimeServiceMock;
56     private CallbackHandlerService testedObject;
57
58     @Before
59     public void setup() {
60         runtimeServiceMock = mock(RuntimeService.class);
61         testedObject = new CallbackHandlerService(runtimeServiceMock);
62         mockEnvironment();
63     }
64
65     @Test
66     public void callbackSuccessful() {
67         // given
68         mockRuntimeService(new ArrayList<>(Arrays.asList(new ExecutionEntity())));
69         // when
70         CallbackResult callbackResult = testedObject.handleCallback(METHOD_NAME, MESSAGE, EVENT_NAME, MESSAGE_VARIABLE,
71                 CORRELATION_VARIABLE, CORRELATION_VALUE, LOG_MARKER, new HashMap<>());
72         // then
73         assertThat(callbackResult).isExactlyInstanceOf(CallbackSuccess.class);
74     }
75
76     @Test
77     public void callbackNotSuccessful_noWaitingProcesses() {
78         // given
79         mockRuntimeService(Collections.emptyList());
80         // when
81         CallbackResult callbackResult = testedObject.handleCallback(METHOD_NAME, MESSAGE, EVENT_NAME, MESSAGE_VARIABLE,
82                 CORRELATION_VARIABLE, CORRELATION_VALUE, LOG_MARKER, new HashMap<>());
83         // then
84         assertThat(callbackResult).isExactlyInstanceOf(CallbackError.class);
85     }
86
87     private void mockRuntimeService(List<Execution> waitingProcesses) {
88         ExecutionQuery executionQueryMock = mock(ExecutionQueryImpl.class);
89         when(runtimeServiceMock.createExecutionQuery()).thenReturn(executionQueryMock);
90         when(executionQueryMock.messageEventSubscriptionName("eventNameTest")).thenReturn(executionQueryMock);
91         when(executionQueryMock.processVariableValueEquals("corrVarTest", "corrValueTest"))
92                 .thenReturn(executionQueryMock);
93         when(executionQueryMock.list()).thenReturn(waitingProcesses);
94     }
95
96     private Environment mockEnvironment() {
97         Environment mockEnvironment = mock(Environment.class);
98         UrnPropertiesReader urnPropertiesReader = new UrnPropertiesReader();
99         urnPropertiesReader.setEnvironment(mockEnvironment);
100         when(mockEnvironment.getProperty("mso.correlation.timeout")).thenReturn("1");
101         return mockEnvironment;
102     }
103 }