Refactor Delete Anchor functionality
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsDataServiceImplSpec.groovy
index 6c899c7..56b0e2d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2021 Nordix Foundation
+ *  Copyright (C) 2021-2022 Nordix Foundation
  *  Modifications Copyright (C) 2021 Pantheon.tech
  *  Modifications Copyright (C) 2021-2022 Bell Canada.
  *  ================================================================================
@@ -44,14 +44,8 @@ class CpsDataServiceImplSpec extends Specification {
     def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
     def mockNotificationService = Mock(NotificationService)
 
-    def objectUnderTest = new CpsDataServiceImpl()
-
-    def setup() {
-        objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService
-        objectUnderTest.cpsAdminService = mockCpsAdminService
-        objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
-        objectUnderTest.notificationService = mockNotificationService
-    }
+    def objectUnderTest = new CpsDataServiceImpl(mockCpsDataPersistenceService, mockCpsAdminService,
+            mockYangTextSchemaSourceSetCache, mockNotificationService)
 
     def dataspaceName = 'some dataspace'
     def anchorName = 'some anchor'
@@ -154,19 +148,19 @@ class CpsDataServiceImplSpec extends Specification {
             'one leaf'        | '{"name": "some-name"}'
     }
 
-    def 'Update cm-handle properties' () {
-        given: 'a dmi registry model'
-            setupSchemaSetMocks('dmi-registry.yang')
+    def 'Update Bookstore node leaves' () {
+        given: 'a DMI registry model'
+            setupSchemaSetMocks('bookstore.yang')
         and: 'the expected json string'
-            def jsonData = '{"cm-handles":[{"id":"cmHandle001", "additional-properties":[{"name":"P1"}]}]}'
+            def jsonData = '{"categories":[{"code":01,"name":"Romance"}]}'
         when: 'update data method is invoked with json data and parent node xpath'
             objectUnderTest.updateNodeLeavesAndExistingDescendantLeaves(dataspaceName, anchorName,
-                '/dmi-registry', jsonData, observedTimestamp)
+                '/bookstore', jsonData, observedTimestamp)
         then: 'the persistence service method is invoked with correct parameters'
             1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName,
-                "/dmi-registry/cm-handles[@id='cmHandle001']", ['id': 'cmHandle001'])
+                "/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, '/dmi-registry', Operation.UPDATE)
+            1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/bookstore', Operation.UPDATE)
     }
 
     def 'Replace data node: #scenario.'() {
@@ -237,6 +231,15 @@ class CpsDataServiceImplSpec extends Specification {
             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, 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)
+    }
+
     def setupSchemaSetMocks(String... yangResources) {
         def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build()
         mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor