From 7915e994ac4eb403461b3a93165a2e3c72871ab6 Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Fri, 21 Jun 2024 11:12:45 +0100 Subject: Fix incorrect use of getAnchors(dataspace, schemasets) There is an issue in a method deleteDataNodes(dataspace, anchors) where it doesn't send data update events. This is because it fetches anchors using a method getAnchors(dataspace, schemaSetNames), when it needs to fetch anchors by name: getAnchors(dataspace, anchorNames) Changes: - Rename getAnchors method using schemasets to getAnchorsBySchemaSetNames - Add a method getAnchors(dataspace, anchorNames) - Update test of deleteDataNodes method using getAnchors so it checks that data update events are sent Issue-ID: CPS-2254 Signed-off-by: danielhanrahan Change-Id: I55fe853f0a9278a66a5724bf4cf2723b0e2fbc8b --- .../cps/api/impl/CpsAnchorServiceImplSpec.groovy | 35 ++++++++++++++++------ .../cps/api/impl/CpsDataServiceImplSpec.groovy | 14 ++++++--- .../cps/api/impl/CpsModuleServiceImplSpec.groovy | 8 ++--- 3 files changed, 40 insertions(+), 17 deletions(-) (limited to 'cps-service/src/test/groovy') diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAnchorServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAnchorServiceImplSpec.groovy index c7865386b..e58a5024b 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAnchorServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAnchorServiceImplSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation + * Copyright (C) 2023-2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,9 +59,9 @@ class CpsAnchorServiceImplSpec extends Specification { def 'Retrieve all anchors for schema-set.'() { given: 'that anchor is associated with the dataspace and schemaset' def anchors = [new Anchor()] - mockCpsAdminPersistenceService.getAnchors('someDataspace', 'someSchemaSet') >> anchors + mockCpsAdminPersistenceService.getAnchorsBySchemaSetName('someDataspace', 'someSchemaSet') >> anchors when: 'get anchors is called for a dataspace name and schema set name' - def result = objectUnderTest.getAnchors('someDataspace', 'someSchemaSet') + def result = objectUnderTest.getAnchorsBySchemaSetName('someDataspace', 'someSchemaSet') then: 'the collection provided by persistence service is returned as result' result == anchors and: 'the CpsValidator is called on the dataspaceName, schemaSetName' @@ -71,9 +71,9 @@ class CpsAnchorServiceImplSpec extends Specification { def 'Retrieve all anchors for multiple schema-sets.'() { given: 'that anchor is associated with the dataspace and schemasets' def anchors = [new Anchor(), new Anchor()] - mockCpsAdminPersistenceService.getAnchors('someDataspace', _ as Collection) >> anchors + mockCpsAdminPersistenceService.getAnchorsBySchemaSetNames('someDataspace', _ as Collection) >> anchors when: 'get anchors is called for a dataspace name and schema set names' - def result = objectUnderTest.getAnchors('someDataspace', ['schemaSet1', 'schemaSet2']) + def result = objectUnderTest.getAnchorsBySchemaSetNames('someDataspace', ['schemaSet1', 'schemaSet2']) then: 'the collection provided by persistence service is returned as result' result == anchors and: 'the CpsValidator is called on the dataspace name and schema-set names' @@ -93,6 +93,21 @@ class CpsAnchorServiceImplSpec extends Specification { 1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someAnchor') } + def 'Retrieve multiple anchors for dataspace and provided anchor names.'() { + given: 'multiple anchors names to get' + def anchorNames = ['anchor1', 'anchor2'] + and: 'that anchors are associated with the dataspace and anchor names' + def anchors = [new Anchor(), new Anchor()] + mockCpsAdminPersistenceService.getAnchors('someDataspace', anchorNames) >> anchors + when: 'get anchors is called for a dataspace name and anchor names' + def result = objectUnderTest.getAnchors('someDataspace', anchorNames) + then: 'the collection provided by persistence service is returned as result' + result == anchors + and: 'the CpsValidator is called on the dataspace name and anchor names' + 1 * mockCpsValidator.validateNameCharacters('someDataspace') + 1 * mockCpsValidator.validateNameCharacters(anchorNames) + } + def 'Delete anchor.'() { when: 'delete anchor is invoked' objectUnderTest.deleteAnchor('someDataspace','someAnchor') @@ -105,15 +120,17 @@ class CpsAnchorServiceImplSpec extends Specification { } def 'Delete multiple anchors.'() { + given: 'multiple anchors to delete' + def anchorNames = ['anchor1', 'anchor2'] when: 'delete anchors is invoked' - objectUnderTest.deleteAnchors('someDataspace', ['anchor1', 'anchor2']) + objectUnderTest.deleteAnchors('someDataspace', anchorNames) then: 'delete data nodes is invoked on the data service with expected parameters' - 1 * mockCpsDataPersistenceService.deleteDataNodes('someDataspace', _ as Collection) + 1 * mockCpsDataPersistenceService.deleteDataNodes('someDataspace', anchorNames) and: 'the persistence service method is invoked with same parameters to delete anchor' - 1 * mockCpsAdminPersistenceService.deleteAnchors('someDataspace',_ as Collection) + 1 * mockCpsAdminPersistenceService.deleteAnchors('someDataspace', anchorNames) and: 'the CpsValidator is called on the dataspace name and anchor names' 1 * mockCpsValidator.validateNameCharacters('someDataspace') - 1 * mockCpsValidator.validateNameCharacters(_) + 1 * mockCpsValidator.validateNameCharacters(anchorNames) } def 'Query all anchor identifiers for a dataspace and module names.'() { 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 edf25715b..4e5807ec8 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 @@ -55,6 +55,8 @@ import spock.lang.Shared import spock.lang.Specification import java.time.OffsetDateTime +import static org.onap.cps.events.model.Data.Operation.DELETE + class CpsDataServiceImplSpec extends Specification { def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService) def mockCpsAnchorService = Mock(CpsAnchorService) @@ -489,15 +491,19 @@ class CpsDataServiceImplSpec extends Specification { def 'Delete all data nodes for given dataspace and multiple anchors.'() { given: 'schema set for given anchors and dataspace references test tree model' setupSchemaSetMocks('test-tree.yang') - mockCpsAnchorService.getAnchors(dataspaceName, ['anchor1', 'anchor2']) >> - [new Anchor(name: 'anchor1', dataspaceName: dataspaceName), - new Anchor(name: 'anchor2', dataspaceName: dataspaceName)] + def anchor1 = new Anchor(name: 'anchor1', dataspaceName: dataspaceName) + def anchor2 = new Anchor(name: 'anchor2', dataspaceName: dataspaceName) + mockCpsAnchorService.getAnchors(dataspaceName, ['anchor1', 'anchor2']) >> [anchor1, anchor2] when: 'delete data node method is invoked with correct parameters' objectUnderTest.deleteDataNodes(dataspaceName, ['anchor1', 'anchor2'], observedTimestamp) then: 'the CpsValidator is called on the dataspace name and the anchor names' - 2 * mockCpsValidator.validateNameCharacters(_) + 1 * mockCpsValidator.validateNameCharacters(dataspaceName) + 1 * mockCpsValidator.validateNameCharacters(['anchor1', 'anchor2']) and: 'the persistence service method is invoked with the correct parameters' 1 * mockCpsDataPersistenceService.deleteDataNodes(dataspaceName, _ as Collection) + and: 'a data update event is sent for each anchor' + 1 * mockDataUpdateEventsService.publishCpsDataUpdateEvent(anchor1, '/', DELETE, observedTimestamp) + 1 * mockDataUpdateEventsService.publishCpsDataUpdateEvent(anchor2, '/', DELETE, observedTimestamp) } def 'Start session.'() { diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy index 0bad0de6a..ad8c54bf2 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy @@ -134,7 +134,7 @@ class CpsModuleServiceImplSpec extends Specification { def 'Delete schema-set when cascade is allowed.'() { given: '#numberOfAnchors anchors are associated with schemaset' def associatedAnchors = createAnchors(numberOfAnchors) - mockCpsAnchorService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors + mockCpsAnchorService.getAnchorsBySchemaSetName('my-dataspace', 'my-schemaset') >> associatedAnchors when: 'schema set deletion is requested with cascade allowed' objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED) then: 'anchor deletion is called #numberOfAnchors times' @@ -153,7 +153,7 @@ class CpsModuleServiceImplSpec extends Specification { def 'Delete schema-set when cascade is prohibited.'() { given: 'no anchors are associated with schemaset' - mockCpsAnchorService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList() + mockCpsAnchorService.getAnchorsBySchemaSetName('my-dataspace', 'my-schemaset') >> Collections.emptyList() when: 'schema set deletion is requested with cascade allowed' objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED) then: 'no anchors are deleted' @@ -170,7 +170,7 @@ class CpsModuleServiceImplSpec extends Specification { def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() { given: '2 anchors are associated with schemaset' - mockCpsAnchorService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2) + mockCpsAnchorService.getAnchorsBySchemaSetName('my-dataspace', 'my-schemaset') >> createAnchors(2) when: 'schema set deletion is requested with cascade allowed' objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED) then: 'Schema-Set in Use exception is thrown' @@ -179,7 +179,7 @@ class CpsModuleServiceImplSpec extends Specification { def 'Delete multiple schema-sets when cascade is allowed.'() { given: '#numberOfAnchors anchors are associated with each schemaset' - mockCpsAnchorService.getAnchors('my-dataspace', ['my-schemaset1', 'my-schemaset2']) >> createAnchors(numberOfAnchors * 2) + mockCpsAnchorService.getAnchorsBySchemaSetNames('my-dataspace', ['my-schemaset1', 'my-schemaset2']) >> createAnchors(numberOfAnchors * 2) when: 'schema set deletion is requested with cascade allowed' objectUnderTest.deleteSchemaSetsWithCascade('my-dataspace', ['my-schemaset1', 'my-schemaset2']) then: 'anchor deletion is called #numberOfAnchors times' -- cgit 1.2.3-korg