Add NCMP to integration-test module
[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.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
47
48 @SpringBootTest(classes = [CpsDataspaceService])
49 @Testcontainers
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 {
55
56     @Shared
57     DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance()
58
59     @Autowired
60     CpsDataspaceService cpsDataspaceService
61
62     @Autowired
63     CpsAnchorService cpsAnchorService
64
65     @Autowired
66     CpsDataService cpsDataService
67
68     @Autowired
69     CpsModuleService cpsModuleService
70
71     @Autowired
72     CpsQueryService cpsQueryService
73
74     @Autowired
75     SessionManager sessionManager
76
77     @Autowired
78     RestTemplate restTemplate
79
80     @Autowired
81     NetworkCmProxyCmHandleQueryService networkCmProxyCmHandleQueryService
82
83     @Autowired
84     NetworkCmProxyDataService networkCmProxyDataService
85
86     @Autowired
87     NetworkCmProxyQueryService networkCmProxyQueryService
88
89     def static GENERAL_TEST_DATASPACE = 'generalTestDataspace'
90     def static BOOKSTORE_SCHEMA_SET = 'bookstoreSchemaSet'
91
92     def static initialized = false
93     def now = OffsetDateTime.now()
94
95     def setup() {
96         if (!initialized) {
97             cpsDataspaceService.createDataspace(GENERAL_TEST_DATASPACE)
98             createStandardBookStoreSchemaSet(GENERAL_TEST_DATASPACE)
99             initialized = true
100         }
101     }
102
103     def static countDataNodesInTree(DataNode dataNode) {
104         return 1 + countDataNodesInTree(dataNode.getChildDataNodes())
105     }
106
107     def static countDataNodesInTree(Collection<DataNode> dataNodes) {
108         int nodeCount = 0
109         for (DataNode parent : dataNodes) {
110             nodeCount += countDataNodesInTree(parent)
111         }
112         return nodeCount
113     }
114
115     def static readResourceDataFile(filename) {
116         return new File('src/test/resources/data/' + filename).text
117     }
118
119     def getBookstoreYangResourcesNameToContentMap() {
120         def bookstoreModelFileContent = readResourceDataFile('bookstore/bookstore.yang')
121         def bookstoreTypesFileContent = readResourceDataFile('bookstore/bookstore-types.yang')
122         return [bookstore: bookstoreModelFileContent, bookstoreTypes: bookstoreTypesFileContent]
123     }
124
125     def createStandardBookStoreSchemaSet(targetDataspace) {
126         cpsModuleService.createSchemaSet(targetDataspace, BOOKSTORE_SCHEMA_SET, getBookstoreYangResourcesNameToContentMap())
127     }
128
129     def createStandardBookStoreSchemaSet(targetDataspace, targetSchemaSet) {
130         cpsModuleService.createSchemaSet(targetDataspace, targetSchemaSet, getBookstoreYangResourcesNameToContentMap())
131     }
132
133     def dataspaceExists(dataspaceName) {
134         try {
135             cpsDataspaceService.getDataspace(dataspaceName)
136         } catch (DataspaceNotFoundException ignored) {
137             return false
138         }
139         return true
140     }
141
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())
146         }
147     }
148
149     def createJsonArray(name, numberOfElements, keyName, keyValuePrefix, dataPerKey) {
150         def innerJson = (1..numberOfElements).collect {
151             '{"' + keyName + '":"' + keyValuePrefix + '-' + it + '"' + (dataPerKey.empty? '': ',' + dataPerKey) + '}'
152         }.join(',')
153         return '{"' + name + '":[' + innerJson + ']}'
154     }
155
156     def createLeafList(name, numberOfElements, valuePrefix) {
157         def innerJson = (1..numberOfElements).collect {'"' + valuePrefix + '-' + it + '"'}.join(',')
158         return '"' + name + '":[' + innerJson + ']'
159     }
160
161 }