aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/delegate/RegisterForPnfReadyEventTest.java
blob: 7ec05fda04e9efe52166ac3bf84cee94713f0854 (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
package org.onap.so.bpmn.infrastructure.pnf.delegate;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import org.camunda.bpm.engine.ProcessEngineServices;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.runtime.MessageCorrelationBuilder;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.onap.so.bpmn.common.BuildingBlockExecution;
import org.onap.so.bpmn.common.DelegateExecutionImpl;
import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
import org.onap.so.client.exception.BBObjectNotFoundException;
import org.onap.so.client.exception.ExceptionBuilder;

public class RegisterForPnfReadyEventTest {

    private static final String PNF_NAME = "pnfNameTest";
    private static final String PNF_ENTRY_NOTIFICATION_TIMEOUT = "P14D";
    private static final String PROCESS_INSTANCE_ID = "testInstanceId";

    private DelegateExecution delegateExecution;
    private ExtractPojosForBB extractPojosForBBMock;
    private DmaapClientTestImpl dmaapClientTest;
    private MessageCorrelationBuilder messageCorrelationBuilder;
    private ExceptionBuilder exceptionBuilderMock;
    private BuildingBlockExecution buildingBlockExecution;

    private RegisterForPnfReadyEvent testedObject;

    @Before
    public void init() {
        delegateExecution = prepareExecution();
        dmaapClientTest = new DmaapClientTestImpl();
        exceptionBuilderMock = mock(ExceptionBuilder.class);
        extractPojosForBBMock = mock(ExtractPojosForBB.class);
        buildingBlockExecution = new DelegateExecutionImpl(new HashMap<>());
        when(delegateExecution.getVariable("gBuildingBlockExecution")).thenReturn(buildingBlockExecution);
    }

    @Test
    public void shouldRegisterForDmaapClient() throws BBObjectNotFoundException {
        // given
        testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
                PNF_ENTRY_NOTIFICATION_TIMEOUT);
        Pnf pnf = new Pnf();
        pnf.setPnfName(PNF_NAME);
        when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(pnf);
        // when
        testedObject.execute(delegateExecution);
        // then
        verify(delegateExecution).setVariable(ExecutionVariableNames.PNF_CORRELATION_ID, PNF_NAME);
        verify(delegateExecution).setVariable(ExecutionVariableNames.TIMEOUT_FOR_NOTIFICATION,
                PNF_ENTRY_NOTIFICATION_TIMEOUT);
        checkIfInformConsumerThreadIsRunProperly(dmaapClientTest);
    }

    @Test
    public void pnfNotFoundInBBexecution_WorkflowExIsThrown() throws BBObjectNotFoundException {
        // given
        testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
                PNF_ENTRY_NOTIFICATION_TIMEOUT);
        when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF))
                .thenThrow(BBObjectNotFoundException.class);
        // when
        testedObject.execute(delegateExecution);
        // then
        verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000,
                "pnf resource not found in buildingBlockExecution while registering to dmaap listener");
    }

    @Test
    public void pnfNameIsNull_WorkflowExIsThrown() throws BBObjectNotFoundException {
        // given
        testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
                PNF_ENTRY_NOTIFICATION_TIMEOUT);
        when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(new Pnf());
        // when
        testedObject.execute(delegateExecution);
        // then
        verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000, "pnf name is not set");
    }

    @Test
    public void pnfEventNotificationTimeoutNotSet_WorkflowExIsThrown() throws BBObjectNotFoundException {
        // given
        testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock, null);
        when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(new Pnf());
        // when
        testedObject.execute(delegateExecution);
        // then
        verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000,
                "pnfEntryNotificationTimeout value not defined");
    }

    private void checkIfInformConsumerThreadIsRunProperly(DmaapClientTestImpl dmaapClientTest) {
        dmaapClientTest.getInformConsumer().run();
        InOrder inOrder = inOrder(messageCorrelationBuilder);
        inOrder.verify(messageCorrelationBuilder).processInstanceId(PROCESS_INSTANCE_ID);
        inOrder.verify(messageCorrelationBuilder).correlateWithResult();
    }

    private DelegateExecution prepareExecution() {
        DelegateExecution delegateExecution = mock(DelegateExecution.class);
        when(delegateExecution.getProcessInstanceId()).thenReturn(PROCESS_INSTANCE_ID);
        ProcessEngineServices processEngineServices = mock(ProcessEngineServices.class);
        when(delegateExecution.getProcessEngineServices()).thenReturn(processEngineServices);
        RuntimeService runtimeService = mock(RuntimeService.class);
        when(processEngineServices.getRuntimeService()).thenReturn(runtimeService);

        messageCorrelationBuilder = mock(MessageCorrelationBuilder.class);
        when(runtimeService.createMessageCorrelation(any())).thenReturn(messageCorrelationBuilder);
        when(messageCorrelationBuilder.processInstanceId(any())).thenReturn(messageCorrelationBuilder);

        return delegateExecution;
    }

}