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