Eliminate blueprint for openflowplugins-impl karaf commands 14/110714/6
authorRuslan Kashapov <ruslan.kashapov@pantheon.tech>
Tue, 12 Mar 2024 15:29:03 +0000 (17:29 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 22 May 2024 11:33:39 +0000 (13:33 +0200)
Karaf commands are refactored to use Karaf-4 new-style definitions.
We do not use karaf-services-maven-plugin, as we justs need the
Karaf-Commands directive and not all the complexity it brings to the
table.

While we are touching tests, convert them to JUnit5.

JIRA: OPNFLWPLUG-1127
Change-Id: Ie9092226457c40f6d0fcd22c39657875b70e8975
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
15 files changed:
openflowplugin-impl/pom.xml
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/karaf/ClearStatsCommandProvider.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/karaf/ResetEventTimesComandProvider.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/karaf/ResetSessionStatsComandProvider.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/karaf/ShowEventTimesComandProvider.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/karaf/ShowSessionStatsCommandProvider.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/karaf/ShowStatsCommandProvider.java
openflowplugin-impl/src/main/resources/OSGI-INF/blueprint/commands.xml [deleted file]
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/karaf/AbstractKarafTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/karaf/ClearStatsCommandProviderTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/karaf/ResetEventTimesComandProviderTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/karaf/ResetSessionStatsComandProviderTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/karaf/ShowEventTimesComandProviderTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/karaf/ShowSessionStatsCommandProviderTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/karaf/ShowStatsCommandProviderTest.java

index 15ff57784e50496d727240159a2e503c5606ee4e..5b8fc1f46a05ebd1229611e7c15c3ae9d086decd 100644 (file)
 
     <dependencies>
         <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.framework</artifactId>
+            <groupId>com.github.spotbugs</groupId>
+            <artifactId>spotbugs-annotations</artifactId>
+            <optional>true</optional>
         </dependency>
         <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.service.cm</artifactId>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.guicedee.services</groupId>
+            <artifactId>javax.inject</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>jakarta.annotation</groupId>
+            <artifactId>jakarta.annotation-api</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.core</artifactId>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>org.opendaylight.openflowplugin</groupId>
             <groupId>org.opendaylight.openflowplugin</groupId>
             <artifactId>openflowplugin-extension-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.opendaylight.openflowplugin</groupId>
+            <artifactId>openflowplugin-common</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.opendaylight.openflowplugin.model</groupId>
             <artifactId>model-flow-base</artifactId>
             <artifactId>openflow-protocol-spi</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.apache.karaf.shell</groupId>
-            <artifactId>org.apache.karaf.shell.console</artifactId>
-            <scope>provided</scope>
-        </dependency>
-        <dependency>
-            <groupId>com.google.guava</groupId>
-            <artifactId>guava</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.commons</groupId>
-            <artifactId>commons-lang3</artifactId>
+            <groupId>${project.groupId}.openflowjava</groupId>
+            <artifactId>openflowjava-util</artifactId>
         </dependency>
         <dependency>
-            <groupId>jakarta.annotation</groupId>
-            <artifactId>jakarta.annotation-api</artifactId>
-            <optional>true</optional>
+            <groupId>${project.groupId}.openflowjava</groupId>
+            <artifactId>openflow-protocol-impl</artifactId>
         </dependency>
         <dependency>
-            <groupId>com.github.spotbugs</groupId>
-            <artifactId>spotbugs-annotations</artifactId>
-            <optional>true</optional>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.framework</artifactId>
         </dependency>
         <dependency>
-            <groupId>com.guicedee.services</groupId>
-            <artifactId>javax.inject</artifactId>
-            <optional>true</optional>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.cm</artifactId>
         </dependency>
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.service.component.annotations</artifactId>
         </dependency>
-        <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-simple</artifactId>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>${project.groupId}.openflowjava</groupId>
-            <artifactId>openflowjava-util</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>${project.groupId}.openflowjava</groupId>
-            <artifactId>openflow-protocol-impl</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.opendaylight.openflowplugin</groupId>
-            <artifactId>openflowplugin-common</artifactId>
-        </dependency>
     </dependencies>
 
     <build>
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
                 <extensions>true</extensions>
+                 <configuration>
+                     <instructions>
+                         <Karaf-Commands>org.opendaylight.openflowplugin.impl.karaf</Karaf-Commands>
+                     </instructions>
+                </configuration>
             </plugin>
         </plugins>
     </build>
index 86aa56274b61583873496d49270b39223c88cac9..b88a5e63a1789e59cb23548cc390816794480a21 100644 (file)
@@ -5,25 +5,27 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
-import java.io.PrintStream;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Reference;
-import org.apache.karaf.shell.commands.Command;
-import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.Session;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
 
 @Command(scope = "ofp", name = "clearStats", description = "Clear openflow statistics.")
-public class ClearStatsCommandProvider extends OsgiCommandSupport {
+@Service
+public class ClearStatsCommandProvider implements Action {
+    @Reference
+    Session session;
     @Reference
     MessageIntelligenceAgency messageIntelligenceAgency;
 
     @Override
-    protected Object doExecute() {
+    public Object execute() {
         messageIntelligenceAgency.resetStatistics();
-        PrintStream out = session.getConsole();
-        out.print("Openflow plugin statistics cleaned.\n");
+        session.getConsole().println("Openflow plugin statistics cleaned.");
         return null;
     }
 }
index 7cd5adb3e0d23bbbd192b5c59cb3ab4aaa7edc43..429858c3842fce9d8f8de1fac9477977354e15bc 100644 (file)
@@ -5,25 +5,28 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
-import java.io.PrintStream;
-import org.apache.karaf.shell.commands.Command;
-import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.Session;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
 
 /**
  * Created by Martin Bobak &lt;mbobak@cisco.com&gt; on 28.5.2015.
  */
 @Command(scope = "ofp", name = "reset-time-counters", description = "Resets events time counters.")
-public class ResetEventTimesComandProvider extends OsgiCommandSupport {
+@Service
+public class ResetEventTimesComandProvider implements Action {
+    @Reference
+    Session session;
 
     @Override
-    protected Object doExecute() {
-        PrintStream out = session.getConsole();
+    public Object execute() {
         EventsTimeCounter.resetAllCounters();
-        out.print("Events time counters reset.\n");
+        session.getConsole().println("Events time counters reset.");
         return null;
     }
 }
index db383c6af413e7cacee7e5f2d44ef330eb0f216b..f1da8c6d156f18f579570ee6520b7eb39c241e17 100644 (file)
@@ -5,25 +5,28 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
-import java.io.PrintStream;
-import org.apache.karaf.shell.commands.Command;
-import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.Session;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
 
 /**
  * Created by Martin Bobak &lt;mbobak@cisco.com&gt; on 28.5.2015.
  */
 @Command(scope = "ofp", name = "reset-session-stats", description = "Resets session statistics counters.")
-public class ResetSessionStatsComandProvider extends OsgiCommandSupport {
+@Service
+public class ResetSessionStatsComandProvider implements Action {
+    @Reference
+    Session session;
 
     @Override
-    protected Object doExecute() {
-        PrintStream out = session.getConsole();
+    public Object execute() {
         SessionStatistics.resetAllCounters();
-        out.print("Session statistics counters reset.\n");
+        session.getConsole().println("Session statistics counters reset.");
         return null;
     }
 }
index 1374857800d1fe456b9b308c50c58ac4bffca599..99b7a0d4ba3ba37d662ae3f3f6351eda376f175a 100644 (file)
@@ -5,31 +5,28 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
-import java.io.PrintStream;
-import java.util.List;
-import org.apache.karaf.shell.commands.Command;
-import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.Session;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
 
 /**
  * Created by Martin Bobak &lt;mbobak@cisco.com&gt; on 28.5.2015.
  */
 @Command(scope = "ofp", name = "show-time-counters", description = "Shows time counts for events.")
-public class ShowEventTimesComandProvider extends OsgiCommandSupport {
+@Service
+public class ShowEventTimesComandProvider implements Action {
+    @Reference
+    Session session;
 
     @Override
-    protected Object doExecute() {
-        PrintStream out = session.getConsole();
-        final List<String> statistics = EventsTimeCounter.provideTimes();
-        final StringBuilder result = new StringBuilder();
-        for (String line : statistics) {
-            result.append(line);
-            result.append("\n");
-        }
-        out.print(result.toString());
+    public Object execute() {
+        final var console = session.getConsole();
+        EventsTimeCounter.provideTimes().forEach(console::println);
         return null;
     }
 }
index 0187ec61d51577f17cb00cf9418ea2b2826e5d04..e97dcc2cf11bee63d82ce0a46480f5f02cd7178f 100644 (file)
@@ -5,13 +5,13 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
-import java.io.PrintStream;
-import java.util.List;
-import org.apache.karaf.shell.commands.Command;
-import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.Session;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
 
 /**
@@ -19,18 +19,15 @@ import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatis
  */
 
 @Command(scope = "ofp", name = "show-session-stats", description = "Show session statistics.")
-public class ShowSessionStatsCommandProvider extends OsgiCommandSupport {
+@Service
+public class ShowSessionStatsCommandProvider implements Action {
+    @Reference
+    Session session;
 
     @Override
-    protected Object doExecute() {
-        PrintStream out = session.getConsole();
-        final List<String> statistics = SessionStatistics.provideStatistics();
-        final StringBuilder result = new StringBuilder();
-        for (String line : statistics) {
-            result.append(line);
-            result.append("\n");
-        }
-        out.print(result.toString());
+    public Object execute() {
+        final var console = session.getConsole();
+        SessionStatistics.provideStatistics().forEach(console::println);
         return null;
     }
 }
index 4042b4cea297d849bd9928ee0b0da706d976882a..37bb9f4be1db8f298e3416f6e945b859509c89fb 100644 (file)
@@ -5,31 +5,27 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
-import java.io.PrintStream;
-import java.util.List;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Reference;
-import org.apache.karaf.shell.commands.Command;
-import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.Session;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
 
 @Command(scope = "ofp", name = "showStats", description = "Show openflow statistics.")
-public class ShowStatsCommandProvider extends OsgiCommandSupport {
+@Service
+public class ShowStatsCommandProvider implements Action {
+    @Reference
+    Session session;
     @Reference
     MessageIntelligenceAgency messageIntelligenceAgency;
 
     @Override
-    protected Object doExecute() {
-        PrintStream out = session.getConsole();
-        final List<String> statistics = messageIntelligenceAgency.provideIntelligence();
-        final StringBuilder result = new StringBuilder();
-        for (String line : statistics) {
-            result.append(line);
-            result.append("\n");
-        }
-        out.print(result.toString());
+    public Object execute() {
+        final var console = session.getConsole();
+        messageIntelligenceAgency.provideIntelligence().forEach(console::println);
         return null;
     }
 }
diff --git a/openflowplugin-impl/src/main/resources/OSGI-INF/blueprint/commands.xml b/openflowplugin-impl/src/main/resources/OSGI-INF/blueprint/commands.xml
deleted file mode 100644 (file)
index 0b8e99a..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  ~ Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
-  ~
-  ~ This program and the accompanying materials are made available under the
-  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
-  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
-  -->
-
-<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
-    <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.0.0">
-        <command name="ofp/showStats">
-            <action class="org.opendaylight.openflowplugin.impl.karaf.ShowStatsCommandProvider"/>
-        </command>
-        <command name="ofp/clearStats">
-            <action class="org.opendaylight.openflowplugin.impl.karaf.ClearStatsCommandProvider"/>
-        </command>
-        <command name="ofp/show-time-counters">
-            <action class="org.opendaylight.openflowplugin.impl.karaf.ShowEventTimesComandProvider"/>
-        </command>
-        <command name="ofp/show-session-stats">
-            <action class="org.opendaylight.openflowplugin.impl.karaf.ShowSessionStatsCommandProvider"/>
-        </command>
-        <command name="ofp/reset-time-counters">
-            <action class="org.opendaylight.openflowplugin.impl.karaf.ResetEventTimesComandProvider"/>
-        </command>
-        <command name="ofp/reset-session-stats">
-            <action class="org.opendaylight.openflowplugin.impl.karaf.ResetSessionStatsComandProvider"/>
-        </command>
-    </command-bundle>
-</blueprint>
index d47c0e1a38dea01b385af73e7b510316370a671b..04a5c22625d89a57629083516c2ab8b913812c01 100644 (file)
@@ -5,42 +5,56 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.doReturn;
+
 import java.io.PrintStream;
 import java.util.List;
 import java.util.function.Function;
-import org.apache.felix.service.command.CommandSession;
-import org.junit.Before;
-import org.junit.runner.RunWith;
+import org.apache.karaf.shell.api.console.Session;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
 
 /**
  * Created by mirehak on 7/29/15.
  */
-@RunWith(MockitoJUnitRunner.class)
-public abstract class AbstractKarafTest {
+@ExtendWith(MockitoExtension.class)
+abstract class AbstractKarafTest {
     @Mock
-    protected CommandSession cmdSession;
+    protected Session session;
     @Mock
     protected PrintStream console;
 
-    @Before
-    public void setUp() {
-        Mockito.when(cmdSession.getConsole()).thenReturn(console);
-        doSetUp();
+    @BeforeEach
+    void beforeEach() {
+        doReturn(console).when(session).getConsole();
+        doBeforeEach();
+    }
+
+    protected abstract void doBeforeEach();
+
+    protected static void assertNoActivity(final List<String> allStatLines,
+            final Function<String, Boolean> checkFunction) {
+        assertTrue(checkNoActivity(allStatLines, checkFunction));
     }
 
-    public abstract void doSetUp();
+    protected static void assertHasActivity(final List<String> allStatLines,
+            final Function<String, Boolean> checkFunction) {
+        assertFalse(checkNoActivity(allStatLines, checkFunction));
+    }
 
-    public static boolean checkNoActivity(List<String> allStatLines, Function<String, Boolean> checkFunction) {
-        boolean noActivity = true;
-        for (String statLine : allStatLines) {
-            noActivity &= checkFunction.apply(statLine);
+    private static boolean checkNoActivity(final List<String> allStatLines,
+            final Function<String, Boolean> checkFunction) {
+        for (var statLine : allStatLines) {
+            if (!checkFunction.apply(statLine)) {
+                return false;
+            }
         }
-        return noActivity;
+        return true;
     }
 }
index 3d24436290eef02ac47a1558b11a2d6bdb834d28..c797fd2ec4a707471a1fb48c1b873ae36a859ef3 100644 (file)
@@ -7,15 +7,12 @@
  */
 package org.opendaylight.openflowplugin.impl.karaf;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
 
 import java.util.function.Function;
-import org.junit.After;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
@@ -24,46 +21,41 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 /**
  * Test for {@link ClearStatsCommandProvider}.
  */
-public class ClearStatsCommandProviderTest extends AbstractKarafTest {
+class ClearStatsCommandProviderTest extends AbstractKarafTest {
     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION =
         input -> input.endsWith(": no activity detected");
 
-    private final MessageIntelligenceAgency mi5 = new MessageIntelligenceAgencyImpl();
-    private final ClearStatsCommandProvider clearStatsCommandProvider = new ClearStatsCommandProvider();
+    private final MessageIntelligenceAgency messageIntelligenceAgency = new MessageIntelligenceAgencyImpl();
 
-    @Override
-    public void doSetUp() {
-        clearStatsCommandProvider.messageIntelligenceAgency = mi5;
-        when(cmdSession.getConsole()).thenReturn(console);
-    }
+    @InjectMocks
+    private ClearStatsCommandProvider clearStatsCommand;
 
-    @After
-    public void tearDown() {
-        verify(console).print(anyString());
-        mi5.resetStatistics();
+    @Override
+    protected void doBeforeEach() {
+        clearStatsCommand.messageIntelligenceAgency = messageIntelligenceAgency;
+        messageIntelligenceAgency.resetStatistics();
+        assertNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ClearStatsCommandProvider#doExecute()} when no stats were touched before.
+     * Test for {@link ClearStatsCommandProvider#execute()} when no stats were touched before.
      */
     @Test
-    public void testDoExecute_clean() throws Exception {
-        assertTrue(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
-        clearStatsCommandProvider.execute(cmdSession);
-
-        assertTrue(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
+    void clearNoActivity() {
+        clearStatsCommand.execute();
+        verify(console).println(anyString());
+        assertNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ClearStatsCommandProvider#doExecute()} when stats were touched before.
+     * Test for {@link ClearStatsCommandProvider#execute()} when stats were touched before.
      */
     @Test
-    public void testDoExecute_dirty() throws Exception {
-        assertTrue(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
-        mi5.spyMessage(OfHeader.class, MessageSpy.StatisticsGroup.FROM_SWITCH);
-        assertFalse(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
-
-        clearStatsCommandProvider.execute(cmdSession);
-        assertTrue(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
+    void clearHavingActivity() {
+        messageIntelligenceAgency.spyMessage(OfHeader.class, MessageSpy.StatisticsGroup.FROM_SWITCH);
+        assertHasActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION);
+        clearStatsCommand.execute();
+        verify(console).println(anyString());
+        assertNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 }
index 02315e81251c7ac77b638aa47a49bc90bcc0dd02..048873ae5d9e8a1e23ae34a420a4896d53c77a45 100644 (file)
@@ -5,62 +5,54 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.verify;
 
 import java.util.function.Function;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Test;
-import org.mockito.Mockito;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
 
 /**
  * Test for {@link  ResetEventTimesComandProvider}.
  */
-public class ResetEventTimesComandProviderTest extends AbstractKarafTest {
-
-    private ResetEventTimesComandProvider resetEventTimesComandProvider;
+class ResetEventTimesComandProviderTest extends AbstractKarafTest {
     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION = String::isEmpty;
 
-    @Override
-    public void doSetUp() {
-        resetEventTimesComandProvider = new ResetEventTimesComandProvider();
-        EventsTimeCounter.resetAllCounters();
-    }
+    @InjectMocks
+    private ResetEventTimesComandProvider resetEventTimesCommand;
 
-    @After
-    public void tearDown() {
-        Mockito.verify(console).print(anyString());
+    @Override
+    protected void doBeforeEach() {
         EventsTimeCounter.resetAllCounters();
+        assertNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ResetEventTimesComandProvider#doExecute()} when no stats were touched before.
+     * Test for {@link ResetEventTimesComandProvider#execute()} when no stats were touched before.
      */
     @Test
-    public void testDoExecute_clean() throws Exception {
-        Assert.assertTrue(checkNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION));
-        resetEventTimesComandProvider.execute(cmdSession);
-        Assert.assertTrue(checkNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION));
+    void resetNoActivity() {
+        resetEventTimesCommand.execute();
+        verify(console).println(anyString());
+        assertNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ResetEventTimesComandProvider#doExecute()} when stats were touched before.
+     * Test for {@link ResetEventTimesComandProvider#execute()} when stats were touched before.
      */
     @Test
-    public void testDoExecute_dirty() throws Exception {
-        final EventIdentifier dummyEvent = new EventIdentifier("junit", "junitDevice");
-        Assert.assertTrue(checkNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION));
-
+    void resetHavingActivity() {
+        final var dummyEvent = new EventIdentifier("junit", "junitDevice");
         EventsTimeCounter.markStart(dummyEvent);
         EventsTimeCounter.markEnd(dummyEvent);
-        Assert.assertFalse(checkNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION));
+        assertHasActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION);
 
-        resetEventTimesComandProvider.execute(cmdSession);
-        Assert.assertTrue(checkNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION));
+        resetEventTimesCommand.execute();
+        verify(console).println(anyString());
+        assertNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 }
\ No newline at end of file
index 754988938e45cac605955f72ee255100d3df11f7..3565fc9cb8394c027c7227abc39829467142c14d 100644 (file)
@@ -5,60 +5,52 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.verify;
 
 import java.util.function.Function;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Test;
-import org.mockito.Mockito;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
 
 /**
  * Test for {@link ResetSessionStatsComandProvider}.
  */
-public class ResetSessionStatsComandProviderTest extends AbstractKarafTest {
-
-    private ResetSessionStatsComandProvider resetSessionStatsComandProvider;
+class ResetSessionStatsComandProviderTest extends AbstractKarafTest {
+    @InjectMocks
+    private ResetSessionStatsComandProvider resetSessionStatsCommand;
     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION = String::isEmpty;
 
     @Override
-    public void doSetUp() {
-        resetSessionStatsComandProvider = new ResetSessionStatsComandProvider();
-        SessionStatistics.resetAllCounters();
-    }
-
-    @After
-    public void tearDown() {
-        Mockito.verify(console).print(anyString());
+    protected void doBeforeEach() {
         SessionStatistics.resetAllCounters();
+        assertNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ResetSessionStatsComandProvider#doExecute()} when no stats were touched before.
+     * Test for {@link ResetSessionStatsComandProvider#execute()} when no stats were touched before.
      */
     @Test
-    public void testDoExecute_clean() throws Exception {
-        Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
-        resetSessionStatsComandProvider.execute(cmdSession);
-        Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
+    void resetNoActivity() {
+        resetSessionStatsCommand.execute();
+        verify(console, atLeastOnce()).println(anyString());
+        assertNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ResetSessionStatsComandProvider#doExecute()} when stats were touched before.
+     * Test for {@link ResetSessionStatsComandProvider#execute()} when stats were touched before.
      */
     @Test
-    public void testDoExecute_dirty() throws Exception {
+    void resetHavingActivity() {
         final String dummySessionId = "junitSessionId";
-        Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
-
         SessionStatistics.countEvent(dummySessionId, SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
-        Assert.assertFalse(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
+        assertHasActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION);
 
-        resetSessionStatsComandProvider.execute(cmdSession);
-        Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
+        resetSessionStatsCommand.execute();
+        verify(console, atLeastOnce()).println(anyString());
+        assertNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 }
\ No newline at end of file
index 69b0704d83030891127a67e1e4fdc047f52ec1a9..2cd8b0f7e3fe8f252a5b1429f8fc9308532de7e6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2024 PANTHEON.tech s.r.o. and others. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -7,61 +7,53 @@
  */
 package org.opendaylight.openflowplugin.impl.karaf;
 
+import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.contains;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
 
 import java.util.function.Function;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Test;
-import org.mockito.Mockito;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
 
 /**
  * Test for {@link ShowEventTimesComandProvider}.
  */
-public class ShowEventTimesComandProviderTest extends AbstractKarafTest {
-
-    private ShowEventTimesComandProvider showEventTimesComandProvider;
+class ShowEventTimesComandProviderTest extends AbstractKarafTest {
     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION = String::isEmpty;
 
-    @Override
+    @InjectMocks
+    private ShowEventTimesComandProvider showEventTimesCommand;
 
-    public void doSetUp() {
-        showEventTimesComandProvider = new ShowEventTimesComandProvider();
-        EventsTimeCounter.resetAllCounters();
-    }
-
-    @After
-    public void tearDown() {
+    @Override
+    protected void doBeforeEach() {
         EventsTimeCounter.resetAllCounters();
+        assertNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ShowEventTimesComandProvider#doExecute()} when no stats were touched before.
+     * Test for {@link ShowEventTimesComandProvider#execute()} when no stats were touched before.
      */
     @Test
-    public void testDoExecute_clean() throws Exception {
-        Assert.assertTrue(checkNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION));
-        showEventTimesComandProvider.execute(cmdSession);
-        Assert.assertTrue(checkNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION));
-        Mockito.verify(console).print("");
+    void showNoActivity() {
+        showEventTimesCommand.execute();
+        verify(console, never()).println(anyString());
+        assertNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ShowEventTimesComandProvider#doExecute()} when stats were touched before.
+     * Test for {@link ShowEventTimesComandProvider#execute()} when stats were touched before.
      */
     @Test
-    public void testDoExecute_dirty() throws Exception {
-        final EventIdentifier dummyEvent = new EventIdentifier("junit", "junitDevice");
-        Assert.assertTrue(checkNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION));
-
+    void showHavingActivity() {
+        final var dummyEvent = new EventIdentifier("junit", "junitDevice");
         EventsTimeCounter.markStart(dummyEvent);
         EventsTimeCounter.markEnd(dummyEvent);
-        Assert.assertFalse(checkNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION));
+        assertHasActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION);
 
-        showEventTimesComandProvider.execute(cmdSession);
-        Assert.assertFalse(checkNoActivity(EventsTimeCounter.provideTimes(), CHECK_NO_ACTIVITY_FUNCTION));
-        Mockito.verify(console).print(contains("junitDevice"));
+        showEventTimesCommand.execute();
+        verify(console).println(contains("junitDevice"));
     }
 }
\ No newline at end of file
index 5571901276b836ef86d117ba0736e6b691717863..72df5c1e50ea2d20ae1afa7cd841c8faa7bbdf28 100644 (file)
@@ -5,62 +5,55 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
+import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.contains;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
 
 import java.util.function.Function;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Test;
-import org.mockito.Mockito;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
 
 /**
  * Test for {@link ShowSessionStatsCommandProvider}.
  */
-public class ShowSessionStatsCommandProviderTest extends AbstractKarafTest {
-
-    private ShowSessionStatsCommandProvider showSessionStatsCommandProvider;
+class ShowSessionStatsCommandProviderTest extends AbstractKarafTest {
     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION = String::isEmpty;
 
-    @Override
-
-    public void doSetUp() {
-        showSessionStatsCommandProvider = new ShowSessionStatsCommandProvider();
-        SessionStatistics.resetAllCounters();
-    }
+    @InjectMocks
+    private ShowSessionStatsCommandProvider showSessionStatsCommand;
 
-    @After
-    public void tearDown() {
+    @Override
+    protected void doBeforeEach() {
         SessionStatistics.resetAllCounters();
+        assertNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ShowEventTimesComandProvider#doExecute()} when no stats were touched before.
+     * Test for {@link ShowEventTimesComandProvider#execute()} when no stats were touched before.
      */
     @Test
-    public void testDoExecute_clean() throws Exception {
-        Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
-        showSessionStatsCommandProvider.execute(cmdSession);
-        Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
-        Mockito.verify(console).print("");
+    void showNoActivity() {
+        showSessionStatsCommand.execute();
+        verify(console, never()).println(anyString());
+        assertNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ShowEventTimesComandProvider#doExecute()} when stats were touched before.
+     * Test for {@link ShowEventTimesComandProvider#execute()} when stats were touched before.
      */
     @Test
-    public void testDoExecute_dirty() throws Exception {
-        final String dummySessionId = "junitSessionId";
-        Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
-
+    void showHavingActivity() {
+        final var dummySessionId = "junitSessionId";
         SessionStatistics.countEvent(dummySessionId, SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
-        Assert.assertFalse(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
+        assertHasActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION);
 
-        showSessionStatsCommandProvider.execute(cmdSession);
-        Assert.assertFalse(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
-        Mockito.verify(console).print(contains(dummySessionId));
+        showSessionStatsCommand.execute();
+        verify(console, atLeastOnce()).println(contains(dummySessionId));
+        assertHasActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 }
\ No newline at end of file
index c7b517c0506554a3f81228035852be09f0f72dce..69a07f9eda44e41ebd9507cba76157845161f901 100644 (file)
@@ -5,17 +5,16 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.karaf;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.matches;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 
 import java.util.function.Function;
-import org.junit.After;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
@@ -24,46 +23,42 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 /**
  * Test for {@link ShowStatsCommandProvider}.
  */
-public class ShowStatsCommandProviderTest extends AbstractKarafTest {
+class ShowStatsCommandProviderTest extends AbstractKarafTest {
     private final MessageIntelligenceAgency messageIntelligenceAgency = new MessageIntelligenceAgencyImpl();
-    private final ShowStatsCommandProvider showStatsCommandProvider = new ShowStatsCommandProvider();
+
+    @InjectMocks
+    private ShowStatsCommandProvider showStatsCommand;
 
     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION =
         input -> input.endsWith(": no activity detected");
 
     @Override
-    public void doSetUp() {
-        showStatsCommandProvider.messageIntelligenceAgency = messageIntelligenceAgency;
-    }
-
-    @After
-    public void tearDown() {
-        // Pattern.DOTALL is set inline via "(?s)" at the beginning
-        verify(console).print(matches("(?s).+"));
+    protected void doBeforeEach() {
+        showStatsCommand.messageIntelligenceAgency = messageIntelligenceAgency;
         messageIntelligenceAgency.resetStatistics();
+        assertNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ShowEventTimesComandProvider#doExecute()} when no stats were touched before.
+     * Test for {@link ShowEventTimesComandProvider#execute()} when no stats were touched before.
      */
     @Test
-    public void testDoExecute_clean() throws Exception {
-        assertTrue(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
-        showStatsCommandProvider.execute(cmdSession);
-        assertTrue(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
+    void showNoActivity() {
+        showStatsCommand.execute();
+        verify(console, never()).println();
+        assertNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION);
     }
 
     /**
-     * Test for {@link ShowEventTimesComandProvider#doExecute()} when stats were touched before.
+     * Test for {@link ShowEventTimesComandProvider#execute()} when stats were touched before.
      */
     @Test
-    public void testDoExecute_dirty() throws Exception {
-        assertTrue(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
-
+    void showHavingActivity() {
         messageIntelligenceAgency.spyMessage(OfHeader.class, MessageSpy.StatisticsGroup.FROM_SWITCH);
-        assertFalse(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
+        assertHasActivity(messageIntelligenceAgency.provideIntelligence(),CHECK_NO_ACTIVITY_FUNCTION);
 
-        showStatsCommandProvider.execute(cmdSession);
-        assertFalse(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
+        showStatsCommand.execute();
+        verify(console, atLeastOnce()).println(anyString());
+        assertHasActivity(messageIntelligenceAgency.provideIntelligence(),CHECK_NO_ACTIVITY_FUNCTION);
     }
 }