Correct use of Spock setup and cleanup methods (no need for @Before @After) 82/137182/4
authorToineSiebelink <toine.siebelink@est.tech>
Wed, 7 Feb 2024 17:07:25 +0000 (17:07 +0000)
committerToineSiebelink <toine.siebelink@est.tech>
Thu, 8 Feb 2024 11:27:01 +0000 (11:27 +0000)
- 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 <toine.siebelink@est.tech>
Change-Id: I8d068541263272fdf99a6e30d365d85e31726432

cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/executor/CpsNcmpTaskExecutorSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionNcmpInEventConsumerSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncTasksSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/config/WatchdogSchedulingConfigurerSpec.groovy

index 5f62aca..010eda9 100644 (file)
@@ -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<ILoggingEvent>)
     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()
     }
 
index 57e77eb..44d6eb6 100644 (file)
@@ -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()
     }
 
 
index 3bdac18..ece2d9f 100644 (file)
@@ -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<ILoggingEvent>)
 
-    @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)
index 7760b39..f37d5a4 100644 (file)
@@ -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.
 
 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'