2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2025 Nordix Foundation
4 * Modifications Copyright (C) 2024 TechMahindra Ltd.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the 'License');
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an 'AS IS' BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.integration.functional.cps
24 import java.time.OffsetDateTime
25 import org.onap.cps.api.CpsAnchorService
26 import org.onap.cps.integration.base.FunctionalSpecBase
27 import org.onap.cps.api.parameters.FetchDescendantsOption
28 import org.onap.cps.api.exceptions.AlreadyDefinedException
29 import org.onap.cps.api.exceptions.AnchorNotFoundException
30 import org.onap.cps.utils.ContentType
32 class AnchorServiceIntegrationSpec extends FunctionalSpecBase {
34 CpsAnchorService objectUnderTest
36 def setup() { objectUnderTest = cpsAnchorService }
38 def 'Anchor CRUD operations.'() {
39 when: 'an anchor is created'
40 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
41 then: 'the anchor be read'
42 assert objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor').name == 'newAnchor'
43 and: 'it can be deleted'
44 objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE,'newAnchor')
45 then: 'the anchor no longer exists i.e. an exception is thrown if an attempt is made to retrieve it'
48 objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
49 } catch(Exception exception) {
52 assert thrown instanceof AnchorNotFoundException
55 def 'Filtering multiple anchors.'() {
56 when: '2 anchors with bookstore schema set are created'
57 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor1')
58 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor2')
59 and: '1 anchor with "other" schema set is created'
60 createStandardBookStoreSchemaSet(GENERAL_TEST_DATASPACE, 'otherSchemaSet')
61 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, 'otherSchemaSet', 'anchor3')
62 then: 'there are 4 anchors in the general test database'
63 assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE).size() == 4
64 and: 'there are 3 anchors associated with bookstore schema set'
65 assert objectUnderTest.getAnchorsBySchemaSetName(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET).size() == 3
66 and: 'there is 1 anchor associated with other schema set'
67 assert objectUnderTest.getAnchorsBySchemaSetName(GENERAL_TEST_DATASPACE, 'otherSchemaSet').size() == 1
70 def 'Querying anchor(name)s (depends on previous test!).'() {
71 expect: 'there are now 4 anchors using the "stores" module (both schema sets use the same modules) '
72 assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores', 'bookstore-types']).size() == 4
73 and: 'there are no anchors using both "stores" and a "unused-model"'
74 assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores', 'unused-model']).size() == 0
77 def 'Duplicate anchors.'() {
78 given: 'an anchor is created'
79 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
80 when: 'attempt to create another anchor with the same name'
81 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
82 then: 'an exception is thrown that the anchor already is defined'
83 thrown(AlreadyDefinedException)
85 objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
88 def 'Query anchors without any known modules'() {
89 when: 'querying for anchors with #scenario'
90 def result = objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['unknownModule'])
91 then: 'an empty result is returned (no error)'
95 def 'Update anchor schema set.'() {
96 when: 'a new schema set with tree yang model is created'
97 def newTreeYangModelAsString = readResourceDataFile('tree/new-test-tree.yang')
98 cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'newTreeSchemaSet', [tree: newTreeYangModelAsString])
99 then: 'an anchor with new schema set is created'
100 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, 'newTreeSchemaSet', 'anchor4')
101 and: 'the new tree datanode is saved'
102 def treeJsonData = readResourceDataFile('tree/new-test-tree.json')
103 cpsDataService.saveData(GENERAL_TEST_DATASPACE, 'anchor4', treeJsonData, OffsetDateTime.now())
104 and: 'saved tree data node can be retrieved by its normalized xpath'
105 def branchName = cpsDataService.getDataNodes(GENERAL_TEST_DATASPACE, 'anchor4', "/test-tree/branch", FetchDescendantsOption.DIRECT_CHILDREN_ONLY)[0].leaves['name']
106 assert branchName == 'left'
107 and: 'a another schema set with updated tree yang model is created'
108 def updatedTreeYangModelAsString = readResourceDataFile('tree/updated-test-tree.yang')
109 cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'anotherTreeSchemaSet', [tree: updatedTreeYangModelAsString])
110 and: 'anchor4 schema set is updated with another schema set successfully'
111 objectUnderTest.updateAnchorSchemaSet(GENERAL_TEST_DATASPACE, 'anchor4', 'anotherTreeSchemaSet')
112 when: 'updated tree data node with new leaves'
113 def updatedTreeJsonData = readResourceDataFile('tree/updated-test-tree.json')
114 cpsDataService.updateNodeLeaves(GENERAL_TEST_DATASPACE, "anchor4", "/test-tree/branch[@name='left']", updatedTreeJsonData, OffsetDateTime.now(), ContentType.JSON)
115 then: 'updated tree data node can be retrieved by its normalized xpath'
116 def birdsName = cpsDataService.getDataNodes(GENERAL_TEST_DATASPACE, 'anchor4',"/test-tree/branch[@name='left']/nest", FetchDescendantsOption.DIRECT_CHILDREN_ONLY)[0].leaves['birds'] as List
117 assert birdsName.size() == 3
118 assert birdsName.containsAll('Night Owl', 'Raven', 'Crow')