From ebdd198e47b2da08a2aa470177a3baa4a2cf1c4c Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Wed, 27 Nov 2019 19:42:17 -0500 Subject: Optimize spring data JPA UT. Test case based database configuration, so that we can define what repositories and entities can be used for testing. Issue-ID: CCSDK-1735 Signed-off-by: Brinda Santh Change-Id: I4f8a7eb4ed47fec9ab17eb9552648ebd0de01236 --- .../db/ResourceConfigSnapshotRepository.kt | 2 + .../snapshots/db/ResourceConfigSnapshotService.kt | 10 ++-- .../ComponentConfigSnapshotsExecutorTest.kt | 69 +++++++++++++--------- .../config/snapshots/TestDatabaseConfiguration.kt | 58 ++++++++++++++++++ 4 files changed, 105 insertions(+), 34 deletions(-) create mode 100644 ms/blueprintsprocessor/functions/config-snapshots/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/TestDatabaseConfiguration.kt (limited to 'ms/blueprintsprocessor/functions/config-snapshots') diff --git a/ms/blueprintsprocessor/functions/config-snapshots/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/db/ResourceConfigSnapshotRepository.kt b/ms/blueprintsprocessor/functions/config-snapshots/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/db/ResourceConfigSnapshotRepository.kt index cb7467b60..e1806438b 100644 --- a/ms/blueprintsprocessor/functions/config-snapshots/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/db/ResourceConfigSnapshotRepository.kt +++ b/ms/blueprintsprocessor/functions/config-snapshots/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/db/ResourceConfigSnapshotRepository.kt @@ -16,6 +16,7 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository import javax.transaction.Transactional /** @@ -24,6 +25,7 @@ import javax.transaction.Transactional * @author Serge Simard * @version 1.0 */ +@Repository interface ResourceConfigSnapshotRepository : JpaRepository { fun findByResourceIdAndResourceTypeAndStatus( diff --git a/ms/blueprintsprocessor/functions/config-snapshots/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/db/ResourceConfigSnapshotService.kt b/ms/blueprintsprocessor/functions/config-snapshots/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/db/ResourceConfigSnapshotService.kt index fcbc15702..9260b79e8 100644 --- a/ms/blueprintsprocessor/functions/config-snapshots/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/db/ResourceConfigSnapshotService.kt +++ b/ms/blueprintsprocessor/functions/config-snapshots/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/db/ResourceConfigSnapshotService.kt @@ -32,7 +32,7 @@ import java.util.UUID * @version 1.0 */ @Service -class ResourceConfigSnapshotService(private val repository: ResourceConfigSnapshotRepository) { +open class ResourceConfigSnapshotService(private val resourceConfigSnapshotRepository: ResourceConfigSnapshotRepository) { private val log = LoggerFactory.getLogger(ResourceConfigSnapshotService::class.toString()) @@ -42,7 +42,7 @@ class ResourceConfigSnapshotService(private val repository: ResourceConfigSnapsh status: ResourceConfigSnapshot.Status = RUNNING ): String = withContext(Dispatchers.IO) { - repository.findByResourceIdAndResourceTypeAndStatus(resourceId, resourceType, status) + resourceConfigSnapshotRepository.findByResourceIdAndResourceTypeAndStatus(resourceId, resourceType, status) ?.config_snapshot ?: Strings.EMPTY } @@ -63,18 +63,18 @@ class ResourceConfigSnapshotService(private val repository: ResourceConfigSnapsh // Overwrite configuration snapshot entry of resId/resType if (resId.isNotEmpty() && resType.isNotEmpty()) { - repository.findByResourceIdAndResourceTypeAndStatus(resId, resType, status) + resourceConfigSnapshotRepository.findByResourceIdAndResourceTypeAndStatus(resId, resType, status) ?.let { log.info( "Overwriting configuration snapshot entry for resourceId=($resId), " + "resourceType=($resType), status=($status)" ) - repository.deleteByResourceIdAndResourceTypeAndStatus(resId, resType, status) + resourceConfigSnapshotRepository.deleteByResourceIdAndResourceTypeAndStatus(resId, resType, status) } } var storedSnapshot: ResourceConfigSnapshot try { - storedSnapshot = repository.saveAndFlush(resourceConfigSnapshotEntry) + storedSnapshot = resourceConfigSnapshotRepository.saveAndFlush(resourceConfigSnapshotEntry) log.info( "Stored configuration snapshot for resourceId=($resId), " + "resourceType=($resType), status=($status), " + diff --git a/ms/blueprintsprocessor/functions/config-snapshots/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/ComponentConfigSnapshotsExecutorTest.kt b/ms/blueprintsprocessor/functions/config-snapshots/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/ComponentConfigSnapshotsExecutorTest.kt index c472337ac..450da1c9f 100644 --- a/ms/blueprintsprocessor/functions/config-snapshots/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/ComponentConfigSnapshotsExecutorTest.kt +++ b/ms/blueprintsprocessor/functions/config-snapshots/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/ComponentConfigSnapshotsExecutorTest.kt @@ -23,10 +23,7 @@ import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService -import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput -import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.DIFF_JSON import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.DIFF_XML import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.OPERATION_DIFF @@ -36,11 +33,9 @@ import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db.Reso import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db.ResourceConfigSnapshotService import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive -import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.context.annotation.ComponentScan import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.TestPropertySource @@ -48,13 +43,12 @@ import org.springframework.test.context.junit4.SpringRunner @RunWith(SpringRunner::class) @ContextConfiguration( - classes = [ResourceConfigSnapshotService::class, - BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class, - BluePrintDBLibConfiguration::class, BluePrintLoadConfiguration::class] + classes = [ResourceConfigSnapshotService::class, TestDatabaseConfiguration::class] ) @TestPropertySource(locations = ["classpath:application-test.properties"]) -@ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"]) -@EnableAutoConfiguration +@ComponentScan( + basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots"] +) @Suppress("SameParameterValue") class ComponentConfigSnapshotsExecutorTest { @@ -346,9 +340,9 @@ class ComponentConfigSnapshotsExecutorTest { // then; we should get JSON-patches differences in our response property val diffJson = "[{\"op\":\"add\",\"path\":\"/system-uptime-information/last-configured-time/new-child-object\",\"value\":{\"property\":\"value\"}}," + - "{\"op\":\"replace\",\"path\":\"/system-uptime-information/system-booted-time/time-length\",\"value\":\"14:52:54\"}," + - "{\"op\":\"replace\",\"path\":\"/system-uptime-information/time-source\",\"value\":\" DNS CLOCK \"}," + - "{\"op\":\"add\",\"path\":\"/system-uptime-information/uptime-information/load-average-10\",\"value\":\"0.05\"}]" + "{\"op\":\"replace\",\"path\":\"/system-uptime-information/system-booted-time/time-length\",\"value\":\"14:52:54\"}," + + "{\"op\":\"replace\",\"path\":\"/system-uptime-information/time-source\",\"value\":\" DNS CLOCK \"}," + + "{\"op\":\"add\",\"path\":\"/system-uptime-information/uptime-information/load-average-10\",\"value\":\"0.05\"}]" assertEquals( diffJson.asJsonPrimitive(), bluePrintRuntimeService.getNodeTemplateAttributeValue( @@ -394,13 +388,13 @@ class ComponentConfigSnapshotsExecutorTest { // then; we should get XML-patches differences in our response property val diffXml = "" + - "" + - "2343" + - "34" + - "09098789" + - "2828828" + - "TEGig400-int01" + - "" + "" + + "2343" + + "34" + + "09098789" + + "2828828" + + "TEGig400-int01" + + "" assertEquals( diffXml.asJsonPrimitive(), bluePrintRuntimeService.getNodeTemplateAttributeValue( @@ -411,30 +405,47 @@ class ComponentConfigSnapshotsExecutorTest { } } - private fun preparePayload(filename: String, resId: String, resType: String, status: ResourceConfigSnapshot.Status) { + private fun preparePayload( + filename: String, + resId: String, + resType: String, + status: ResourceConfigSnapshot.Status + ) { runBlocking { - cfgSnapshotService.write(JacksonUtils.getClassPathFileContent("payload/requests/$filename"), resId, resType, status) + cfgSnapshotService.write( + JacksonUtils.getClassPathFileContent("payload/requests/$filename"), + resId, + resType, + status + ) } } private fun prepareRequestProperties(oper: String, resId: String, resType: String, optional: String = "") { cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_OPERATION] = oper.asJsonPrimitive() - cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_ID] = resId.asJsonPrimitive() - cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_TYPE] = resType.asJsonPrimitive() + cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_ID] = + resId.asJsonPrimitive() + cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_TYPE] = + resType.asJsonPrimitive() // Optional inputs - cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_DIFF_CONTENT_TYPE] = "".asJsonPrimitive() - cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_SNAPSHOT] = "".asJsonPrimitive() + cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_DIFF_CONTENT_TYPE] = + "".asJsonPrimitive() + cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_SNAPSHOT] = + "".asJsonPrimitive() cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_STATUS] = ResourceConfigSnapshot.Status.RUNNING.name.asJsonPrimitive() when (oper) { OPERATION_DIFF -> - cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_DIFF_CONTENT_TYPE] = optional.asJsonPrimitive() + cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_DIFF_CONTENT_TYPE] = + optional.asJsonPrimitive() OPERATION_STORE -> - cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_SNAPSHOT] = optional.asJsonPrimitive() + cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_SNAPSHOT] = + optional.asJsonPrimitive() OPERATION_FETCH -> - cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_STATUS] = optional.asJsonPrimitive() + cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_STATUS] = + optional.asJsonPrimitive() } } } diff --git a/ms/blueprintsprocessor/functions/config-snapshots/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/TestDatabaseConfiguration.kt b/ms/blueprintsprocessor/functions/config-snapshots/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/TestDatabaseConfiguration.kt new file mode 100644 index 000000000..96045ee07 --- /dev/null +++ b/ms/blueprintsprocessor/functions/config-snapshots/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/config/snapshots/TestDatabaseConfiguration.kt @@ -0,0 +1,58 @@ +/* + * Copyright © 2018-2019 AT&T Intellectual Property. + * + * 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.config.snapshots + +import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.db.PrimaryDataSourceProperties +import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.PrimaryDatabaseConfiguration +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Import +import org.springframework.data.jpa.repository.config.EnableJpaAuditing +import org.springframework.data.jpa.repository.config.EnableJpaRepositories +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean +import org.springframework.transaction.PlatformTransactionManager +import javax.sql.DataSource + +@Configuration +@Import(BluePrintDBLibConfiguration::class) +@EnableJpaRepositories( + basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots"], + entityManagerFactoryRef = "primaryEntityManager", + transactionManagerRef = "primaryTransactionManager" +) +@EnableJpaAuditing +open class TestDatabaseConfiguration(primaryDataSourceProperties: PrimaryDataSourceProperties) : + PrimaryDatabaseConfiguration(primaryDataSourceProperties) { + + @Bean("primaryEntityManager") + open fun primaryEntityManager(): LocalContainerEntityManagerFactoryBean { + return primaryEntityManager( + "org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots" + ) + } + + @Bean("primaryDataSource") + override fun primaryDataSource(): DataSource { + return super.primaryDataSource() + } + + @Bean("primaryTransactionManager") + override fun primaryTransactionManager(): PlatformTransactionManager { + return super.primaryTransactionManager() + } +} -- cgit 1.2.3-korg