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