Revert "Omit descendants when not need in ncmp inventory queries"
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / CpsIntegrationSpecBase.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
22
23 import org.onap.cps.api.impl.CpsAdminServiceImpl
24 import org.onap.cps.api.impl.CpsDataServiceImpl
25 import org.onap.cps.api.impl.CpsModuleServiceImpl
26 import org.onap.cps.spi.CascadeDeleteAllowed
27 import org.onap.cps.spi.repository.DataspaceRepository
28 import org.onap.cps.spi.impl.utils.CpsValidatorImpl
29 import org.onap.cps.utils.ContentType
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
32 import org.springframework.boot.autoconfigure.domain.EntityScan
33 import org.springframework.boot.test.context.SpringBootTest
34 import org.springframework.context.annotation.ComponentScan
35 import org.springframework.context.annotation.Lazy
36 import org.springframework.data.jpa.repository.config.EnableJpaRepositories
37 import org.testcontainers.spock.Testcontainers
38 import spock.lang.Shared
39 import spock.lang.Specification
40
41 import java.time.OffsetDateTime
42
43 @SpringBootTest(classes = [TestConfig, CpsAdminServiceImpl, CpsValidatorImpl])
44 @Testcontainers
45 @EnableAutoConfiguration
46 @EnableJpaRepositories(basePackageClasses = [DataspaceRepository])
47 @ComponentScan(basePackages = ["org.onap.cps.api", "org.onap.cps.spi.repository"])
48 @EntityScan("org.onap.cps.spi.entities")
49 class CpsIntegrationSpecBase extends Specification {
50
51     @Shared
52     DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance()
53
54     @Autowired
55     @Lazy
56     CpsAdminServiceImpl cpsAdminService
57
58     @Autowired
59     @Lazy
60     CpsDataServiceImpl cpsDataService
61
62     @Autowired
63     @Lazy
64     CpsModuleServiceImpl cpsModuleService
65
66
67     def static TEST_DATASPACE = 'testDataspace'
68     def static BOOKSTORE_SCHEMA_SET = 'bookstoreSchemaSet'
69     def static TEST_ANCHOR = 'testAnchor'
70
71     def createDataspaceSchemaSetAnchor(String dataspaceName, String schemaSetName, String schemaSetFileName, String anchorName) {
72         cpsAdminService.createDataspace(dataspaceName)
73         createSchemaSetAnchor(dataspaceName, schemaSetName, schemaSetFileName, anchorName)
74     }
75
76     def createSchemaSetAnchor(String dataspaceName, String schemaSetName, String schemaSetFileName, String anchorName) {
77         def bookstoreFileContent = readResourceFile(schemaSetFileName)
78         cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, [(schemaSetFileName) : bookstoreFileContent])
79         cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName)
80     }
81
82     def saveDataNodes(String dataspaceName, String anchorName, String parentNodeXpath, String dataNodesFileName) {
83         def dataNodesAsJSON = readResourceFile(dataNodesFileName)
84         if (isRootXpath(parentNodeXpath)) {
85             cpsDataService.saveData(dataspaceName, anchorName, dataNodesAsJSON,
86                 OffsetDateTime.now(), ContentType.JSON);
87         } else {
88             cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath,
89                 dataNodesAsJSON, OffsetDateTime.now(), ContentType.JSON);
90         }
91     }
92
93     def deleteAllFromTestDataspace() {
94         def anchors = cpsAdminService.getAnchors(TEST_DATASPACE)
95         for(anchor in anchors) {
96             cpsDataService.deleteDataNodes(TEST_DATASPACE, anchor.getName(), OffsetDateTime.now())
97             cpsAdminService.deleteAnchor(TEST_DATASPACE, anchor.getName())
98         }
99         def schemaSets = cpsModuleService.getSchemaSets(TEST_DATASPACE)
100         for(schemaSet in schemaSets) {
101             cpsModuleService.deleteSchemaSet(TEST_DATASPACE, schemaSet.getName(), CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED)
102         }
103     }
104
105     def static readResourceFile(String filename) {
106         return new File('src/test/resources/data/' + filename).text
107     }
108
109     def static isRootXpath(final String xpath) {
110         return "/".equals(xpath);
111     }
112 }