aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/MonitorCnfmJobTaskTest.java
blob: 1ef3bc8753fe603ccba8645a90bfa9e03cb47d26 (plain)
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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.net.URI;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.so.bpmn.common.BuildingBlockExecution;
import org.onap.so.client.exception.ExceptionBuilder;
import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;

/**
 * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
 */
@RunWith(MockitoJUnitRunner.class)
public class MonitorCnfmJobTaskTest {

    private final BuildingBlockExecution stubbedExecution = new StubbedBuildingBlockExecution();
    public static final String CREATE_CNF_STATUS_RESPONSE_PARAM_NAME = "createCnfStatusResponse";
    private final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
    public static final String OPERATION_STATUS_PARAM_NAME = "operationStatus";
    private MonitorCnfmJobTask monitorCnfmCreateJobTask;
    @Mock
    private CnfmHttpServiceProvider mockedCnfmHttpServiceProvider;
    @Mock
    private ExceptionBuilder exceptionUtil;

    @Before
    public void setup() {
        monitorCnfmCreateJobTask = new MonitorCnfmJobTask(mockedCnfmHttpServiceProvider, exceptionUtil);
    }

    @Test
    public void test_getCurrentOperationStatus_completed() {
        stubbedExecution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, URI.create("sampleURL"));
        when(mockedCnfmHttpServiceProvider.getOperationJobStatus(Mockito.anyString())).thenReturn(getAsLcmOpOcc());
        monitorCnfmCreateJobTask.getCurrentOperationStatus(stubbedExecution);
        assertEquals(AsLcmOpOcc.OperationStateEnum.COMPLETED,
                stubbedExecution.getVariable(OPERATION_STATUS_PARAM_NAME));
    }

    @Test
    public void test_getCurrentOperationStatus_Exception() {
        stubbedExecution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, URI.create("sampleURL"));
        when(mockedCnfmHttpServiceProvider.getOperationJobStatus(Mockito.anyString()))
                .thenThrow(new RuntimeException());
        monitorCnfmCreateJobTask.getCurrentOperationStatus(stubbedExecution);
        verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1209),
                any(Exception.class));
    }

    @Test
    public void test_checkIfOperationWasSuccessful_status_completed() {
        stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.COMPLETED);
        final MonitorCnfmJobTask mockedMonitorCnfmCreateJobTask = Mockito.spy(monitorCnfmCreateJobTask);
        mockedMonitorCnfmCreateJobTask.checkIfOperationWasSuccessful(stubbedExecution);
        verify(mockedMonitorCnfmCreateJobTask, times(1)).checkIfOperationWasSuccessful(stubbedExecution);
    }

    @Test
    public void test_checkIfOperationWasSuccessful_status_Failed() {
        Optional<AsLcmOpOcc> mockedAsLcmOpOcc = getAsLcmOpOcc();
        mockedAsLcmOpOcc.orElseThrow().setOperationState(AsLcmOpOcc.OperationStateEnum.FAILED);
        stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.FAILED);
        stubbedExecution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME, mockedAsLcmOpOcc.orElseThrow());
        monitorCnfmCreateJobTask.checkIfOperationWasSuccessful(stubbedExecution);
        verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1206),
                any(Exception.class));
    }

    @Test
    public void test_hasOperationFinished_status_completed() {
        stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.COMPLETED);
        boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution);
        assertTrue(returnedValue);
    }

    @Test
    public void test_hasOperationFinished_status_failed() {
        stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.FAILED);
        boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution);
        assertTrue(returnedValue);
    }

    @Test
    public void test_hasOperationFinished_status_rollback() {
        stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.ROLLED_BACK);
        boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution);
        assertTrue(returnedValue);
    }

    @Test
    public void test_hasOperationFinished_status_null() {
        stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, null);
        boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution);
        assertFalse(returnedValue);
    }

    @Test
    public void test_timeOutLogFailure() {
        monitorCnfmCreateJobTask.timeOutLogFailure(stubbedExecution);
        verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1205),
                any(Exception.class));
    }

    private Optional<AsLcmOpOcc> getAsLcmOpOcc() {
        final AsLcmOpOcc asLcmOpOcc = new AsLcmOpOcc();
        asLcmOpOcc.setOperationState(AsLcmOpOcc.OperationStateEnum.COMPLETED);
        return Optional.of(asLcmOpOcc);
    }

}