From 1e7e4a53684df04ba248c20d884ba907ca7c2870 Mon Sep 17 00:00:00 2001 From: Eliezio Oliveira Date: Wed, 31 Jul 2019 11:50:26 +0100 Subject: Add declarative acceptance tests First two UATs are for blueprints Echo and "PNF Configuration". The body of the ODL mount request was changed from XML to JSON, so it can be represented in a YAML file. Initial documentation about the UATs can be found at components/model-catalog/blueprint-model/uat-blueprints/README.md BluePrintArchiveUtils.recurseFiles() replaced by compressFolder() that uses native Java 7. Removed commons-compress as dependency since is no longer used. Change-Id: I96a584ae12ca009f90fe8fe9485eb57ce05e8add Issue-ID: CCSDK-1569 Signed-off-by: Eliezio Oliveira --- .../BlueprintsAcceptanceTests.kt | 280 +++++++++++++++++++++ .../cds/blueprintsprocessor/CollectionUtils2.kt | 31 +++ .../blueprintsprocessor/ExtendedTemporaryFolder.kt | 40 +++ .../blueprintsprocessor/TestSecuritySettings.kt | 45 ++++ .../WorkingFoldersInitializer.kt | 48 ++++ .../src/test/resources/application-test.properties | 50 ++++ .../src/test/resources/logback-test.xml | 20 +- 7 files changed, 507 insertions(+), 7 deletions(-) create mode 100644 ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTests.kt create mode 100644 ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/CollectionUtils2.kt create mode 100644 ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt create mode 100644 ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/TestSecuritySettings.kt create mode 100644 ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/WorkingFoldersInitializer.kt create mode 100644 ms/blueprintsprocessor/application/src/test/resources/application-test.properties (limited to 'ms/blueprintsprocessor/application/src/test') diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTests.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTests.kt new file mode 100644 index 000000000..0a57277ea --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTests.kt @@ -0,0 +1,280 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.cds.blueprintsprocessor + +import com.fasterxml.jackson.databind.ObjectMapper +import com.nhaarman.mockitokotlin2.any +import com.nhaarman.mockitokotlin2.argThat +import com.nhaarman.mockitokotlin2.atLeast +import com.nhaarman.mockitokotlin2.atLeastOnce +import com.nhaarman.mockitokotlin2.eq +import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.verify +import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions +import com.nhaarman.mockitokotlin2.whenever +import org.junit.ClassRule +import org.junit.Rule +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService.WebClientResponse +import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintArchiveUtils.Companion.compressToBytes +import org.skyscreamer.jsonassert.JSONAssert +import org.skyscreamer.jsonassert.JSONCompareMode +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.mock.mockito.MockBean +import org.springframework.core.io.ByteArrayResource +import org.springframework.core.io.Resource +import org.springframework.http.MediaType +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource +import org.springframework.test.context.junit4.rules.SpringClassRule +import org.springframework.test.context.junit4.rules.SpringMethodRule +import org.springframework.test.web.reactive.server.WebTestClient +import org.yaml.snakeyaml.Yaml +import reactor.core.publisher.Mono +import java.io.File +import java.nio.file.Path +import java.nio.file.Paths +import kotlin.test.BeforeTest +import kotlin.test.Test + +@RunWith(Parameterized::class) +// Set blueprintsprocessor.httpPort=0 to trigger a random port selection +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@AutoConfigureWebTestClient(timeout = "PT10S") +@ContextConfiguration(initializers = [ + WorkingFoldersInitializer::class, + TestSecuritySettings.ServerContextInitializer::class +]) +@TestPropertySource(locations = ["classpath:application-test.properties"]) +@Suppress("UNCHECKED_CAST") +class BlueprintsAcceptanceTests(private val blueprintName: String, private val filename: String) { + + companion object { + const val UAT_BLUEPRINTS_BASE_DIR = "../../../components/model-catalog/blueprint-model/uat-blueprints" + const val EMBEDDED_UAT_FILE = "Tests/uat.yaml" + + @ClassRule + @JvmField + val springClassRule = SpringClassRule() + + val log: Logger = LoggerFactory.getLogger(BlueprintsAcceptanceTests::class.java) + + @Parameterized.Parameters(name = "{index} {0}") + @JvmStatic + fun filenames(): List> { + return File(UAT_BLUEPRINTS_BASE_DIR) + .listFiles { file -> file.isDirectory && File(file, EMBEDDED_UAT_FILE).isFile } + ?.map { file -> arrayOf(file.nameWithoutExtension, file.canonicalPath) } + ?: emptyList() + } + } + + @Rule + @JvmField + val springMethodRule = SpringMethodRule() + + @MockBean(name = RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY) + lateinit var restClientFactory: BluePrintRestLibPropertyService + + @Autowired + // Bean is created programmatically by {@link WorkingFoldersInitializer#initialize(String)} + @Suppress("SpringJavaInjectionPointsAutowiringInspection") + lateinit var tempFolder: ExtendedTemporaryFolder + + @Autowired + lateinit var webTestClient: WebTestClient + + @Autowired + lateinit var mapper: ObjectMapper + + @BeforeTest + fun cleanupTemporaryFolder() { + tempFolder.deleteAllFiles() + } + + @Test + fun testBlueprint() { + val yaml: Map = loadYaml(Paths.get(filename, EMBEDDED_UAT_FILE)) + + uploadBlueprint(blueprintName) + + // Configure mocked external services + val services = yaml["external-services"] as List>? ?: emptyList() + val expectationPerClient = services.map { service -> + val selector = service["selector"] as String + val expectations = (service["expectations"] as List>).map { + parseExpectation(it) + } + val mockClient = createRestClientMock(selector, expectations) + mockClient to expectations + }.toMap() + + // Run processes + for (process in (yaml["processes"] as List>)) { + val processName = process["name"] + log.info("Executing process '$processName'") + val request = mapper.writeValueAsString(process["request"]) + val expectedResponse = mapper.writeValueAsString(process["expectedResponse"]) + processBlueprint(request, expectedResponse) + } + + // Validate request payloads + for ((mockClient, expectations) in expectationPerClient) { + expectations.forEach { expectation -> + verify(mockClient, atLeastOnce()).exchangeResource( + eq(expectation.method), + eq(expectation.path), + argThat { assertJsonEqual(expectation.expectedRequestBody, this) }, + expectation.requestHeadersMatcher()) + } + // Don't mind the invocations to the overloaded exchangeResource(String, String, String) + verify(mockClient, atLeast(0)).exchangeResource(any(), any(), any()) + verifyNoMoreInteractions(mockClient) + } + } + + private fun createRestClientMock(selector: String, restExpectations: List): BlueprintWebClientService { + val restClient = mock(verboseLogging = true) + + // Delegates to overloaded exchangeResource(String, String, String, Map) + whenever(restClient.exchangeResource(any(), any(), any())) + .thenAnswer { invocation -> + val method = invocation.arguments[0] as String + val path = invocation.arguments[1] as String + val request = invocation.arguments[2] as String + restClient.exchangeResource(method, path, request, emptyMap()) + } + for (expectation in restExpectations) { + whenever(restClient.exchangeResource( + eq(expectation.method), + eq(expectation.path), + any(), + any())) + .thenReturn(WebClientResponse(expectation.statusCode, expectation.responseBody)) + } + + whenever(restClientFactory.blueprintWebClientService(selector)) + .thenReturn(restClient) + return restClient + } + + private fun uploadBlueprint(blueprintName: String) { + val body = toMultiValueMap("file", getBlueprintAsResource(blueprintName)) + webTestClient + .post() + .uri("/api/v1/execution-service/upload") + .header("Authorization", TestSecuritySettings.clientAuthToken()) + .syncBody(body) + .exchange() + .expectStatus().isOk + } + + private fun processBlueprint(request: String, expectedResponse: String) { + webTestClient + .post() + .uri("/api/v1/execution-service/process") + .header("Authorization", TestSecuritySettings.clientAuthToken()) + .contentType(MediaType.APPLICATION_JSON_UTF8) + .body(Mono.just(request), String::class.java) + .exchange() + .expectStatus().isOk + .expectBody() + .json(expectedResponse) + } + + private fun getBlueprintAsResource(blueprintName: String): Resource { + val baseDir = Paths.get(UAT_BLUEPRINTS_BASE_DIR, blueprintName) + val zipBytes = compressToBytes(baseDir) + return object : ByteArrayResource(zipBytes) { + // Filename has to be returned in order to be able to post + override fun getFilename() = "$blueprintName.zip" + } + } + + private fun loadYaml(path: Path): Map { + return path.toFile().reader().use { reader -> + Yaml().load(reader) + } + } + + private fun assertJsonEqual(expected: Any, actual: String): Boolean { + if (actual != expected) { + // assertEquals throws an exception whenever match fails + JSONAssert.assertEquals(mapper.writeValueAsString(expected), actual, JSONCompareMode.LENIENT) + } + return true + } + + private fun parseExpectation(expectation: Map): RestExpectation { + val request = expectation["request"] as Map + val method = request["method"] as String + val path = joinPath(request.getValue("path")) + val contentType = request["content-type"] as String? + val requestBody = request.getOrDefault("body", "") + + val response = expectation["response"] as Map? ?: emptyMap() + val status = response["status"] as Int? ?: 200 + val responseBody = when (val body = response["body"] ?: "") { + is String -> body + else -> mapper.writeValueAsString(body) + } + + return RestExpectation(method, path, contentType, requestBody, status, responseBody) + } + + /** + * Join a multilevel lists of strings. + * Example: joinPath(listOf("a", listOf("b", "c"), "d")) will result in "a/b/c/d". + */ + private fun joinPath(any: Any): String { + fun recursiveJoin(any: Any, sb: StringBuilder): StringBuilder { + when (any) { + is List<*> -> any.filterNotNull().forEach { recursiveJoin(it, sb) } + is String -> { + if (sb.isNotEmpty()) { + sb.append('/') + } + sb.append(any) + } + else -> throw IllegalArgumentException("Unsupported type: ${any.javaClass}") + } + return sb + } + + return recursiveJoin(any, StringBuilder()).toString() + } + + data class RestExpectation(val method: String, val path: String, val contentType: String?, + val expectedRequestBody: Any, + val statusCode: Int, val responseBody: String) { + + fun requestHeadersMatcher(): Map { + return if (contentType != null) eq(mapOf("Content-Type" to contentType)) else any() + } + } +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/CollectionUtils2.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/CollectionUtils2.kt new file mode 100644 index 000000000..63d64cae4 --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/CollectionUtils2.kt @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.cds.blueprintsprocessor + +import org.springframework.util.CollectionUtils +import org.springframework.util.MultiValueMap + + +/** + * Convenient method to create a single-entry MultiValueMap. + */ +fun toMultiValueMap(key: K, vararg values: V): MultiValueMap { + return CollectionUtils.toMultiValueMap(mapOf(key to values.asList())) +} diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt new file mode 100644 index 000000000..4576f2761 --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt @@ -0,0 +1,40 @@ +package org.onap.ccsdk.cds.blueprintsprocessor + +import org.junit.rules.TemporaryFolder +import java.io.File +import java.io.IOException +import java.nio.file.FileVisitResult +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.SimpleFileVisitor +import java.nio.file.attribute.BasicFileAttributes +import javax.annotation.PreDestroy + +class ExtendedTemporaryFolder { + private val tempFolder = TemporaryFolder() + + init { + tempFolder.create() + } + + @PreDestroy + fun delete() = tempFolder.delete() + + /** + * A delegate to org.junit.rules.TemporaryFolder.TemporaryFolder.newFolder(String). + */ + fun newFolder(folder: String): File = tempFolder.newFolder(folder) + + /** + * Delete all files under the root temporary folder recursively. The folders are preserved. + */ + fun deleteAllFiles() { + Files.walkFileTree(tempFolder.root.toPath(), object : SimpleFileVisitor() { + @Throws(IOException::class) + override fun visitFile(file: Path?, attrs: BasicFileAttributes?): FileVisitResult { + file?.toFile()?.delete() + return FileVisitResult.CONTINUE + } + }) + } +} diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/TestSecuritySettings.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/TestSecuritySettings.kt new file mode 100644 index 000000000..f7ab2554c --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/TestSecuritySettings.kt @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.cds.blueprintsprocessor + +import org.springframework.context.ApplicationContextInitializer +import org.springframework.context.ConfigurableApplicationContext +import org.springframework.test.context.support.TestPropertySourceUtils +import org.springframework.util.Base64Utils +import java.nio.charset.StandardCharsets + +class TestSecuritySettings { + companion object { + private const val authUsername = "walter.white" + private const val authPassword = "Heisenberg" + + fun clientAuthToken() = + "Basic " + Base64Utils.encodeToString("$authUsername:$authPassword".toByteArray(StandardCharsets.UTF_8)) + } + + class ServerContextInitializer : ApplicationContextInitializer { + override fun initialize(context: ConfigurableApplicationContext) { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context, + "security.user.name=$authUsername", + "security.user.password={noop}$authPassword" + ) + } + } +} diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/WorkingFoldersInitializer.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/WorkingFoldersInitializer.kt new file mode 100644 index 000000000..37615cb1a --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/WorkingFoldersInitializer.kt @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.cds.blueprintsprocessor + +import org.springframework.beans.factory.support.BeanDefinitionBuilder +import org.springframework.beans.factory.support.BeanDefinitionRegistry +import org.springframework.context.ApplicationContextInitializer +import org.springframework.context.ConfigurableApplicationContext +import org.springframework.stereotype.Component +import org.springframework.test.context.support.TestPropertySourceUtils + +@Component +class WorkingFoldersInitializer : ApplicationContextInitializer { + + override fun initialize(context: ConfigurableApplicationContext) { + val tempFolder = ExtendedTemporaryFolder() + val properties = listOf("Deploy", "Archive", "Working") + .map { "blueprintsprocessor.blueprint${it}Path=${tempFolder.newFolder(it)}" } + .toTypedArray() + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context, *properties) + // Expose tempFolder as a bean so it can be accessed via DI + registerSingleton(context, "tempFolder", ExtendedTemporaryFolder::class.java, tempFolder) + } + + @Suppress("SameParameterValue") + private fun registerSingleton(context: ConfigurableApplicationContext, + beanName: String, beanClass: Class, instance: T) { + val builder = BeanDefinitionBuilder.genericBeanDefinition(beanClass) { instance } + (context.beanFactory as BeanDefinitionRegistry).registerBeanDefinition(beanName, builder.beanDefinition) + } +} diff --git a/ms/blueprintsprocessor/application/src/test/resources/application-test.properties b/ms/blueprintsprocessor/application/src/test/resources/application-test.properties new file mode 100644 index 000000000..b8b80f2dd --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/resources/application-test.properties @@ -0,0 +1,50 @@ +# +# Copyright © 2019 Nordix Foundation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +spring.http.log-request-details=true + +blueprintsprocessor.httpPort=0 +blueprintsprocessor.grpcEnable=true +blueprintsprocessor.grpcPort=0 + +blueprintsprocessor.db.primary.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1 +blueprintsprocessor.db.primary.username=sa +blueprintsprocessor.db.primary.password= +blueprintsprocessor.db.primary.driverClassName=org.h2.Driver +blueprintsprocessor.db.primary.hibernateHbm2ddlAuto=create-drop +blueprintsprocessor.db.primary.hibernateDDLAuto=update +blueprintsprocessor.db.primary.hibernateNamingStrategy=org.hibernate.cfg.ImprovedNamingStrategy +blueprintsprocessor.db.primary.hibernateDialect=org.hibernate.dialect.H2Dialect + +# The properties bellow are set programmatically +#blueprintsprocessor.blueprintDeployPath= +#blueprintsprocessor.blueprintArchivePath= +#blueprintsprocessor.blueprintWorkingPath= +#security.user.name= +#security.user.password= + +# Python executor +blueprints.processor.functions.python.executor.executionPath=../../../components/scripts/python/ccsdk_blueprints +blueprints.processor.functions.python.executor.modulePaths=\ + ../../../components/scripts/python/ccsdk_blueprints,\ + ../../../components/scripts/python/ccsdk_netconf,\ + ../../../components/scripts/python/ccsdk_restconf + +# Executor Options +blueprintsprocessor.cliExecutor.enabled=true +blueprintprocessor.netconfExecutor.enabled=true + +blueprintsprocessor.restconfEnabled=true \ No newline at end of file diff --git a/ms/blueprintsprocessor/application/src/test/resources/logback-test.xml b/ms/blueprintsprocessor/application/src/test/resources/logback-test.xml index 16e7d3d1b..eaa51c0a3 100644 --- a/ms/blueprintsprocessor/application/src/test/resources/logback-test.xml +++ b/ms/blueprintsprocessor/application/src/test/resources/logback-test.xml @@ -1,5 +1,6 @@ - %d{HH:mm:ss.SSS} %-5level [%thread] %logger{50} - %msg%n + %d{HH:mm:ss.SSS} %-5level %-40.40logger{39} : %msg%n + + - - - + + - + + + + + -- cgit 1.2.3-korg