1 package org.onap.so.bpmn.infrastructure.pnf.delegate;
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;
24 public class RegisterForPnfReadyEventTest {
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";
30 private DelegateExecution delegateExecution;
31 private ExtractPojosForBB extractPojosForBBMock;
32 private DmaapClientTestImpl dmaapClientTest;
33 private MessageCorrelationBuilder messageCorrelationBuilder;
34 private ExceptionBuilder exceptionBuilderMock;
35 private BuildingBlockExecution buildingBlockExecution;
37 private RegisterForPnfReadyEvent testedObject;
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);
50 public void shouldRegisterForDmaapClient() throws BBObjectNotFoundException {
52 testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
53 PNF_ENTRY_NOTIFICATION_TIMEOUT);
55 pnf.setPnfName(PNF_NAME);
56 when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(pnf);
58 testedObject.execute(delegateExecution);
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);
67 public void pnfNotFoundInBBexecution_WorkflowExIsThrown() throws BBObjectNotFoundException {
69 testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
70 PNF_ENTRY_NOTIFICATION_TIMEOUT);
71 when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF))
72 .thenThrow(BBObjectNotFoundException.class);
74 testedObject.execute(delegateExecution);
76 verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000,
77 "pnf resource not found in buildingBlockExecution while registering to dmaap listener");
81 public void pnfNameIsNull_WorkflowExIsThrown() throws BBObjectNotFoundException {
83 testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
84 PNF_ENTRY_NOTIFICATION_TIMEOUT);
85 when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(new Pnf());
87 testedObject.execute(delegateExecution);
89 verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000, "pnf name is not set");
93 public void pnfEventNotificationTimeoutNotSet_WorkflowExIsThrown() throws BBObjectNotFoundException {
95 testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock, null);
96 when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(new Pnf());
98 testedObject.execute(delegateExecution);
100 verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000,
101 "pnfEntryNotificationTimeout value not defined");
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();
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);
119 messageCorrelationBuilder = mock(MessageCorrelationBuilder.class);
120 when(runtimeService.createMessageCorrelation(any())).thenReturn(messageCorrelationBuilder);
121 when(messageCorrelationBuilder.processInstanceId(any())).thenReturn(messageCorrelationBuilder);
123 return delegateExecution;