Replace deprecated WebSecurityConfigurerAdapter
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / base / 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.base
22
23 import org.onap.cps.api.CpsQueryService
24 import org.onap.cps.api.impl.CpsAdminServiceImpl
25 import org.onap.cps.api.impl.CpsDataServiceImpl
26 import org.onap.cps.api.impl.CpsModuleServiceImpl
27 import org.onap.cps.integration.DatabaseTestContainer
28 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
29 import org.onap.cps.spi.model.DataNode
30 import org.onap.cps.spi.repository.DataspaceRepository
31 import org.onap.cps.spi.impl.utils.CpsValidatorImpl
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
34 import org.springframework.boot.autoconfigure.domain.EntityScan
35 import org.springframework.boot.test.context.SpringBootTest
36 import org.springframework.context.annotation.ComponentScan
37 import org.springframework.context.annotation.Lazy
38 import org.springframework.data.jpa.repository.config.EnableJpaRepositories
39 import org.testcontainers.spock.Testcontainers
40 import spock.lang.Shared
41 import spock.lang.Specification
42
43 import java.time.OffsetDateTime
44
45 @SpringBootTest(classes = [TestConfig, CpsAdminServiceImpl, CpsValidatorImpl])
46 @Testcontainers
47 @EnableAutoConfiguration
48 @EnableJpaRepositories(basePackageClasses = [DataspaceRepository])
49 @ComponentScan(basePackages = ["org.onap.cps.api", "org.onap.cps.spi.repository"])
50 @EntityScan("org.onap.cps.spi.entities")
51 class CpsIntegrationSpecBase extends Specification {
52
53     @Shared
54     DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance()
55
56     @Autowired
57     @Lazy
58     CpsAdminServiceImpl cpsAdminService
59
60     @Autowired
61     @Lazy
62     CpsDataServiceImpl cpsDataService
63
64     @Autowired
65     @Lazy
66     CpsModuleServiceImpl cpsModuleService
67
68     @Autowired
69     @Lazy
70     CpsQueryService cpsQueryService
71
72     def static GENERAL_TEST_DATASPACE = 'generalTestDataspace'
73     def static BOOKSTORE_SCHEMA_SET = 'bookstoreSchemaSet'
74
75     def static initialized = false
76
77     def setup() {
78         if (!initialized) {
79             cpsAdminService.createDataspace(GENERAL_TEST_DATASPACE)
80             def bookstoreModelFileContent = readResourceDataFile('bookstore/bookstore.yang')
81             cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, [bookstore : bookstoreModelFileContent])
82             initialized = true;
83         }
84     }
85
86     def static countDataNodesInTree(DataNode dataNode) {
87         return 1 + countDataNodesInTree(dataNode.getChildDataNodes())
88     }
89
90     def static countDataNodesInTree(Collection<DataNode> dataNodes) {
91         int nodeCount = 0
92         for (DataNode parent : dataNodes) {
93             nodeCount += countDataNodesInTree(parent)
94         }
95         return nodeCount
96     }
97
98     def static readResourceDataFile(filename) {
99         return new File('src/test/resources/data/' + filename).text
100     }
101
102     def dataspaceExists(dataspaceName) {
103         try {
104             cpsAdminService.getDataspace(dataspaceName)
105         } catch (DataspaceNotFoundException e) {
106             return false
107         }
108         return true
109     }
110
111     def addAnchorsWithData(numberOfAnchors, dataspaceName, schemaSetName, anchorNamePrefix, data) {
112         (1..numberOfAnchors).each {
113             cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorNamePrefix + it)
114             cpsDataService.saveData(dataspaceName, anchorNamePrefix + it, data, OffsetDateTime.now())
115         }
116     }
117 }