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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.integration.base
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.ncmp.api.NetworkCmProxyCmHandleQueryService
31 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
32 import org.onap.cps.ncmp.api.NetworkCmProxyQueryService
33 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
34 import org.onap.cps.spi.model.DataNode
35 import org.onap.cps.spi.repository.DataspaceRepository
36 import org.onap.cps.spi.utils.SessionManager
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
39 import org.springframework.boot.autoconfigure.domain.EntityScan
40 import org.springframework.boot.test.context.SpringBootTest
41 import org.springframework.context.annotation.ComponentScan
42 import org.springframework.data.jpa.repository.config.EnableJpaRepositories
43 import org.springframework.web.client.RestTemplate
44 import org.testcontainers.spock.Testcontainers
45 import spock.lang.Shared
46 import spock.lang.Specification
48 @SpringBootTest(classes = [CpsDataspaceService])
50 @EnableAutoConfiguration
51 @EnableJpaRepositories(basePackageClasses = [DataspaceRepository])
52 @ComponentScan(basePackages = ['org.onap.cps'])
53 @EntityScan('org.onap.cps.spi.entities')
54 abstract class CpsIntegrationSpecBase extends Specification {
57 DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance()
60 CpsDataspaceService cpsDataspaceService
63 CpsAnchorService cpsAnchorService
66 CpsDataService cpsDataService
69 CpsModuleService cpsModuleService
72 CpsQueryService cpsQueryService
75 SessionManager sessionManager
78 RestTemplate restTemplate
81 NetworkCmProxyCmHandleQueryService networkCmProxyCmHandleQueryService
84 NetworkCmProxyDataService networkCmProxyDataService
87 NetworkCmProxyQueryService networkCmProxyQueryService
89 def static GENERAL_TEST_DATASPACE = 'generalTestDataspace'
90 def static BOOKSTORE_SCHEMA_SET = 'bookstoreSchemaSet'
92 def static initialized = false
93 def now = OffsetDateTime.now()
97 cpsDataspaceService.createDataspace(GENERAL_TEST_DATASPACE)
98 createStandardBookStoreSchemaSet(GENERAL_TEST_DATASPACE)
103 def static countDataNodesInTree(DataNode dataNode) {
104 return 1 + countDataNodesInTree(dataNode.getChildDataNodes())
107 def static countDataNodesInTree(Collection<DataNode> dataNodes) {
109 for (DataNode parent : dataNodes) {
110 nodeCount += countDataNodesInTree(parent)
115 def static readResourceDataFile(filename) {
116 return new File('src/test/resources/data/' + filename).text
119 def getBookstoreYangResourcesNameToContentMap() {
120 def bookstoreModelFileContent = readResourceDataFile('bookstore/bookstore.yang')
121 def bookstoreTypesFileContent = readResourceDataFile('bookstore/bookstore-types.yang')
122 return [bookstore: bookstoreModelFileContent, bookstoreTypes: bookstoreTypesFileContent]
125 def createStandardBookStoreSchemaSet(targetDataspace) {
126 cpsModuleService.createSchemaSet(targetDataspace, BOOKSTORE_SCHEMA_SET, getBookstoreYangResourcesNameToContentMap())
129 def createStandardBookStoreSchemaSet(targetDataspace, targetSchemaSet) {
130 cpsModuleService.createSchemaSet(targetDataspace, targetSchemaSet, getBookstoreYangResourcesNameToContentMap())
133 def dataspaceExists(dataspaceName) {
135 cpsDataspaceService.getDataspace(dataspaceName)
136 } catch (DataspaceNotFoundException ignored) {
142 def addAnchorsWithData(numberOfAnchors, dataspaceName, schemaSetName, anchorNamePrefix, data) {
143 (1..numberOfAnchors).each {
144 cpsAnchorService.createAnchor(dataspaceName, schemaSetName, anchorNamePrefix + it)
145 cpsDataService.saveData(dataspaceName, anchorNamePrefix + it, data.replace("Easons", "Easons-"+it.toString()), OffsetDateTime.now())
149 def createJsonArray(name, numberOfElements, keyName, keyValuePrefix, dataPerKey) {
150 def innerJson = (1..numberOfElements).collect {
151 '{"' + keyName + '":"' + keyValuePrefix + '-' + it + '"' + (dataPerKey.empty? '': ',' + dataPerKey) + '}'
153 return '{"' + name + '":[' + innerJson + ']}'
156 def createLeafList(name, numberOfElements, valuePrefix) {
157 def innerJson = (1..numberOfElements).collect {'"' + valuePrefix + '-' + it + '"'}.join(',')
158 return '"' + name + '":[' + innerJson + ']'