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";
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";
36 private RegisterForPnfReadyEvent testedObject;
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);
49 public void shouldRegisterForDmaapClient() throws BBObjectNotFoundException {
51 testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
52 PNF_ENTRY_NOTIFICATION_TIMEOUT);
54 pnf.setPnfName(PNF_NAME);
55 when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(pnf);
57 testedObject.execute(delegateExecution);
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);
66 public void pnfNotFoundInBBexecution_WorkflowExIsThrown() throws BBObjectNotFoundException {
68 testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
69 PNF_ENTRY_NOTIFICATION_TIMEOUT);
70 when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF))
71 .thenThrow(BBObjectNotFoundException.class);
73 testedObject.execute(delegateExecution);
75 verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000,
76 "pnf resource not found in buildingBlockExecution while registering to dmaap listener");
80 public void pnfNameIsNull_WorkflowExIsThrown() throws BBObjectNotFoundException {
82 testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock,
83 PNF_ENTRY_NOTIFICATION_TIMEOUT);
84 when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(new Pnf());
86 testedObject.execute(delegateExecution);
88 verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000, "pnf name is not set");
92 public void pnfEventNotificationTimeoutNotSet_WorkflowExIsThrown() throws BBObjectNotFoundException {
94 testedObject = new RegisterForPnfReadyEvent(dmaapClientTest, extractPojosForBBMock, exceptionBuilderMock, null);
95 when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.PNF)).thenReturn(new Pnf());
97 testedObject.execute(delegateExecution);
99 verify(exceptionBuilderMock).buildAndThrowWorkflowException(delegateExecution, 7000,
100 "pnfEntryNotificationTimeout value not defined");
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();
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);
118 messageCorrelationBuilder = mock(MessageCorrelationBuilder.class);
119 when(runtimeService.createMessageCorrelation(any())).thenReturn(messageCorrelationBuilder);
120 when(messageCorrelationBuilder.processInstanceBusinessKey(any())).thenReturn(messageCorrelationBuilder);
122 return delegateExecution;