Replace sleep with PollingConditions
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / functional / CpsAnchorServiceIntegrationSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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 org.onap.cps.api.CpsAnchorService
24 import org.onap.cps.integration.base.CpsIntegrationSpecBase
25 import org.onap.cps.spi.FetchDescendantsOption
26 import org.onap.cps.spi.exceptions.AlreadyDefinedException
27 import org.onap.cps.spi.exceptions.AnchorNotFoundException
28
29 import java.time.OffsetDateTime
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             def bookstoreModelFileContent = readResourceDataFile('bookstore/bookstore.yang')
60             cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'otherSchemaSet', [someFileName: bookstoreModelFileContent])
61             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, 'otherSchemaSet', 'anchor3')
62         then: 'there are 3 anchors in the general test database'
63             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE).size() == 3
64         and: 'there are 2 anchors associated with bookstore schema set'
65             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET).size() == 2
66         and: 'there is 1 anchor associated with other schema set'
67             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE, 'otherSchemaSet').size() == 1
68     }
69
70     def 'Querying anchor(name)s (depends on previous test!).'() {
71         expect: 'there are now 3 anchors using the "stores" module (both schema sets use the same modules) '
72             assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores']).size() == 3
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 and #scenario'() {
89         when: 'querying for anchors with #scenario'
90             def result = objectUnderTest.queryAnchorNames(dataspaceName, ['unknownModule'])
91         then: 'an empty result is returned (no error)'
92             assert result == []
93         where:
94             scenario                 | dataspaceName
95             'non existing database'  | 'nonExistingDataspace'
96             'just unknown module(s)' | GENERAL_TEST_DATASPACE
97     }
98
99     def 'Update anchor schema set.'() {
100         when: 'a new schema set with tree yang model is created'
101             def newTreeYangModelAsString = readResourceDataFile('tree/new-test-tree.yang')
102             cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'newTreeSchemaSet', [tree: newTreeYangModelAsString])
103         then: 'an anchor with new schema set is created'
104             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, 'newTreeSchemaSet', 'anchor4')
105         and: 'the new tree datanode is saved'
106             def treeJsonData = readResourceDataFile('tree/new-test-tree.json')
107             cpsDataService.saveData(GENERAL_TEST_DATASPACE, 'anchor4', treeJsonData, OffsetDateTime.now())
108         and: 'saved tree data node can be retrieved by its normalized xpath'
109             def branchName = cpsDataService.getDataNodes(GENERAL_TEST_DATASPACE, 'anchor4', "/test-tree/branch", FetchDescendantsOption.DIRECT_CHILDREN_ONLY)[0].leaves['name']
110             assert branchName == 'left'
111         and: 'a another schema set with updated tree yang model is created'
112             def updatedTreeYangModelAsString = readResourceDataFile('tree/updated-test-tree.yang')
113             cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'anotherTreeSchemaSet', [tree: updatedTreeYangModelAsString])
114         and: 'anchor4 schema set is updated with another schema set successfully'
115             objectUnderTest.updateAnchorSchemaSet(GENERAL_TEST_DATASPACE, 'anchor4', 'anotherTreeSchemaSet')
116         when: 'updated tree data node with new leaves'
117             def updatedTreeJsonData = readResourceDataFile('tree/updated-test-tree.json')
118             cpsDataService.updateNodeLeaves(GENERAL_TEST_DATASPACE, "anchor4", "/test-tree/branch[@name='left']", updatedTreeJsonData, OffsetDateTime.now())
119         then: 'updated tree data node can be retrieved by its normalized xpath'
120             def birdsName = cpsDataService.getDataNodes(GENERAL_TEST_DATASPACE, 'anchor4',"/test-tree/branch[@name='left']/nest", FetchDescendantsOption.DIRECT_CHILDREN_ONLY)[0].leaves['birds'] as List
121             assert birdsName.size() == 3
122             assert birdsName.containsAll('Night Owl', 'Raven', 'Crow')
123     }
124 }