From ab3aca4b7ad97b9068a9366653da7de03405232c Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 18 Jul 2018 13:11:39 +0200 Subject: [PATCH] Remove use of powermock Rather than mocking a static method setup the class properly. Change-Id: Idfd4c0c655c708f5c0e8c83287051b77a4544774 Signed-off-by: Robert Varga --- netconf/netconf-console/pom.xml | 14 ----------- .../commands/NetconfConnectDeviceCommand.java | 12 ++++++++++ .../NetconfCommandsImplCallsTest.java | 23 ++++++------------- netconf/sal-netconf-connector/pom.xml | 10 -------- .../netconf/sal/NetconfDeviceSalProvider.java | 5 ++-- .../sal/NetconfDeviceTopologyAdapter.java | 2 +- .../sal/NetconfDeviceSalFacadeTest.java | 12 ++-------- netconf/yanglib/pom.xml | 10 -------- 8 files changed, 25 insertions(+), 63 deletions(-) diff --git a/netconf/netconf-console/pom.xml b/netconf/netconf-console/pom.xml index 6e9943246b..bd430b18e7 100644 --- a/netconf/netconf-console/pom.xml +++ b/netconf/netconf-console/pom.xml @@ -39,20 +39,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL mockito-core test - - org.powermock - powermock-module-junit4 - test - - - org.powermock - powermock-api-mockito - test - - - org.powermock - powermock-core - org.opendaylight.yangtools yang-test-util diff --git a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfConnectDeviceCommand.java b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfConnectDeviceCommand.java index 88186de9cc..cedb5db576 100644 --- a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfConnectDeviceCommand.java +++ b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfConnectDeviceCommand.java @@ -8,6 +8,8 @@ package org.opendaylight.netconf.console.commands; +import static java.util.Objects.requireNonNull; + import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import java.util.Arrays; @@ -44,6 +46,16 @@ public class NetconfConnectDeviceCommand implements Action { this.devicePort = devicePort; } + @VisibleForTesting + NetconfConnectDeviceCommand(final NetconfCommands service, final String deviceIp, final String devicePort, + final String username, final String password) { + this.service = requireNonNull(service); + this.deviceIp = requireNonNull(deviceIp); + this.devicePort = requireNonNull(devicePort); + this.username = requireNonNull(username); + this.password = requireNonNull(password); + } + @Option(name = "-i", aliases = { "--ipaddress" }, description = "IP address of the netconf device", diff --git a/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/commands/NetconfCommandsImplCallsTest.java b/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/commands/NetconfCommandsImplCallsTest.java index f0e0af565d..2ceb066808 100644 --- a/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/commands/NetconfCommandsImplCallsTest.java +++ b/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/commands/NetconfCommandsImplCallsTest.java @@ -10,7 +10,6 @@ package org.opendaylight.netconf.console.commands; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertTrue; -import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doNothing; @@ -19,24 +18,17 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.MockitoAnnotations.initMocks; -import com.google.common.base.Strings; -import com.google.common.collect.Lists; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.opendaylight.netconf.console.api.NetconfCommands; import org.opendaylight.netconf.console.utils.NetconfConsoleConstants; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -@RunWith(PowerMockRunner.class) -@PrepareForTest(Strings.class) public class NetconfCommandsImplCallsTest { @Mock @@ -54,10 +46,9 @@ public class NetconfCommandsImplCallsTest { netconfConnectDeviceCommand.execute(); verify(netconfCommands, times(0)).connectDevice(any(), any()); - netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands, "192.168.1.1", "7777"); + netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands, "192.168.1.1", "7777", "user", + "pass"); - PowerMockito.mockStatic(Strings.class); - given(Strings.isNullOrEmpty(any())).willReturn(false); netconfConnectDeviceCommand.execute(); doNothing().when(netconfCommands).connectDevice(any(), any()); verify(netconfCommands, times(1)).connectDevice(any(), any()); @@ -141,10 +132,10 @@ public class NetconfCommandsImplCallsTest { private static HashMap>> getDeviceHashMap() { final HashMap>> devices = new HashMap<>(); final HashMap> deviceMap = new HashMap<>(); - deviceMap.put(NetconfConsoleConstants.NETCONF_IP, Lists.newArrayList("192.168.1.1")); - deviceMap.put(NetconfConsoleConstants.NETCONF_PORT, Lists.newArrayList("7777")); - deviceMap.put(NetconfConsoleConstants.STATUS, Lists.newArrayList("connecting")); - deviceMap.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Lists.newArrayList("cap1", "cap2", "cap3")); + deviceMap.put(NetconfConsoleConstants.NETCONF_IP, Arrays.asList("192.168.1.1")); + deviceMap.put(NetconfConsoleConstants.NETCONF_PORT, Arrays.asList("7777")); + deviceMap.put(NetconfConsoleConstants.STATUS, Arrays.asList("connecting")); + deviceMap.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Arrays.asList("cap1", "cap2", "cap3")); devices.put("device", deviceMap); return devices; } diff --git a/netconf/sal-netconf-connector/pom.xml b/netconf/sal-netconf-connector/pom.xml index 2b306d858e..07c027e36d 100644 --- a/netconf/sal-netconf-connector/pom.xml +++ b/netconf/sal-netconf-connector/pom.xml @@ -147,16 +147,6 @@ com.google.code.gson gson - - org.powermock - powermock-module-junit4 - test - - - org.powermock - powermock-api-mockito - test - org.awaitility awaitility diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceSalProvider.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceSalProvider.java index e4d16e9394..f4b44d9bc4 100644 --- a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceSalProvider.java +++ b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceSalProvider.java @@ -90,6 +90,7 @@ public class NetconfDeviceSalProvider implements AutoCloseable { } + @Override public void close() throws Exception { mountInstance.close(); if (topologyDatastoreAdapter != null) { @@ -101,7 +102,7 @@ public class NetconfDeviceSalProvider implements AutoCloseable { } } - public static final class MountInstance implements AutoCloseable { + public static class MountInstance implements AutoCloseable { private final DOMMountPointService mountService; private final RemoteDeviceId id; @@ -109,7 +110,7 @@ public class NetconfDeviceSalProvider implements AutoCloseable { private ObjectRegistration topologyRegistration; - public MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) { + MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) { this.mountService = Preconditions.checkNotNull(mountService); this.id = Preconditions.checkNotNull(id); } diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceTopologyAdapter.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceTopologyAdapter.java index c9b2251027..5bc7903634 100644 --- a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceTopologyAdapter.java +++ b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceTopologyAdapter.java @@ -50,7 +50,7 @@ import org.opendaylight.yangtools.yang.common.QName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public final class NetconfDeviceTopologyAdapter implements AutoCloseable { +public class NetconfDeviceTopologyAdapter implements AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceTopologyAdapter.class); diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceSalFacadeTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceSalFacadeTest.java index 89f0ee2642..05678f14ff 100644 --- a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceSalFacadeTest.java +++ b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/NetconfDeviceSalFacadeTest.java @@ -22,7 +22,6 @@ import java.util.Arrays; import java.util.List; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mock; import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; import org.opendaylight.controller.md.sal.dom.api.DOMNotification; @@ -32,18 +31,14 @@ import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPrefe import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil; import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -@RunWith(PowerMockRunner.class) -@PrepareForTest({NetconfDeviceTopologyAdapter.class, NetconfDeviceSalProvider.MountInstance.class, - NetconfSessionPreferences.class}) public class NetconfDeviceSalFacadeTest { private NetconfDeviceSalFacade deviceFacade; + @Mock private NetconfDeviceTopologyAdapter netconfDeviceTopologyAdapter; + @Mock private NetconfDeviceSalProvider.MountInstance mountInstance; @Mock @@ -57,9 +52,6 @@ public class NetconfDeviceSalFacadeTest { deviceFacade = new NetconfDeviceSalFacade(remoteDeviceId, salProvider); - netconfDeviceTopologyAdapter = PowerMockito.mock(NetconfDeviceTopologyAdapter.class); - mountInstance = PowerMockito.mock(NetconfDeviceSalProvider.MountInstance.class); - doReturn(netconfDeviceTopologyAdapter).when(salProvider).getTopologyDatastoreAdapter(); doNothing().when(netconfDeviceTopologyAdapter) .updateDeviceData(any(Boolean.class), any(NetconfDeviceCapabilities.class)); diff --git a/netconf/yanglib/pom.xml b/netconf/yanglib/pom.xml index 2751298d07..82a1b5c474 100644 --- a/netconf/yanglib/pom.xml +++ b/netconf/yanglib/pom.xml @@ -79,16 +79,6 @@ org.slf4j slf4j-api - - org.powermock - powermock-module-junit4 - test - - - org.powermock - powermock-api-mockito - test - commons-io commons-io -- 2.36.6