summaryrefslogtreecommitdiffstats
path: root/cps-service/src/test
diff options
context:
space:
mode:
authorArpit Singh <as00745003@techmahindra.com>2023-08-02 18:35:31 +0530
committerArpit Singh <as00745003@techmahindra.com>2024-02-08 13:26:53 +0000
commite140c60532de6487225ee7860ade0825cb350648 (patch)
tree65b326595091936131744f61cd8203f49c95f212 /cps-service/src/test
parent60c73123ba15404816282dd36b370a3fec10c91d (diff)
CPS Delta API 1: Delta between 2 anchors
- CPS Delta Feature Part 1: To find delta between two anchors - created new endpoint deltaByDataspaceAndAnchors - endpoint to take dataspaceName, source anchor, target anchor, xpath, descendants as input - added new service CpsDeltaService - added method to find delta between DataNodes: getDeltaReport - added method to find removed data nodes: getRemovedDeltaReports - added method to get Added DataNodes: getAddedDeltaReports - added method to get Map of xpath to DataNode: convertToXPathToDataNodesMap - added a POJO for delta report - Added new JSON data for delta feature testing - Added groovy test files CpsDeltaServiceImplSpec and DeltaReportBuilderSpec - code related to update operation, will be added in separate commit Issue-ID: CPS-1824 Signed-off-by: Arpit Singh <as00745003@techmahindra.com> Change-Id: I313f0f71d04b03878be7643f709d8af1aa6df6ba
Diffstat (limited to 'cps-service/src/test')
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy33
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDeltaServiceImplSpec.groovy66
-rwxr-xr-xcps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy6
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/spi/model/DeltaReportBuilderSpec.groovy52
4 files changed, 153 insertions, 4 deletions
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 e1d15d68a..a91459852 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
@@ -25,6 +25,7 @@ package org.onap.cps.api.impl
import org.onap.cps.TestUtils
import org.onap.cps.api.CpsAdminService
+import org.onap.cps.api.CpsDeltaService
import org.onap.cps.notification.NotificationService
import org.onap.cps.notification.Operation
import org.onap.cps.spi.CpsDataPersistenceService
@@ -37,12 +38,14 @@ import org.onap.cps.spi.exceptions.SessionTimeoutException
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.spi.model.DeltaReportBuilder
+import org.onap.cps.spi.utils.CpsValidator
import org.onap.cps.utils.ContentType
import org.onap.cps.utils.TimedYangParser
import org.onap.cps.yang.YangTextSchemaSourceSet
import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
+import spock.lang.Shared
import spock.lang.Specification
-import org.onap.cps.spi.utils.CpsValidator
import java.time.OffsetDateTime
import java.util.stream.Collectors
@@ -54,18 +57,28 @@ class CpsDataServiceImplSpec extends Specification {
def mockNotificationService = Mock(NotificationService)
def mockCpsValidator = Mock(CpsValidator)
def timedYangParser = new TimedYangParser()
+ def mockCpsDeltaService = Mock(CpsDeltaService);
def objectUnderTest = new CpsDataServiceImpl(mockCpsDataPersistenceService, mockCpsAdminService,
- mockYangTextSchemaSourceSetCache, mockNotificationService, mockCpsValidator, timedYangParser)
+ mockYangTextSchemaSourceSetCache, mockNotificationService, mockCpsValidator, timedYangParser, mockCpsDeltaService)
def setup() {
+
mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
+ mockCpsAdminService.getAnchor(dataspaceName, ANCHOR_NAME_1) >> anchor1
+ mockCpsAdminService.getAnchor(dataspaceName, ANCHOR_NAME_2) >> anchor2
}
+ @Shared
+ static def ANCHOR_NAME_1 = 'some-anchor-1'
+ @Shared
+ static def ANCHOR_NAME_2 = 'some-anchor-2'
def dataspaceName = 'some-dataspace'
def anchorName = 'some-anchor'
def schemaSetName = 'some-schema-set'
def anchor = Anchor.builder().name(anchorName).dataspaceName(dataspaceName).schemaSetName(schemaSetName).build()
+ def anchor1 = Anchor.builder().name(ANCHOR_NAME_1).dataspaceName(dataspaceName).schemaSetName(schemaSetName).build()
+ def anchor2 = Anchor.builder().name(ANCHOR_NAME_2).dataspaceName(dataspaceName).schemaSetName(schemaSetName).build()
def observedTimestamp = OffsetDateTime.now()
def 'Saving #scenario data.'() {
@@ -228,6 +241,22 @@ class CpsDataServiceImplSpec extends Specification {
fetchDescendantsOption << [FetchDescendantsOption.OMIT_DESCENDANTS, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS]
}
+ def 'Get delta between 2 anchors'() {
+ given: 'some xpath, source and target data nodes'
+ def xpath = '/xpath'
+ def sourceDataNodes = [new DataNodeBuilder().withXpath(xpath).build()]
+ def targetDataNodes = [new DataNodeBuilder().withXpath(xpath).build()]
+ when: 'attempt to get delta between 2 anchors'
+ objectUnderTest.getDeltaByDataspaceAndAnchors(dataspaceName, ANCHOR_NAME_1, ANCHOR_NAME_2, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
+ then: 'the dataspace and anchor names are validated'
+ 2 * mockCpsValidator.validateNameCharacters(_)
+ and: 'data nodes are fetched using appropriate persistence layer method'
+ mockCpsDataPersistenceService.getDataNodesForMultipleXpaths(dataspaceName, ANCHOR_NAME_1, [xpath], FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> sourceDataNodes
+ mockCpsDataPersistenceService.getDataNodesForMultipleXpaths(dataspaceName, ANCHOR_NAME_2, [xpath], FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> targetDataNodes
+ and: 'appropriate delta service method is invoked once with correct source and target data nodes'
+ 1 * mockCpsDeltaService.getDeltaReports(sourceDataNodes, targetDataNodes)
+ }
+
def 'Update data node leaves: #scenario.'() {
given: 'schema set for given anchor and dataspace references test-tree model'
setupSchemaSetMocks('test-tree.yang')
diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDeltaServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDeltaServiceImplSpec.groovy
new file mode 100644
index 000000000..a4f433973
--- /dev/null
+++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDeltaServiceImplSpec.groovy
@@ -0,0 +1,66 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2023 TechMahindra Ltd.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.api.impl
+
+import org.onap.cps.spi.model.DataNode
+import org.onap.cps.spi.model.DataNodeBuilder
+import spock.lang.Shared
+import spock.lang.Specification
+
+class CpsDeltaServiceImplSpec extends Specification{
+
+ def objectUnderTest = new CpsDeltaServiceImpl()
+
+ @Shared
+ def dataNodeWithLeafAndChildDataNode = [new DataNodeBuilder().withXpath('/parent').withLeaves(['parent-leaf': 'parent-payload'])
+ .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").withLeaves('child-leaf': 'child-payload').build()]).build()]
+ @Shared
+ def dataNodeWithChildDataNode = [new DataNodeBuilder().withXpath('/parent').withLeaves(['parent-leaf': 'parent-payload'])
+ .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()]
+ @Shared
+ def emptyDataNode = [new DataNodeBuilder().withXpath('/parent').build()]
+
+ def 'Get delta between data nodes for removed data where source data node has #scenario'() {
+ when: 'attempt to get delta between 2 data nodes'
+ def result = objectUnderTest.getDeltaReports(sourceDataNode as Collection<DataNode>, emptyDataNode)
+ then: 'the delta report contains "remove" action with right data'
+ assert result.first().action.equals("remove")
+ assert result.first().xpath == "/parent/child"
+ assert result.first().sourceData == expectedSourceData
+ where: 'following data was used'
+ scenario | sourceDataNode || expectedSourceData
+ 'leaf data' | dataNodeWithLeafAndChildDataNode || ['child-leaf': 'child-payload']
+ 'no leaf data' | dataNodeWithChildDataNode || null
+ }
+
+ def 'Get delta between data nodes with new data where target data node has #scenario'() {
+ when: 'attempt to get delta between 2 data nodes'
+ def result = objectUnderTest.getDeltaReports(emptyDataNode, targetDataNode)
+ then: 'the delta report contains "add" action with right data'
+ assert result.first().action.equals("add")
+ assert result.first().xpath == "/parent/child"
+ assert result.first().targetData == expectedTargetData
+ where: 'following data was used'
+ scenario | targetDataNode || expectedTargetData
+ 'leaf data' | dataNodeWithLeafAndChildDataNode || ['child-leaf': 'child-payload']
+ 'no leaf data' | dataNodeWithChildDataNode || null
+ }
+}
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 75f29746d..1b873ec12 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
@@ -3,7 +3,7 @@
* Copyright (C) 2021-2023 Nordix Foundation.
* Modifications Copyright (C) 2021-2022 Bell Canada.
* Modifications Copyright (C) 2021 Pantheon.tech
- * Modifications Copyright (C) 2022 TechMahindra Ltd.
+ * Modifications Copyright (C) 2022-2023 TechMahindra Ltd.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ package org.onap.cps.api.impl
import org.onap.cps.TestUtils
import org.onap.cps.api.CpsAdminService
+import org.onap.cps.api.CpsDeltaService
import org.onap.cps.notification.NotificationService
import org.onap.cps.spi.CpsDataPersistenceService
import org.onap.cps.spi.CpsModulePersistenceService
@@ -45,12 +46,13 @@ class E2ENetworkSliceSpec extends Specification {
def mockCpsValidator = Mock(CpsValidator)
def timedYangTextSchemaSourceSetBuilder = new TimedYangTextSchemaSourceSetBuilder()
def timedYangParser = new TimedYangParser()
+ def mockCpsDeltaService = Mock(CpsDeltaService)
def cpsModuleServiceImpl = new CpsModuleServiceImpl(mockModuleStoreService,
mockYangTextSchemaSourceSetCache, mockCpsAdminService, mockCpsValidator,timedYangTextSchemaSourceSetBuilder)
def cpsDataServiceImpl = new CpsDataServiceImpl(mockDataStoreService, mockCpsAdminService,
- mockYangTextSchemaSourceSetCache, mockNotificationService, mockCpsValidator, timedYangParser)
+ mockYangTextSchemaSourceSetCache, mockNotificationService, mockCpsValidator, timedYangParser, mockCpsDeltaService)
def dataspaceName = 'someDataspace'
def anchorName = 'someAnchor'
diff --git a/cps-service/src/test/groovy/org/onap/cps/spi/model/DeltaReportBuilderSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/spi/model/DeltaReportBuilderSpec.groovy
new file mode 100644
index 000000000..e19d12042
--- /dev/null
+++ b/cps-service/src/test/groovy/org/onap/cps/spi/model/DeltaReportBuilderSpec.groovy
@@ -0,0 +1,52 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2023 TechMahindra Ltd.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.model
+
+import spock.lang.Specification
+
+class DeltaReportBuilderSpec extends Specification{
+
+ def 'Generating delta report with for add action'() {
+ when: 'delta report is generated'
+ def result = new DeltaReportBuilder()
+ .actionAdd()
+ .withXpath('/xpath')
+ .withTargetData(['data':'leaf-data'])
+ .build()
+ then: 'the delta report contains the "add" action with expected target data'
+ assert result.action == 'add'
+ assert result.xpath == '/xpath'
+ assert result.targetData == ['data': 'leaf-data']
+ }
+
+ def 'Generating delta report with attributes for remove action'() {
+ when: 'delta report is generated'
+ def result = new DeltaReportBuilder()
+ .actionRemove()
+ .withXpath('/xpath')
+ .withSourceData(['data':'leaf-data'])
+ .build()
+ then: 'the delta report contains the "remove" action with expected source data'
+ assert result.action == 'remove'
+ assert result.xpath == '/xpath'
+ assert result.sourceData == ['data': 'leaf-data']
+ }
+}