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