From 3b3f509d9fb044e78bb63e6a0ff5935a1243d238 Mon Sep 17 00:00:00 2001 From: niamhcore Date: Tue, 24 Aug 2021 16:07:51 +0100 Subject: Fix exception when registration has no additionProperties Issue-ID: CPS-587 Signed-off-by: niamhcore Change-Id: Ic65b274e66cc7b2967826045a5da43ef2ee888f5 --- .../api/impl/NetworkCmProxyDataServiceImpl.java | 80 ++++++++++++---------- .../impl/NetworkCmProxyDataServiceImplSpec.groovy | 15 ++++ 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index 1ea95d3e65..a8c92d11f5 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -25,6 +25,7 @@ package org.onap.cps.ncmp.api.impl; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Collection; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; @@ -70,10 +71,11 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService /** * Constructor Injection for Dependencies. - * @param dmiOperations dmi operation - * @param cpsDataService Data Service Interface + * + * @param dmiOperations dmi operation + * @param cpsDataService Data Service Interface * @param cpsQueryService Query Service Interface - * @param objectMapper Object Mapper + * @param objectMapper Object Mapper */ public NetworkCmProxyDataServiceImpl(final DmiOperations dmiOperations, final CpsDataService cpsDataService, final CpsQueryService cpsQueryService, final ObjectMapper objectMapper) { @@ -95,7 +97,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService @Override public Collection queryDataNodes(final String cmHandle, final String cpsPath, - final FetchDescendantsOption fetchDescendantsOption) { + final FetchDescendantsOption fetchDescendantsOption) { return cpsQueryService.queryDataNodes(getDataspaceName(), cmHandle, cpsPath, fetchDescendantsOption); } @@ -137,7 +139,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService try { final List createdPersistenceCmHandles = new LinkedList<>(); - for (final CmHandle cmHandle: dmiPluginRegistration.getCreatedCmHandles()) { + for (final CmHandle cmHandle : dmiPluginRegistration.getCreatedCmHandles()) { createdPersistenceCmHandles.add(toPersistenceCmHandle(dmiPluginRegistration, cmHandle)); } final PersistenceCmHandlesList persistenceCmHandlesList = new PersistenceCmHandlesList(); @@ -157,7 +159,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService try { final List updatedPersistenceCmHandles = new LinkedList<>(); - for (final CmHandle cmHandle: dmiPluginRegistration.getUpdatedCmHandles()) { + for (final CmHandle cmHandle : dmiPluginRegistration.getUpdatedCmHandles()) { updatedPersistenceCmHandles.add(toPersistenceCmHandle(dmiPluginRegistration, cmHandle)); } final PersistenceCmHandlesList persistenceCmHandlesList = new PersistenceCmHandlesList(); @@ -175,56 +177,56 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService @Override public Object getResourceDataOperationalForCmHandle(final @NotNull String cmHandle, - final @NotNull String resourceIdentifier, - final String acceptParam, - final String fieldsQueryParam, - final Integer depthQueryParam) { + final @NotNull String resourceIdentifier, + final String acceptParam, + final String fieldsQueryParam, + final Integer depthQueryParam) { final var dataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle); final var dmiServiceName = String.valueOf(dataNode.getLeaves().get("dmi-service-name")); final Collection additionalPropsList = dataNode.getChildDataNodes(); final var jsonBody = prepareOperationBody(GenericRequestBody.OperationEnum.READ, additionalPropsList); final ResponseEntity response = dmiOperations.getResouceDataOperationalFromDmi(dmiServiceName, - cmHandle, - resourceIdentifier, - fieldsQueryParam, - depthQueryParam, - acceptParam, - jsonBody); + cmHandle, + resourceIdentifier, + fieldsQueryParam, + depthQueryParam, + acceptParam, + jsonBody); return handleResponse(response); } @Override public Object getResourceDataPassThroughRunningForCmHandle(final @NotNull String cmHandle, - final @NotNull String resourceIdentifier, - final String accept, - final String fields, - final Integer depth) { + final @NotNull String resourceIdentifier, + final String accept, + final String fields, + final Integer depth) { final var cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle); final var dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get("dmi-service-name")); final Collection additionalPropsList = cmHandleDataNode.getChildDataNodes(); final var dmiRequesBody = prepareOperationBody(GenericRequestBody.OperationEnum.READ, additionalPropsList); final ResponseEntity response = dmiOperations.getResouceDataPassThroughRunningFromDmi(dmiServiceName, - cmHandle, - resourceIdentifier, - fields, - depth, - accept, - dmiRequesBody); + cmHandle, + resourceIdentifier, + fields, + depth, + accept, + dmiRequesBody); return handleResponse(response); } private DataNode fetchDataNodeFromDmiRegistryForCmHandle(final String cmHandle) { final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']"; final var dataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, - NCMP_DMI_REGISTRY_ANCHOR, - xpathForDmiRegistryToFetchCmHandle, - FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); + NCMP_DMI_REGISTRY_ANCHOR, + xpathForDmiRegistryToFetchCmHandle, + FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); return dataNode; } private String prepareOperationBody(final GenericRequestBody.OperationEnum operation, - final Collection additionalPropertyList) { + final Collection additionalPropertyList) { final var requestBody = new GenericRequestBody(); final Map additionalPropertyMap = getAdditionalPropertiesMap(additionalPropertyList); requestBody.setOperation(GenericRequestBody.OperationEnum.READ); @@ -235,7 +237,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } catch (final JsonProcessingException je) { log.error("Parsing error occurred while converting Object to JSON."); throw new NcmpException("Parsing error occurred while converting given object to JSON.", - je.getMessage()); + je.getMessage()); } } @@ -244,9 +246,9 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return null; } final Map additionalPropertyMap = new LinkedHashMap<>(); - for (final var node: additionalPropertyList) { + for (final var node : additionalPropertyList) { additionalPropertyMap.put(String.valueOf(node.getLeaves().get("name")), - String.valueOf(node.getLeaves().get("value"))); + String.valueOf(node.getLeaves().get("value"))); } return additionalPropertyMap; } @@ -256,17 +258,21 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return responseEntity.getBody(); } else { throw new NcmpException("Not able to get resource data.", - "DMI status code: " + responseEntity.getStatusCodeValue() - + ", DMI response body: " + responseEntity.getBody()); + "DMI status code: " + responseEntity.getStatusCodeValue() + + ", DMI response body: " + responseEntity.getBody()); } } private PersistenceCmHandle toPersistenceCmHandle(final DmiPluginRegistration dmiPluginRegistration, - final CmHandle cmHandle) { + final CmHandle cmHandle) { final PersistenceCmHandle persistenceCmHandle = new PersistenceCmHandle(); persistenceCmHandle.setDmiServiceName(dmiPluginRegistration.getDmiPlugin()); persistenceCmHandle.setId(cmHandle.getCmHandleID()); - persistenceCmHandle.setAdditionalProperties(cmHandle.getCmHandleProperties()); + if (cmHandle.getCmHandleProperties() == null) { + persistenceCmHandle.setAdditionalProperties(Collections.EMPTY_MAP); + } else { + persistenceCmHandle.setAdditionalProperties(cmHandle.getCmHandleProperties()); + } return persistenceCmHandle; } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy index f356ce66c1..34c9541759 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy @@ -131,6 +131,21 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { 'create and update' | [persistenceCmHandle ] | [persistenceCmHandle ] || 1 | 1 } + + def 'Register a DMI Plugin for the given cmHandle without additional properties.'() { + given: 'a registration without cmHandle properties ' + def dmiPluginRegistration = new DmiPluginRegistration() + dmiPluginRegistration.dmiPlugin = 'my-server' + persistenceCmHandle.cmHandleID = '123' + persistenceCmHandle.cmHandleProperties = null + dmiPluginRegistration.createdCmHandles = [persistenceCmHandle ] + def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","additional-properties":[]}]}' + when: 'registration is updated' + objectUnderTest.updateDmiPluginRegistration(dmiPluginRegistration) + then: 'the CPS save list node data is invoked with the expected parameters' + 1 * mockCpsDataService.saveListNodeData('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData) + } + def 'Get resource data for pass-through operational from dmi.'() { given: 'xpath' def xpath = "/dmi-registry/cm-handles[@id='testCmHandle']" -- cgit 1.2.3-korg