Add integration test for extending API: Get Module Definitions
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / base / CpsIntegrationSpecBase.groovy
index 567b33c..b59e0ff 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2023 Nordix Foundation
+ *  Copyright (C) 2023-2024 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the 'License');
  *  you may not use this file except in compliance with the License.
 
 package org.onap.cps.integration.base
 
-import org.onap.cps.api.impl.CpsAdminServiceImpl
-import org.onap.cps.api.impl.CpsDataServiceImpl
-import org.onap.cps.api.impl.CpsModuleServiceImpl
+import org.onap.cps.api.CpsAnchorService
+import org.onap.cps.api.CpsDataService
+import org.onap.cps.api.CpsDataspaceService
+import org.onap.cps.api.CpsModuleService
+import org.onap.cps.api.CpsQueryService
 import org.onap.cps.integration.DatabaseTestContainer
+import org.onap.cps.spi.config.CpsSessionFactory
+import org.onap.cps.spi.exceptions.DataspaceNotFoundException
 import org.onap.cps.spi.model.DataNode
 import org.onap.cps.spi.repository.DataspaceRepository
 import org.onap.cps.spi.impl.utils.CpsValidatorImpl
+import org.onap.cps.spi.utils.SessionManager
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
 import org.springframework.boot.autoconfigure.domain.EntityScan
@@ -38,12 +43,14 @@ import org.testcontainers.spock.Testcontainers
 import spock.lang.Shared
 import spock.lang.Specification
 
-@SpringBootTest(classes = [TestConfig, CpsAdminServiceImpl, CpsValidatorImpl])
+import java.time.OffsetDateTime
+
+@SpringBootTest(classes = [TestConfig, CpsValidatorImpl, SessionManager, CpsSessionFactory])
 @Testcontainers
 @EnableAutoConfiguration
 @EnableJpaRepositories(basePackageClasses = [DataspaceRepository])
-@ComponentScan(basePackages = ["org.onap.cps.api", "org.onap.cps.spi.repository"])
-@EntityScan("org.onap.cps.spi.entities")
+@ComponentScan(basePackages = ['org.onap.cps.api', 'org.onap.cps.spi.repository'])
+@EntityScan('org.onap.cps.spi.entities')
 class CpsIntegrationSpecBase extends Specification {
 
     @Shared
@@ -51,28 +58,38 @@ class CpsIntegrationSpecBase extends Specification {
 
     @Autowired
     @Lazy
-    CpsAdminServiceImpl cpsAdminService
+    CpsDataspaceService cpsDataspaceService
+
+    @Autowired
+    @Lazy
+    CpsAnchorService cpsAnchorService
 
     @Autowired
     @Lazy
-    CpsDataServiceImpl cpsDataService
+    CpsDataService cpsDataService
 
     @Autowired
     @Lazy
-    CpsModuleServiceImpl cpsModuleService
+    CpsModuleService cpsModuleService
 
-    def static GENERAL_TEST_DATASPACE = 'generalTestDataSpace'
-    def static BOOKSTORE_DATASPACE = 'bookstoreDataspace'
+    @Autowired
+    @Lazy
+    CpsQueryService cpsQueryService
+
+    @Autowired
+    @Lazy
+    SessionManager sessionManager
+
+    def static GENERAL_TEST_DATASPACE = 'generalTestDataspace'
     def static BOOKSTORE_SCHEMA_SET = 'bookstoreSchemaSet'
-    def static BOOKSTORE_ANCHOR = 'bookstoreAnchor'
 
     def static initialized = false
+    def now = OffsetDateTime.now()
 
     def setup() {
         if (!initialized) {
-            cpsAdminService.createDataspace(GENERAL_TEST_DATASPACE)
-            def bookstoreModelFileContent = readResourceFile('bookstore.yang')
-            cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, [bookstore : bookstoreModelFileContent])
+            cpsDataspaceService.createDataspace(GENERAL_TEST_DATASPACE)
+            createStandardBookStoreSchemaSet(GENERAL_TEST_DATASPACE)
             initialized = true;
         }
     }
@@ -89,7 +106,64 @@ class CpsIntegrationSpecBase extends Specification {
         return nodeCount
     }
 
-    def static readResourceFile(filename) {
+    def static readResourceDataFile(filename) {
         return new File('src/test/resources/data/' + filename).text
     }
+
+    def getBookstoreYangResourcesNameToContentMap() {
+        def bookstoreModelFileContent = readResourceDataFile('bookstore/bookstore.yang')
+        def bookstoreTypesFileContent = readResourceDataFile('bookstore/bookstore-types.yang')
+        return [bookstore: bookstoreModelFileContent, bookstoreTypes: bookstoreTypesFileContent]
+    }
+
+    def createStandardBookStoreSchemaSet(targetDataspace) {
+        cpsModuleService.createSchemaSet(targetDataspace, BOOKSTORE_SCHEMA_SET, getBookstoreYangResourcesNameToContentMap())
+    }
+
+    def createStandardBookStoreSchemaSet(targetDataspace, targetSchemaSet) {
+        cpsModuleService.createSchemaSet(targetDataspace, targetSchemaSet, getBookstoreYangResourcesNameToContentMap())
+    }
+
+    def dataspaceExists(dataspaceName) {
+        try {
+            cpsDataspaceService.getDataspace(dataspaceName)
+        } catch (DataspaceNotFoundException dataspaceNotFoundException) {
+            return false
+        }
+        return true
+    }
+
+    def addAnchorsWithData(numberOfAnchors, dataspaceName, schemaSetName, anchorNamePrefix, data) {
+        (1..numberOfAnchors).each {
+            cpsAnchorService.createAnchor(dataspaceName, schemaSetName, anchorNamePrefix + it)
+            cpsDataService.saveData(dataspaceName, anchorNamePrefix + it, data.replace("Easons", "Easons-"+it.toString()), OffsetDateTime.now())
+        }
+    }
+
+    def createJsonArray(name, numberOfElements, keyName, keyValuePrefix, dataPerKey) {
+        def json = '{"' + name + '":['
+        (1..numberOfElements).each {
+            json += '{"' + keyName + '":"' + keyValuePrefix + '-' + it + '"'
+            if (!dataPerKey.isEmpty()) {
+                json += ',' + dataPerKey
+            }
+            json += '}'
+            if (it < numberOfElements) {
+                json += ','
+            }
+        }
+        json += ']}'
+    }
+
+    def createLeafList(name, numberOfElements, valuePrefix) {
+        def json = '"' + name + '":['
+        (1..numberOfElements).each {
+            json += '"' + valuePrefix + '-' + it + '"'
+            if (it < numberOfElements) {
+                json += ','
+            }
+        }
+        json += ']'
+    }
+
 }