From 4f736db2a0ab0df49b5cae6599da8e655c5ea8cd Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Mon, 20 Sep 2021 13:29:09 +0100 Subject: Address Sonar Qube issues - Add some basic test for missed coverage - Refactored NetworkCmProxyDataServiceImpl to addres duplcaied code and code coverage - Increased Coverage treshold where possible Issue-ID: CPS-475 Signed-off-by: ToineSiebelink Change-Id: Id05f41ac242aeaf57606748009c0e370199e054f --- .../api/impl/NetworkCmProxyDataServiceImpl.java | 75 +++++++++++----------- 1 file changed, 37 insertions(+), 38 deletions(-) (limited to 'cps-ncmp-service/src/main') 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 a28b73c427..719508b6c5 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 @@ -149,14 +149,18 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService @Override public void updateDmiRegistrationAndSyncModule(final DmiPluginRegistration dmiPluginRegistration) { - if (dmiPluginRegistration.getCreatedCmHandles() != null) { - parseAndCreateCmHandlesInDmiRegistrationAndSyncModule(dmiPluginRegistration); - } - if (dmiPluginRegistration.getUpdatedCmHandles() != null) { - parseAndUpdateCmHandlesInDmiRegistration(dmiPluginRegistration); - } - if (dmiPluginRegistration.getRemovedCmHandles() != null) { - parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration); + try { + if (dmiPluginRegistration.getCreatedCmHandles() != null) { + parseAndCreateCmHandlesInDmiRegistrationAndSyncModule(dmiPluginRegistration); + } + if (dmiPluginRegistration.getUpdatedCmHandles() != null) { + parseAndUpdateCmHandlesInDmiRegistration(dmiPluginRegistration); + } + if (dmiPluginRegistration.getRemovedCmHandles() != null) { + parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration); + } + } catch (final JsonProcessingException e) { + handleJsonProcessingException(dmiPluginRegistration, e); } } @@ -286,38 +290,32 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return prepareOperationBody(requestBodyObject); } - private void parseAndUpdateCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) { - try { - final PersistenceCmHandlesList persistenceCmHandlesList = new PersistenceCmHandlesList(); - - for (final CmHandle cmHandle : dmiPluginRegistration.getUpdatedCmHandles()) { - final PersistenceCmHandle persistenceCmHandle = - toPersistenceCmHandle(dmiPluginRegistration.getDmiPlugin(), cmHandle); - persistenceCmHandlesList.add(persistenceCmHandle); - } - final String cmHandlesJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList); - cpsDataService.updateNodeLeavesAndExistingDescendantLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - "/dmi-registry", cmHandlesJsonData, NO_TIMESTAMP); - } catch (final JsonProcessingException e) { - handleJsonProcessingException(dmiPluginRegistration, e); - } + private void parseAndUpdateCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) + throws JsonProcessingException { + final PersistenceCmHandlesList updatedPersistenceCmHandlesList = toPersistenceCmHandlesList( + dmiPluginRegistration.getDmiPlugin(), + dmiPluginRegistration.getUpdatedCmHandles()); + final String cmHandlesAsJson = objectMapper.writeValueAsString(updatedPersistenceCmHandlesList); + cpsDataService.updateNodeLeavesAndExistingDescendantLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + "/dmi-registry", cmHandlesAsJson, NO_TIMESTAMP); } private void parseAndCreateCmHandlesInDmiRegistrationAndSyncModule( - final DmiPluginRegistration dmiPluginRegistration) { - try { - final var persistenceCmHandlesList = new PersistenceCmHandlesList(); - for (final CmHandle cmHandle : dmiPluginRegistration.getCreatedCmHandles()) { - final PersistenceCmHandle persistenceCmHandle = - toPersistenceCmHandle(dmiPluginRegistration.getDmiPlugin(), cmHandle); - persistenceCmHandlesList.add(persistenceCmHandle); - } - final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList); + final DmiPluginRegistration dmiPluginRegistration) throws JsonProcessingException { + final PersistenceCmHandlesList createdPersistenceCmHandlesList = toPersistenceCmHandlesList( + dmiPluginRegistration.getDmiPlugin(), + dmiPluginRegistration.getCreatedCmHandles()); + registerAndSyncNewCmHandles(createdPersistenceCmHandlesList); + } - registerAndSyncNode(persistenceCmHandlesList, cmHandleJsonData); - } catch (final JsonProcessingException e) { - handleJsonProcessingException(dmiPluginRegistration, e); + private static PersistenceCmHandlesList toPersistenceCmHandlesList(final String dmiPlugin, + final Collection cmHandles) { + final PersistenceCmHandlesList persistenceCmHandlesList = new PersistenceCmHandlesList(); + for (final CmHandle cmHandle : cmHandles) { + final PersistenceCmHandle persistenceCmHandle = toPersistenceCmHandle(dmiPlugin, cmHandle); + persistenceCmHandlesList.add(persistenceCmHandle); } + return persistenceCmHandlesList; } private static void handleJsonProcessingException(final DmiPluginRegistration dmiPluginRegistration, @@ -328,8 +326,9 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService throw new DataValidationException(message, e.getMessage(), e); } - private void registerAndSyncNode(final PersistenceCmHandlesList persistenceCmHandlesList, - final String cmHandleJsonData) { + private void registerAndSyncNewCmHandles(final PersistenceCmHandlesList persistenceCmHandlesList) + throws JsonProcessingException { + final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList); cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, "/dmi-registry", cmHandleJsonData, NO_TIMESTAMP); @@ -435,7 +434,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService private static YangResource toYangResource(final JsonObject yangResourceAsJson) { final YangResource yangResource = new YangResource(); yangResource.setModuleName(yangResourceAsJson.get("moduleName").getAsString()); - yangResource.setRevision(yangResourceAsJson.get("revision").getAsString()); + yangResource.setRevision(yangResourceAsJson.get(REVISION).getAsString()); final String yangSourceJson = yangResourceAsJson.get("yangSource").getAsString(); String yangSource = JsonUtils.removeWrappingTokens(yangSourceJson); -- cgit 1.2.3-korg