Merge "Clean up of integration-test base classes"
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / base / CpsIntegrationSpecBase.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 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 java.time.OffsetDateTime
24 import org.onap.cps.api.CpsAnchorService
25 import org.onap.cps.api.CpsDataService
26 import org.onap.cps.api.CpsDataspaceService
27 import org.onap.cps.api.CpsModuleService
28 import org.onap.cps.api.CpsQueryService
29 import org.onap.cps.integration.DatabaseTestContainer
30 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
31 import org.onap.cps.spi.model.DataNode
32 import org.onap.cps.spi.repository.DataspaceRepository
33 import org.onap.cps.spi.utils.SessionManager
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
36 import org.springframework.boot.autoconfigure.domain.EntityScan
37 import org.springframework.boot.test.context.SpringBootTest
38 import org.springframework.context.annotation.ComponentScan
39 import org.springframework.data.jpa.repository.config.EnableJpaRepositories
40 import org.testcontainers.spock.Testcontainers
41 import spock.lang.Shared
42 import spock.lang.Specification
43
44 @SpringBootTest(classes = [CpsDataspaceService])
45 @Testcontainers
46 @EnableAutoConfiguration
47 @EnableJpaRepositories(basePackageClasses = [DataspaceRepository])
48 @ComponentScan(basePackages = ['org.onap.cps'])
49 @EntityScan('org.onap.cps.spi.entities')
50 abstract class CpsIntegrationSpecBase extends Specification {
51
52     @Shared
53     DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance()
54
55     @Autowired
56     CpsDataspaceService cpsDataspaceService
57
58     @Autowired
59     CpsAnchorService cpsAnchorService
60
61     @Autowired
62     CpsDataService cpsDataService
63
64     @Autowired
65     CpsModuleService cpsModuleService
66
67     @Autowired
68     CpsQueryService cpsQueryService
69
70     @Autowired
71     SessionManager sessionManager
72
73     def static GENERAL_TEST_DATASPACE = 'generalTestDataspace'
74     def static BOOKSTORE_SCHEMA_SET = 'bookstoreSchemaSet'
75
76     def static initialized = false
77     def now = OffsetDateTime.now()
78
79     def setup() {
80         if (!initialized) {
81             cpsDataspaceService.createDataspace(GENERAL_TEST_DATASPACE)
82             createStandardBookStoreSchemaSet(GENERAL_TEST_DATASPACE)
83             initialized = true
84         }
85     }
86
87     def static countDataNodesInTree(DataNode dataNode) {
88         return 1 + countDataNodesInTree(dataNode.getChildDataNodes())
89     }
90
91     def static countDataNodesInTree(Collection<DataNode> dataNodes) {
92         int nodeCount = 0
93         for (DataNode parent : dataNodes) {
94             nodeCount += countDataNodesInTree(parent)
95         }
96         return nodeCount
97     }
98
99     def static readResourceDataFile(filename) {
100         return new File('src/test/resources/data/' + filename).text
101     }
102
103     def getBookstoreYangResourcesNameToContentMap() {
104         def bookstoreModelFileContent = readResourceDataFile('bookstore/bookstore.yang')
105         def bookstoreTypesFileContent = readResourceDataFile('bookstore/bookstore-types.yang')
106         return [bookstore: bookstoreModelFileContent, bookstoreTypes: bookstoreTypesFileContent]
107     }
108
109     def createStandardBookStoreSchemaSet(targetDataspace) {
110         cpsModuleService.createSchemaSet(targetDataspace, BOOKSTORE_SCHEMA_SET, getBookstoreYangResourcesNameToContentMap())
111     }
112
113     def createStandardBookStoreSchemaSet(targetDataspace, targetSchemaSet) {
114         cpsModuleService.createSchemaSet(targetDataspace, targetSchemaSet, getBookstoreYangResourcesNameToContentMap())
115     }
116
117     def dataspaceExists(dataspaceName) {
118         try {
119             cpsDataspaceService.getDataspace(dataspaceName)
120         } catch (DataspaceNotFoundException ignored) {
121             return false
122         }
123         return true
124     }
125
126     def addAnchorsWithData(numberOfAnchors, dataspaceName, schemaSetName, anchorNamePrefix, data) {
127         (1..numberOfAnchors).each {
128             cpsAnchorService.createAnchor(dataspaceName, schemaSetName, anchorNamePrefix + it)
129             cpsDataService.saveData(dataspaceName, anchorNamePrefix + it, data.replace("Easons", "Easons-"+it.toString()), OffsetDateTime.now())
130         }
131     }
132
133     def createJsonArray(name, numberOfElements, keyName, keyValuePrefix, dataPerKey) {
134         def innerJson = (1..numberOfElements).collect {
135             '{"' + keyName + '":"' + keyValuePrefix + '-' + it + '"' + (dataPerKey.empty? '': ',' + dataPerKey) + '}'
136         }.join(',')
137         return '{"' + name + '":[' + innerJson + ']}'
138     }
139
140     def createLeafList(name, numberOfElements, valuePrefix) {
141         def innerJson = (1..numberOfElements).collect {'"' + valuePrefix + '-' + it + '"'}.join(',')
142         return '"' + name + '":[' + innerJson + ']'
143     }
144
145 }