b25f07c4ce7c3189f8c7ea5bdad14c597195047a
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2025 OpenInfra Foundation Europe. All rights reserved.
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.cps
22
23 import static org.onap.cps.api.parameters.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
24 import static org.onap.cps.api.parameters.PaginationOption.NO_PAGINATION
25
26 import org.onap.cps.api.CpsDataspaceService
27 import java.time.OffsetDateTime
28 import org.onap.cps.api.exceptions.DataNodeNotFoundException
29 import org.onap.cps.integration.base.FunctionalSpecBase
30 import org.onap.cps.api.exceptions.AlreadyDefinedException
31 import org.onap.cps.api.exceptions.DataspaceInUseException
32 import org.onap.cps.api.exceptions.DataspaceNotFoundException
33 import org.onap.cps.ri.repository.FragmentRepository
34 import org.onap.cps.utils.ContentType
35
36 class DataspaceServiceIntegrationSpec extends FunctionalSpecBase {
37
38     CpsDataspaceService objectUnderTest
39
40     def setup() { objectUnderTest = cpsDataspaceService }
41
42     def 'Dataspace CRUD operations.'() {
43         when: 'a dataspace is created'
44             objectUnderTest.createDataspace('newDataspace')
45         then: 'the dataspace can be read'
46             assert objectUnderTest.getDataspace('newDataspace').name == 'newDataspace'
47         and: 'it can be deleted'
48             objectUnderTest.deleteDataspace('newDataspace')
49         then: 'the dataspace no longer exists i.e. an exception is thrown if an attempt is made to retrieve it'
50             def thrown = null
51             try {
52                 objectUnderTest.getDataspace('newDataspace')
53             } catch(Exception exception) {
54                 thrown = exception
55             }
56            assert thrown instanceof DataspaceNotFoundException
57     }
58
59     def 'Attempt to delete a non-existing dataspace'() {
60         when: 'attempt to delete a non-existing dataspace'
61             objectUnderTest.deleteDataspace('non-existing-name')
62         then: 'a not found exception is thrown with the relevant dataspace name'
63             def thrownException = thrown(DataspaceNotFoundException)
64             assert thrownException.details.contains('non-existing-name does not exist')
65     }
66
67     def 'Attempt Delete dataspace with a schema set and anchor'() {
68         setup: 'a dataspace with a schema set and anchor'
69             objectUnderTest.createDataspace('targetDataspace')
70             cpsModuleService.createSchemaSet('targetDataspace','someSchemaSet',[:])
71             cpsAnchorService.createAnchor('targetDataspace', 'someSchemaSet', 'some_anchor')
72         when: 'attempt to delete dataspace'
73             objectUnderTest.deleteDataspace('targetDataspace')
74         then: 'an in-use exception is thrown mentioning anchors'
75             def thrownException = thrown(DataspaceInUseException)
76             assert thrownException.details.contains('contains 1 anchor(s)')
77         cleanup:
78             cpsModuleService.deleteSchemaSetsWithCascade('targetDataspace',['someSchemaSet'])
79             objectUnderTest.deleteDataspace('targetDataspace')
80     }
81
82     def 'Attempt to delete dataspace with just a schema set'() {
83         setup: 'a dataspace with a schema set'
84             objectUnderTest.createDataspace('targetDataspace')
85             cpsModuleService.createSchemaSet('targetDataspace','someSchemaSet',[:])
86         when: 'attempt to delete dataspace'
87             objectUnderTest.deleteDataspace('targetDataspace')
88         then: 'an in-use exception is thrown mentioning schema sets'
89             def thrownException = thrown(DataspaceInUseException)
90             assert thrownException.details.contains('contains 1 schema set(s)')
91         cleanup:
92             cpsModuleService.deleteSchemaSetsWithCascade('targetDataspace',['someSchemaSet'])
93             objectUnderTest.deleteDataspace('targetDataspace')
94     }
95
96     def 'Retrieve all dataspaces (depends on total test suite).'() {
97         given: 'two addtional dataspaces are created'
98             objectUnderTest.createDataspace('dataspace1')
99             objectUnderTest.createDataspace('dataspace2')
100         when: 'all datespaces are retreived'
101             def result = objectUnderTest.getAllDataspaces()
102         then: 'there are at least 3 dataspaces (2 new ones plus the general test dataspace)'
103             result.size() >= 3
104             assert result.name.containsAll([GENERAL_TEST_DATASPACE, 'dataspace1', 'dataspace2'])
105     }
106
107     def 'Duplicate dataspaces.'() {
108         when: 'attempting to create a dataspace with the same name as an existing one'
109             objectUnderTest.createDataspace(GENERAL_TEST_DATASPACE)
110         then: 'an exception is thrown indicating the dataspace already exists'
111             thrown(AlreadyDefinedException)
112     }
113
114     def 'Delete all orphaned data in a dataspace.'() {
115         setup: 'an anchor'
116             cpsAnchorService.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'testAnchor')
117         and: 'orphaned data'
118             def jsonDataMap = [:]
119             jsonDataMap.put('/bookstore/categories[@code=\'3\']', '{"books":[{"title": "Matilda"}]}')
120             jsonDataMap.put('/bookstore/categories[@code=\'3\']', '{"sub-categories":{"code":"1","additional-info":{"info-name":"sample"}}}')
121             cpsDataService.updateDataNodesAndDescendants(GENERAL_TEST_DATASPACE, 'testAnchor', jsonDataMap,  OffsetDateTime.now(), ContentType.JSON)
122             def dataNodes = cpsDataService.getDataNodes(GENERAL_TEST_DATASPACE, 'testAnchor','/', INCLUDE_ALL_DESCENDANTS)
123             assert dataNodes.size() == 1
124             assert dataNodes.childDataNodes.size() == 1
125         and: 'parent node does not exist'
126             assert cpsQueryService.queryDataNodesAcrossAnchors(GENERAL_TEST_DATASPACE, '/bookstore', INCLUDE_ALL_DESCENDANTS, NO_PAGINATION).size() == 0
127         when: 'deleting all orphaned data in a dataspace'
128             objectUnderTest.deleteAllOrphanedData(GENERAL_TEST_DATASPACE)
129         and: 'get data nodes in dataspace'
130             cpsDataService.getDataNodes(GENERAL_TEST_DATASPACE, 'testAnchor','/', INCLUDE_ALL_DESCENDANTS)
131         then: 'there will be no more data nodes available'
132             thrown(DataNodeNotFoundException)
133         cleanup: 'remove the data for this test'
134             cpsAnchorService.deleteAnchor(GENERAL_TEST_DATASPACE, 'testAnchor')
135     }
136
137 }