From: Bruno Sakoto Date: Wed, 9 Feb 2022 16:53:20 +0000 (+0000) Subject: Merge "Fix for retry mechanism on concurrent CmHandle registration" X-Git-Tag: 3.0.0~29 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=d3bb89c442eaad9d635deffd34fe5ae680394790;hp=62ac0c3b172ef5dbbc9d22d10bb45186b446d4c9;p=cps.git Merge "Fix for retry mechanism on concurrent CmHandle registration" --- diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java index 51b248295..5b89d9f4c 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java @@ -24,9 +24,9 @@ package org.onap.cps.spi.impl; import java.util.Collection; import java.util.List; -import java.util.Set; import java.util.stream.Collectors; import javax.transaction.Transactional; +import lombok.AllArgsConstructor; import org.onap.cps.spi.CpsAdminPersistenceService; import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.DataspaceEntity; @@ -38,30 +38,19 @@ import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.repository.AnchorRepository; import org.onap.cps.spi.repository.DataspaceRepository; -import org.onap.cps.spi.repository.FragmentRepository; import org.onap.cps.spi.repository.SchemaSetRepository; import org.onap.cps.spi.repository.YangResourceRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Component; @Component +@AllArgsConstructor public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService { - @Autowired - private DataspaceRepository dataspaceRepository; - - @Autowired - private AnchorRepository anchorRepository; - - @Autowired - private SchemaSetRepository schemaSetRepository; - - @Autowired - private FragmentRepository fragmentRepository; - - @Autowired - private YangResourceRepository yangResourceRepository; + private final DataspaceRepository dataspaceRepository; + private final AnchorRepository anchorRepository; + private final SchemaSetRepository schemaSetRepository; + private final YangResourceRepository yangResourceRepository; @Override public void createDataspace(final String dataspaceName) { @@ -140,7 +129,6 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic @Override public void deleteAnchor(final String dataspaceName, final String anchorName) { final var anchorEntity = getAnchorEntity(dataspaceName, anchorName); - fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity)); anchorRepository.delete(anchorEntity); } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index c6e28aba9..ed414fc30 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2021 Pantheon.tech - * Modifications Copyright (C) 2020-2021 Bell Canada. + * Modifications Copyright (C) 2020-2022 Bell Canada. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,6 +48,7 @@ import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.entities.FragmentEntity; import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.onap.cps.spi.exceptions.ConcurrencyException; +import org.onap.cps.spi.exceptions.CpsAdminException; import org.onap.cps.spi.exceptions.CpsPathException; import org.onap.cps.spi.exceptions.DataNodeNotFoundException; import org.onap.cps.spi.model.DataNode; @@ -319,6 +320,14 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService fragmentRepository.save(parentEntity); } + @Override + @Transactional + public void deleteDataNodes(final String dataspaceName, final String anchorName) { + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName) + .ifPresent( + anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity))); + } @Override @Transactional @@ -384,6 +393,10 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService } private static String getListElementXpathPrefix(final Collection newListElements) { + if (newListElements.isEmpty()) { + throw new CpsAdminException("Invalid list replacement", + "Cannot replace list elements with empty collection"); + } final String firstChildNodeXpath = newListElements.iterator().next().getXpath(); return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("[") + 1); } diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy index 2218014ec..063bd5b5a 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy @@ -41,8 +41,7 @@ class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase { static final String SET_DATA = '/data/anchor.sql' static final String SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES = '/data/anchors-schemaset-modules.sql' static final String DATASPACE_WITH_NO_DATA = 'DATASPACE-002-NO-DATA' - static final Integer DELETED_ANCHOR_ID = 3001 - static final Long DELETED_FRAGMENT_ID = 4001 + static final Integer DELETED_ANCHOR_ID = 3002 @Sql(CLEAR_DATA) def 'Create and retrieve a new dataspace.'() { @@ -150,10 +149,9 @@ class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase { @Sql([CLEAR_DATA, SET_DATA]) def 'Delete anchor'() { when: 'delete anchor action is invoked' - objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME1) - then: 'anchor and associated data fragment are deleted' + objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME2) + then: 'anchor is deleted' assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty() - assert fragmentRepository.findById(DELETED_FRAGMENT_ID).isEmpty() } @Sql([CLEAR_DATA, SET_DATA]) diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy index 69e6aa81c..41e7a419a 100755 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2021 Pantheon.tech - * Modifications Copyright (C) 2021 Bell Canada. + * Modifications Copyright (C) 2021-2022 Bell Canada. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import org.onap.cps.spi.CpsDataPersistenceService import org.onap.cps.spi.entities.FragmentEntity import org.onap.cps.spi.exceptions.AlreadyDefinedException import org.onap.cps.spi.exceptions.AnchorNotFoundException +import org.onap.cps.spi.exceptions.CpsAdminException import org.onap.cps.spi.exceptions.DataNodeNotFoundException import org.onap.cps.spi.exceptions.DataspaceNotFoundException import org.onap.cps.spi.model.DataNode @@ -34,9 +35,7 @@ import org.onap.cps.spi.model.DataNodeBuilder import org.onap.cps.utils.JsonObjectMapper import org.springframework.beans.factory.annotation.Autowired import org.springframework.test.context.jdbc.Sql - import javax.validation.ConstraintViolationException -import java.util.stream.Collectors import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS @@ -49,6 +48,8 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { static final JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper()) static final String SET_DATA = '/data/fragment.sql' + static final int DATASPACE_1001_ID = 1001L + static final int ANCHOR_3003_ID = 3003L static final long ID_DATA_NODE_WITH_DESCENDANTS = 4001 static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1' static final String XPATH_DATA_NODE_WITH_LEAVES = '/parent-100' @@ -56,10 +57,8 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { static final long UPDATE_DATA_NODE_SUB_FRAGMENT_ID = 4203L static final long LIST_DATA_NODE_PARENT201_FRAGMENT_ID = 4206L static final long LIST_DATA_NODE_PARENT203_FRAGMENT_ID = 4214L - static final long LIST_DATA_NODE_PARENT204_FRAGMENT_ID = 4219L - static final long LIST_DATA_NODE_PARENT205_FRAGMENT_ID = 4221L - static final long LIST_DATA_NODE_CHILD202_FRAGMENT_ID = 4204L static final long LIST_DATA_NODE_PARENT202_FRAGMENT_ID = 4211L + static final long PARENT_3_FRAGMENT_ID = 4003L static final DataNode newDataNode = new DataNodeBuilder().build() static DataNode existingDataNode @@ -136,11 +135,11 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { then: 'the parent is now has to 2 children' def expectedExistingChildPath = '/parent-1/child-1' def parentFragment = fragmentRepository.findById(ID_DATA_NODE_WITH_DESCENDANTS).orElseThrow() - parentFragment.getChildFragments().size() == 2 + parentFragment.childFragments.size() == 2 and: 'it still has the old child' - parentFragment.getChildFragments().find({ it.xpath == expectedExistingChildPath }) + parentFragment.childFragments.find({ it.xpath == expectedExistingChildPath }) and: 'it has the new child' - parentFragment.getChildFragments().find({ it.xpath == newChild.xpath }) + parentFragment.childFragments.find({ it.xpath == newChild.xpath }) } @Sql([CLEAR_DATA, SET_DATA]) @@ -158,16 +157,16 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { @Sql([CLEAR_DATA, SET_DATA]) def 'Add multiple list elements including an element with a child datanode.'() { given: 'two new data nodes for an existing list' - def listElementXpaths = ['/parent-201/child-204[@key="B"]', '/parent-201/child-204[@key="C"]'] + def listElementXpaths = ['/parent-201/child-204[@key="NEW1"]', '/parent-201/child-204[@key="NEW2"]'] def listElements = toDataNodes(listElementXpaths) and: 'a child node for one of the new data nodes' - def childDataNode = buildDataNode('/parent-201/child-204[@key="C"]/grand-child-204[@key2="Z"]', [leave:'value'], []) + def childDataNode = buildDataNode('/parent-201/child-204[@key="NEW1"]/grand-child-204[@key2="NEW1-CHILD"]', [leave:'value'], []) listElements[0].childDataNodes = [childDataNode] when: 'the data nodes (list elements) are added to existing parent node' objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', listElements) then: 'new entries successfully persisted, parent node now contains 5 children (2 new + 3 existing before)' def parentFragment = fragmentRepository.getById(LIST_DATA_NODE_PARENT201_FRAGMENT_ID) - def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() } + def allChildXpaths = parentFragment.childFragments.collect { it.xpath } assert allChildXpaths.size() == 5 assert allChildXpaths.containsAll(listElementXpaths) and: 'the child node of the new list entry is also present' @@ -189,23 +188,6 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { scenario | parentNodeXpath | listElementXpaths || expectedException 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException 'already existing fragment' | '/parent-201' | ['/parent-201/child-204[@key="A"]'] || AlreadyDefinedException - - } - - static def createDataNodeTree(String... xpaths) { - def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0]) - if (xpaths.length > 1) { - def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length) - def childDataNode = createDataNodeTree(xPathsDescendant) - dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode)) - } - dataNodeBuilder.build() - } - - def getFragmentByXpath(dataspaceName, anchorName, xpath) { - def dataspace = dataspaceRepository.getByName(dataspaceName) - def anchor = anchorRepository.getByDataspaceAndName(dataspace, anchorName) - return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow() } @Sql([CLEAR_DATA, SET_DATA]) @@ -214,10 +196,10 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, inputXPath, OMIT_DESCENDANTS) then: 'data node is returned with no descendants' - assert result.getXpath() == XPATH_DATA_NODE_WITH_LEAVES + assert result.xpath == XPATH_DATA_NODE_WITH_LEAVES and: 'expected leaves' - assert result.getChildDataNodes().size() == 0 - assertLeavesMaps(result.getLeaves(), expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES]) + assert result.childDataNodes.size() == 0 + assertLeavesMaps(result.leaves, expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES]) where: 'the following data is used' scenario | inputXPath 'some xpath' | '/parent-100' @@ -233,12 +215,12 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result) then: 'data node is returned with all the descendants populated' assert mappedResult.size() == 4 - assert result.getChildDataNodes().size() == 2 - assert mappedResult.get('/parent-100/child-001').getChildDataNodes().size() == 0 - assert mappedResult.get('/parent-100/child-002').getChildDataNodes().size() == 1 + assert result.childDataNodes.size() == 2 + assert mappedResult.get('/parent-100/child-001').childDataNodes.size() == 0 + assert mappedResult.get('/parent-100/child-002').childDataNodes.size() == 1 and: 'extracted leaves maps are matching expected' mappedResult.forEach( - (xPath, dataNode) -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xPath])) + (xPath, dataNode) -> assertLeavesMaps(dataNode.leaves, expectedLeavesByXpathMap[xPath])) where: 'the following data is used' scenario | inputXPath 'some xpath' | '/parent-100' @@ -270,9 +252,9 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { assert updatedLeaves.size() == 1 assert updatedLeaves.'leaf-value' == 'new' and: 'existing child entry remains as is' - def childFragment = updatedFragment.getChildFragments().iterator().next() + def childFragment = updatedFragment.childFragments.iterator().next() def childLeaves = getLeavesMap(childFragment) - assert childFragment.getId() == UPDATE_DATA_NODE_SUB_FRAGMENT_ID + assert childFragment.id == UPDATE_DATA_NODE_SUB_FRAGMENT_ID assert childLeaves.'leaf-value' == 'original' } @@ -301,7 +283,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { assert updatedLeaves.size() == 1 assert updatedLeaves.'leaf-value' == 'new' and: 'updated entry has no children' - updatedFragment.getChildFragments().isEmpty() + updatedFragment.childFragments.isEmpty() and: 'previously attached child entry is removed from database' fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty() } @@ -320,8 +302,8 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { assert updatedLeaves.size() == 1 assert updatedLeaves.'leaf-value' == 'new' and: 'existing child entry is not updated as content is same' - def childFragment = updatedFragment.getChildFragments().iterator().next() - childFragment.getXpath() == '/parent-200/child-201/grand-child' + def childFragment = updatedFragment.childFragments.iterator().next() + childFragment.xpath == '/parent-200/child-201/grand-child' def childLeaves = getLeavesMap(childFragment) assert childLeaves.'leaf-value' == 'original' } @@ -340,8 +322,8 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { assert updatedLeaves.size() == 1 assert updatedLeaves.'leaf-value' == 'new' and: 'existing child entry is updated with the new content' - def childFragment = updatedFragment.getChildFragments().iterator().next() - childFragment.getXpath() == '/parent-200/child-201/grand-child' + def childFragment = updatedFragment.childFragments.iterator().next() + childFragment.xpath == '/parent-200/child-201/grand-child' def childLeaves = getLeavesMap(childFragment) assert childLeaves.'leaf-value' == 'new' } @@ -362,8 +344,8 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { and: 'previously attached child entry is removed from database' fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty() and: 'new child entry is persisted' - def childFragment = updatedFragment.getChildFragments().iterator().next() - childFragment.getXpath() == '/parent-200/child-201/grand-child-new' + def childFragment = updatedFragment.childFragments.iterator().next() + childFragment.xpath == '/parent-200/child-201/grand-child-new' def childLeaves = getLeavesMap(childFragment) assert childLeaves.'leaf-value' == 'new' } @@ -384,92 +366,95 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { } @Sql([CLEAR_DATA, SET_DATA]) - def 'Replace list content of #scenario.'() { - given: 'list element as a collection of data nodes' - def listElementCollection = toDataNodes(listElementXpaths) - when: 'list elements are replaced within the existing parent node' - objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, listElementCollection) - then: 'list elements are updated as expected, non-list element remains as is' - def parentFragment = fragmentRepository.getById(listElementFragmentID) - def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() } - assert allChildXpaths.size() == expectedChildXpaths.size() - assert allChildXpaths.containsAll(expectedChildXpaths) - where: 'following parameters were used' - scenario | listElementXpaths | parentXpath | listElementFragmentID || expectedChildXpaths - 'existing list element with non existing key' | ['/parent-201/child-204[@key="B"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="B"]'] - 'non existing list element with non existing key' | ['/parent-201/child-205[@key="1"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="A"]', '/parent-201/child-204[@key="X"]', '/parent-201/child-205[@key="1"]'] - 'list element with 1 existing key' | ['/parent-201/child-204[@key="X"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="X"]'] - 'list element with combined keys' | ['/parent-202/child-205[@key="A"]'] | '/parent-202' | LIST_DATA_NODE_PARENT202_FRAGMENT_ID || ['/parent-202/child-206[@key="A"]', '/parent-202/child-205[@key="A"]'] - 'grandchild list element' | ['/parent-200/child-202/grand-child-202[@key="E"]'] | '/parent-200/child-202' | LIST_DATA_NODE_CHILD202_FRAGMENT_ID || ['/parent-200/child-202/grand-child-202[@key="E"]'] - 'list element with two list elements' | ['/parent-201/child-204[@key="new X"]', '/parent-201/child-204[@key="Y"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="new X"]', '/parent-201/child-204[@key="Y"]'] - 'list element with compounded list element' | ['/parent-202/child-205[@key="A" and @key2="B"]'] | '/parent-202' | LIST_DATA_NODE_PARENT202_FRAGMENT_ID || ['/parent-202/child-206[@key="A"]', '/parent-202/child-205[@key="A" and @key2="B"]'] - 'list element with list element with parent with key value' | ['/parent-204[@key="L"]/child-210[@key="N"]'] | '/parent-204[@key="L"]' | LIST_DATA_NODE_PARENT204_FRAGMENT_ID || ['/parent-204[@key="L"]/child-210[@key="N"]'] + def 'Update existing list with #scenario.'() { + given: 'a parent having a list of data nodes containing: #originalKeys (ech list element has a child too)' + def parentXpath = '/parent-3' + if (originalKeys.size() > 0) { + def originalListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'original value', originalKeys, true) + objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, originalListEntriesAsDataNodes) + } + and: 'each original list element has one child' + def originalParentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID) + originalParentFragment.childFragments.each {assert it.childFragments.size() == 1 } + when: 'it is updated with #scenario' + def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'new value', replacementKeys, false) + objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, replacementListEntriesAsDataNodes) + then: 'the result list ONLY contains the expected replacement elements' + def parentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID) + def allChildXpaths = parentFragment.childFragments.collect { it.xpath } + def expectedListEntriesAfterUpdateAsXpaths = keysToXpaths(parentXpath, replacementKeys) + assert allChildXpaths.size() == replacementKeys.size() + assert allChildXpaths.containsAll(expectedListEntriesAfterUpdateAsXpaths) + and: 'all the list elements have the new values' + assert parentFragment.childFragments.stream().allMatch(childFragment -> childFragment.attributes.contains('new value')) + and: 'there are no more grandchildren as none of the replacement list entries had a child' + parentFragment.childFragments.each {assert it.childFragments.size() == 0 } + where: 'the following replacement lists are applied' + scenario | originalKeys | replacementKeys + 'one existing entry only' | [] | ['NEW'] + 'multiple new entries' | [] | ['NEW1', 'NEW2'] + 'one new entry only (existing entries are deleted)' | ['A', 'B'] | ['NEW1', 'NEW2'] + 'one existing on new entry' | ['A', 'B'] | ['A', 'NEW'] + 'one existing entry only' | ['A', 'B'] | ['A'] } @Sql([CLEAR_DATA, SET_DATA]) - def 'Replace list content that has #scenario'() { - given: 'list element with child list element as a collection of data nodes' - def grandChildDataNodes = toDataNodes(grandChildXpaths) - def listElementCollection = new DataNodeBuilder().withXpath(childXpath).withChildDataNodes(grandChildDataNodes).build() - when: 'list elements replaced within the existing parent node' - objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, [listElementCollection ]) - then: 'list elements are updated as expected with non-list elements remaining as is' - def parentFragment = fragmentRepository.getById(listElementFragmentId) - def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() } - assert allChildXpaths.size() == expectedRemainingChildXpaths.size() - assert allChildXpaths.containsAll(expectedRemainingChildXpaths) - and: 'grandchild list elements are updated as expected' - def allGrandChildXpaths = parentFragment.getChildFragments().collect(){ - it.getChildFragments().collect(){ - it.getXpath()}} - allGrandChildXpaths.removeIf(list -> list.isEmpty()) - def grandChildXpathsToList = allGrandChildXpaths.stream().flatMap(List::stream).collect(Collectors.toList()) - def expectedGrandChildXpaths = grandChildXpaths - assert grandChildXpathsToList.size() == expectedGrandChildXpaths.size() - assert grandChildXpathsToList.containsAll(expectedGrandChildXpaths) - where: 'the following parameters are used' - scenario | parentXpath | childXpath | grandChildXpaths | listElementFragmentId || expectedRemainingChildXpaths - 'grandchild of list' | '/parent-203' | '/parent-203/child-204[@key="X"]' | ['/parent-203/child-204/grandchild[@key="2"]'] | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="X"]'] - 'grandchild of list with two new element' | '/parent-203' | '/parent-203/child-204[@key="X"]' | ['/parent-203/child-204/grandchild[@key="2"]' , '/parent-203/child-204/grandchild[@key="3"]'] | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="X"]'] - 'grandchild with compound list elements' | '/parent-205' | '/parent-205/child-205[@key="X"]' | ['/parent-205/child-205/grand-child-206[@key="Y" and @key2="Z"]'] | LIST_DATA_NODE_PARENT205_FRAGMENT_ID || ['/parent-205/child-205', '/parent-205/child-205[@key="X"]'] + def 'Replacing existing list element with attributes and (grand)child.'() { + given: 'a parent with list elements A and B with attribute and grandchild tagged as "org"' + def parentXpath = '/parent-3' + def originalListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'org', ['A','B'], true) + objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, originalListEntriesAsDataNodes) + when: 'A is replaced with an entry with attribute and grandchild tagged tagged as "new" (B is not in replacement list)' + def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'new', ['A'], true) + objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, replacementListEntriesAsDataNodes) + then: 'The updated fragment has a child-list with ONLY element "A"' + def parentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID) + parentFragment.childFragments.size() == 1 + def childListElementA = parentFragment.childFragments[0] + childListElementA.xpath == "/parent-3/child-list[@key='A']" + and: 'element "A" has an attribute with the "new" (tag) value' + childListElementA.attributes == '{"attr1": "new"}' + and: 'element "A" has a only one (grand)child' + childListElementA.childFragments.size() == 1 + and: 'the grandchild is the new grandchild (tag)' + def grandChild = childListElementA.childFragments[0] + grandChild.xpath == "/parent-3/child-list[@key='A']/new-grand-child" + and: 'the grandchild has an attribute with the "new" (tag) value' + grandChild.attributes == '{"attr1": "new"}' } @Sql([CLEAR_DATA, SET_DATA]) - def 'Replace list content of #scenario with grandchildren.'() { - given: 'list element as a collection of data nodes' - def listElementCollection = toDataNodes(listElementXpaths) - when: 'list elements are replaced within the existing parent node' - objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, listElementCollection) - then: 'child list elements are updated as expected with non-list elements remaining as is' - def parentFragment = fragmentRepository.getById(listElementFragmentID) - def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() } - assert allChildXpaths.size() == expectedChildXpaths.size() - assert allChildXpaths.containsAll(expectedChildXpaths) - and: 'grandchild list elements are updated as expected' - def allGrandChildXpaths = parentFragment.getChildFragments().collect { - it.getChildFragments().collect { - it.getXpath()}} - allGrandChildXpaths.removeIf(list -> list.isEmpty()) - assert allGrandChildXpaths.size() == expectedGrandChildXpaths.size() - assert allGrandChildXpaths.containsAll(expectedGrandChildXpaths) - where: 'following parameters were used' - scenario | listElementXpaths | parentXpath | listElementFragmentID || expectedChildXpaths | expectedGrandChildXpaths - 'existing list element with existing keys' | ['/parent-203/child-204[@key="X"]'] | '/parent-203' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="X"]'] | [] - 'non existing list element with existing keys' | ['/parent-203/child-204[@key="V"]'] | '/parent-203' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="V"]'] | [] + def 'Replace list element for a parent (parent-1) with existing one (non-list) child'() { + when: 'a list element is added under the parent' + def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(XPATH_DATA_NODE_WITH_DESCENDANTS, 'new', ['A','B'], false) + objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, replacementListEntriesAsDataNodes) + then: 'the parent will have 3 children after the replacement' + def parentFragment = fragmentRepository.getById(ID_DATA_NODE_WITH_DESCENDANTS) + parentFragment.childFragments.size() == 3 + def xpaths = parentFragment.childFragments.collect {it.xpath} + and: 'one of the children is the original child fragment' + xpaths.contains('/parent-1/child-1') + and: 'it has the two new list elements' + xpaths.containsAll("/parent-1/child-list[@key='A']", "/parent-1/child-list[@key='B']") } - @Sql([CLEAR_DATA, SET_DATA]) - def 'Replace content error scenario: #scenario.'() { + def 'Replace list content using unknown parent'() { given: 'list element as a collection of data nodes' - def listElementCollection = toDataNodes(listElementXpaths) - when: 'list elements were replaced under existing parent node' - objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listElementCollection) - then: 'a #expectedException is thrown' - thrown(expectedException) - where: 'following parameters were used' - scenario | parentNodeXpath | listElementXpaths || expectedException - 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException + def listElementCollection = toDataNodes(['irrelevant']) + when: 'attempt to replace list elements under unknown parent node' + objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, '/unknown', listElementCollection) + then: 'a datanode not found exception is thrown' + thrown(DataNodeNotFoundException) + } + + @Sql([CLEAR_DATA, SET_DATA]) + def 'Replace list content with empty collection is not supported'() { + when: 'attempt to replace list elements with empty collection' + objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, '/parent-203', []) + then: 'a CPS admin exception is thrown' + def thrown = thrown(CpsAdminException) + assert thrown.message == 'Invalid list replacement' } @Sql([CLEAR_DATA, SET_DATA]) @@ -478,15 +463,15 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths) then: 'only the expected children remain' def parentFragment = fragmentRepository.getById(parentFragmentId) - def remainingChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() } + def remainingChildXpaths = parentFragment.childFragments.collect { it.xpath } assert remainingChildXpaths.size() == expectedRemainingChildXpaths.size() assert remainingChildXpaths.containsAll(expectedRemainingChildXpaths) where: 'following parameters were used' scenario | targetXpaths | parentFragmentId || expectedRemainingChildXpaths - 'list element with key' | '/parent-203/child-204[@key="A"]' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="X"]'] + 'list element with key' | '/parent-203/child-204[@key="A"]' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="B"]'] 'list element with combined keys' | '/parent-202/child-205[@key="A" and @key2="B"]' | LIST_DATA_NODE_PARENT202_FRAGMENT_ID || ['/parent-202/child-206[@key="A"]'] 'whole list' | '/parent-203/child-204' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203'] - 'list element under list element' | '/parent-203/child-204[@key="X"]/grand-child-204[@key2="Y"]' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="X"]', '/parent-203/child-204[@key="A"]'] + 'list element under list element' | '/parent-203/child-204[@key="B"]/grand-child-204[@key2="Y"]' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="A"]', '/parent-203/child-204[@key="B"]'] } @Sql([CLEAR_DATA, SET_DATA]) @@ -515,7 +500,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { then: 'verify data nodes are removed' try { dataNode = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, getDataNodesXpaths, INCLUDE_ALL_DESCENDANTS) - dataNodeXpath = dataNode.getXpath() + dataNodeXpath = dataNode.xpath assert dataNodeXpath == expectedXpaths } catch (DataNodeNotFoundException) { assert dataNodeXpath == expectedXpaths @@ -540,6 +525,20 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { 'invalid list element' | '/parent-206/child-206/grand-child-206@key="A"]' } + @Sql([CLEAR_DATA, SET_DATA]) + def 'Delete data node for an anchor.'() { + given: 'a data-node exists for an anchor' + assert fragmentsExistInDB(DATASPACE_1001_ID, ANCHOR_3003_ID) + when: 'data nodes are deleted ' + objectUnderTest.deleteDataNodes(DATASPACE_NAME, ANCHOR_NAME3) + then: 'all data-nodes are deleted successfully' + assert !fragmentsExistInDB(DATASPACE_1001_ID, ANCHOR_3003_ID) + } + + def fragmentsExistInDB(dataSpaceId, anchorId) { + !fragmentRepository.findRootsByDataspaceAndAnchor(dataSpaceId, anchorId).isEmpty() + } + static Collection toDataNodes(xpaths) { return xpaths.collect { new DataNodeBuilder().withXpath(it).build() } } @@ -549,7 +548,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { } static Map getLeavesMap(FragmentEntity fragmentEntity) { - return jsonObjectMapper.convertJsonString(fragmentEntity.getAttributes(), Map.class) + return jsonObjectMapper.convertJsonString(fragmentEntity.attributes, Map.class) } def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) { @@ -566,10 +565,51 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { } def static treeToFlatMapByXpath(Map flatMap, DataNode dataNodeTree) { - flatMap.put(dataNodeTree.getXpath(), dataNodeTree) - dataNodeTree.getChildDataNodes() + flatMap.put(dataNodeTree.xpath, dataNodeTree) + dataNodeTree.childDataNodes .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode)) return flatMap } + def keysToXpaths(parent, Collection keys) { + return keys.collect { "${parent}/child-list[@key='${it}']".toString() } + } + + def static createDataNodeTree(String... xpaths) { + def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0]) + if (xpaths.length > 1) { + def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length) + def childDataNode = createDataNodeTree(xPathsDescendant) + dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode)) + } + dataNodeBuilder.build() + } + + def getFragmentByXpath(dataspaceName, anchorName, xpath) { + def dataspace = dataspaceRepository.getByName(dataspaceName) + def anchor = anchorRepository.getByDataspaceAndName(dataspace, anchorName) + return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow() + } + + + def createChildListAllHavingAttributeValue(parentXpath, tag, Collection keys, boolean addGrandChild) { + def listElementAsDataNodes = keysToXpaths(parentXpath, keys).collect { + new DataNodeBuilder() + .withXpath(it) + .withLeaves([attr1: tag]) + .build() + } + if (addGrandChild) { + listElementAsDataNodes.each {it.childDataNodes = [createGrandChild(it.xpath, tag)]} + } + return listElementAsDataNodes + } + + def createGrandChild(parentXPath, tag) { + new DataNodeBuilder() + .withXpath("${parentXPath}/${tag}-grand-child") + .withLeaves([attr1: tag]) + .build() + } + } diff --git a/cps-ri/src/test/resources/application.yml b/cps-ri/src/test/resources/application.yml index 73292bbfa..18e6ed624 100644 --- a/cps-ri/src/test/resources/application.yml +++ b/cps-ri/src/test/resources/application.yml @@ -18,10 +18,12 @@ spring: jpa: ddl-auto: create + show-sql: true properties: hibernate: enable_lazy_load_no_trans: true dialect: org.hibernate.dialect.PostgreSQLDialect + format_sql: true datasource: url: ${DB_URL} diff --git a/cps-ri/src/test/resources/data/fragment.sql b/cps-ri/src/test/resources/data/fragment.sql index 5d3a32832..49c4c9f3e 100755 --- a/cps-ri/src/test/resources/data/fragment.sql +++ b/cps-ri/src/test/resources/data/fragment.sql @@ -48,29 +48,20 @@ INSERT INTO FRAGMENT (ID, DATASPACE_ID, ANCHOR_ID, PARENT_ID, XPATH, ATTRIBUTES) (4201, 1001, 3003, null, '/parent-200', '{"leaf-value": "original"}'), (4202, 1001, 3003, 4201, '/parent-200/child-201', '{"leaf-value": "original"}'), (4203, 1001, 3003, 4202, '/parent-200/child-201/grand-child', '{"leaf-value": "original"}'), - (4204, 1001, 3003, 4201, '/parent-200/child-202', '{"common-leaf-name": "common-leaf value", "common-leaf-name-int" : 5}'), - (4205, 1001, 3003, 4204, '/parent-200/child-202/grand-child-202[@key="D"]', '{"common-leaf-name": "common-leaf value", "common-leaf-name-int" : 5}'), (4206, 1001, 3003, null, '/parent-201', '{"leaf-value": "original"}'), (4207, 1001, 3003, 4206, '/parent-201/child-203', '{}'), (4208, 1001, 3003, 4206, '/parent-201/child-204[@key="A"]', '{"key": "A"}'), - (4209, 1001, 3003, 4206, '/parent-201/child-204[@key="X"]', '{"key": "X"}'), + (4209, 1001, 3003, 4206, '/parent-201/child-204[@key="B"]', '{"key": "B"}'), (4211, 1001, 3003, null, '/parent-202', '{"leaf-value": "original"}'), (4212, 1001, 3003, 4211, '/parent-202/child-205[@key="A" and @key2="B"]', '{"key": "A", "key2": "B"}'), (4213, 1001, 3003, 4211, '/parent-202/child-206[@key="A"]', '{"key": "A"}'), (4214, 1001, 3003, null, '/parent-203', '{"leaf-value": "original"}'), (4215, 1001, 3003, 4214, '/parent-203/child-203', '{}'), (4216, 1001, 3003, 4214, '/parent-203/child-204[@key="A"]', '{"key": "A"}'), - (4217, 1001, 3003, 4214, '/parent-203/child-204[@key="X"]', '{"key": "X"}'), - (4218, 1001, 3003, 4217, '/parent-203/child-204[@key="X"]/grand-child-204[@key2="Y"]', '{"key": "X", "key2": "Y"}'), - (4219, 1001, 3003, null, '/parent-204[@key="L"]', '{"key": "L"}'), - (4220, 1001, 3003, 4219, '/parent-204[@key="L"]/child-210[@key="M"]', '{"key": "M"}'), - (4221, 1001, 3003, null, '/parent-205', '{"leaf-value": "original"}'), - (4222, 1001, 3003, 4221, '/parent-205/child-205', '{}'), - (4223, 1001, 3003, 4221, '/parent-205/child-205[@key="X"]', '{"key": "X"}'), - (4224, 1001, 3003, 4223, '/parent-205/child-205[@key="X"]/grand-child-206[@key="Y"]', '{"key": "Y", "key2": "Z"}'), - (4225, 1001, 3003, 4223, '/parent-205/child-205[@key="X"]/grand-child-206[@key="Y" and @key2="Z"]', '{"key": "Y", "key2": "Z"}'), + (4217, 1001, 3003, 4214, '/parent-203/child-204[@key="B"]', '{"key": "B"}'), + (4218, 1001, 3003, 4217, '/parent-203/child-204[@key="B"]/grand-child-204[@key2="Y"]', '{"key": "B", "key2": "Y"}'), (4226, 1001, 3003, null, '/parent-206', '{"leaf-value": "original"}'), (4227, 1001, 3003, 4226, '/parent-206/child-206', '{}'), (4228, 1001, 3003, 4227, '/parent-206/child-206/grand-child-206', '{}'), (4229, 1001, 3003, 4227, '/parent-206/child-206/grand-child-206[@key="A"]', '{"key": "A"}'), - (4230, 1001, 3003, 4227, '/parent-206/child-206/grand-child-206[@key="X"]', '{"key": "X"}'); \ No newline at end of file + (4230, 1001, 3003, 4227, '/parent-206/child-206/grand-child-206[@key="X"]', '{"key": "X"}'); diff --git a/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java b/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java index f455e47ef..d2482d50a 100644 --- a/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java +++ b/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2020 Nordix Foundation * Modifications Copyright (C) 2021 Pantheon.tech - * Modifications Copyright (C) 2021 Bell Canada + * Modifications Copyright (C) 2021-2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ package org.onap.cps.api; import java.time.OffsetDateTime; -import org.checkerframework.checker.nullness.qual.NonNull; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.model.DataNode; @@ -40,8 +39,7 @@ public interface CpsDataService { * @param jsonData json data * @param observedTimestamp observedTimestamp */ - void saveData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String jsonData, - OffsetDateTime observedTimestamp); + void saveData(String dataspaceName, String anchorName, String jsonData, OffsetDateTime observedTimestamp); /** * Persists child data fragment under existing data node for the given anchor and dataspace. @@ -52,8 +50,8 @@ public interface CpsDataService { * @param jsonData json data * @param observedTimestamp observedTimestamp */ - void saveData(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath, - @NonNull String jsonData, OffsetDateTime observedTimestamp); + void saveData(String dataspaceName, String anchorName, String parentNodeXpath, String jsonData, + OffsetDateTime observedTimestamp); /** * Persists child data fragment representing one or more list elements under existing data node for the @@ -65,9 +63,8 @@ public interface CpsDataService { * @param jsonData json data representing list element(s) * @param observedTimestamp observedTimestamp */ - void saveListElements(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String parentNodeXpath, - @NonNull String jsonData, OffsetDateTime observedTimestamp); + void saveListElements(String dataspaceName, String anchorName, String parentNodeXpath, String jsonData, + OffsetDateTime observedTimestamp); /** * Retrieves datanode by XPath for given dataspace and anchor. @@ -79,8 +76,8 @@ public interface CpsDataService { * (recursively) as well * @return data node object */ - DataNode getDataNode(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String xpath, - @NonNull FetchDescendantsOption fetchDescendantsOption); + DataNode getDataNode(String dataspaceName, String anchorName, String xpath, + FetchDescendantsOption fetchDescendantsOption); /** * Updates data node for given dataspace and anchor using xpath to parent node. @@ -91,8 +88,8 @@ public interface CpsDataService { * @param jsonData json data * @param observedTimestamp observedTimestamp */ - void updateNodeLeaves(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath, - @NonNull String jsonData, OffsetDateTime observedTimestamp); + void updateNodeLeaves(String dataspaceName, String anchorName, String parentNodeXpath, String jsonData, + OffsetDateTime observedTimestamp); /** * Replaces existing data node content including descendants. @@ -103,8 +100,8 @@ public interface CpsDataService { * @param jsonData json data * @param observedTimestamp observedTimestamp */ - void replaceNodeTree(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath, - @NonNull String jsonData, OffsetDateTime observedTimestamp); + void replaceNodeTree(String dataspaceName, String anchorName, String parentNodeXpath, String jsonData, + OffsetDateTime observedTimestamp); /** * Replaces list content by removing all existing elements and inserting the given new elements as json @@ -116,8 +113,8 @@ public interface CpsDataService { * @param jsonData json data representing the new list elements * @param observedTimestamp observedTimestamp */ - void replaceListContent(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath, - @NonNull String jsonData, OffsetDateTime observedTimestamp); + void replaceListContent(String dataspaceName, String anchorName, String parentNodeXpath, String jsonData, + OffsetDateTime observedTimestamp); /** * Deletes data node for given anchor and dataspace. @@ -127,8 +124,17 @@ public interface CpsDataService { * @param dataNodeXpath data node xpath * @param observedTimestamp observed timestamp */ - void deleteDataNode(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String dataNodeXpath, - OffsetDateTime observedTimestamp); + void deleteDataNode(String dataspaceName, String anchorName, String dataNodeXpath, + OffsetDateTime observedTimestamp); + + /** + * Deletes all data nodes for a given anchor in a dataspace. + * + * @param dataspaceName dataspace name + * @param anchorName anchor name + * @param observedTimestamp observed timestamp + */ + void deleteDataNodes(String dataspaceName, String anchorName, OffsetDateTime observedTimestamp); /** * Deletes a list or a list-element under given anchor and dataspace. @@ -138,8 +144,8 @@ public interface CpsDataService { * @param listElementXpath list element xpath * @param observedTimestamp observedTimestamp */ - void deleteListOrListElement(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String listElementXpath, OffsetDateTime observedTimestamp); + void deleteListOrListElement(String dataspaceName, String anchorName, String listElementXpath, + OffsetDateTime observedTimestamp); /** * Updates leaves of DataNode for given dataspace and anchor using xpath, along with the leaves of each Child Data diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsAdminServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsAdminServiceImpl.java index d30a6571d..1013addbe 100755 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsAdminServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsAdminServiceImpl.java @@ -22,19 +22,24 @@ package org.onap.cps.api.impl; +import java.time.OffsetDateTime; import java.util.Collection; import java.util.stream.Collectors; +import lombok.AllArgsConstructor; import org.onap.cps.api.CpsAdminService; +import org.onap.cps.api.CpsDataService; import org.onap.cps.spi.CpsAdminPersistenceService; import org.onap.cps.spi.model.Anchor; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component("CpsAdminServiceImpl") +@AllArgsConstructor(onConstructor = @__(@Lazy)) public class CpsAdminServiceImpl implements CpsAdminService { - @Autowired - private CpsAdminPersistenceService cpsAdminPersistenceService; + private final CpsAdminPersistenceService cpsAdminPersistenceService; + @Lazy + private final CpsDataService cpsDataService; @Override public void createDataspace(final String dataspaceName) { @@ -68,6 +73,7 @@ public class CpsAdminServiceImpl implements CpsAdminService { @Override public void deleteAnchor(final String dataspaceName, final String anchorName) { + cpsDataService.deleteDataNodes(dataspaceName, anchorName, OffsetDateTime.now()); cpsAdminPersistenceService.deleteAnchor(dataspaceName, anchorName); } diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java index a23bc95f3..af06e5fc1 100755 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java @@ -24,6 +24,7 @@ package org.onap.cps.api.impl; import java.time.OffsetDateTime; import java.util.Collection; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.CpsAdminService; import org.onap.cps.api.CpsDataService; @@ -32,31 +33,25 @@ import org.onap.cps.notification.Operation; import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.exceptions.DataValidationException; +import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.model.DataNode; import org.onap.cps.spi.model.DataNodeBuilder; import org.onap.cps.utils.YangUtils; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @Slf4j +@AllArgsConstructor public class CpsDataServiceImpl implements CpsDataService { private static final String ROOT_NODE_XPATH = "/"; - @Autowired - private CpsDataPersistenceService cpsDataPersistenceService; - - @Autowired - private CpsAdminService cpsAdminService; - - @Autowired - private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache; - - @Autowired - private NotificationService notificationService; + private final CpsDataPersistenceService cpsDataPersistenceService; + private final CpsAdminService cpsAdminService; + private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache; + private final NotificationService notificationService; @Override public void saveData(final String dataspaceName, final String anchorName, final String jsonData, @@ -137,6 +132,14 @@ public class CpsDataServiceImpl implements CpsDataService { processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE); } + @Override + public void deleteDataNodes(final String dataspaceName, final String anchorName, + final OffsetDateTime observedTimestamp) { + final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); + cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName); + processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, Operation.DELETE, observedTimestamp); + } + @Override public void deleteListOrListElement(final String dataspaceName, final String anchorName, final String listNodeXpath, final OffsetDateTime observedTimestamp) { @@ -185,9 +188,16 @@ public class CpsDataServiceImpl implements CpsDataService { private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName, final OffsetDateTime observedTimestamp, final String xpath, final Operation operation) { + final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); + this.processDataUpdatedEventAsync(anchor, xpath, operation, observedTimestamp); + } + + private void processDataUpdatedEventAsync(final Anchor anchor, final String xpath, final Operation operation, + final OffsetDateTime observedTimestamp) { try { - notificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, xpath, operation); + notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath, operation); } catch (final Exception exception) { + //If async message can't be queued for notification service, the initial request should not failed. log.error("Failed to send message to notification service", exception); } } diff --git a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java index 6054ce5d7..e7b639d48 100644 --- a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java +++ b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java @@ -25,7 +25,7 @@ import java.net.URISyntaxException; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.util.UUID; -import org.onap.cps.api.CpsAdminService; +import lombok.AllArgsConstructor; import org.onap.cps.api.CpsDataService; import org.onap.cps.event.model.Content; import org.onap.cps.event.model.CpsDataUpdatedEvent; @@ -38,6 +38,7 @@ import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component +@AllArgsConstructor(onConstructor = @__(@Lazy)) public class CpsDataUpdatedEventFactory { private static final URI EVENT_SCHEMA; @@ -56,29 +57,22 @@ public class CpsDataUpdatedEventFactory { } } + @Lazy private final CpsDataService cpsDataService; - private final CpsAdminService cpsAdminService; - - public CpsDataUpdatedEventFactory(@Lazy final CpsDataService cpsDataService, - final CpsAdminService cpsAdminService) { - this.cpsDataService = cpsDataService; - this.cpsAdminService = cpsAdminService; - } /** * Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used. * - * @param dataspaceName dataspaceName - * @param anchorName anchorName + * @param anchor anchor * @param observedTimestamp observedTimestamp * @param operation operation * @return CpsDataUpdatedEvent */ - public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final String dataspaceName, final String anchorName, + public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final Anchor anchor, final OffsetDateTime observedTimestamp, final Operation operation) { - final var dataNode = cpsDataService - .getDataNode(dataspaceName, anchorName, "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); - final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); + final var dataNode = (operation == Operation.DELETE) ? null : + cpsDataService.getDataNode(anchor.getDataspaceName(), anchor.getName(), + "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp, operation); } @@ -105,10 +99,12 @@ public class CpsDataUpdatedEventFactory { content.withAnchorName(anchor.getName()); content.withDataspaceName(anchor.getDataspaceName()); content.withSchemaSetName(anchor.getSchemaSetName()); - content.withData(createData(dataNode)); content.withOperation(Content.Operation.fromValue(operation.name())); content.withObservedTimestamp( DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp)); + if (dataNode != null) { + content.withData(createData(dataNode)); + } return content; } } diff --git a/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java b/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java index 97a14797b..5ad59df2a 100644 --- a/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java +++ b/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java @@ -29,6 +29,7 @@ import java.util.concurrent.Future; import java.util.regex.Pattern; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; +import org.onap.cps.spi.model.Anchor; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -80,22 +81,20 @@ public class NotificationService { /** * Process Data Updated Event and publishes the notification. * - * @param dataspaceName dataspace name - * @param anchorName anchor name + * @param anchor anchor * @param observedTimestamp observedTimestamp * @param xpath xpath of changed data node * @param operation operation * @return future */ @Async("notificationExecutor") - public Future processDataUpdatedEvent(final String dataspaceName, final String anchorName, - final OffsetDateTime observedTimestamp, + public Future processDataUpdatedEvent(final Anchor anchor, final OffsetDateTime observedTimestamp, final String xpath, final Operation operation) { - log.debug("process data updated event for dataspace '{}' & anchor '{}'", dataspaceName, anchorName); + log.debug("process data updated event for anchor '{}'", anchor); try { - if (shouldSendNotification(dataspaceName)) { + if (shouldSendNotification(anchor.getDataspaceName())) { final var cpsDataUpdatedEvent = - cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, anchorName, + cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, observedTimestamp, getRootNodeOperation(xpath, operation)); log.debug("data updated event to be published {}", cpsDataUpdatedEvent); notificationPublisher.sendNotification(cpsDataUpdatedEvent); @@ -105,7 +104,7 @@ public class NotificationService { CPS operation should not fail if sending event fails for any reason. */ notificationErrorHandler.onException("Failed to process cps-data-updated-event.", - exception, dataspaceName, anchorName); + exception, anchor, xpath, operation); } return CompletableFuture.completedFuture(null); } diff --git a/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java index b8c472f27..fd658861c 100644 --- a/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java +++ b/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2020 Nordix Foundation. * Modifications Copyright (C) 2021 Pantheon.tech + * Modifications Copyright (C) 2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +24,6 @@ package org.onap.cps.spi; import java.util.Collection; import java.util.Map; -import org.checkerframework.checker.nullness.qual.NonNull; import org.onap.cps.spi.model.DataNode; /* @@ -40,8 +40,7 @@ public interface CpsDataPersistenceService { * @param anchorName anchor name * @param dataNode data node */ - void storeDataNode(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull DataNode dataNode); + void storeDataNode(String dataspaceName, String anchorName, DataNode dataNode); /** * Add a child to a Fragment. @@ -51,8 +50,7 @@ public interface CpsDataPersistenceService { * @param parentXpath parent xpath * @param dataNode dataNode */ - void addChildDataNode(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentXpath, - @NonNull DataNode dataNode); + void addChildDataNode(String dataspaceName, String anchorName, String parentXpath, DataNode dataNode); /** * Adds list child elements to a Fragment. @@ -63,8 +61,8 @@ public interface CpsDataPersistenceService { * @param listElementsCollection collection of data nodes representing list elements */ - void addListElements(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String parentNodeXpath, - @NonNull Collection listElementsCollection); + void addListElements(String dataspaceName, String anchorName, String parentNodeXpath, + Collection listElementsCollection); /** * Retrieves datanode by XPath for given dataspace and anchor. @@ -76,8 +74,8 @@ public interface CpsDataPersistenceService { * (recursively) as well * @return data node object */ - DataNode getDataNode(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String xpath, - @NonNull FetchDescendantsOption fetchDescendantsOption); + DataNode getDataNode(String dataspaceName, String anchorName, String xpath, + FetchDescendantsOption fetchDescendantsOption); /** @@ -88,8 +86,7 @@ public interface CpsDataPersistenceService { * @param xpath xpath * @param leaves the leaves as a map where key is a leaf name and a value is a leaf value */ - void updateDataLeaves(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull String xpath, - @NonNull Map leaves); + void updateDataLeaves(String dataspaceName, String anchorName, String xpath, Map leaves); /** * Replaces existing data node content including descendants. @@ -98,7 +95,7 @@ public interface CpsDataPersistenceService { * @param anchorName anchor name * @param dataNode data node */ - void replaceDataNodeTree(@NonNull String dataspaceName, @NonNull String anchorName, @NonNull DataNode dataNode); + void replaceDataNodeTree(String dataspaceName, String anchorName, DataNode dataNode); /** * Replaces list content by removing all existing elements and inserting the given new elements @@ -109,8 +106,8 @@ public interface CpsDataPersistenceService { * @param parentNodeXpath parent node xpath * @param newListElements collection of data nodes representing the new list content */ - void replaceListContent(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String parentNodeXpath, @NonNull Collection newListElements); + void replaceListContent(String dataspaceName, String anchorName, + String parentNodeXpath, Collection newListElements); /** * Deletes any dataNode, yang container or yang list or yang list element. @@ -119,8 +116,15 @@ public interface CpsDataPersistenceService { * @param anchorName anchor name * @param targetXpath xpath to list or list element (include [@key=value] to delete a single list element) */ - void deleteDataNode(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String targetXpath); + void deleteDataNode(String dataspaceName, String anchorName, String targetXpath); + + /** + * Deletes all dataNodes in a given anchor. + * + * @param dataspaceName dataspace name + * @param anchorName anchor name + */ + void deleteDataNodes(String dataspaceName, String anchorName); /** * Deletes existing a single list element or the whole list. @@ -129,8 +133,7 @@ public interface CpsDataPersistenceService { * @param anchorName anchor name * @param targetXpath xpath to list or list element (include [@key=value] to delete a single list element) */ - void deleteListDataNode(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String targetXpath); + void deleteListDataNode(String dataspaceName, String anchorName, String targetXpath); /** * Get a datanode by cps path. @@ -142,7 +145,7 @@ public interface CpsDataPersistenceService { * included in the output * @return the data nodes found i.e. 0 or more data nodes */ - Collection queryDataNodes(@NonNull String dataspaceName, @NonNull String anchorName, - @NonNull String cpsPath, @NonNull FetchDescendantsOption fetchDescendantsOption); + Collection queryDataNodes(String dataspaceName, String anchorName, + String cpsPath, FetchDescendantsOption fetchDescendantsOption); } diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy index fe6e46086..bb122d1ae 100755 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy @@ -22,17 +22,16 @@ package org.onap.cps.api.impl +import org.onap.cps.api.CpsDataService import org.onap.cps.spi.CpsAdminPersistenceService import org.onap.cps.spi.model.Anchor import spock.lang.Specification +import java.time.OffsetDateTime class CpsAdminServiceImplSpec extends Specification { def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService) - def objectUnderTest = new CpsAdminServiceImpl() - - def setup() { - objectUnderTest.cpsAdminPersistenceService = mockCpsAdminPersistenceService - } + def mockCpsDataService = Mock(CpsDataService) + def objectUnderTest = new CpsAdminServiceImpl(mockCpsAdminPersistenceService, mockCpsDataService) def 'Create dataspace method invokes persistence service.'() { when: 'create dataspace method is invoked' @@ -75,7 +74,9 @@ class CpsAdminServiceImplSpec extends Specification { def 'Delete anchor.'() { when: 'delete anchor is invoked' objectUnderTest.deleteAnchor('someDataspace','someAnchor') - then: 'associated persistence service method is invoked with same parameters' + then: 'delete data nodes is invoked on the data service with expected parameters' + 1 * mockCpsDataService.deleteDataNodes('someDataspace','someAnchor', _ as OffsetDateTime ) + and: 'the persistence service method is invoked with same parameters to delete anchor' 1 * mockCpsAdminPersistenceService.deleteAnchor('someDataspace','someAnchor') } diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy index a302eb5e7..785788be9 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy @@ -24,7 +24,6 @@ package org.onap.cps.api.impl import org.onap.cps.TestUtils import org.onap.cps.api.CpsAdminService -import org.onap.cps.api.CpsModuleService import org.onap.cps.notification.NotificationService import org.onap.cps.notification.Operation import org.onap.cps.spi.CpsDataPersistenceService @@ -44,18 +43,17 @@ class CpsDataServiceImplSpec extends Specification { def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache) def mockNotificationService = Mock(NotificationService) - def objectUnderTest = new CpsDataServiceImpl() + def objectUnderTest = new CpsDataServiceImpl(mockCpsDataPersistenceService, mockCpsAdminService, + mockYangTextSchemaSourceSetCache, mockNotificationService) def setup() { - objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService - objectUnderTest.cpsAdminService = mockCpsAdminService - objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache - objectUnderTest.notificationService = mockNotificationService + mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor } def dataspaceName = 'some dataspace' def anchorName = 'some anchor' def schemaSetName = 'some schema set' + def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build() def observedTimestamp = OffsetDateTime.now() def 'Saving json data.'() { @@ -68,7 +66,7 @@ class CpsDataServiceImplSpec extends Specification { 1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, { dataNode -> dataNode.xpath == '/test-tree' }) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/', Operation.CREATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/', Operation.CREATE) } def 'Saving child data fragment under existing node.'() { @@ -81,7 +79,7 @@ class CpsDataServiceImplSpec extends Specification { 1 * mockCpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, '/test-tree', { dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' }) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.CREATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.CREATE) } def 'Saving list element data fragment under existing node.'() { @@ -101,7 +99,7 @@ class CpsDataServiceImplSpec extends Specification { } ) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.UPDATE) } def 'Saving empty list element data fragment.'() { @@ -133,7 +131,7 @@ class CpsDataServiceImplSpec extends Specification { then: 'the persistence service method is invoked with correct parameters' 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, expectedNodeXpath, leaves) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, parentNodeXpath, Operation.UPDATE) where: 'following parameters were used' scenario | parentNodeXpath | jsonData || expectedNodeXpath | leaves 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' | Collections.emptyMap() @@ -166,7 +164,7 @@ class CpsDataServiceImplSpec extends Specification { 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, "/bookstore/categories[@code='01']", ['name':'Romance', 'code': '01']) and: 'the data updated event is sent to the notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/bookstore', Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/bookstore', Operation.UPDATE) } def 'Replace data node: #scenario.'() { @@ -178,7 +176,7 @@ class CpsDataServiceImplSpec extends Specification { 1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, { dataNode -> dataNode.xpath == expectedNodeXpath }) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, parentNodeXpath, Operation.UPDATE) where: 'following parameters were used' scenario | parentNodeXpath | jsonData || expectedNodeXpath 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' @@ -202,7 +200,7 @@ class CpsDataServiceImplSpec extends Specification { } ) and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.UPDATE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.UPDATE) } def 'Replace whole list content with empty list element.'() { @@ -223,7 +221,7 @@ class CpsDataServiceImplSpec extends Specification { then: 'the persistence service method is invoked with correct parameters' 1 * mockCpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, '/test-tree/branch') and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree/branch', Operation.DELETE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree/branch', Operation.DELETE) } def 'Delete data node under anchor and dataspace.'() { @@ -234,12 +232,22 @@ class CpsDataServiceImplSpec extends Specification { then: 'the persistence service method is invoked with the correct parameters' 1 * mockCpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, '/data-node') and: 'data updated event is sent to notification service' - 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/data-node', Operation.DELETE) + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/data-node', Operation.DELETE) + } + + def 'Delete all data nodes for a given anchor and dataspace.'() { + given: 'schema set for given anchor and dataspace references test tree model' + setupSchemaSetMocks('test-tree.yang') + when: 'delete data node method is invoked with correct parameters' + objectUnderTest.deleteDataNodes(dataspaceName, anchorName, observedTimestamp) + then: 'the persistence service method is invoked with the correct parameters' + 1 * mockCpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName) + and: 'data updated event is sent to notification service' + 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/', Operation.DELETE) + } def setupSchemaSetMocks(String... yangResources) { - def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build() - mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet) mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(yangResources) diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy index d18bcf55e..52389522f 100755 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy @@ -37,23 +37,17 @@ class E2ENetworkSliceSpec extends Specification { def mockDataStoreService = Mock(CpsDataPersistenceService) def mockCpsAdminService = Mock(CpsAdminService) def mockNotificationService = Mock(NotificationService) - def cpsDataServiceImpl = new CpsDataServiceImpl() def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache) def cpsModuleServiceImpl = new CpsModuleServiceImpl(mockModuleStoreService, mockYangTextSchemaSourceSetCache,mockCpsAdminService ) + def cpsDataServiceImpl = new CpsDataServiceImpl(mockDataStoreService, mockCpsAdminService, + mockYangTextSchemaSourceSetCache, mockNotificationService) def dataspaceName = 'someDataspace' def anchorName = 'someAnchor' def schemaSetName = 'someSchemaSet' def noTimestamp = null - def setup() { - cpsDataServiceImpl.cpsDataPersistenceService = mockDataStoreService - cpsDataServiceImpl.cpsAdminService = mockCpsAdminService - cpsDataServiceImpl.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache - cpsDataServiceImpl.notificationService = mockNotificationService - } - def 'E2E model can be parsed by CPS.'() { given: 'Valid yang resource as name-to-content map' def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap( diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy index 67ed3d90f..5b13fa516 100644 --- a/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy @@ -36,27 +36,22 @@ import spock.lang.Specification class CpsDataUpdateEventFactorySpec extends Specification { def mockCpsDataService = Mock(CpsDataService) - def mockCpsAdminService = Mock(CpsAdminService) - def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockCpsAdminService) + def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService) - def myDataspaceName = 'my-dataspace' - def myAnchorName = 'my-anchorname' - def mySchemasetName = 'my-schemaset-name' def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ' def 'Create a CPS data updated event successfully: #scenario'() { - given: 'cps admin service is able to return anchor details' - mockCpsAdminService.getAnchor(myDataspaceName, myAnchorName) >> - new Anchor(myAnchorName, myDataspaceName, mySchemasetName) + given: 'an anchor which has been updated' + def anchor = new Anchor('my-anchorname', 'my-dataspace', 'my-schemaset-name') and: 'cps data service returns the data node details' def xpath = '/' def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build() mockCpsDataService.getDataNode( - myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode + 'my-dataspace', 'my-anchorname', xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode when: 'CPS data updated event is created' - def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName, - myAnchorName, DateTimeUtility.toOffsetDateTime(inputObservedTimestamp), Operation.CREATE) + def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(anchor, + DateTimeUtility.toOffsetDateTime(inputObservedTimestamp), Operation.CREATE) then: 'CPS data updated event is created with correct envelope' with(cpsDataUpdatedEvent) { type == 'org.onap.cps.data-updated-event' @@ -72,10 +67,10 @@ class CpsDataUpdateEventFactorySpec extends Specification { assert observedTimestamp == inputObservedTimestamp else assert OffsetDateTime.now().minusSeconds(20).isBefore( - DateTimeUtility.toOffsetDateTime(observedTimestamp)) - assert anchorName == myAnchorName - assert dataspaceName == myDataspaceName - assert schemaSetName == mySchemasetName + DateTimeUtility.toOffsetDateTime(observedTimestamp)) + assert anchorName == 'my-anchorname' + assert dataspaceName == 'my-dataspace' + assert schemaSetName == 'my-schemaset-name' assert operation == Content.Operation.CREATE assert data == new Data().withAdditionalProperty('leafName', 'leafValue') } @@ -86,6 +81,33 @@ class CpsDataUpdateEventFactorySpec extends Specification { 'missing observed timestamp' | null } + def 'Create a delete CPS data updated event successfully'() { + given: 'an anchor which has been deleted' + def anchor = new Anchor('my-anchorname', 'my-dataspace', 'my-schemaset-name') + def deletionTimestamp = '2021-01-01T23:00:00.345-0400' + when: 'a delete root data node event is created' + def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(anchor, + DateTimeUtility.toOffsetDateTime(deletionTimestamp), Operation.DELETE) + then: 'CPS data updated event is created with correct envelope' + with(cpsDataUpdatedEvent) { + type == 'org.onap.cps.data-updated-event' + source == new URI('urn:cps:org.onap.cps') + schema == new URI('urn:cps:org.onap.cps:data-updated-event-schema:v1') + StringUtils.hasText(id) + content != null + } + and: 'correct content' + with(cpsDataUpdatedEvent.content) { + assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format" + assert observedTimestamp == deletionTimestamp + assert anchorName == 'my-anchorname' + assert dataspaceName == 'my-dataspace' + assert schemaSetName == 'my-schemaset-name' + assert operation == Content.Operation.DELETE + assert data == null + } + } + def isExpectedDateTimeFormat(String observedTimestamp) { try { DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp) diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy index 306e187a0..c20bdeea7 100644 --- a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy @@ -23,6 +23,7 @@ package org.onap.cps.notification import java.time.OffsetDateTime import org.onap.cps.config.AsyncConfig import org.onap.cps.event.model.CpsDataUpdatedEvent +import org.onap.cps.spi.model.Anchor import org.spockframework.spring.SpringBean import org.spockframework.spring.SpringSpy import org.springframework.beans.factory.annotation.Autowired @@ -53,15 +54,14 @@ class NotificationServiceSpec extends Specification { NotificationService objectUnderTest @Shared - def myDataspacePublishedName = 'my-dataspace-published' - def myAnchorName = 'my-anchorname' + def anchor = new Anchor('my-anchorname', 'my-dataspace-published', 'my-schemaset-name') def myObservedTimestamp = OffsetDateTime.now() def 'Skip sending notification when disabled.'() { given: 'notification is disabled' spyNotificationProperties.isEnabled() >> false when: 'dataUpdatedEvent is received' - objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/', Operation.CREATE) + objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE) then: 'the notification is not sent' 0 * mockNotificationPublisher.sendNotification(_) } @@ -69,13 +69,14 @@ class NotificationServiceSpec extends Specification { def 'Send notification when enabled: #scenario.'() { given: 'notification is enabled' spyNotificationProperties.isEnabled() >> true + and: 'an anchor is in dataspace where #scenario' + def anchor = new Anchor('my-anchorname', dataspaceName, 'my-schemaset-name') and: 'event factory can create event successfully' def cpsDataUpdatedEvent = new CpsDataUpdatedEvent() - mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, myAnchorName, myObservedTimestamp, - Operation.CREATE) >> + mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >> cpsDataUpdatedEvent when: 'dataUpdatedEvent is received' - def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, myAnchorName, myObservedTimestamp, + def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) @@ -86,7 +87,7 @@ class NotificationServiceSpec extends Specification { where: scenario | dataspaceName || expectedSendNotificationCount 'dataspace name does not match filter' | 'does-not-match-pattern' || 0 - 'dataspace name matches filter' | myDataspacePublishedName || 1 + 'dataspace name matches filter' | 'my-dataspace-published' || 1 } def 'Send UPDATE operation when non-root data nodes are changed.'() { @@ -94,10 +95,10 @@ class NotificationServiceSpec extends Specification { spyNotificationProperties.isEnabled() >> true and: 'event factory creates event if operation is UPDATE' def cpsDataUpdatedEvent = new CpsDataUpdatedEvent() - mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, + mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.UPDATE) >> cpsDataUpdatedEvent when: 'dataUpdatedEvent is received for non-root xpath' - def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/non-root-node', + def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/non-root-node', operation) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) @@ -114,11 +115,10 @@ class NotificationServiceSpec extends Specification { spyNotificationProperties.isEnabled() >> true and: 'event factory creates event if operation is #operation' def cpsDataUpdatedEvent = new CpsDataUpdatedEvent() - mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, - operation) >> cpsDataUpdatedEvent + mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, operation) >> + cpsDataUpdatedEvent when: 'dataUpdatedEvent is received for root xpath' - def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/', - operation) + def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', operation) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) then: 'async process completed successfully' @@ -134,19 +134,17 @@ class NotificationServiceSpec extends Specification { given: 'notification is enabled' spyNotificationProperties.isEnabled() >> true and: 'event factory can not create event successfully' - mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, - myObservedTimestamp, Operation.CREATE) >> + mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >> { throw new Exception("Could not create event") } when: 'event is sent for processing' - def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, - myObservedTimestamp, '/', Operation.CREATE) + def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE) and: 'wait for async processing to complete' future.get(10, TimeUnit.SECONDS) then: 'async process completed successfully' future.isDone() and: 'error is handled and not thrown to caller' notThrown Exception - 1 * spyNotificationErrorHandler.onException(_, _, _, _) + 1 * spyNotificationErrorHandler.onException(_, _, _, '/', Operation.CREATE) } }