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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.integration
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
41 import java.time.OffsetDateTime
43 @SpringBootTest(classes = [TestConfig, CpsAdminServiceImpl, CpsValidatorImpl])
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 {
52 DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance()
56 CpsAdminServiceImpl cpsAdminService
60 CpsDataServiceImpl cpsDataService
64 CpsModuleServiceImpl cpsModuleService
67 def static TEST_DATASPACE = 'testDataspace'
68 def static BOOKSTORE_SCHEMA_SET = 'bookstoreSchemaSet'
69 def static TEST_ANCHOR = 'testAnchor'
71 def createDataspaceSchemaSetAnchor(String dataspaceName, String schemaSetName, String schemaSetFileName, String anchorName) {
72 cpsAdminService.createDataspace(dataspaceName)
73 createSchemaSetAnchor(dataspaceName, schemaSetName, schemaSetFileName, anchorName)
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)
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);
88 cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath,
89 dataNodesAsJSON, OffsetDateTime.now(), ContentType.JSON);
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())
99 def schemaSets = cpsModuleService.getSchemaSets(TEST_DATASPACE)
100 for(schemaSet in schemaSets) {
101 cpsModuleService.deleteSchemaSet(TEST_DATASPACE, schemaSet.getName(), CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED)
105 def static readResourceFile(String filename) {
106 return new File('src/test/resources/data/' + filename).text
109 def static isRootXpath(final String xpath) {
110 return "/".equals(xpath);