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>
<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>
* 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;
}
}
* 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 <mbobak@cisco.com> 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;
}
}
* 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 <mbobak@cisco.com> 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;
}
}
* 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 <mbobak@cisco.com> 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;
}
}
* 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;
/**
*/
@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;
}
}
* 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;
}
}
+++ /dev/null
-<?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>
* 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;
}
}
*/
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;
/**
* 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);
}
}
* 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
* 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
/*
- * 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,
*/
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
* 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
* 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;
/**
* 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);
}
}