diff options
Diffstat (limited to 'bpmn/so-bpmn-infrastructure-common/src')
2 files changed, 224 insertions, 0 deletions
diff --git a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/delegate/RegisterForPnfReadyEvent.java b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/delegate/RegisterForPnfReadyEvent.java new file mode 100644 index 0000000000..f0eadbbf78 --- /dev/null +++ b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/delegate/RegisterForPnfReadyEvent.java @@ -0,0 +1,99 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2020 Nokia. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.bpmn.infrastructure.pnf.delegate; + +import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.TIMEOUT_FOR_NOTIFICATION; +import com.google.common.base.Strings; +import org.camunda.bpm.engine.RuntimeService; +import org.camunda.bpm.engine.delegate.DelegateExecution; +import org.camunda.bpm.engine.delegate.JavaDelegate; +import org.onap.so.bpmn.common.BuildingBlockExecution; +import org.onap.so.bpmn.infrastructure.pnf.dmaap.DmaapClient; +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * This class is designed to be used within WaitForPnfReadyBB + */ +@Component +public class RegisterForPnfReadyEvent implements JavaDelegate { + + private static final String ERROR_MESSAGE_PNF_NOT_FOUND = + "pnf resource not found in buildingBlockExecution while registering to dmaap listener"; + private static final Logger LOGGER = LoggerFactory.getLogger(RegisterForPnfReadyEvent.class); + + private DmaapClient dmaapClient; + private ExtractPojosForBB extractPojosForBB; + private ExceptionBuilder exceptionBuilder; + private String pnfEntryNotificationTimeout; + + @Autowired + public RegisterForPnfReadyEvent(DmaapClient dmaapClient, ExtractPojosForBB extractPojosForBB, + ExceptionBuilder exceptionBuilder, + @Value("${aai.pnfEntryNotificationTimeout}") String pnfEntryNotificationTimeout) { + this.dmaapClient = dmaapClient; + this.extractPojosForBB = extractPojosForBB; + this.exceptionBuilder = exceptionBuilder; + this.pnfEntryNotificationTimeout = pnfEntryNotificationTimeout; + } + + @Override + public void execute(DelegateExecution execution) { + try { + String pnfName = getPnfName(execution); + fillExecution(execution, pnfName); + RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService(); + dmaapClient.registerForUpdate(pnfName, () -> runtimeService.createMessageCorrelation("WorkflowMessage") + .processInstanceBusinessKey(execution.getProcessBusinessKey()).correlateWithResult()); + } catch (BBObjectNotFoundException e) { + LOGGER.error(ERROR_MESSAGE_PNF_NOT_FOUND); + exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ERROR_MESSAGE_PNF_NOT_FOUND); + } + } + + private void fillExecution(DelegateExecution execution, String pnfName) { + execution.setVariable(ExecutionVariableNames.PNF_CORRELATION_ID, pnfName); + if (Strings.isNullOrEmpty(pnfEntryNotificationTimeout)) { + exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, + "pnfEntryNotificationTimeout value not defined"); + } + execution.setVariable(TIMEOUT_FOR_NOTIFICATION, pnfEntryNotificationTimeout); + } + + private String getPnfName(DelegateExecution execution) throws BBObjectNotFoundException { + BuildingBlockExecution buildingBlockExecution = + (BuildingBlockExecution) execution.getVariable("gBuildingBlockExecution"); + Pnf pnf = extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.PNF); + String pnfName = pnf.getPnfName(); + if (Strings.isNullOrEmpty(pnfName)) { + exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "pnf name is not set"); + } + return pnfName; + } +} diff --git a/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/delegate/RegisterForPnfReadyEventTest.java b/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/delegate/RegisterForPnfReadyEventTest.java new file mode 100644 index 0000000000..a82fb5d823 --- /dev/null +++ b/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/delegate/RegisterForPnfReadyEventTest.java @@ -0,0 +1,125 @@ +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 DelegateExecution delegateExecution; + private ExtractPojosForBB extractPojosForBBMock; + private DmaapClientTestImpl dmaapClientTest; + private MessageCorrelationBuilder messageCorrelationBuilder; + private ExceptionBuilder exceptionBuilderMock; + private BuildingBlockExecution buildingBlockExecution; + private static final String PNF_ENTRY_NOTIFICATION_TIMEOUT = "P14D"; + + 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).processInstanceBusinessKey("testBusinessKey"); + inOrder.verify(messageCorrelationBuilder).correlateWithResult(); + } + + private DelegateExecution prepareExecution() { + DelegateExecution delegateExecution = mock(DelegateExecution.class); + when(delegateExecution.getProcessBusinessKey()).thenReturn("testBusinessKey"); + 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.processInstanceBusinessKey(any())).thenReturn(messageCorrelationBuilder); + + return delegateExecution; + } + +} |