7ec05fda04e9efe52166ac3bf84cee94713f0854
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / java / org / onap / so / bpmn / infrastructure / pnf / delegate / RegisterForPnfReadyEventTest.java
1 package org.onap.so.bpmn.infrastructure.pnf.delegate;
2
3 import static org.mockito.ArgumentMatchers.any;
4 import static org.mockito.Mockito.inOrder;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.verify;
7 import static org.mockito.Mockito.when;
8 import java.util.HashMap;
9 import org.camunda.bpm.engine.ProcessEngineServices;
10 import org.camunda.bpm.engine.RuntimeService;
11 import org.camunda.bpm.engine.delegate.DelegateExecution;
12 import org.camunda.bpm.engine.runtime.MessageCorrelationBuilder;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.InOrder;
16 import org.onap.so.bpmn.common.BuildingBlockExecution;
17 import org.onap.so.bpmn.common.DelegateExecutionImpl;
18 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
19 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
20 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
21 import org.onap.so.client.exception.BBObjectNotFoundException;
22 import org.onap.so.client.exception.ExceptionBuilder;
23
24 public class RegisterForPnfReadyEventTest {
25
26     private static final String PNF_NAME = "pnfNameTest";
27     private static final String PNF_ENTRY_NOTIFICATION_TIMEOUT = "P14D";
28     private static final String PROCESS_INSTANCE_ID = "testInstanceId";
29
30     private DelegateExecution delegateExecution;
31     private ExtractPojosForBB extractPojosForBBMock;
32     private DmaapClientTestImpl dmaapClientTest;
33     private MessageCorrelationBuilder messageCorrelationBuilder;
34     private ExceptionBuilder exceptionBuilderMock;
35     private BuildingBlockExecution buildingBlockExecution;
36
37     private RegisterForPnfReadyEvent testedObject;
38
39     @Before
40     public void init() {
41         delegateExecution = prepareExecution();
42         dmaapClientTest = new DmaapClientTestImpl();
43         exceptionBuilderMock = mock(ExceptionBuilder.class);
44         extractPojosForBBMock = mock(ExtractPojosForBB.class);
45         buildingBlockExecution = new DelegateExecutionImpl(new HashMap<>());
46         when(delegateExecution.getVariable("gBuildingBlockExecution")).thenReturn(buildingBlockExecution);
47     }
48
49     @Test
50     public void shouldRegisterForDmaapClient() throws BBObjectNotFoundException {
51         // given
52         testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
53                 PNF_ENTRY_NOTIFICATION_TIMEOUT);
54         Pnf pnf = new Pnf();
55         pnf.setPnfName(PNF_NAME);
56         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(pnf);
57         // when
58         testedObject.execute(delegateExecution);
59         // then
60         verify(delegateExecution).setVariable(ExecutionVariableNames.PNF_CORRELATION_ID, PNF_NAME);
61         verify(delegateExecution).setVariable(ExecutionVariableNames.TIMEOUT_FOR_NOTIFICATION,
62                 PNF_ENTRY_NOTIFICATION_TIMEOUT);
63         checkIfInformConsumerThreadIsRunProperly(dmaapClientTest);
64     }
65
66     @Test
67     public void pnfNotFoundInBBexecution_WorkflowExIsThrown() throws BBObjectNotFoundException {
68         // given
69         testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
70                 PNF_ENTRY_NOTIFICATION_TIMEOUT);
71         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF))
72                 .thenThrow(BBObjectNotFoundException.class);
73         // when
74         testedObject.execute(delegateExecution);
75         // then
76         verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000,
77                 "pnf resource not found in buildingBlockExecution while registering to dmaap listener");
78     }
79
80     @Test
81     public void pnfNameIsNull_WorkflowExIsThrown() throws BBObjectNotFoundException {
82         // given
83         testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
84                 PNF_ENTRY_NOTIFICATION_TIMEOUT);
85         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(new Pnf());
86         // when
87         testedObject.execute(delegateExecution);
88         // then
89         verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000, "pnf name is not set");
90     }
91
92     @Test
93     public void pnfEventNotificationTimeoutNotSet_WorkflowExIsThrown() throws BBObjectNotFoundException {
94         // given
95         testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock, null);
96         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(new Pnf());
97         // when
98         testedObject.execute(delegateExecution);
99         // then
100         verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000,
101                 "pnfEntryNotificationTimeout value not defined");
102     }
103
104     private void checkIfInformConsumerThreadIsRunProperly(DmaapClientTestImpl dmaapClientTest) {
105         dmaapClientTest.getInformConsumer().run();
106         InOrder inOrder = inOrder(messageCorrelationBuilder);
107         inOrder.verify(messageCorrelationBuilder).processInstanceId(PROCESS_INSTANCE_ID);
108         inOrder.verify(messageCorrelationBuilder).correlateWithResult();
109     }
110
111     private DelegateExecution prepareExecution() {
112         DelegateExecution delegateExecution = mock(DelegateExecution.class);
113         when(delegateExecution.getProcessInstanceId()).thenReturn(PROCESS_INSTANCE_ID);
114         ProcessEngineServices processEngineServices = mock(ProcessEngineServices.class);
115         when(delegateExecution.getProcessEngineServices()).thenReturn(processEngineServices);
116         RuntimeService runtimeService = mock(RuntimeService.class);
117         when(processEngineServices.getRuntimeService()).thenReturn(runtimeService);
118
119         messageCorrelationBuilder = mock(MessageCorrelationBuilder.class);
120         when(runtimeService.createMessageCorrelation(any())).thenReturn(messageCorrelationBuilder);
121         when(messageCorrelationBuilder.processInstanceId(any())).thenReturn(messageCorrelationBuilder);
122
123         return delegateExecution;
124     }
125
126 }