From a670176caac46fb68ef0a8ced0160c980f4cce05 Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Wed, 7 Feb 2024 17:07:25 +0000 Subject: [PATCH] Correct use of Spock setup and cleanup methods (no need for @Before @After) - unfortunately we copied the bad test code for logging about 4 times alraedy... - simplified setup for WatchdogSchedulingConfigurerSpec Issue-ID:CPS-465 Signed-off-by: ToineSiebelink Change-Id: I8d068541263272fdf99a6e30d365d85e31726432 --- .../rest/executor/CpsNcmpTaskExecutorSpec.groovy | 6 +----- .../CmSubscriptionNcmpInEventConsumerSpec.groovy | 12 ++++------- .../impl/inventory/sync/ModuleSyncTasksSpec.groovy | 15 +++++--------- .../config/WatchdogSchedulingConfigurerSpec.groovy | 24 +++------------------- 4 files changed, 13 insertions(+), 44 deletions(-) diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/executor/CpsNcmpTaskExecutorSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/executor/CpsNcmpTaskExecutorSpec.groovy index 5f62aca68..010eda964 100644 --- a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/executor/CpsNcmpTaskExecutorSpec.groovy +++ b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/executor/CpsNcmpTaskExecutorSpec.groovy @@ -24,8 +24,6 @@ import ch.qos.logback.classic.Level import ch.qos.logback.classic.Logger import ch.qos.logback.classic.spi.ILoggingEvent import ch.qos.logback.core.read.ListAppender -import org.junit.jupiter.api.AfterEach -import org.junit.jupiter.api.BeforeEach import org.slf4j.LoggerFactory import spock.lang.Specification import spock.util.concurrent.PollingConditions @@ -36,14 +34,12 @@ class CpsNcmpTaskExecutorSpec extends Specification { def logger = Spy(ListAppender) def enoughTime = 100 - @BeforeEach void setup() { ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).addAppender(logger) logger.start() } - @AfterEach - void teardown() { + void cleanup() { ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).detachAndStopAllAppenders() } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionNcmpInEventConsumerSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionNcmpInEventConsumerSpec.groovy index 57e77eba3..44d6eb6a0 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionNcmpInEventConsumerSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionNcmpInEventConsumerSpec.groovy @@ -28,8 +28,6 @@ import com.fasterxml.jackson.databind.ObjectMapper import io.cloudevents.CloudEvent import io.cloudevents.core.builder.CloudEventBuilder import org.apache.kafka.clients.consumer.ConsumerRecord -import org.junit.jupiter.api.AfterEach -import org.junit.jupiter.api.BeforeEach import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.client_to_ncmp.CmSubscriptionNcmpInEvent import org.onap.cps.ncmp.utils.TestUtils @@ -50,15 +48,13 @@ class CmSubscriptionNcmpInEventConsumerSpec extends MessagingBaseSpec { @Autowired ObjectMapper objectMapper - @BeforeEach void setup() { - ((Logger) LoggerFactory.getLogger(CmSubscriptionNcmpInEventConsumer.class)).addAppender(logger); - logger.start(); + ((Logger) LoggerFactory.getLogger(CmSubscriptionNcmpInEventConsumer.class)).addAppender(logger) + logger.start() } - @AfterEach - void teardown() { - ((Logger) LoggerFactory.getLogger(CmSubscriptionNcmpInEventConsumer.class)).detachAndStopAllAppenders(); + void cleanup() { + ((Logger) LoggerFactory.getLogger(CmSubscriptionNcmpInEventConsumer.class)).detachAndStopAllAppenders() } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncTasksSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncTasksSpec.groovy index 3bdac1855..ece2d9f86 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncTasksSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncTasksSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation + * Copyright (C) 2022-2024 Nordix Foundation * Modifications Copyright (C) 2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,7 +21,6 @@ package org.onap.cps.ncmp.api.impl.inventory.sync - import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_SYNC_FAILED import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_UPGRADE_FAILED @@ -32,8 +31,6 @@ import ch.qos.logback.core.read.ListAppender import com.hazelcast.config.Config import com.hazelcast.instance.impl.HazelcastInstanceFactory import com.hazelcast.map.IMap -import org.junit.jupiter.api.AfterEach -import org.junit.jupiter.api.BeforeEach import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle import org.onap.cps.ncmp.api.impl.inventory.CmHandleState @@ -50,15 +47,13 @@ class ModuleSyncTasksSpec extends Specification { def logger = Spy(ListAppender) - @BeforeEach void setup() { - ((Logger) LoggerFactory.getLogger(ModuleSyncTasks.class)).addAppender(logger); - logger.start(); + ((Logger) LoggerFactory.getLogger(ModuleSyncTasks.class)).addAppender(logger) + logger.start() } - @AfterEach - void teardown() { - ((Logger) LoggerFactory.getLogger(ModuleSyncTasks.class)).detachAndStopAllAppenders(); + void cleanup() { + ((Logger) LoggerFactory.getLogger(ModuleSyncTasks.class)).detachAndStopAllAppenders() } def mockInventoryPersistence = Mock(InventoryPersistence) diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/config/WatchdogSchedulingConfigurerSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/config/WatchdogSchedulingConfigurerSpec.groovy index 7760b39cb..f37d5a41e 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/config/WatchdogSchedulingConfigurerSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/config/WatchdogSchedulingConfigurerSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation + * Copyright (C) 2022-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. @@ -20,35 +20,17 @@ package org.onap.cps.ncmp.api.impl.inventory.sync.config -import org.junit.jupiter.api.AfterEach -import org.junit.jupiter.api.BeforeEach -import org.onap.cps.ncmp.api.impl.inventory.sync.config.WatchdogSchedulingConfigurer import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest -import org.springframework.context.ConfigurableApplicationContext import org.springframework.test.context.ContextConfiguration import spock.lang.Specification @SpringBootTest -@ContextConfiguration(classes = [ConfigurableApplicationContext, WatchdogSchedulingConfigurer]) +@ContextConfiguration(classes = [WatchdogSchedulingConfigurer]) class WatchdogSchedulingConfigurerSpec extends Specification { @Autowired - private ConfigurableApplicationContext applicationContext; - - def watchdogSchedulingConfigurer; - - @BeforeEach - void setup() { - watchdogSchedulingConfigurer = (WatchdogSchedulingConfigurer) applicationContext.getBean("watchdogSchedulingConfigurer") - } - - @AfterEach - void tearDown() { - if (applicationContext != null) { - applicationContext.close() - } - } + WatchdogSchedulingConfigurer watchdogSchedulingConfigurer def 'Validate watchdog scheduling configuration'() { given: 'task scheduler configuration properties are loaded as map' -- 2.16.6