Merge "Expand CPS Service Integration Test (framework)"
[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.impl.CpsAdminServiceImpl
24 import org.onap.cps.api.impl.CpsDataServiceImpl
25 import org.onap.cps.api.impl.CpsModuleServiceImpl
26 import org.onap.cps.integration.DatabaseTestContainer
27 import org.onap.cps.spi.model.DataNode
28 import org.onap.cps.spi.repository.DataspaceRepository
29 import org.onap.cps.spi.impl.utils.CpsValidatorImpl
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 @SpringBootTest(classes = [TestConfig, CpsAdminServiceImpl, CpsValidatorImpl])
42 @Testcontainers
43 @EnableAutoConfiguration
44 @EnableJpaRepositories(basePackageClasses = [DataspaceRepository])
45 @ComponentScan(basePackages = ["org.onap.cps.api", "org.onap.cps.spi.repository"])
46 @EntityScan("org.onap.cps.spi.entities")
47 class CpsIntegrationSpecBase extends Specification {
48
49     @Shared
50     DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance()
51
52     @Autowired
53     @Lazy
54     CpsAdminServiceImpl cpsAdminService
55
56     @Autowired
57     @Lazy
58     CpsDataServiceImpl cpsDataService
59
60     @Autowired
61     @Lazy
62     CpsModuleServiceImpl cpsModuleService
63
64     def static GENERAL_TEST_DATASPACE = 'generalTestDataSpace'
65     def static BOOKSTORE_DATASPACE = 'bookstoreDataspace'
66     def static BOOKSTORE_SCHEMA_SET = 'bookstoreSchemaSet'
67     def static BOOKSTORE_ANCHOR = 'bookstoreAnchor'
68
69     def static initialized = false
70
71     def setup() {
72         if (!initialized) {
73             cpsAdminService.createDataspace(GENERAL_TEST_DATASPACE)
74             def bookstoreModelFileContent = readResourceFile('bookstore.yang')
75             cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, [bookstore : bookstoreModelFileContent])
76             initialized = true;
77         }
78     }
79
80     def static countDataNodesInTree(DataNode dataNode) {
81         return 1 + countDataNodesInTree(dataNode.getChildDataNodes())
82     }
83
84     def static countDataNodesInTree(Collection<DataNode> dataNodes) {
85         int nodeCount = 0
86         for (DataNode parent : dataNodes) {
87             nodeCount += countDataNodesInTree(parent)
88         }
89         return nodeCount
90     }
91
92     def static readResourceFile(filename) {
93         return new File('src/test/resources/data/' + filename).text
94     }
95 }