diff options
author | DylanB95EST <dylan.byrne@est.tech> | 2022-04-04 13:09:22 +0100 |
---|---|---|
committer | DylanB95EST <dylan.byrne@est.tech> | 2022-04-08 12:51:34 +0100 |
commit | b130fd3158c537df1ce2cb1e5a7a86c3d7e0d2a7 (patch) | |
tree | 84129ee13058d2ae9b7e79d1eaa82633ca1f95eb /cps-ncmp-service/src/main/java/org/onap | |
parent | deac4777c1a245be1dc4c423658523b41071b110 (diff) |
Refactoring/ Adding Tests for Validation
Refactored classes affected by validation
Have added tests for anywhere where validation is used
Have refactored the parse and sync modules validation
to be validated at the public api method
Issue-ID: CPS-322
Change-Id: I4989cfd03300fbdca41571d0aa2d0b96978858ba
Signed-off-by: DylanB95EST <dylan.byrne@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/main/java/org/onap')
7 files changed, 86 insertions, 70 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 9c3d9448ef..f498e5d4fb 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 @@ -188,33 +188,12 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService yangModelCmHandleRetriever.getDmiServiceNamesAndProperties(cmHandleId); final List<YangModelCmHandle.Property> dmiProperties = yangModelCmHandle.getDmiProperties(); final List<YangModelCmHandle.Property> publicProperties = yangModelCmHandle.getPublicProperties(); - ncmpServiceCmHandle.setCmHandleID(yangModelCmHandle.getId()); + ncmpServiceCmHandle.setCmHandleId(yangModelCmHandle.getId()); setDmiProperties(dmiProperties, ncmpServiceCmHandle); setPublicProperties(publicProperties, ncmpServiceCmHandle); return ncmpServiceCmHandle; } - private void setDmiProperties(final List<YangModelCmHandle.Property> dmiProperties, - final NcmpServiceCmHandle ncmpServiceCmHandle) { - final Map<String, String> dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size()); - asPropertiesMap(dmiProperties, dmiPropertiesMap); - ncmpServiceCmHandle.setDmiProperties(dmiPropertiesMap); - } - - private void setPublicProperties(final List<YangModelCmHandle.Property> publicProperties, - final NcmpServiceCmHandle ncmpServiceCmHandle) { - final Map<String, String> publicPropertiesMap = new LinkedHashMap<>(); - asPropertiesMap(publicProperties, publicPropertiesMap); - ncmpServiceCmHandle.setPublicProperties(publicPropertiesMap); - } - - private void asPropertiesMap(final List<YangModelCmHandle.Property> properties, - final Map<String, String> propertiesMap) { - for (final YangModelCmHandle.Property property: properties) { - propertiesMap.put(property.getName(), property.getValue()); - } - } - /** * THis method registers a cm handle and initiates modules sync. * @@ -223,45 +202,24 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService */ public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules( final DmiPluginRegistration dmiPluginRegistration) { - return dmiPluginRegistration.getCreatedCmHandles().stream() - .map(cmHandle -> - YangModelCmHandle.toYangModelCmHandle( - dmiPluginRegistration.getDmiPlugin(), - dmiPluginRegistration.getDmiDataPlugin(), - dmiPluginRegistration.getDmiModelPlugin(), cmHandle) - ) - .map(this::registerAndSyncNewCmHandle) - .collect(Collectors.toList()); - } - - 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()); - } - } - - private CmHandleRegistrationResponse registerAndSyncNewCmHandle(final YangModelCmHandle yangModelCmHandle) { + List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(); try { - CpsValidator.validateNameCharacters(yangModelCmHandle.getId()); - final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}", - jsonObjectMapper.asJsonString(yangModelCmHandle)); - cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT, - cmHandleJsonData, NO_TIMESTAMP); - syncModulesAndCreateAnchor(yangModelCmHandle); - return CmHandleRegistrationResponse.createSuccessResponse(yangModelCmHandle.getId()); - } catch (final AlreadyDefinedException alreadyDefinedException) { - return CmHandleRegistrationResponse.createFailureResponse( - yangModelCmHandle.getId(), RegistrationError.CM_HANDLE_ALREADY_EXIST); + cmHandleRegistrationResponses = dmiPluginRegistration.getCreatedCmHandles().stream() + .map(cmHandle -> + YangModelCmHandle.toYangModelCmHandle( + dmiPluginRegistration.getDmiPlugin(), + dmiPluginRegistration.getDmiDataPlugin(), + dmiPluginRegistration.getDmiModelPlugin(), cmHandle) + ) + .map(this::registerAndSyncNewCmHandle) + .collect(Collectors.toList()); } catch (final DataValidationException dataValidationException) { - return CmHandleRegistrationResponse.createFailureResponse(yangModelCmHandle.getId(), - RegistrationError.CM_HANDLE_INVALID_ID); - } catch (final Exception exception) { - return CmHandleRegistrationResponse.createFailureResponse(yangModelCmHandle.getId(), exception); + cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration + .getCreatedCmHandles().stream() + .map(NcmpServiceCmHandle::getCmHandleId).findFirst().orElse(null), + RegistrationError.CM_HANDLE_INVALID_ID)); } + return cmHandleRegistrationResponses; } protected void syncModulesAndCreateAnchor(final YangModelCmHandle yangModelCmHandle) { @@ -348,4 +306,53 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService 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()); + asPropertiesMap(dmiProperties, dmiPropertiesMap); + ncmpServiceCmHandle.setDmiProperties(dmiPropertiesMap); + } + + private void setPublicProperties(final List<YangModelCmHandle.Property> publicProperties, + final NcmpServiceCmHandle ncmpServiceCmHandle) { + final Map<String, String> publicPropertiesMap = new LinkedHashMap<>(); + asPropertiesMap(publicProperties, publicPropertiesMap); + ncmpServiceCmHandle.setPublicProperties(publicPropertiesMap); + } + + private void asPropertiesMap(final List<YangModelCmHandle.Property> properties, + final Map<String, String> propertiesMap) { + for (final YangModelCmHandle.Property property: properties) { + propertiesMap.put(property.getName(), property.getValue()); + } + } + + + private CmHandleRegistrationResponse registerAndSyncNewCmHandle(final YangModelCmHandle yangModelCmHandle) { + try { + final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}", + jsonObjectMapper.asJsonString(yangModelCmHandle)); + cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT, + cmHandleJsonData, NO_TIMESTAMP); + syncModulesAndCreateAnchor(yangModelCmHandle); + return CmHandleRegistrationResponse.createSuccessResponse(yangModelCmHandle.getId()); + } catch (final AlreadyDefinedException alreadyDefinedException) { + return CmHandleRegistrationResponse.createFailureResponse( + yangModelCmHandle.getId(), RegistrationError.CM_HANDLE_ALREADY_EXIST); + } catch (final Exception exception) { + return CmHandleRegistrationResponse.createFailureResponse(yangModelCmHandle.getId(), exception); + } + } + + 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/NetworkCmProxyDataServicePropertyHandler.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServicePropertyHandler.java index ff79f87245..aae2f209ae 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServicePropertyHandler.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServicePropertyHandler.java @@ -72,7 +72,7 @@ public class NetworkCmProxyDataServicePropertyHandler { final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles) { final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(); for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) { - final String cmHandle = ncmpServiceCmHandle.getCmHandleID(); + final String cmHandle = ncmpServiceCmHandle.getCmHandleId(); try { CpsValidator.validateNameCharacters(cmHandle); final String cmHandleXpath = String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandle); 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 855e52dcda..ad85edde7b 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 @@ -29,6 +29,7 @@ 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.utils.DmiServiceUrlBuilder; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; +import org.onap.cps.utils.CpsValidator; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; @@ -69,6 +70,7 @@ public class DmiDataOperations extends DmiOperations { final DataStoreEnum dataStore, final String requestId, final String topicParamInQuery) { + CpsValidator.validateNameCharacters(cmHandleId); final YangModelCmHandle yangModelCmHandle = yangModelCmHandleRetriever.getDmiServiceNamesAndProperties(cmHandleId); final DmiRequestBody dmiRequestBody = DmiRequestBody.builder() @@ -77,7 +79,7 @@ public class DmiDataOperations extends DmiOperations { .build(); dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties()); final String jsonBody = jsonObjectMapper.asJsonString(dmiRequestBody); - final var dmiResourceDataUrl = dmiServiceUrlBuilder.getDmiDatastoreUrl( + final String dmiResourceDataUrl = dmiServiceUrlBuilder.getDmiDatastoreUrl( dmiServiceUrlBuilder.populateQueryParams(resourceId, optionsParamInQuery, topicParamInQuery), dmiServiceUrlBuilder.populateUriVariables( yangModelCmHandle, cmHandleId, dataStore)); @@ -100,6 +102,7 @@ public class DmiDataOperations extends DmiOperations { final OperationEnum operation, final String requestData, final String dataType) { + CpsValidator.validateNameCharacters(cmHandleId); final YangModelCmHandle yangModelCmHandle = yangModelCmHandleRetriever.getDmiServiceNamesAndProperties(cmHandleId); final DmiRequestBody dmiRequestBody = DmiRequestBody.builder() @@ -110,9 +113,9 @@ public class DmiDataOperations extends DmiOperations { dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties()); final String jsonBody = jsonObjectMapper.asJsonString(dmiRequestBody); final String dmiUrl = - dmiServiceUrlBuilder.getDmiDatastoreUrl(dmiServiceUrlBuilder.populateQueryParams(resourceId, - null, null), - dmiServiceUrlBuilder.populateUriVariables(yangModelCmHandle, cmHandleId, PASSTHROUGH_RUNNING)); + dmiServiceUrlBuilder.getDmiDatastoreUrl(dmiServiceUrlBuilder.populateQueryParams(resourceId, + null, null), + dmiServiceUrlBuilder.populateUriVariables(yangModelCmHandle, cmHandleId, PASSTHROUGH_RUNNING)); return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonBody); } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/YangModelCmHandleRetriever.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/YangModelCmHandleRetriever.java index 6b6bdf5be4..0efe8d5b62 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/YangModelCmHandleRetriever.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/YangModelCmHandleRetriever.java @@ -28,6 +28,7 @@ import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.model.DataNode; +import org.onap.cps.utils.CpsValidator; import org.springframework.stereotype.Component; /** @@ -48,9 +49,10 @@ public class YangModelCmHandleRetriever { * @return yang model cm handle */ public YangModelCmHandle getDmiServiceNamesAndProperties(final String cmHandleId) { + CpsValidator.validateNameCharacters(cmHandleId); final DataNode cmHandleDataNode = getCmHandleDataNode(cmHandleId); final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle(); - ncmpServiceCmHandle.setCmHandleID(cmHandleId); + ncmpServiceCmHandle.setCmHandleId(cmHandleId); populateCmHandleProperties(cmHandleDataNode, ncmpServiceCmHandle); return YangModelCmHandle.toYangModelCmHandle( String.valueOf(cmHandleDataNode.getLeaves().get("dmi-service-name")), diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilder.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilder.java index b60aac9518..b679107251 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilder.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilder.java @@ -30,6 +30,7 @@ import org.apache.logging.log4j.util.TriConsumer; import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration; import org.onap.cps.ncmp.api.impl.operations.DmiOperations; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; +import org.onap.cps.utils.CpsValidator; import org.springframework.stereotype.Component; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -70,25 +71,26 @@ public class DmiServiceUrlBuilder { .pathSegment("{dmiBasePath}") .pathSegment("v1") .pathSegment("ch") - .pathSegment("{cmHandle}"); + .pathSegment("{cmHandleId}"); } /** * This method populates uri variables. * * @param yangModelCmHandle get dmi service name - * @param cmHandle cm handle name for dmi registration + * @param cmHandleId cm handle id for dmi registration * @return {@code String} dmi service url as string */ public Map<String, Object> populateUriVariables(final YangModelCmHandle yangModelCmHandle, - final String cmHandle, + final String cmHandleId, final DmiOperations.DataStoreEnum dataStore) { + CpsValidator.validateNameCharacters(cmHandleId); final Map<String, Object> uriVariables = new HashMap<>(); final String dmiBasePath = dmiProperties.getDmiBasePath(); uriVariables.put("dmiServiceName", yangModelCmHandle.resolveDmiServiceName(DATA)); uriVariables.put("dmiBasePath", dmiBasePath); - uriVariables.put("cmHandle", cmHandle); + uriVariables.put("cmHandleId", cmHandleId); uriVariables.put("dataStore", dataStore.getValue()); return uriVariables; } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java index e46b9e3da5..fd3528187e 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java @@ -35,6 +35,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; +import org.onap.cps.utils.CpsValidator; /** * Cm Handle which follows the Yang resource dmi registry model when persisting data to DMI or the DB. @@ -75,8 +76,9 @@ public class YangModelCmHandle { final String dmiDataServiceName, final String dmiModelServiceName, final NcmpServiceCmHandle ncmpServiceCmHandle) { + CpsValidator.validateNameCharacters(ncmpServiceCmHandle.getCmHandleId()); final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle(); - yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleID()); + yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleId()); yangModelCmHandle.setDmiServiceName(dmiServiceName); yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName); yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/NcmpServiceCmHandle.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/NcmpServiceCmHandle.java index 938127020c..6811b59e00 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/NcmpServiceCmHandle.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/NcmpServiceCmHandle.java @@ -39,7 +39,7 @@ import org.springframework.validation.annotation.Validated; @NoArgsConstructor public class NcmpServiceCmHandle { - private String cmHandleID; + private String cmHandleId; @JsonSetter(nulls = Nulls.AS_EMPTY) private Map<String, String> dmiProperties = Collections.emptyMap(); |