Fix compile issue after facade introduction
[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  *  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
23
24 import java.time.OffsetDateTime
25
26 import org.onap.cps.api.CpsAnchorService
27 import org.onap.cps.integration.base.CpsIntegrationSpecBase
28 import org.onap.cps.spi.FetchDescendantsOption
29 import org.onap.cps.spi.exceptions.AlreadyDefinedException
30 import org.onap.cps.spi.exceptions.AnchorNotFoundException
31 import org.onap.cps.utils.ContentType
32
33 class CpsAnchorServiceIntegrationSpec extends CpsIntegrationSpecBase {
34
35     CpsAnchorService objectUnderTest
36
37     def setup() { objectUnderTest = cpsAnchorService }
38
39     def 'Anchor CRUD operations.'() {
40         when: 'an anchor is created'
41             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
42         then: 'the anchor be read'
43             assert objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor').name == 'newAnchor'
44         and: 'it can be deleted'
45             objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE,'newAnchor')
46         then: 'the anchor no longer exists i.e. an exception is thrown if an attempt is made to retrieve it'
47             def thrown = null
48             try {
49                 objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
50             } catch(Exception exception) {
51                 thrown = exception
52             }
53             assert thrown instanceof AnchorNotFoundException
54     }
55
56     def 'Filtering multiple anchors.'() {
57         when: '2 anchors with bookstore schema set are created'
58             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor1')
59             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor2')
60         and: '1 anchor with "other" schema set is created'
61             createStandardBookStoreSchemaSet(GENERAL_TEST_DATASPACE, 'otherSchemaSet')
62             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, 'otherSchemaSet', 'anchor3')
63         then: 'there are 3 anchors in the general test database'
64             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE).size() == 3
65         and: 'there are 2 anchors associated with bookstore schema set'
66             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET).size() == 2
67         and: 'there is 1 anchor associated with other schema set'
68             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE, 'otherSchemaSet').size() == 1
69     }
70
71     def 'Querying anchor(name)s (depends on previous test!).'() {
72         expect: 'there are now 3 anchors using the "stores" module (both schema sets use the same modules) '
73             assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores', 'bookstore-types']).size() == 3
74         and: 'there are no anchors using both "stores" and a "unused-model"'
75             assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores', 'unused-model']).size() == 0
76     }
77
78     def 'Duplicate anchors.'() {
79         given: 'an anchor is created'
80             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
81         when: 'attempt to create another anchor with the same name'
82             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
83         then: 'an exception is thrown that the anchor already is defined'
84             thrown(AlreadyDefinedException)
85         cleanup:
86             objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
87     }
88
89     def 'Query anchors without any known modules'() {
90         when: 'querying for anchors with #scenario'
91             def result = objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['unknownModule'])
92         then: 'an empty result is returned (no error)'
93             assert result == []
94     }
95
96     def 'Update anchor schema set.'() {
97         when: 'a new schema set with tree yang model is created'
98             def newTreeYangModelAsString = readResourceDataFile('tree/new-test-tree.yang')
99             cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'newTreeSchemaSet', [tree: newTreeYangModelAsString])
100         then: 'an anchor with new schema set is created'
101             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, 'newTreeSchemaSet', 'anchor4')
102         and: 'the new tree datanode is saved'
103             def treeJsonData = readResourceDataFile('tree/new-test-tree.json')
104             cpsDataService.saveData(GENERAL_TEST_DATASPACE, 'anchor4', treeJsonData, OffsetDateTime.now())
105         and: 'saved tree data node can be retrieved by its normalized xpath'
106             def branchName = cpsDataService.getDataNodes(GENERAL_TEST_DATASPACE, 'anchor4', "/test-tree/branch", FetchDescendantsOption.DIRECT_CHILDREN_ONLY)[0].leaves['name']
107             assert branchName == 'left'
108         and: 'a another schema set with updated tree yang model is created'
109             def updatedTreeYangModelAsString = readResourceDataFile('tree/updated-test-tree.yang')
110             cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'anotherTreeSchemaSet', [tree: updatedTreeYangModelAsString])
111         and: 'anchor4 schema set is updated with another schema set successfully'
112             objectUnderTest.updateAnchorSchemaSet(GENERAL_TEST_DATASPACE, 'anchor4', 'anotherTreeSchemaSet')
113         when: 'updated tree data node with new leaves'
114             def updatedTreeJsonData = readResourceDataFile('tree/updated-test-tree.json')
115             cpsDataService.updateNodeLeaves(GENERAL_TEST_DATASPACE, "anchor4", "/test-tree/branch[@name='left']", updatedTreeJsonData, OffsetDateTime.now(), ContentType.JSON)
116         then: 'updated tree data node can be retrieved by its normalized xpath'
117             def birdsName = cpsDataService.getDataNodes(GENERAL_TEST_DATASPACE, 'anchor4',"/test-tree/branch[@name='left']/nest", FetchDescendantsOption.DIRECT_CHILDREN_ONLY)[0].leaves['birds'] as List
118             assert birdsName.size() == 3
119             assert birdsName.containsAll('Night Owl', 'Raven', 'Crow')
120     }
121 }