Create WaitForPnfReadyBB
[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
28     private DelegateExecution delegateExecution;
29     private ExtractPojosForBB extractPojosForBBMock;
30     private DmaapClientTestImpl dmaapClientTest;
31     private MessageCorrelationBuilder messageCorrelationBuilder;
32     private ExceptionBuilder exceptionBuilderMock;
33     private BuildingBlockExecution buildingBlockExecution;
34     private static final String PNF_ENTRY_NOTIFICATION_TIMEOUT = "P14D";
35
36     private RegisterForPnfReadyEvent testedObject;
37
38     @Before
39     public void init() {
40         delegateExecution = prepareExecution();
41         dmaapClientTest = new DmaapClientTestImpl();
42         exceptionBuilderMock = mock(ExceptionBuilder.class);
43         extractPojosForBBMock = mock(ExtractPojosForBB.class);
44         buildingBlockExecution = new DelegateExecutionImpl(new HashMap<>());
45         when(delegateExecution.getVariable("gBuildingBlockExecution")).thenReturn(buildingBlockExecution);
46     }
47
48     @Test
49     public void shouldRegisterForDmaapClient() throws BBObjectNotFoundException {
50         // given
51         testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
52                 PNF_ENTRY_NOTIFICATION_TIMEOUT);
53         Pnf pnf = new Pnf();
54         pnf.setPnfName(PNF_NAME);
55         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(pnf);
56         // when
57         testedObject.execute(delegateExecution);
58         // then
59         verify(delegateExecution).setVariable(ExecutionVariableNames.PNF_CORRELATION_ID, PNF_NAME);
60         verify(delegateExecution).setVariable(ExecutionVariableNames.TIMEOUT_FOR_NOTIFICATION,
61                 PNF_ENTRY_NOTIFICATION_TIMEOUT);
62         checkIfInformConsumerThreadIsRunProperly(dmaapClientTest);
63     }
64
65     @Test
66     public void pnfNotFoundInBBexecution_WorkflowExIsThrown() throws BBObjectNotFoundException {
67         // given
68         testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
69                 PNF_ENTRY_NOTIFICATION_TIMEOUT);
70         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF))
71                 .thenThrow(BBObjectNotFoundException.class);
72         // when
73         testedObject.execute(delegateExecution);
74         // then
75         verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000,
76                 "pnf resource not found in buildingBlockExecution while registering to dmaap listener");
77     }
78
79     @Test
80     public void pnfNameIsNull_WorkflowExIsThrown() throws BBObjectNotFoundException {
81         // given
82         testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
83                 PNF_ENTRY_NOTIFICATION_TIMEOUT);
84         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(new Pnf());
85         // when
86         testedObject.execute(delegateExecution);
87         // then
88         verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000, "pnf name is not set");
89     }
90
91     @Test
92     public void pnfEventNotificationTimeoutNotSet_WorkflowExIsThrown() throws BBObjectNotFoundException {
93         // given
94         testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock, null);
95         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(new Pnf());
96         // when
97         testedObject.execute(delegateExecution);
98         // then
99         verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000,
100                 "pnfEntryNotificationTimeout value not defined");
101     }
102
103     private void checkIfInformConsumerThreadIsRunProperly(DmaapClientTestImpl dmaapClientTest) {
104         dmaapClientTest.getInformConsumer().run();
105         InOrder inOrder = inOrder(messageCorrelationBuilder);
106         inOrder.verify(messageCorrelationBuilder).processInstanceBusinessKey("testBusinessKey");
107         inOrder.verify(messageCorrelationBuilder).correlateWithResult();
108     }
109
110     private DelegateExecution prepareExecution() {
111         DelegateExecution delegateExecution = mock(DelegateExecution.class);
112         when(delegateExecution.getProcessBusinessKey()).thenReturn("testBusinessKey");
113         ProcessEngineServices processEngineServices = mock(ProcessEngineServices.class);
114         when(delegateExecution.getProcessEngineServices()).thenReturn(processEngineServices);
115         RuntimeService runtimeService = mock(RuntimeService.class);
116         when(processEngineServices.getRuntimeService()).thenReturn(runtimeService);
117
118         messageCorrelationBuilder = mock(MessageCorrelationBuilder.class);
119         when(runtimeService.createMessageCorrelation(any())).thenReturn(messageCorrelationBuilder);
120         when(messageCorrelationBuilder.processInstanceBusinessKey(any())).thenReturn(messageCorrelationBuilder);
121
122         return delegateExecution;
123     }
124
125 }