diff options
Diffstat (limited to 'cps-ncmp-service/src')
9 files changed, 79 insertions, 102 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 240d6b5ca9..82145efc3d 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 @@ -27,13 +27,13 @@ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.CpsAdminService; import org.onap.cps.api.CpsDataService; @@ -50,11 +50,13 @@ import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList; import org.onap.cps.spi.exceptions.DataNodeNotFoundException; import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.spi.model.ModuleReference; +import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; @Slf4j @Service +@RequiredArgsConstructor public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService { private static final String NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME = "NFP-Operational"; @@ -65,37 +67,17 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService private static final OffsetDateTime NO_TIMESTAMP = null; - private CpsDataService cpsDataService; + private final CpsDataService cpsDataService; - private ObjectMapper objectMapper; + private final JsonObjectMapper jsonObjectMapper; - private DmiDataOperations dmiDataOperations; + private final DmiDataOperations dmiDataOperations; - private DmiModelOperations dmiModelOperations; + private final DmiModelOperations dmiModelOperations; - private CpsModuleService cpsModuleService; + private final CpsModuleService cpsModuleService; - private CpsAdminService cpsAdminService; - - /** - * Constructor Injection for Dependencies. - * @param dmiDataOperations DMI operation - * @param cpsDataService Data Service Interface - * @param objectMapper Object Mapper - */ - public NetworkCmProxyDataServiceImpl(final DmiDataOperations dmiDataOperations, - final DmiModelOperations dmiModelOperations, - final CpsModuleService cpsModuleService, - final CpsDataService cpsDataService, - final CpsAdminService cpsAdminService, - final ObjectMapper objectMapper) { - this.dmiDataOperations = dmiDataOperations; - this.dmiModelOperations = dmiModelOperations; - this.cpsModuleService = cpsModuleService; - this.cpsDataService = cpsDataService; - this.cpsAdminService = cpsAdminService; - this.objectMapper = objectMapper; - } + private final CpsAdminService cpsAdminService; @Override public void updateDmiRegistrationAndSyncModule(final DmiPluginRegistration dmiPluginRegistration) { @@ -198,7 +180,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService throws JsonProcessingException { final PersistenceCmHandlesList updatedPersistenceCmHandlesList = getUpdatedPersistenceCmHandlesList(dmiPluginRegistration, dmiPluginRegistration.getUpdatedCmHandles()); - final String cmHandlesAsJson = objectMapper.writeValueAsString(updatedPersistenceCmHandlesList); + final String cmHandlesAsJson = jsonObjectMapper.asJsonString(updatedPersistenceCmHandlesList); cpsDataService.updateNodeLeavesAndExistingDescendantLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, "/dmi-registry", cmHandlesAsJson, NO_TIMESTAMP); } @@ -223,7 +205,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService private void registerAndSyncNewCmHandles(final PersistenceCmHandlesList persistenceCmHandlesList) throws JsonProcessingException { - final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList); + final String cmHandleJsonData = jsonObjectMapper.asJsonString(persistenceCmHandlesList); cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, "/dmi-registry", cmHandleJsonData, NO_TIMESTAMP); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java index 095f677ef8..3bfb424a2e 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,10 +25,10 @@ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ; import static org.onap.cps.ncmp.api.impl.operations.RequiredDmiService.DATA; -import com.fasterxml.jackson.databind.ObjectMapper; import org.onap.cps.ncmp.api.impl.client.DmiRestClient; import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration; import org.onap.cps.ncmp.api.models.PersistenceCmHandle; +import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; @@ -45,10 +45,10 @@ public class DmiDataOperations extends DmiOperations { * @param dmiRestClient {@code DmiRestClient} */ public DmiDataOperations(final PersistenceCmHandleRetriever cmHandlePropertiesRetriever, - final ObjectMapper objectMapper, + final JsonObjectMapper jsonObjectMapper, final NcmpConfiguration.DmiProperties dmiProperties, final DmiRestClient dmiRestClient) { - super(cmHandlePropertiesRetriever, objectMapper, dmiProperties, dmiRestClient); + super(cmHandlePropertiesRetriever, jsonObjectMapper, dmiProperties, dmiRestClient); } /** @@ -73,7 +73,7 @@ public class DmiDataOperations extends DmiOperations { .operation(READ) .build(); dmiRequestBody.asCmHandleProperties(persistenceCmHandle.getAdditionalProperties()); - final String jsonBody = getDmiRequestBodyAsString(dmiRequestBody); + final String jsonBody = jsonObjectMapper.asJsonString(dmiRequestBody); final var dmiResourceDataUrl = getDmiDatastoreUrlWithOptions( persistenceCmHandle.resolveDmiServiceName(DATA), cmHandle, resourceId, @@ -106,7 +106,7 @@ public class DmiDataOperations extends DmiOperations { .dataType(dataType) .build(); dmiRequestBody.asCmHandleProperties(persistenceCmHandle.getAdditionalProperties()); - final String jsonBody = getDmiRequestBodyAsString(dmiRequestBody); + final String jsonBody = jsonObjectMapper.asJsonString(dmiRequestBody); final String dmiUrl = getResourceInDataStoreUrl(persistenceCmHandle.resolveDmiServiceName(DATA), cmHandle, resourceId, PASSTHROUGH_RUNNING); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java index 20a3309d01..1ce6dcfcd5 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,6 @@ package org.onap.cps.ncmp.api.impl.operations; import static org.onap.cps.ncmp.api.impl.operations.RequiredDmiService.MODEL; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.util.ArrayList; @@ -34,10 +33,14 @@ import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration; import org.onap.cps.ncmp.api.models.PersistenceCmHandle; import org.onap.cps.ncmp.api.models.YangResource; import org.onap.cps.spi.model.ModuleReference; +import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; +/** + * Operations class for DMI Model. + */ @Component public class DmiModelOperations extends DmiOperations { @@ -47,10 +50,10 @@ public class DmiModelOperations extends DmiOperations { * @param dmiRestClient {@code DmiRestClient} */ public DmiModelOperations(final PersistenceCmHandleRetriever cmHandlePropertiesRetriever, - final ObjectMapper objectMapper, + final JsonObjectMapper jsonObjectMapper, final NcmpConfiguration.DmiProperties dmiProperties, final DmiRestClient dmiRestClient) { - super(cmHandlePropertiesRetriever, objectMapper, dmiProperties, dmiRestClient); + super(cmHandlePropertiesRetriever, jsonObjectMapper, dmiProperties, dmiRestClient); } /** @@ -65,7 +68,7 @@ public class DmiModelOperations extends DmiOperations { dmiRequestBody.asCmHandleProperties(persistenceCmHandle.getAdditionalProperties()); final ResponseEntity<Object> dmiFetchModulesResponseEntity = getResourceFromDmiWithJsonData( persistenceCmHandle.resolveDmiServiceName(MODEL), - getDmiRequestBodyAsString(dmiRequestBody), persistenceCmHandle.getId(), "modules"); + jsonObjectMapper.asJsonString(dmiRequestBody), persistenceCmHandle.getId(), "modules"); return toModuleReferences((Map) dmiFetchModulesResponseEntity.getBody()); } @@ -144,7 +147,7 @@ public class DmiModelOperations extends DmiOperations { if (moduleReferencesAsList != null) { moduleReferencesAsList.forEach(moduleReferenceAsMap -> { final ModuleReference moduleReference = - objectMapper.convertValue(moduleReferenceAsMap, ModuleReference.class); + jsonObjectMapper.convertToValueType(moduleReferenceAsMap, ModuleReference.class); moduleReferences.add(moduleReference); }); } @@ -159,7 +162,7 @@ public class DmiModelOperations extends DmiOperations { if (yangResourcesAsList != null) { yangResourcesAsList.forEach(yangResourceAsMap -> { final YangResource yangResource = - objectMapper.convertValue(yangResourceAsMap, YangResource.class); + jsonObjectMapper.convertToValueType(yangResourceAsMap, YangResource.class); yangResourcesModuleNameToContentMap.put(yangResource.getModuleName(), yangResource.getYangSource()); }); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiOperations.java index 509728061e..2f7376e87c 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiOperations.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,17 +20,19 @@ package org.onap.cps.ncmp.api.impl.operations; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Strings; import lombok.Getter; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.impl.client.DmiRestClient; import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration; -import org.onap.cps.ncmp.api.impl.exception.NcmpException; +import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.HttpHeaders; +import org.springframework.stereotype.Service; @Slf4j +@RequiredArgsConstructor +@Service public class DmiOperations { @Getter @@ -44,28 +46,13 @@ public class DmiOperations { } } - protected ObjectMapper objectMapper; - protected PersistenceCmHandleRetriever cmHandlePropertiesRetriever; - protected DmiRestClient dmiRestClient; - protected NcmpConfiguration.DmiProperties dmiProperties; + protected final PersistenceCmHandleRetriever cmHandlePropertiesRetriever; + protected final JsonObjectMapper jsonObjectMapper; + protected final NcmpConfiguration.DmiProperties dmiProperties; + protected final DmiRestClient dmiRestClient; static final String URL_SEPARATOR = "/"; - /** - * Constructor for {@code DmiOperations}. This method also manipulates url properties. - * - * @param dmiRestClient {@code DmiRestClient} - */ - public DmiOperations(final PersistenceCmHandleRetriever cmHandlePropertiesRetriever, - final ObjectMapper objectMapper, - final NcmpConfiguration.DmiProperties dmiProperties, - final DmiRestClient dmiRestClient) { - this.cmHandlePropertiesRetriever = cmHandlePropertiesRetriever; - this.objectMapper = objectMapper; - this.dmiRestClient = dmiRestClient; - this.dmiProperties = dmiProperties; - } - String getCmHandleUrl(final String dmiServiceName, final String cmHandle) { return dmiServiceName + dmiProperties.getDmiBasePath() @@ -95,20 +82,4 @@ public class DmiOperations { return httpHeaders; } - /** - * Convert DmiRequestBody to JSON. - * - * @param dmiRequestBody the dmi request body - * @return DmiRequestBody as JSON - */ - String getDmiRequestBodyAsString(final DmiRequestBody dmiRequestBody) { - try { - return objectMapper.writeValueAsString(dmiRequestBody); - } catch (final JsonProcessingException e) { - log.error("Parsing error occurred while converting Object to JSON."); - throw new NcmpException("Parsing error occurred while converting given object to JSON.", - e.getMessage()); - } - } - } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplModelSyncSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplModelSyncSpec.groovy index b7f059a7ce..464757158f 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplModelSyncSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplModelSyncSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,22 +20,25 @@ package org.onap.cps.ncmp.api.impl -import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.api.CpsAdminService import org.onap.cps.api.CpsModuleService +import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations import org.onap.cps.ncmp.api.models.PersistenceCmHandle import org.onap.cps.spi.model.ModuleReference +import org.onap.cps.utils.JsonObjectMapper import spock.lang.Specification class NetworkCmProxyDataServiceImplModelSyncSpec extends Specification { + def mockJsonObjectMapper = Mock(JsonObjectMapper) def mockCpsModuleService = Mock(CpsModuleService) def mockCpsAdminService = Mock(CpsAdminService) def mockDmiModelOperations = Mock(DmiModelOperations) + def mockDmiDataOperations = Mock(DmiDataOperations) - def objectUnderTest = new NetworkCmProxyDataServiceImpl(null, mockDmiModelOperations, - mockCpsModuleService, null, mockCpsAdminService, new ObjectMapper()) + def objectUnderTest = new NetworkCmProxyDataServiceImpl(null, mockJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations, + mockCpsModuleService, mockCpsAdminService) def expectedDataspaceName = 'NFP-Operational' diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy index 90fcbfc612..b954b41ef2 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,13 +22,17 @@ package org.onap.cps.ncmp.api.impl import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.databind.ObjectMapper +import org.onap.cps.api.CpsAdminService import org.onap.cps.api.CpsDataService import org.onap.cps.api.CpsModuleService import org.onap.cps.ncmp.api.impl.exception.NcmpException +import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations +import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations import org.onap.cps.ncmp.api.models.CmHandle import org.onap.cps.ncmp.api.models.DmiPluginRegistration import org.onap.cps.spi.exceptions.DataNodeNotFoundException import org.onap.cps.spi.exceptions.DataValidationException +import org.onap.cps.utils.JsonObjectMapper import spock.lang.Shared import spock.lang.Specification @@ -44,7 +48,10 @@ class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification { def mockCpsDataService = Mock(CpsDataService) def mockCpsModuleService = Mock(CpsModuleService) - def spyObjectMapper = Spy(ObjectMapper) + def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper())) + def mockCpsAdminService = Mock(CpsAdminService) + def mockDmiModelOperations = Mock(DmiModelOperations) + def mockDmiDataOperations = Mock(DmiDataOperations) def noTimestamp = null @@ -102,7 +109,7 @@ class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification { dmiPluginRegistration.createdCmHandles = createdCmHandles dmiPluginRegistration.updatedCmHandles = updatedCmHandles and: 'an json processing exception occurs' - spyObjectMapper.writeValueAsString(_) >> { throw (new JsonProcessingException('')) } + spiedJsonObjectMapper.asJsonString(_) >> { throw (new JsonProcessingException('')) } when: 'registration is updated and modules are synced' objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration) then: 'a data validation exception is thrown' @@ -182,8 +189,8 @@ class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification { } def getObjectUnderTestWithModelSyncDisabled() { - def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(null, null, mockCpsModuleService, - mockCpsDataService, null, spyObjectMapper)) + def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations, + mockCpsModuleService, mockCpsAdminService)) objectUnderTest.syncModulesAndCreateAnchor(*_) >> null return objectUnderTest } 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 5753d7b675..51af0f55e1 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 @@ -28,6 +28,8 @@ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE +import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations +import org.onap.cps.utils.JsonObjectMapper import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.api.CpsAdminService @@ -46,11 +48,12 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { def mockCpsDataService = Mock(CpsDataService) def mockCpsModuleService = Mock(CpsModuleService) def mockCpsAdminService = Mock(CpsAdminService) - def spyObjectMapper = Spy(ObjectMapper) + def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper())) + def mockDmiModelOperations = Mock(DmiModelOperations) def mockDmiDataOperations = Mock(DmiDataOperations) - def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockDmiDataOperations, null, - mockCpsModuleService, mockCpsDataService, mockCpsAdminService, spyObjectMapper) + def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations, + mockCpsModuleService, mockCpsAdminService) def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']" @@ -126,9 +129,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode and: 'objectMapper not able to parse object' - def mockObjectMapper = Mock(ObjectMapper) - objectUnderTest.objectMapper = mockObjectMapper - mockObjectMapper.writeValueAsString(_) >> { throw new JsonProcessingException('testException') } + spiedJsonObjectMapper.asJsonString(_) >> { throw new JsonProcessingException('testException') } and: 'dmi returns NOK response' mockDmiDataOperations.getResourceDataFromDmi(*_) >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND) diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy index b9704b1ea0..54ae0aa6fe 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,8 +20,10 @@ package org.onap.cps.ncmp.api.impl.operations -import org.onap.cps.ncmp.api.impl.client.DmiRestClient +import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration +import org.onap.cps.utils.JsonObjectMapper +import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.http.ResponseEntity @@ -37,6 +39,9 @@ import org.springframework.http.HttpStatus @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiDataOperations]) class DmiDataOperationsSpec extends DmiOperationsBaseSpec { + @SpringBean + JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper()) + @Autowired DmiDataOperations objectUnderTest diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy index 335bc062cc..2efd6c2b3a 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,9 +21,11 @@ package org.onap.cps.ncmp.api.impl.operations import com.fasterxml.jackson.core.JsonProcessingException +import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration -import org.onap.cps.ncmp.api.impl.exception.NcmpException import org.onap.cps.spi.model.ModuleReference +import org.onap.cps.utils.JsonObjectMapper +import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.http.HttpStatus @@ -41,6 +43,9 @@ class DmiModelOperationsSpec extends DmiOperationsBaseSpec { @Autowired DmiModelOperations objectUnderTest + @SpringBean + JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper())) + def 'Retrieving module references.'() { given: 'a persistence cm handle' mockPersistenceCmHandleRetrieval([]) @@ -158,11 +163,11 @@ class DmiModelOperationsSpec extends DmiOperationsBaseSpec { given: 'a persistence cm handle' mockPersistenceCmHandleRetrieval([]) and: 'a Json processing exception occurs' - spyObjectMapper.writeValueAsString(_) >> {throw (new JsonProcessingException(''))} + spiedJsonObjectMapper.asJsonString(_) >> {throw (new JsonProcessingException('parsing error'))} when: 'a dmi operation is executed' objectUnderTest.getModuleReferences(persistenceCmHandle) then: 'an ncmp exception is thrown' - def exceptionThrown = thrown(NcmpException) + def exceptionThrown = thrown(JsonProcessingException) and: 'the message indicates a parsing error' exceptionThrown.message.toLowerCase().contains('parsing error') } |