From ef0045e16049ca80a39f4c81db25e4cead7d5cd3 Mon Sep 17 00:00:00 2001 From: Alexis de Talhouët Date: Sat, 29 Jun 2019 18:27:23 -0400 Subject: Add unit test for resolution repository MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: If66670cc5397b0bcce340092ec6ff9ec008677bc Issue-ID: CCSDK-1423 Signed-off-by: Alexis de Talhouët --- .../db/ResourceResolutionDBServiceTest.kt | 211 ++++++++++++++++++ .../resolution/db/TemplateResolutionServiceTest.kt | 140 ++++++++++++ .../mock/MockRestResourceResolutionProcessor.kt | 2 +- .../api/ResourceControllerTest.kt | 237 --------------------- .../resource/api/ResourceControllerTest.kt | 235 ++++++++++++++++++++ 5 files changed, 587 insertions(+), 238 deletions(-) create mode 100644 ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBServiceTest.kt create mode 100644 ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionServiceTest.kt delete mode 100644 ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceControllerTest.kt create mode 100644 ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceControllerTest.kt diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBServiceTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBServiceTest.kt new file mode 100644 index 000000000..cfd00ac1d --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBServiceTest.kt @@ -0,0 +1,211 @@ +/* + * Copyright (C) 2019 Bell Canada. + * + * 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. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db + +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.runBlocking +import org.junit.Before +import org.junit.Test +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext +import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment +import org.springframework.dao.EmptyResultDataAccessException +import kotlin.test.assertEquals + +open class ResourceResolutionDBServiceTest { + + private val resourceResolutionRepository = mockk() + + private val resourceResolutionDBService = ResourceResolutionDBService(resourceResolutionRepository) + + private val resolutionKey = "resolutionKey" + private val resourceId = "1" + private val resourceType = "ServiceInstance" + private val occurrence = 0 + private val artifactPrefix = "template" + private val blueprintName = "blueprintName" + private val blueprintVersion = "1.0.0" + private val metadata = hashMapOf() + private val props = hashMapOf() + private val bluePrintContext = mockk() + private val bluePrintRuntimeService = mockk() + + @Before + fun setup() { + metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion + metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName + + props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY] = resolutionKey + props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId + props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType + props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence + + every { bluePrintContext.metadata } returns metadata + + every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext + } + + @Test + fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTest() { + + val rr1 = ResourceResolution() + val rr2 = ResourceResolution() + + val list = listOf(rr1, rr2) + every { + resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence( + any(), any(), any(), any(), any()) + } returns list + runBlocking { + + val res = + resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence( + bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix) + + assertEquals(2, res.size) + } + } + + @Test + fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTestException() { + every { + resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence( + any(), any(), any(), any(), any()) + } throws EmptyResultDataAccessException(1) + runBlocking { + val res = + resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence( + bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix) + + assert(res.isEmpty()) + } + } + + @Test + fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTest() { + + val rr1 = ResourceResolution() + val rr2 = ResourceResolution() + val list = listOf(rr1, rr2) + every { + resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence( + any(), any(), any(), any(), any(), any()) + } returns list + runBlocking { + + val res = + resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence( + bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix) + + assertEquals(2, res.size) + } + } + + @Test + fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTestException() { + every { + resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence( + any(), any(), any(), any(), any(), any()) + } throws EmptyResultDataAccessException(1) + runBlocking { + val res = + resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence( + bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix) + + assert(res.isEmpty()) + } + } + + @Test + fun readValueTest() { + val rr = ResourceResolution() + rr.name = "bob" + rr.value = "testValue" + every { + resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName( + any(), any(), any(), any(), any()) + } returns rr + runBlocking { + val res = + resourceResolutionDBService.readValue( + blueprintName, blueprintVersion, artifactPrefix, resolutionKey, "bob") + + assertEquals(rr.name, res.name) + assertEquals(rr.value, res.value) + } + } + + @Test + fun readWithResolutionKeyTest() { + val rr1 = ResourceResolution() + val rr2 = ResourceResolution() + val list = listOf(rr1, rr2) + every { + resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName( + any(), any(), any(), any()) + } returns list + runBlocking { + val res = + resourceResolutionDBService.readWithResolutionKey( + blueprintName, blueprintVersion, artifactPrefix, resolutionKey) + assertEquals(2, res.size) + } + } + + @Test + fun readWithResourceIdAndResourceTypeTest() { + val rr1 = ResourceResolution() + val rr2 = ResourceResolution() + val list = listOf(rr1, rr2) + every { + resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType( + any(), any(), any(), any()) + } returns list + runBlocking { + val res = + resourceResolutionDBService.readWithResourceIdAndResourceType( + blueprintName, blueprintVersion, resourceId, resourceType) + assertEquals(2, res.size) + } + } + + @Test + fun writeTest() { + val resourceResolution = ResourceResolution() + val resourceAssignment = ResourceAssignment() + resourceAssignment.property?.status = BluePrintConstants.STATUS_SUCCESS + resourceAssignment.property?.value = "result".asJsonPrimitive() + resourceAssignment.dictionarySource = "ddSource" + resourceAssignment.dictionaryName = "ddName" + resourceAssignment.version = 1 + resourceAssignment.name = "test" + every { + resourceResolutionRepository.saveAndFlush(any()) + } returns resourceResolution + runBlocking { + val res = + resourceResolutionDBService.write( + props, bluePrintRuntimeService, artifactPrefix, resourceAssignment) + + assertEquals(resourceResolution, res) + } + } +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionServiceTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionServiceTest.kt new file mode 100644 index 000000000..da1957190 --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionServiceTest.kt @@ -0,0 +1,140 @@ +package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db + +import io.mockk.confirmVerified +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.runBlocking +import org.junit.Before +import org.junit.Test +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext +import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment +import org.springframework.dao.EmptyResultDataAccessException +import kotlin.test.assertEquals + +class TemplateResolutionServiceTest { + + private val templateResolutionRepository = mockk() + + private val templateResolutionService = TemplateResolutionService(templateResolutionRepository) + + private val resolutionKey = "resolutionKey" + private val resourceId = "1" + private val resourceType = "ServiceInstance" + private val occurrence = 0 + private val artifactPrefix = "template" + private val blueprintName = "blueprintName" + private val blueprintVersion = "1.0.0" + private val result = "result" + private val metadata = hashMapOf() + private val props = hashMapOf() + private val bluePrintContext = mockk() + private val bluePrintRuntimeService = mockk() + + @Before + fun setup() { + metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion + metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName + + props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY] = resolutionKey + props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId + props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType + props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence + + every { bluePrintContext.metadata } returns metadata + + every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext + } + + @Test + fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTest() { + val tr = TemplateResolution() + tr.result = "res" + runBlocking { + every { + templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence( + any(), any(), any(), any(), any()) + } returns tr + val res = + templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName( + bluePrintRuntimeService, artifactPrefix, resolutionKey) + assertEquals(tr.result, res) + } + } + + @Test(expected = EmptyResultDataAccessException::class) + fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTestException() { + val tr = TemplateResolution() + runBlocking { + every { + templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence( + any(), any(), any(), any(), any()) + } returns tr + templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName( + bluePrintRuntimeService, artifactPrefix, resolutionKey) + } + } + + @Test + fun writeWithResolutionKeyTest() { + val tr = TemplateResolution() + runBlocking { + every { templateResolutionRepository.saveAndFlush(any()) } returns tr + every { + templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence( + any(), any(), any(), any(), any()) + } returns null + val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix) + assertEquals(tr, res) + } + } + + @Test + fun writeWithResolutionKeyExistingTest() { + val tr = TemplateResolution() + runBlocking { + every { templateResolutionRepository.saveAndFlush(any()) } returns tr + every { + templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence( + any(), any(), any(), any(), any()) + } returns tr + every { + templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence( + any(), any(), any(), any(), any()) + } returns Unit + val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix) + verify { + templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence( + eq(resolutionKey), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence)) + } + assertEquals(tr, res) + } + } + + @Test + fun writeWithResourceIdResourceTypeExistingTest() { + props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY] = "" + val tr = TemplateResolution() + runBlocking { + every { templateResolutionRepository.saveAndFlush(any()) } returns tr + every { + templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence( + any(), any(), any(), any(), any(), any()) + } returns tr + every { + templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence( + any(), any(), any(), any(), any(), any()) + } returns Unit + val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix) + verify { + templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence( + eq(resourceId), eq(resourceType), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence)) + } + assertEquals(tr, res) + } + } +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/mock/MockRestResourceResolutionProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/mock/MockRestResourceResolutionProcessor.kt index be023307b..e80663094 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/mock/MockRestResourceResolutionProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/mock/MockRestResourceResolutionProcessor.kt @@ -98,7 +98,7 @@ class MockRestResourceResolutionProcessor(private val blueprintRestLibPropertySe } } catch (e: Exception) { ResourceAssignmentUtils.setFailedResourceDataValue(executionRequest, e.message) - throw BluePrintProcessorException("Failed in template key ($executionRequest) assignments with: ${e.message}", + throw BluePrintProcessorException("Failed in template resolutionKey ($executionRequest) assignments with: ${e.message}", e) } } diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceControllerTest.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceControllerTest.kt deleted file mode 100644 index 9ed97df1a..000000000 --- a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceControllerTest.kt +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Copyright © 2019 Bell Canada. - * - * 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. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults.api - -import kotlinx.coroutines.runBlocking -import org.junit.Assert -import org.junit.Test -import org.junit.runner.RunWith -import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution -import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService -import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive -import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition -import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils -import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment -import org.slf4j.LoggerFactory -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.autoconfigure.security.SecurityProperties -import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest -import org.springframework.context.annotation.ComponentScan -import org.springframework.test.context.ContextConfiguration -import org.springframework.test.context.TestPropertySource -import org.springframework.test.context.junit4.SpringRunner -import org.springframework.test.web.reactive.server.WebTestClient -import org.onap.ccsdk.cds.blueprintsprocessor.resource.api.ErrorMessage -import org.onap.ccsdk.cds.blueprintsprocessor.resource.api.ResourceController -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants - - -@RunWith(SpringRunner::class) -@WebFluxTest -@ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class]) -@ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"]) -@TestPropertySource(locations = ["classpath:application-test.properties"]) -class ResourceControllerTest { - - private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString()) - - @Autowired - lateinit var resourceResolutionDBService: ResourceResolutionDBService - @Autowired - lateinit var webTestClient: WebTestClient - - val blueprintName = "baseconfiguration" - val blueprintVersion = "1.0.0" - val templatePrefix = "activate" - - @Test - fun `ping return Success`() { - runBlocking { - webTestClient.get().uri("/api/v1/resources/health-check") - .exchange() - .expectStatus().isOk - .expectBody() - .equals("Success") - } - } - - @Test - fun getAllFromResolutionKeyTest() { - - val resolutionKey = "1" - val ra1 = createRA("bob") - val ra2 = createRA("dylan") - - runBlocking { - - store(ra1, resKey = resolutionKey) - store(ra2, resKey = resolutionKey) - - webTestClient - .get() - .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey") - .exchange() - .expectStatus().isOk - .expectBody() - .consumeWith { - val json = String(it.responseBody!!) - val typeFactory = JacksonUtils.objectMapper.typeFactory - val list: List = JacksonUtils.objectMapper.readValue(json, - typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)) - Assert.assertEquals(2, list.size) - assertEqual(ra1, list[0]) - assertEqual(ra1, list[0]) - } - } - } - - @Test - fun getAllFromFromResourceTypeAndIdTest() { - - val resourceId = "1" - val resourceType = "ServiceInstance" - val ra1 = createRA("bob") - val ra2 = createRA("dylan") - - runBlocking { - - store(ra1, resId = resourceId, resType = resourceType) - store(ra2, resId = resourceId, resType = resourceType) - - webTestClient - .get() - .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId") - .exchange() - .expectStatus().isOk - .expectBody() - .consumeWith { - val json = String(it.responseBody!!) - val typeFactory = JacksonUtils.objectMapper.typeFactory - val list: List = JacksonUtils.objectMapper.readValue(json, - typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)) - Assert.assertEquals(2, list.size) - assertEqual(ra1, list[0]) - assertEqual(ra1, list[0]) - } - } - } - - - @Test - fun getAllFromMissingParamTest() { - runBlocking { - webTestClient - .get() - .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion") - .exchange() - .expectStatus().is4xxClientError - .expectBody() - .consumeWith { - val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java) - Assert.assertEquals("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.", - r.message) - } - } - } - - @Test - fun getAllFromWrongInputTest() { - runBlocking { - webTestClient - .get() - .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1") - .exchange() - .expectStatus().is4xxClientError - .expectBody() - .consumeWith { - val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java) - Assert.assertEquals("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.", - r.message) - } - } - } - - @Test - fun getOneFromResolutionKeyTest() { - val resolutionKey = "3" - val ra = createRA("joe") - runBlocking { - store(ra, resKey = resolutionKey) - } - runBlocking { - webTestClient.get() - .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe") - .exchange() - .expectStatus().isOk - .expectBody() - .consumeWith { - val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java) - assertEqual(ra, r) - } - } - } - - @Test - fun getOneFromResolutionKey404Test() { - val resolutionKey = "3" - runBlocking { - webTestClient.get() - .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist") - .exchange() - .expectStatus().is4xxClientError - .expectBody() - } - } - - private suspend fun store(resourceAssignment: ResourceAssignment, resKey: String = "", resId: String = "", - resType: String = "") { - resourceResolutionDBService.write(blueprintName, - blueprintVersion, - resKey, - resId, - resType, - templatePrefix, - resourceAssignment) - } - - private fun createRA(prefix: String): ResourceAssignment { - val property = PropertyDefinition() - property.value = "value$prefix".asJsonPrimitive() - - val resourceAssignment = ResourceAssignment() - resourceAssignment.name = prefix - resourceAssignment.dictionaryName = "dd$prefix" - resourceAssignment.dictionarySource = "source$prefix" - resourceAssignment.version = 2 - resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS - resourceAssignment.property = property - return resourceAssignment - } - - private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) { - Assert.assertEquals(JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(), - resourceResolution.value) - Assert.assertEquals(resourceAssignment.status, resourceResolution.status) - Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource) - Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName) - Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion) - Assert.assertEquals(resourceAssignment.name, resourceResolution.name) - Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion) - Assert.assertEquals(blueprintName, resourceResolution.blueprintName) - - } -} \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceControllerTest.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceControllerTest.kt new file mode 100644 index 000000000..85ac7bddd --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceControllerTest.kt @@ -0,0 +1,235 @@ +/* + * Copyright © 2019 Bell Canada. + * + * 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. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.resource.api + +import kotlinx.coroutines.runBlocking +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.autoconfigure.security.SecurityProperties +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest +import org.springframework.context.annotation.ComponentScan +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource +import org.springframework.test.context.junit4.SpringRunner +import org.springframework.test.web.reactive.server.WebTestClient +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants + + +@RunWith(SpringRunner::class) +@WebFluxTest +@ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class]) +@ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"]) +@TestPropertySource(locations = ["classpath:application-test.properties"]) +class ResourceControllerTest { + + private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString()) + + @Autowired + lateinit var resourceResolutionDBService: ResourceResolutionDBService + @Autowired + lateinit var webTestClient: WebTestClient + + val blueprintName = "baseconfiguration" + val blueprintVersion = "1.0.0" + val templatePrefix = "activate" + + @Test + fun `ping return Success`() { + runBlocking { + webTestClient.get().uri("/api/v1/resources/health-check") + .exchange() + .expectStatus().isOk + .expectBody() + .equals("Success") + } + } + + @Test + fun getAllFromResolutionKeyTest() { + + val resolutionKey = "1" + val ra1 = createRA("bob") + val ra2 = createRA("dylan") + + runBlocking { + + store(ra1, resKey = resolutionKey) + store(ra2, resKey = resolutionKey) + + webTestClient + .get() + .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey") + .exchange() + .expectStatus().isOk + .expectBody() + .consumeWith { + val json = String(it.responseBody!!) + val typeFactory = JacksonUtils.objectMapper.typeFactory + val list: List = JacksonUtils.objectMapper.readValue(json, + typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)) + Assert.assertEquals(2, list.size) + assertEqual(ra1, list[0]) + assertEqual(ra1, list[0]) + } + } + } + + @Test + fun getAllFromFromResourceTypeAndIdTest() { + + val resourceId = "1" + val resourceType = "ServiceInstance" + val ra1 = createRA("bob") + val ra2 = createRA("dylan") + + runBlocking { + + store(ra1, resId = resourceId, resType = resourceType) + store(ra2, resId = resourceId, resType = resourceType) + + webTestClient + .get() + .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId") + .exchange() + .expectStatus().isOk + .expectBody() + .consumeWith { + val json = String(it.responseBody!!) + val typeFactory = JacksonUtils.objectMapper.typeFactory + val list: List = JacksonUtils.objectMapper.readValue(json, + typeFactory.constructCollectionType(List::class.java, ResourceResolution::class.java)) + Assert.assertEquals(2, list.size) + assertEqual(ra1, list[0]) + assertEqual(ra1, list[0]) + } + } + } + + + @Test + fun getAllFromMissingParamTest() { + runBlocking { + webTestClient + .get() + .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion") + .exchange() + .expectStatus().is4xxClientError + .expectBody() + .consumeWith { + val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java) + Assert.assertEquals("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.", + r.message) + } + } + } + + @Test + fun getAllFromWrongInputTest() { + runBlocking { + webTestClient + .get() + .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1") + .exchange() + .expectStatus().is4xxClientError + .expectBody() + .consumeWith { + val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java) + Assert.assertEquals("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.", + r.message) + } + } + } + + @Test + fun getOneFromResolutionKeyTest() { + val resolutionKey = "3" + val ra = createRA("joe") + runBlocking { + store(ra, resKey = resolutionKey) + } + runBlocking { + webTestClient.get() + .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe") + .exchange() + .expectStatus().isOk + .expectBody() + .consumeWith { + val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java) + assertEqual(ra, r) + } + } + } + + @Test + fun getOneFromResolutionKey404Test() { + val resolutionKey = "3" + runBlocking { + webTestClient.get() + .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist") + .exchange() + .expectStatus().is4xxClientError + .expectBody() + } + } + + private suspend fun store(resourceAssignment: ResourceAssignment, resKey: String = "", resId: String = "", + resType: String = "") { + resourceResolutionDBService.write(blueprintName, + blueprintVersion, + resKey, + resId, + resType, + templatePrefix, + resourceAssignment) + } + + private fun createRA(prefix: String): ResourceAssignment { + val property = PropertyDefinition() + property.value = "value$prefix".asJsonPrimitive() + + val resourceAssignment = ResourceAssignment() + resourceAssignment.name = prefix + resourceAssignment.dictionaryName = "dd$prefix" + resourceAssignment.dictionarySource = "source$prefix" + resourceAssignment.version = 2 + resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS + resourceAssignment.property = property + return resourceAssignment + } + + private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) { + Assert.assertEquals(JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(), + resourceResolution.value) + Assert.assertEquals(resourceAssignment.status, resourceResolution.status) + Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource) + Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName) + Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion) + Assert.assertEquals(resourceAssignment.name, resourceResolution.name) + Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion) + Assert.assertEquals(blueprintName, resourceResolution.blueprintName) + + } +} \ No newline at end of file -- cgit 1.2.3-korg