diff options
author | Joseph Keenan <joseph.keenan@est.tech> | 2022-06-01 08:41:24 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2022-06-01 08:41:24 +0000 |
commit | e7ba1dee7d0431d00a67e078434778b559019e98 (patch) | |
tree | 1265c27701eb78030098adc609d29c83c49854fe | |
parent | 85d4473a5994e2111bb481a641e49badb8602fb0 (diff) | |
parent | d1a7dad5af98a7f3397f8535ba88129a0f7dce11 (diff) |
Merge "Handle RestTemplate Error handling so NCMP cna report correct server status"
9 files changed, 108 insertions, 229 deletions
diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy index fd3203b5b1..45ed3d3070 100644 --- a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy +++ b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy @@ -47,6 +47,7 @@ import spock.lang.Specification import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMP import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMPINVENTORY +import static org.springframework.http.HttpStatus.BAD_GATEWAY import static org.springframework.http.HttpStatus.BAD_REQUEST import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR import static org.springframework.http.HttpStatus.NOT_FOUND @@ -121,10 +122,9 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification { def 'Failing DMI Request - passthrough scenario'() { given: 'failing DMI request' - mockNetworkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(*_) >> { throw new HttpClientRequestException('Error Message Details NCMP', 'Bad Request from DMI', 400) } + setupTestException(new HttpClientRequestException('Error Message Details NCMP', 'Bad Request from DMI', 400) , NCMP) when: 'the DMI request is executed' - def response = mvc.perform(get("$dataNodeBaseEndpointNcmp/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=stores:bookstore/categories=100")) - .andReturn().response + def response = performTestRequest(NCMP) then: 'NCMP service responds with 502 Bad Gateway status' response.status == HttpStatus.BAD_GATEWAY.value() and: 'the NCMP response also contains the original DMI response details' 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 0e748c7fe7..240e3c57d1 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 @@ -46,7 +46,6 @@ import org.onap.cps.api.CpsAdminService; import org.onap.cps.api.CpsDataService; import org.onap.cps.api.CpsModuleService; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; -import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException; import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations; import org.onap.cps.ncmp.api.impl.operations.DmiOperations; import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever; @@ -114,9 +113,12 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final String optionsParamInQuery, final String topicParamInQuery, final String requestId) { - CpsValidator.validateNameCharacters(cmHandleId); - return getResourceDataResponse(cmHandleId, resourceIdentifier, - DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, optionsParamInQuery, topicParamInQuery, requestId); + final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId, + resourceIdentifier, + optionsParamInQuery, + DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, + requestId, topicParamInQuery); + return responseEntity.getBody(); } @Override @@ -125,21 +127,23 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final String optionsParamInQuery, final String topicParamInQuery, final String requestId) { - CpsValidator.validateNameCharacters(cmHandleId); - return getResourceDataResponse(cmHandleId, resourceIdentifier, - DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING, optionsParamInQuery, topicParamInQuery, requestId); + final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId, + resourceIdentifier, + optionsParamInQuery, + DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING, + requestId, topicParamInQuery); + return responseEntity.getBody(); } @Override public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId, - final String resourceIdentifier, - final OperationEnum operation, - final String requestData, - final String dataType) { + final String resourceIdentifier, + final OperationEnum operation, + final String requestData, + final String dataType) { CpsValidator.validateNameCharacters(cmHandleId); - return handleResponse( - dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation, - requestData, dataType), operation); + return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation, + requestData, dataType); } @@ -171,7 +175,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService }); return cpsAdminService.queryCmHandles(jsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, - org.onap.cps.spi.model.CmHandleQueryParameters.class)); + org.onap.cps.spi.model.CmHandleQueryParameters.class)); } /** @@ -243,7 +247,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final String schemaSetName = moduleSyncService.syncAndCreateSchemaSet(yangModelCmHandle); final String anchorName = yangModelCmHandle.getId(); cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName, - anchorName); + anchorName); } protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration( @@ -286,17 +290,6 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } } - private Object getResourceDataResponse(final String cmHandleId, - final String resourceIdentifier, - final DmiOperations.DataStoreEnum dataStore, - final String optionsParamInQuery, - final String topicParamInQuery, - final String requestId) { - final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi( - cmHandleId, resourceIdentifier, optionsParamInQuery, dataStore, requestId, topicParamInQuery); - return handleResponse(responseEntity, OperationEnum.READ); - } - private void setDmiProperties(final List<YangModelCmHandle.Property> dmiProperties, final NcmpServiceCmHandle ncmpServiceCmHandle) { final Map<String, String> dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size()); @@ -318,7 +311,6 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } } - private CmHandleRegistrationResponse registerAndSyncNewCmHandle(final YangModelCmHandle yangModelCmHandle) { try { final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}", @@ -335,14 +327,4 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } } - private static Object handleResponse(final ResponseEntity<?> responseEntity, final OperationEnum operation) { - if (responseEntity.getStatusCode().is2xxSuccessful()) { - return responseEntity.getBody(); - } else { - final String exceptionMessage = "Unable to " + operation.toString() + " resource data."; - throw new HttpClientRequestException(exceptionMessage, (String) responseEntity.getBody(), - responseEntity.getStatusCodeValue()); - } - } - -}
\ No newline at end of file +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java index f1bb95f34e..d457f2601b 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,11 +23,14 @@ package org.onap.cps.ncmp.api.impl.client; import lombok.AllArgsConstructor; import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties; +import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException; +import org.onap.cps.ncmp.api.impl.operations.DmiRequestBody; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; +import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.RestTemplate; @Component @@ -37,17 +40,24 @@ public class DmiRestClient { private RestTemplate restTemplate; private DmiProperties dmiProperties; - /** * Sends POST operation to DMI with json body containing module references. * @param dmiResourceUrl dmi resource url * @param jsonData json data body + * @param operation the type of operation being executed (for error reporting only) * @return response entity of type String */ public ResponseEntity<Object> postOperationWithJsonData(final String dmiResourceUrl, - final String jsonData) { + final String jsonData, + final DmiRequestBody.OperationEnum operation) { final var httpEntity = new HttpEntity<>(jsonData, configureHttpHeaders(new HttpHeaders())); - return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Object.class); + try { + return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Object.class); + } catch (final HttpStatusCodeException httpStatusCodeException) { + final String exceptionMessage = "Unable to " + operation.toString() + " resource data."; + throw new HttpClientRequestException(exceptionMessage, httpStatusCodeException.getResponseBodyAsString(), + httpStatusCodeException.getRawStatusCode()); + } } private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) { 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 f145379406..8e2c0946a2 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 @@ -83,7 +83,7 @@ public class DmiDataOperations extends DmiOperations { dmiServiceUrlBuilder.populateQueryParams(resourceId, optionsParamInQuery, topicParamInQuery), dmiServiceUrlBuilder.populateUriVariables( yangModelCmHandle, cmHandleId, dataStore)); - return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonBody); + return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonBody, READ); } /** @@ -116,7 +116,7 @@ public class DmiDataOperations extends DmiOperations { dmiServiceUrlBuilder.getDmiDatastoreUrl(dmiServiceUrlBuilder.populateQueryParams(resourceId, null, null), dmiServiceUrlBuilder.populateUriVariables(yangModelCmHandle, cmHandleId, PASSTHROUGH_RUNNING)); - return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonBody); + return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonBody, operation); } } 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 b033af87cd..7ab5798694 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 @@ -107,7 +107,7 @@ public class DmiModelOperations extends DmiOperations { final String cmHandle, final String resourceName) { final String dmiResourceDataUrl = getDmiResourceUrl(dmiServiceName, cmHandle, resourceName); - return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonData); + return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonData, DmiRequestBody.OperationEnum.READ); } private static String getRequestBodyToFetchYangResources(final Collection<ModuleReference> newModuleReferences, 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 01f3bfed7b..161cf9892f 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 @@ -22,7 +22,6 @@ package org.onap.cps.ncmp.api.impl -import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle import org.onap.cps.ncmp.api.models.DmiPluginRegistration @@ -34,11 +33,9 @@ import spock.lang.Shared import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE -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.utils.JsonObjectMapper -import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.api.CpsAdminService import org.onap.cps.api.CpsDataService @@ -76,11 +73,11 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { def dataNode = new DataNode(leaves: ['id': 'some-cm-handle', 'dmi-service-name': 'testDmiService']) - def 'Write resource data for pass-through running from DMI using POST #scenario cm handle properties.'() { + def 'Write resource data for pass-through running from DMI using POST.'() { given: 'cpsDataService returns valid datanode' mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode - when: 'get resource data is called' + when: 'write resource data is called' objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle', 'testResourceId', CREATE, '{some-json}', 'application/json') @@ -101,102 +98,26 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { 0 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(_, _, _, _, _) } - def 'Write resource data for pass-through running from DMI using POST "not found" response (from DMI).'() { - given: 'cpsDataService returns valid dataNode' - mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', - cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode - and: 'DMI returns a response with 404 status code' - mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', - 'testResourceId', CREATE, - '{some-json}', 'application/json') - >> { new ResponseEntity<>(HttpStatus.NOT_FOUND) } - when: 'write resource data is called' - objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle', - 'testResourceId', CREATE, - '{some-json}', 'application/json') - then: 'exception is thrown' - def exceptionThrown = thrown(HttpClientRequestException.class) - and: 'http status (not found) error code: 404' - exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value() - } - def 'Get resource data for pass-through operational from DMI.'() { given: 'get data node is called' mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode and: 'get resource data from DMI is called' mockDmiDataOperations.getResourceDataFromDmi( - 'testCmHandle', - 'testResourceId', - OPTIONS_PARAM, - PASSTHROUGH_OPERATIONAL, - NO_REQUEST_ID, - NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK) + 'testCmHandle', + 'testResourceId', + OPTIONS_PARAM, + PASSTHROUGH_OPERATIONAL, + NO_REQUEST_ID, + NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK) when: 'get resource data operational for cm-handle is called' def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle', - 'testResourceId', - OPTIONS_PARAM, - NO_TOPIC, - NO_REQUEST_ID) - then: 'DMI returns a json response' - response == 'dmi-response' - } - - def 'Get resource data for pass-through operational from DMI with invalid name.'() {\ - when: 'get resource data operational for cm-handle is called' - objectUnderTest.getResourceDataOperationalForCmHandle('invalid test cm handle', 'testResourceId', OPTIONS_PARAM, NO_TOPIC, NO_REQUEST_ID) - then: 'A data validation Exception is thrown' - thrown(DataValidationException) - } - - def 'Get resource data for pass-through operational from DMI with Json Processing Exception.'() { - given: 'cps data service returns valid data node' - mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', - cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode - and: 'objectMapper not able to parse object' - spiedJsonObjectMapper.asJsonString(_) >> { throw new JsonProcessingException('testException') } - and: 'DMI returns NOK response' - mockDmiDataOperations.getResourceDataFromDmi(*_) - >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND) - when: 'get resource data is called' - objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle', - 'testResourceId', - OPTIONS_PARAM, - NO_TOPIC, - NO_REQUEST_ID) - then: 'exception is thrown with the expected response code and details' - def exceptionThrown = thrown(HttpClientRequestException.class) - exceptionThrown.details.contains('NOK-json') - exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value() - } - - def 'Get resource data for pass-through operational from DMI return NOK response.'() { - given: 'cps data service returns valid data node' - mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', - cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode - and: 'DMI returns NOK response' - mockDmiDataOperations.getResourceDataFromDmi('testCmHandle', - 'testResourceId', - OPTIONS_PARAM, - PASSTHROUGH_OPERATIONAL, - NO_REQUEST_ID, - NO_TOPIC) - >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND) - when: 'get resource data is called' - objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle', - 'testResourceId', - OPTIONS_PARAM, - NO_TOPIC, - NO_REQUEST_ID) - then: 'exception is thrown' - def exceptionThrown = thrown(HttpClientRequestException.class) - and: 'details contain the original response' - exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value() - exceptionThrown.details.contains('NOK-json') + then: 'DMI returns a json response' + response == 'dmi-response' } def 'Get resource data for pass-through running from DMI.'() { @@ -205,55 +126,19 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode and: 'DMI returns valid response and data' mockDmiDataOperations.getResourceDataFromDmi('testCmHandle', - 'testResourceId', - OPTIONS_PARAM, - PASSTHROUGH_RUNNING, - NO_REQUEST_ID, - NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK) + 'testResourceId', + OPTIONS_PARAM, + PASSTHROUGH_RUNNING, + NO_REQUEST_ID, + NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK) when: 'get resource data is called' def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle', - 'testResourceId', - OPTIONS_PARAM, - NO_TOPIC, - NO_REQUEST_ID) - then: 'get resource data returns expected response' - response == '{dmi-response}' - } - - def 'Get resource data for pass-through running from DMI with invalid name.'() { - when: 'get resource data operational for cm-handle is called' - objectUnderTest.getResourceDataPassThroughRunningForCmHandle('invalid test cm handle', 'testResourceId', OPTIONS_PARAM, NO_TOPIC, NO_REQUEST_ID) - then: 'A data validation Exception is thrown' - thrown(DataValidationException) - } - - def 'Get resource data for pass-through running from DMI return NOK response.'() { - given: 'cpsDataService returns valid dataNode' - mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', - cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode - and: 'DMI returns NOK response' - mockDmiDataOperations.getResourceDataFromDmi('testCmHandle', - 'testResourceId', - OPTIONS_PARAM, - PASSTHROUGH_RUNNING, - NO_REQUEST_ID, - NO_TOPIC) - >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND) - when: 'get resource data is called' - objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle', - 'testResourceId', - OPTIONS_PARAM, - NO_TOPIC, - NO_REQUEST_ID) - then: 'exception is thrown' - def exceptionThrown = thrown(HttpClientRequestException.class) - and: 'details contain the original response' - exceptionThrown.details.contains('NOK-json') - exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value() + then: 'get resource data returns expected response' + response == '{dmi-response}' } def 'Getting Yang Resources.'() { @@ -269,7 +154,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { then: 'a data validation exception is thrown' thrown(DataValidationException) and: 'CPS module services is not invoked' - 0 * mockCpsModuleService.getYangResourcesModuleReferences(_, _) + 0 * mockCpsModuleService.getYangResourcesModuleReferences(*_) } def 'Get cm handle identifiers for the given module names.'() { @@ -282,17 +167,16 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { def 'Get a cm handle.'() { given: 'the system returns a yang modelled cm handle' def dmiServiceName = 'some service name' - def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')] - def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')] + def dmiProperties = [new YangModelCmHandle.Property('aDmiProperty', 'a dmi value')] + def publicProperties = [new YangModelCmHandle.Property('aPublicProperty', 'a public value')] def yangModelCmHandle = new YangModelCmHandle(id:'some-cm-handle', dmiServiceName: dmiServiceName, dmiProperties: dmiProperties, publicProperties: publicProperties) 1 * mockYangModelCmHandleRetriever.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle when: 'getting cm handle details for a given cm handle id from ncmp service' def result = objectUnderTest.getNcmpServiceCmHandle('some-cm-handle') then: 'the result returns the correct data' result.cmHandleId == 'some-cm-handle' - result.dmiProperties ==[ Book:'Romance Novel' ] - result.publicProperties == [ "Public Book":'Public Romance Novel' ] - + result.dmiProperties ==[ aDmiProperty:'a dmi value' ] + result.publicProperties == [ aPublicProperty:'a public value' ] } def 'Get a cm handle with an invalid id.'() { @@ -301,7 +185,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { then: 'an exception is thrown' thrown(DataValidationException) and: 'the yang model cm handle retriever is not invoked' - 0 * mockYangModelCmHandleRetriever.getYangModelCmHandle(_) + 0 * mockYangModelCmHandleRetriever.getYangModelCmHandle(*_) } def 'Get cm handle public properties'() { @@ -323,7 +207,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { then: 'an exception is thrown' thrown(DataValidationException) and: 'the yang model cm handle retriever is not invoked' - 0 * mockYangModelCmHandleRetriever.getYangModelCmHandle(_) + 0 * mockYangModelCmHandleRetriever.getYangModelCmHandle(*_) } def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() { @@ -340,40 +224,19 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { >> { new ResponseEntity<>(HttpStatus.OK) } } - def 'Verify error message from handleResponse is correct for #scenario operation.'() { - given: 'writeResourceDataPassThroughRunningFromDmi fails to return OK HttpStatus' - mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(*_) - >> new ResponseEntity<>(HttpStatus.NOT_FOUND) - when: 'get resource data is called' - objectUnderTest.writeResourceDataPassThroughRunningForCmHandle( - 'testCmHandle', - 'testResourceId', - givenOperation, - '{some-json}', - 'application/json') - then: 'an exception is thrown with the expected error message details with correct operation' - def exceptionThrown = thrown(HttpClientRequestException.class) - exceptionThrown.getMessage().contains(expectedResponseMessage) - where: - scenario | givenOperation || expectedResponseMessage - 'CREATE' | CREATE || 'Unable to create resource data.' - 'READ' | READ || 'Unable to read resource data.' - 'UPDATE' | UPDATE || 'Unable to update resource data.' - } - def 'Verify modules and create anchor params'() { given: 'dmi plugin registration return created cm handles' def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'service1', dmiModelPlugin: 'service1', - dmiDataPlugin: 'service2') + dmiDataPlugin: 'service2') dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle] mockDmiPluginRegistration.getCreatedCmHandles() >> [ncmpServiceCmHandle] when: 'parse and create cm handle in dmi registration then sync module' objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(mockDmiPluginRegistration) then: 'validate params for creating anchor and list elements' 1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry', - '/dmi-registry', '{"cm-handles":[{"id":"some-cm-handle-id",' + - '"additional-properties":[],"public-properties":[]}]}', null) + '/dmi-registry', '{"cm-handles":[{"id":"some-cm-handle-id",' + + '"additional-properties":[],"public-properties":[]}]}', null) 1 * mockCpsAdminService.createAnchor('NFP-Operational', null, - 'some-cm-handle-id') + 'some-cm-handle-id') } } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy index 394df1d076..90839f8ac0 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,17 +22,23 @@ package org.onap.cps.ncmp.api.impl.client import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration +import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.http.HttpEntity -import org.springframework.http.HttpHeaders -import org.springframework.http.HttpMethod +import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.test.context.ContextConfiguration +import org.springframework.web.client.HttpServerErrorException import org.springframework.web.client.RestTemplate import spock.lang.Specification +import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ +import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH +import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE + + @SpringBootTest @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiRestClient]) class DmiRestClientSpec extends Specification { @@ -44,14 +50,32 @@ class DmiRestClientSpec extends Specification { DmiRestClient objectUnderTest def resourceUrl = 'some url' + def mockResponseEntity = Mock(ResponseEntity) + def 'DMI POST operation with JSON.'() { given: 'the rest template returns a valid response entity' - def mockResponseEntity = Mock(ResponseEntity) mockRestTemplate.postForEntity(resourceUrl, _ as HttpEntity, Object.class) >> mockResponseEntity when: 'POST operation is invoked' - def result = objectUnderTest.postOperationWithJsonData(resourceUrl, 'json-data') + def result = objectUnderTest.postOperationWithJsonData(resourceUrl, 'json-data', READ) then: 'the output of the method is equal to the output from the test template' result == mockResponseEntity } + def 'Failing DMI POST operation.'() { + given: 'the rest template returns a valid response entity' + def serverResponse = 'server response'.getBytes() + def httpServerErrorException = new HttpServerErrorException(HttpStatus.FORBIDDEN, 'status text', serverResponse, null) + mockRestTemplate.postForEntity(*_) >> { throw httpServerErrorException } + when: 'POST operation is invoked' + def result = objectUnderTest.postOperationWithJsonData('some url', 'some json', operation) + then: 'a Http Client Exception is thrown' + def thrown = thrown(HttpClientRequestException) + and: 'the exception has the relevant details from the error response' + assert thrown.httpStatus == 403 + assert thrown.message == "Unable to ${operation} resource data." + assert thrown.details == 'server response' + where: 'the following operation is executed' + operation << [CREATE, READ, PATCH] + } + } 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 2a19df1723..b7ebf2965a 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 @@ -30,12 +30,12 @@ import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.http.ResponseEntity import org.springframework.test.context.ContextConfiguration -import org.springframework.util.MultiValueMap import spock.lang.Shared import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE +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.springframework.http.HttpStatus @@ -63,7 +63,7 @@ class DmiDataOperationsSpec extends DmiOperationsBaseSpec { and: 'a positive response from DMI service when it is called with the expected parameters' def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK) def expectedUrl = dmiServiceBaseUrl + "${expectedDatastoreInUrl}?resourceIdentifier=${resourceIdentifier}${expectedOptionsInUrl}" - mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson) >> responseFromDmi + mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, READ) >> responseFromDmi dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl when: 'get resource data is invoked' def result = objectUnderTest.getResourceDataFromDmi(cmHandleId, resourceIdentifier, @@ -88,7 +88,7 @@ class DmiDataOperationsSpec extends DmiOperationsBaseSpec { def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}' def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK) dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl - mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson) >> responseFromDmi + mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, operation) >> responseFromDmi when: 'write resource method is invoked' def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId, 'parent/child', operation, 'requestData', 'some data type') then: 'the result is the response from the DMI service' @@ -98,4 +98,4 @@ class DmiDataOperationsSpec extends DmiOperationsBaseSpec { CREATE || 'create' UPDATE || 'update' } -}
\ No newline at end of file +} 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 574f609e9e..ed8f08698d 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 @@ -24,7 +24,6 @@ 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.utils.DmiServiceUrlBuilder import org.onap.cps.spi.model.ModuleReference import org.onap.cps.utils.JsonObjectMapper import org.spockframework.spring.SpringBean @@ -33,9 +32,10 @@ import org.springframework.boot.test.context.SpringBootTest import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.test.context.ContextConfiguration -import org.springframework.web.util.UriComponentsBuilder import spock.lang.Shared +import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ + @SpringBootTest @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiModelOperations]) class DmiModelOperationsSpec extends DmiOperationsBaseSpec { @@ -56,7 +56,7 @@ class DmiModelOperationsSpec extends DmiOperationsBaseSpec { def moduleReferencesAsLisOfMaps = [[moduleName: 'mod1', revision: 'A'], [moduleName: 'mod2', revision: 'X']] def expectedUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules" def responseFromDmi = new ResponseEntity([schemas: moduleReferencesAsLisOfMaps], HttpStatus.OK) - mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"cmHandleProperties":{}}') + mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"cmHandleProperties":{}}', READ) >> responseFromDmi when: 'get module references is called' def result = objectUnderTest.getModuleReferences(yangModelCmHandle) @@ -89,7 +89,7 @@ class DmiModelOperationsSpec extends DmiOperationsBaseSpec { and: 'a positive response from DMI service when it is called with tha expected parameters' def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK) mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules", - '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}') >> responseFromDmi + '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}', READ) >> responseFromDmi when: 'a get module references is called' def result = objectUnderTest.getModuleReferences(yangModelCmHandle) then: 'the result is the response from DMI service' @@ -108,7 +108,7 @@ class DmiModelOperationsSpec extends DmiOperationsBaseSpec { [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK) def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}' mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources", - '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}') >> responseFromDmi + '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}', READ) >> responseFromDmi when: 'get new yang resources from DMI service' def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences) then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)' @@ -140,7 +140,7 @@ class DmiModelOperationsSpec extends DmiOperationsBaseSpec { and: 'a positive response from DMI service when it is called with the expected parameters' def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK) mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources", - '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":'+expectedAdditionalPropertiesInRequest+'}') >> responseFromDmi + '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}', READ) >> responseFromDmi when: 'get new yang resources from DMI service' def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, unknownModuleReferences) then: 'the result is the response from DMI service' |