2bd5a4a1be7feaccc1e911e4888d82f9f9e517c3
[cps.git] /
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.integration.functional.cps
23
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
31
32 class AnchorServiceIntegrationSpec extends FunctionalSpecBase {
33
34     CpsAnchorService objectUnderTest
35
36     def setup() { objectUnderTest = cpsAnchorService }
37
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'
46             def thrown = null
47             try {
48                 objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
49             } catch(Exception exception) {
50                 thrown = exception
51             }
52             assert thrown instanceof AnchorNotFoundException
53     }
54
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
68     }
69
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
75     }
76
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)
84         cleanup:
85             objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
86     }
87
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)'
92             assert result == []
93     }
94
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')
119     }
120 }