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