1 package org.onap.ccsdk.cds.blueprintsprocessor.uat.base
3 import kotlinx.coroutines.runBlocking
4 import org.junit.runner.RunWith
5 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.MessagePrioritizationStateService
6 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.service.SampleMessagePrioritizationService
7 import org.onap.ccsdk.cds.blueprintsprocessor.uat.base.BaseBlueprintsAcceptanceTest.TestSecuritySettings
8 import org.onap.ccsdk.cds.blueprintsprocessor.uat.base.BaseBlueprintsAcceptanceTest.WorkingFoldersInitializer
9 import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor
10 import org.onap.ccsdk.cds.blueprintsprocessor.uat.utils.UatExecutor
11 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintArchiveUtils.Companion.compressToBytes
12 import org.slf4j.Logger
13 import org.slf4j.LoggerFactory
14 import org.springframework.beans.factory.annotation.Autowired
15 import org.springframework.beans.factory.support.BeanDefinitionBuilder
16 import org.springframework.beans.factory.support.BeanDefinitionRegistry
17 import org.springframework.boot.test.context.SpringBootTest
18 import org.springframework.context.ApplicationContextInitializer
19 import org.springframework.context.ConfigurableApplicationContext
20 import org.springframework.stereotype.Component
21 import org.springframework.stereotype.Service
22 import org.springframework.test.context.ContextConfiguration
23 import org.springframework.test.context.TestPropertySource
24 import org.springframework.test.context.junit4.SpringRunner
25 import org.springframework.test.context.support.TestPropertySourceUtils
26 import org.springframework.util.Base64Utils
28 import java.io.IOException
29 import java.nio.file.*
30 import java.nio.file.attribute.BasicFileAttributes
31 import javax.annotation.PreDestroy
32 import kotlin.test.BeforeTest
33 import kotlin.test.AfterTest
37 * This is a SpringBootTest abstract Base class, that executes UAT (User Acceptance Tests) by calling
38 * callRunUat in an implementation class.
39 * See https://docs.onap.org/projects/onap-ccsdk-cds/en/latest/modelingconcepts/test.html and
40 * https://github.com/onap/ccsdk-cds/blob/master/components/model-catalog/blueprint-model/uat-blueprints/README.md
41 * for further information concerning the CDS UAT concept.
44 @RunWith(SpringRunner::class)
45 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
46 @ContextConfiguration(
48 WorkingFoldersInitializer::class,
49 TestSecuritySettings.ServerContextInitializer::class
52 @TestPropertySource(locations = ["classpath:application-test.properties"])
53 abstract class BaseBlueprintsAcceptanceTest() {
56 LogColor.setContextColor(LogColor.COLOR_TEST_CLIENT)
61 LogColor.resetContextColor()
65 private val log: Logger = LoggerFactory.getLogger(BaseBlueprintsAcceptanceTest::class.java)
69 // Bean is created programmatically by {@link WorkingFoldersInitializer#initialize(String)}
70 @Suppress("SpringJavaInjectionPointsAutowiringInspection")
71 lateinit var tempFolder: ExtendedTemporaryFolder
74 lateinit var uatExecutor: UatExecutor
77 fun cleanupTemporaryFolder() {
78 tempFolder.deleteAllFiles()
81 protected suspend fun callRunUat(pathName: String, uatFilename: String) {
83 val dir = File(pathName)
84 val rootFs = FileSystems.newFileSystem(dir.canonicalFile.toPath(), null)
85 log.info("dirname: ${dir.toString()} rootFs ${rootFs}")
87 val uatSpec = rootFs.getPath(uatFilename).toFile().readText()
88 val cbaBytes = compressToBytes(rootFs.getPath("/"))
89 uatExecutor.execute(uatSpec, cbaBytes)
94 open class TestMessagePrioritizationService(messagePrioritizationStateService: MessagePrioritizationStateService) :
95 SampleMessagePrioritizationService(messagePrioritizationStateService)
97 class WorkingFoldersInitializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
99 override fun initialize(context: ConfigurableApplicationContext) {
100 val tempFolder = ExtendedTemporaryFolder()
101 val properties = listOf("Deploy", "Archive", "Working")
102 .map { "blueprintsprocessor.blueprint${it}Path=${tempFolder.newFolder(it).absolutePath.replace("\\", "/")}" }
104 TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context, *properties)
105 // Expose tempFolder as a bean so it can be accessed via DI
106 registerSingleton(context, "tempFolder", ExtendedTemporaryFolder::class.java, tempFolder)
109 @Suppress("SameParameterValue")
110 private fun <T> registerSingleton(
111 context: ConfigurableApplicationContext,
116 val builder = BeanDefinitionBuilder.genericBeanDefinition(beanClass) { instance }
117 (context.beanFactory as BeanDefinitionRegistry).registerBeanDefinition(beanName, builder.beanDefinition)
121 class ExtendedTemporaryFolder {
123 private val tempFolder = createTempDir("uat")
126 fun delete() = tempFolder.deleteRecursively()
129 * A delegate to org.junit.rules.TemporaryFolder.TemporaryFolder.newFolder(String).
131 fun newFolder(folderName: String): File {
132 val dir = File(tempFolder, folderName)
134 throw IOException("Unable to create temporary directory $dir.")
140 * Delete all files under the root temporary folder recursively. The folders are preserved.
142 fun deleteAllFiles() {
145 object : SimpleFileVisitor<Path>() {
146 @Throws(IOException::class)
147 override fun visitFile(file: Path?, attrs: BasicFileAttributes?): FileVisitResult {
148 file?.toFile()?.delete()
149 return FileVisitResult.CONTINUE
155 class TestSecuritySettings {
158 private const val authUsername = "walter.white"
159 private const val authPassword = "Heisenberg"
161 fun clientAuthToken() =
162 "Basic " + Base64Utils.encodeToString("$authUsername:$authPassword".toByteArray())
165 class ServerContextInitializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
167 override fun initialize(context: ConfigurableApplicationContext) {
168 TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
170 "security.user.name=$authUsername",
171 "security.user.password={noop}$authPassword"