Added unit tests for org.opendaylight.unimgr.cli package 72/33372/1
authorJohann David <jdavid@inocybe.com>
Thu, 21 Jan 2016 19:23:12 +0000 (14:23 -0500)
committerAlexis de Talhouët <adetalhouet@inocybe.com>
Fri, 22 Jan 2016 15:54:31 +0000 (15:54 +0000)
Change-Id: Ieab7cbc46fcd663ec0ce412706057d343604213c
Signed-off-by: Johann David <jdavid@inocybe.com>
(cherry picked from commit 08c08d5dcf07ba4f496f2efbb21a86c338795712)

cli/pom.xml
cli/src/test/java/org/opendaylight/unimgr/cli/EvcAddShellCommandTest.java [new file with mode: 0644]
cli/src/test/java/org/opendaylight/unimgr/cli/EvcRemoveShellCommandTest.java [new file with mode: 0644]
cli/src/test/java/org/opendaylight/unimgr/cli/UniAddShellCommandTest.java [new file with mode: 0644]
cli/src/test/java/org/opendaylight/unimgr/cli/UniListShellCommandTest.java [new file with mode: 0644]
cli/src/test/java/org/opendaylight/unimgr/cli/UniRemoveShellCommandTest.java [new file with mode: 0644]
cli/src/test/java/org/opendaylight/unimgr/cli/UniShowShellCommandTest.java [new file with mode: 0644]
cli/src/test/java/org/opendaylight/unimgr/cli/UnimgrConsoleProviderTest.java [new file with mode: 0644]

index 02a62b806efe5bde96883e4080b53789ca5c1113..092049fb5f392e825b072198223a26433dd97d19 100644 (file)
@@ -15,6 +15,10 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL
     <relativePath/>
   </parent>
 
+  <properties>
+    <powermock.version>1.5.2</powermock.version>
+  </properties>
+
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.opendaylight.unimgr</groupId>
   <artifactId>unimgr-cli</artifactId>
@@ -45,6 +49,37 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL
       <artifactId>mockito-all</artifactId>
       <scope>test</scope>
     </dependency>
+
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-core</artifactId>
+      <version>${powermock.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-module-junit4</artifactId>
+      <version>${powermock.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-api-mockito</artifactId>
+      <version>${powermock.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-api-support</artifactId>
+      <version>${powermock.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-reflect</artifactId>
+      <version>${powermock.version}</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
diff --git a/cli/src/test/java/org/opendaylight/unimgr/cli/EvcAddShellCommandTest.java b/cli/src/test/java/org/opendaylight/unimgr/cli/EvcAddShellCommandTest.java
new file mode 100644 (file)
index 0000000..4dd6a81
--- /dev/null
@@ -0,0 +1,167 @@
+/*
+ * Copyright (c) 2016 CableLabs 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
+ */
+package org.opendaylight.unimgr.cli;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.powermock.api.support.membermodification.MemberMatcher.method;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.opendaylight.unimgr.impl.UnimgrConstants;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentationBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.EgressBw;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.IngressBw;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100M;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100MBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10G;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10GBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10M;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10MBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1G;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1GBuilder;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.api.support.membermodification.MemberModifier;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.Whitebox;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(EvcAddShellCommand.class)
+public class EvcAddShellCommandTest {
+
+    private final String IPs = "192.168.1.1";
+    private final String IPd = "192.168.1.2";
+    private final String egress = "100M";
+    private final String ingress = "10M";
+
+    private final UnimgrConsoleProviderTest provider = new UnimgrConsoleProviderTest();
+
+    @Mock EvcAddShellCommand evcAddShellCommand;
+
+    @Before
+    public void setUp() throws IllegalArgumentException, IllegalAccessException{
+        evcAddShellCommand = mock(EvcAddShellCommand.class, Mockito.CALLS_REAL_METHODS);
+        MemberModifier.field(EvcAddShellCommand.class, "provider").set(evcAddShellCommand, provider);
+
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.unimgr.cli.EvcAddShellCommand#getSpeed()}.
+     * @throws Exception
+     */
+    @Test
+    public void testgetSpeed() throws Exception {
+        // Test 10M
+        String speed = "10M";
+        final Speed10M speed10M = mock(Speed10M.class);
+        final Speed10MBuilder speed10MBuilder = mock(Speed10MBuilder.class);
+        when(speed10MBuilder.build()).thenReturn(speed10M);
+        PowerMockito.whenNew(Speed10MBuilder.class).withNoArguments().thenReturn(speed10MBuilder);
+        Object getSpeed = Whitebox.invokeMethod(evcAddShellCommand, "getSpeed", speed);
+        assertEquals(speed10M, getSpeed);
+
+        // Test 100M
+        speed = "100M";
+        final Speed100M speedObject100M = mock(Speed100M.class);
+        final Speed100MBuilder speed100M = mock(Speed100MBuilder.class);
+        PowerMockito.whenNew(Speed100MBuilder.class).withNoArguments().thenReturn(speed100M);
+        when(speed100M.build()).thenReturn(speedObject100M);
+        getSpeed = Whitebox.invokeMethod(evcAddShellCommand, "getSpeed", speed);
+        assertEquals(speedObject100M, getSpeed);
+
+        // Test 1G
+        speed = "1G";
+        final Speed1G speedObject1G = mock(Speed1G.class);
+        final Speed1GBuilder speed1G = mock(Speed1GBuilder.class);
+        PowerMockito.whenNew(Speed1GBuilder.class).withNoArguments().thenReturn(speed1G);
+        when(speed1G.build()).thenReturn(speedObject1G);
+        getSpeed = Whitebox.invokeMethod(evcAddShellCommand, "getSpeed", speed);
+        assertEquals(speedObject1G, getSpeed);
+
+        // Test 10G
+        speed = "10G";
+        final Speed10G speedObject10G = mock(Speed10G.class);
+        final Speed10GBuilder speed10G = mock(Speed10GBuilder.class);
+        PowerMockito.whenNew(Speed10GBuilder.class).withNoArguments().thenReturn(speed10G);
+        when(speed10G.build()).thenReturn(speedObject10G);
+        getSpeed = Whitebox.invokeMethod(evcAddShellCommand, "getSpeed", speed);
+        assertEquals(speedObject10G, getSpeed);
+
+        // Test other
+        speed = "other";
+        getSpeed = Whitebox.invokeMethod(evcAddShellCommand, "getSpeed", speed);
+        assertEquals(null, getSpeed);
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.unimgr.cli.EvcAddShellCommand#doExecute()}.
+     * @throws Exception
+     */
+    @Test
+    public void testDoExecute() throws Exception {
+        final EgressBw egressBw100M = mock(EgressBw.class);
+        final IngressBw ingressBw10M = mock(IngressBw.class);
+        final EvcAddShellCommand spyEvc = PowerMockito.spy(new EvcAddShellCommand(provider));
+        PowerMockito.when(spyEvc, method(EvcAddShellCommand.class, "getSpeed", String.class))
+            .withArguments("100M").thenReturn(egressBw100M);
+        PowerMockito.when(spyEvc, method(EvcAddShellCommand.class, "getSpeed", String.class))
+            .withArguments("10M").thenReturn(ingressBw10M);
+        Whitebox.setInternalState(spyEvc, "IPs", IPs);
+        Whitebox.setInternalState(spyEvc, "IPd", IPd);
+        Whitebox.setInternalState(spyEvc, "egress", egress);
+        Whitebox.setInternalState(spyEvc, "ingress", ingress);
+        final Short order = new Short("0");
+        final IpAddress ipAddreSource = new IpAddress(IPs.toCharArray());
+        final UniSource uniSource = new UniSourceBuilder()
+                                  .setIpAddress(ipAddreSource)
+                                  .setKey(new UniSourceKey(order))
+                                  .setOrder(order)
+                                  .build();
+        final List<UniSource> uniSourceList = new ArrayList<UniSource>();
+        uniSourceList.add(uniSource);
+        final IpAddress ipAddreDest = new IpAddress(IPd.toCharArray());
+        final UniDest uniDest = new UniDestBuilder()
+                          .setOrder(order)
+                          .setKey(new UniDestKey(order))
+                          .setIpAddress(ipAddreDest)
+                          .build();
+        final List<UniDest> uniDestList = new ArrayList<UniDest>();
+        uniDestList.add(uniDest);
+        final EvcAugmentation evcAug = new EvcAugmentationBuilder()
+                                     .setCosId(UnimgrConstants.EVC_PREFIX + 1)
+                                     .setEgressBw((EgressBw) egressBw100M)
+                                     .setIngressBw((IngressBw) ingressBw10M)
+                                     .setUniDest(uniDestList)
+                                     .setUniSource(uniSourceList)
+                                     .build();
+        final Object success = new String("Evc with Source Uni " +IPs+" and destenation Uni " +IPd+" created");
+        final Object error = new String("Error creating new Evc");
+        Object stringReturn = spyEvc.doExecute();
+        assertEquals(success, stringReturn);
+        assertEquals(evcAug, provider.getEvc(null));
+        stringReturn = spyEvc.doExecute();
+        assertEquals(error, stringReturn);
+    }
+
+}
diff --git a/cli/src/test/java/org/opendaylight/unimgr/cli/EvcRemoveShellCommandTest.java b/cli/src/test/java/org/opendaylight/unimgr/cli/EvcRemoveShellCommandTest.java
new file mode 100644 (file)
index 0000000..e6e04ed
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2016 CableLabs 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
+ */
+package org.opendaylight.unimgr.cli;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.powermock.api.support.membermodification.MemberModifier;
+import org.powermock.reflect.Whitebox;
+
+public class EvcRemoveShellCommandTest {
+
+    private final UnimgrConsoleProviderTest provider = new UnimgrConsoleProviderTest();
+
+    @Mock EvcRemoveShellCommand evcRemoveShellCommand;
+
+    @Before
+    public void setUp() throws IllegalArgumentException, IllegalAccessException{
+        evcRemoveShellCommand = mock(EvcRemoveShellCommand.class, Mockito.CALLS_REAL_METHODS);
+        MemberModifier.field(EvcRemoveShellCommand.class, "provider").set(evcRemoveShellCommand, provider);
+    }
+    /**
+     * Test method for {@link org.opendaylight.unimgr.cli.EvcRemoveShellCommand#doExecute()}.
+     * @throws Exception
+     */
+    @Test
+    public void testDoExecute() throws Exception {
+        final String success = "Evc successfully removed";
+        final String error = "Error removing Evc";
+        Object answer = Whitebox.invokeMethod(evcRemoveShellCommand, "doExecute");
+        assertEquals(success, answer);
+        answer = Whitebox.invokeMethod(evcRemoveShellCommand, "doExecute");
+        assertEquals(error, answer);
+    }
+
+}
diff --git a/cli/src/test/java/org/opendaylight/unimgr/cli/UniAddShellCommandTest.java b/cli/src/test/java/org/opendaylight/unimgr/cli/UniAddShellCommandTest.java
new file mode 100644 (file)
index 0000000..f024441
--- /dev/null
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2016 CableLabs 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
+ */
+package org.opendaylight.unimgr.cli;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.math.BigInteger;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100M;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100MBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10G;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10GBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10M;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10MBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1G;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1GBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.uni.Speed;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.api.support.membermodification.MemberModifier;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.Whitebox;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(UniAddShellCommand.class)
+public class UniAddShellCommandTest {
+
+    private final String physicalMedium = "UNI TypeFull Duplex 2 Physical Interface";
+    private final String macAddress = "00:00:00:00:00:00";
+    private final String mode = "Full Duplex";
+    private final String macLayer = "IEEE 802.3-2005";
+    private final String type = "";
+    private final String mtuSize = "0";
+    private final String ipAddress = "192.168.1.1";
+
+    private final UnimgrConsoleProviderTest provider = new UnimgrConsoleProviderTest();
+
+    @Mock UniAddShellCommand uniAddShellCommand;
+    //@Mock UnimgrProvider provider;
+
+    @Before
+    public void setUp() throws IllegalArgumentException, IllegalAccessException{
+        uniAddShellCommand = mock(UniAddShellCommand.class, Mockito.CALLS_REAL_METHODS);
+        MemberModifier.field(UniAddShellCommand.class, "provider").set(uniAddShellCommand, provider);
+        Whitebox.setInternalState(uniAddShellCommand, "macAddress", macAddress);
+        Whitebox.setInternalState(uniAddShellCommand, "macLayer", macLayer);
+        Whitebox.setInternalState(uniAddShellCommand, "mode", mode);
+        Whitebox.setInternalState(uniAddShellCommand, "mtuSize", mtuSize);
+        Whitebox.setInternalState(uniAddShellCommand, "physicalMedium", physicalMedium);
+        Whitebox.setInternalState(uniAddShellCommand, "type", type);
+        Whitebox.setInternalState(uniAddShellCommand, "ipAddress", ipAddress);
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.unimgr.cli.UniAddShellCommand#getSpeed()}.
+     * @throws Exception
+     */
+    @Test
+    public void testgetSpeed() throws Exception {
+        // Test 10M
+        Whitebox.setInternalState(uniAddShellCommand, "speed", "10M");
+        final Speed10M speed10M = mock(Speed10M.class);
+        final Speed10MBuilder speed10MBuilder = mock(Speed10MBuilder.class);
+        when(speed10MBuilder.build()).thenReturn(speed10M);
+        PowerMockito.whenNew(Speed10MBuilder.class).withNoArguments().thenReturn(speed10MBuilder);
+        Object getSpeed = Whitebox.invokeMethod(uniAddShellCommand, "getSpeed");
+        assertEquals(speed10M, getSpeed);
+
+        // Test 100M
+        Whitebox.setInternalState(uniAddShellCommand, "speed", "100M");
+        final Speed100M speedObject100M = mock(Speed100M.class);
+        final Speed100MBuilder speed100M = mock(Speed100MBuilder.class);
+        PowerMockito.whenNew(Speed100MBuilder.class).withNoArguments().thenReturn(speed100M);
+        when(speed100M.build()).thenReturn(speedObject100M);
+        getSpeed = Whitebox.invokeMethod(uniAddShellCommand, "getSpeed");
+        assertEquals(speedObject100M, getSpeed);
+
+        // Test 1G
+        Whitebox.setInternalState(uniAddShellCommand, "speed", "1G");
+        final Speed1G speedObject1G = mock(Speed1G.class);
+        final Speed1GBuilder speed1G = mock(Speed1GBuilder.class);
+        PowerMockito.whenNew(Speed1GBuilder.class).withNoArguments().thenReturn(speed1G);
+        when(speed1G.build()).thenReturn(speedObject1G);
+        getSpeed = Whitebox.invokeMethod(uniAddShellCommand, "getSpeed");
+        assertEquals(speedObject1G, getSpeed);
+
+        // Test 10G
+        Whitebox.setInternalState(uniAddShellCommand, "speed", "10G");
+        final Speed10G speedObject10G = mock(Speed10G.class);
+        final Speed10GBuilder speed10G = mock(Speed10GBuilder.class);
+        PowerMockito.whenNew(Speed10GBuilder.class).withNoArguments().thenReturn(speed10G);
+        when(speed10G.build()).thenReturn(speedObject10G);
+        getSpeed = Whitebox.invokeMethod(uniAddShellCommand, "getSpeed");
+        assertEquals(speedObject10G, getSpeed);
+
+        // Test other
+        Whitebox.setInternalState(uniAddShellCommand, "speed", "other");
+        getSpeed = Whitebox.invokeMethod(uniAddShellCommand, "getSpeed");
+        assertEquals(null, getSpeed);
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.unimgr.cli.UniAddShellCommand#doExecute()}.
+     * @throws Exception
+     */
+    @Test
+    public void testDoExecute() throws Exception {
+        Whitebox.setInternalState(uniAddShellCommand, "speed", "other");
+        final UniAugmentation uni = new UniAugmentationBuilder()
+                .setMacAddress(new MacAddress(macAddress))
+                .setMacLayer(macLayer)
+                .setMode(mode)
+                .setMtuSize(BigInteger.valueOf(Long.valueOf(mtuSize)))
+                .setPhysicalMedium(physicalMedium)
+                .setSpeed((Speed) null)
+                .setType(type)
+                .setIpAddress(new IpAddress(ipAddress.toCharArray()))
+                .build();
+        final Object success = new String("Uni with ip " +ipAddress+" created");
+        final Object error = new String("Error creating new Uni");
+        Object stringReturn = uniAddShellCommand.doExecute();
+        assertEquals(success, stringReturn);
+        assertEquals(uni, provider.getUni(null));
+        stringReturn = uniAddShellCommand.doExecute();
+        assertEquals(error, stringReturn);
+    }
+
+}
\ No newline at end of file
diff --git a/cli/src/test/java/org/opendaylight/unimgr/cli/UniListShellCommandTest.java b/cli/src/test/java/org/opendaylight/unimgr/cli/UniListShellCommandTest.java
new file mode 100644 (file)
index 0000000..6e85493
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2016 CableLabs 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
+ */
+package org.opendaylight.unimgr.cli;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.powermock.api.support.membermodification.MemberModifier;
+import org.powermock.reflect.Whitebox;
+
+public class UniListShellCommandTest {
+
+    private static final String ipAddress = "192.168.1.1";
+
+    private final UnimgrConsoleProviderTest provider = new UnimgrConsoleProviderTest();
+
+    @Mock UniListShellCommand uniListShellCommand;
+
+    @Before
+    public void setUp() throws IllegalArgumentException, IllegalAccessException{
+        uniListShellCommand = mock(UniListShellCommand.class, Mockito.CALLS_REAL_METHODS);
+        MemberModifier.field(UniListShellCommand.class, "provider").set(uniListShellCommand, provider);
+        MemberModifier.field(UniListShellCommand.class, "isConfigurationData").set(uniListShellCommand, false);
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.unimgr.cli.UniListShellCommand#doExecute()}.
+     * @throws Exception
+     */
+    @Test
+    public void testDoExecute() throws Exception {
+        // Test empty list
+        final String error = "No uni found. Check the logs for more details.";
+        Object answer = Whitebox.invokeMethod(uniListShellCommand, "doExecute");
+        assertEquals(error, answer);
+
+        // Test list with 3 uni
+        final StringBuilder sb = new StringBuilder();
+        Integer counter = 1;
+        final int amount = 3;
+        for (int i=0; i<amount; i++) {
+            sb.append(String.format("#%d - IpAddress: %s\n", counter, ipAddress));
+            counter++;
+        }
+        final String success = sb.toString();
+        provider.setListUnis(3, ipAddress);
+        answer = Whitebox.invokeMethod(uniListShellCommand, "doExecute");
+        assertEquals(success, answer);
+        //fail("Not yet implemented");
+    }
+
+}
diff --git a/cli/src/test/java/org/opendaylight/unimgr/cli/UniRemoveShellCommandTest.java b/cli/src/test/java/org/opendaylight/unimgr/cli/UniRemoveShellCommandTest.java
new file mode 100644 (file)
index 0000000..dfa5dca
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016 CableLabs 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
+ */
+package org.opendaylight.unimgr.cli;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Evc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
+import org.powermock.api.support.membermodification.MemberModifier;
+import org.powermock.reflect.Whitebox;
+
+public class UniRemoveShellCommandTest {
+    private final String ipAddress = "192.168.1.1";
+    private final UnimgrConsoleProviderTest provider = new UnimgrConsoleProviderTest();
+
+    @Mock UniRemoveShellCommand uniRemoveShellCommand;
+
+    @Before
+    public void setUp() throws IllegalArgumentException, IllegalAccessException{
+        uniRemoveShellCommand = mock(UniRemoveShellCommand.class, Mockito.CALLS_REAL_METHODS);
+        MemberModifier.field(UniRemoveShellCommand.class, "provider").set(uniRemoveShellCommand, provider);
+        MemberModifier.field(UniRemoveShellCommand.class, "ipAddress").set(uniRemoveShellCommand, ipAddress);
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.unimgr.cli.UniRemoveShellCommand#doExecute()}.
+     * @throws Exception
+     */
+    @Test
+    public void testDoExecute() throws Exception {
+        final String success = "Uni successfully removed";
+        final String error = "Error removing Uni";
+        Object answer = Whitebox.invokeMethod(uniRemoveShellCommand, "doExecute");
+        assertEquals(success, answer);
+        answer = Whitebox.invokeMethod(uniRemoveShellCommand, "doExecute");
+        assertEquals(error, answer);
+    }
+
+}
diff --git a/cli/src/test/java/org/opendaylight/unimgr/cli/UniShowShellCommandTest.java b/cli/src/test/java/org/opendaylight/unimgr/cli/UniShowShellCommandTest.java
new file mode 100644 (file)
index 0000000..ca72124
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2016 CableLabs 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
+ */
+package org.opendaylight.unimgr.cli;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.math.BigInteger;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.uni.Speed;
+import org.powermock.api.support.membermodification.MemberModifier;
+import org.powermock.reflect.Whitebox;
+
+public class UniShowShellCommandTest {
+
+    private static final String macAddress = "00:00:00:00:00:00";
+    private static final String physicalMedium = "UNI TypeFull Duplex 2 Physical Interface";
+    private static final String speed = "";
+    private static final String mode = "Full Duplex";
+    private static final String macLayer = "IEEE 802.3-2005";
+    private static final String type = "";
+    private static final BigInteger mtuSize = BigInteger.valueOf(0);
+    private static final String ipAddress = "192.168.1.1";
+
+    private final UnimgrConsoleProviderTest provider = new UnimgrConsoleProviderTest();
+
+    @Mock UniShowShellCommand uniShowShellCommand;
+
+    @Before
+    public void setUp() throws IllegalArgumentException, IllegalAccessException{
+        uniShowShellCommand = mock(UniShowShellCommand.class, Mockito.CALLS_REAL_METHODS);
+        MemberModifier.field(UniShowShellCommand.class, "provider").set(uniShowShellCommand, provider);
+        MemberModifier.field(UniShowShellCommand.class, "ipAddress").set(uniShowShellCommand, ipAddress);
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.unimgr.cli.UniShowShellCommandTest#doExecute()}.
+     * @throws Exception
+     */
+    @Test
+    public void testDoExecute() throws Exception {
+        // Test null uni
+        final String error = "No uni found. Check the logs for more details.";
+        Object answer = Whitebox.invokeMethod(uniShowShellCommand, "doExecute");
+        assertEquals(error, answer);
+
+        // Test uni
+        final StringBuilder sb = new StringBuilder();
+        sb.append(String.format("Ip Address: <%s>\n", ipAddress));
+        sb.append(String.format("Mac address: <%s>\n", macAddress));
+        sb.append(String.format("Physical medium: <%s>\n", physicalMedium));
+        sb.append(String.format("Speed: " + speed + "\n"));
+        sb.append(String.format("Mode: <%s>\n", mode));
+        sb.append(String.format("Mac layer: <%s>\n", macLayer));
+        sb.append(String.format("Type: <%s>\n", type));
+        sb.append(String.format("Mtu size: <%s>\n", mtuSize));
+        final String success = sb.toString();
+
+        final UniAugmentation uniAug = mock(UniAugmentation.class);
+        final IpAddress ipAddr = mock(IpAddress.class);
+        final Ipv4Address ip4 = mock(Ipv4Address.class);
+        when(uniAug.getIpAddress()).thenReturn(ipAddr);
+        when(ipAddr.getIpv4Address()).thenReturn(ip4);
+        when(ip4.getValue()).thenReturn(ipAddress);
+        final MacAddress mac = mock(MacAddress.class);
+        when(uniAug.getMacAddress()).thenReturn(mac);
+        when(mac.toString()).thenReturn(macAddress);
+        when(uniAug.getPhysicalMedium()).thenReturn(physicalMedium);
+        final Speed speedClass = mock(Speed.class);
+        when(uniAug.getSpeed()).thenReturn(speedClass);
+        when(speedClass.toString()).thenReturn(speed);
+        when(uniAug.getMode()).thenReturn(mode);
+        when(uniAug.getMacLayer()).thenReturn(macLayer);
+        when(uniAug.getType()).thenReturn(type);
+        when(uniAug.getMtuSize()).thenReturn(mtuSize);
+        provider.addUni(uniAug);
+        answer = Whitebox.invokeMethod(uniShowShellCommand, "doExecute");
+        assertEquals(success, answer);
+    }
+
+}
diff --git a/cli/src/test/java/org/opendaylight/unimgr/cli/UnimgrConsoleProviderTest.java b/cli/src/test/java/org/opendaylight/unimgr/cli/UnimgrConsoleProviderTest.java
new file mode 100644 (file)
index 0000000..0584b57
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2016 CableLabs 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
+ */
+package org.opendaylight.unimgr.cli;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Evc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
+
+public class UnimgrConsoleProviderTest implements IUnimgrConsoleProvider {
+
+    private final List<UniAugmentation> listUni = new ArrayList<UniAugmentation>();
+    private final List<EvcAugmentation> listEvc = new ArrayList<EvcAugmentation>();
+    private boolean firstTest = true;
+
+    @Override
+    public boolean addUni(UniAugmentation uni) {
+        return (listUni.contains(uni)) ? false : listUni.add(uni);
+    }
+
+    @Override
+    public boolean removeUni(IpAddress ipAddress) {
+        firstTest = !firstTest;
+        return !firstTest;
+    }
+
+    @Override
+    public List<UniAugmentation> listUnis(LogicalDatastoreType dataStoreType) {
+        return listUni;
+    }
+
+    public void setListUnis(int amount, String ipAddress) {
+        for(int i=0; i<amount; i++){
+            final UniAugmentation uniAug = mock(UniAugmentation.class);
+            final IpAddress ipAddr = mock(IpAddress.class);
+            final Ipv4Address ip4 = mock(Ipv4Address.class);
+            when(uniAug.getIpAddress()).thenReturn(ipAddr);
+            when(ipAddr.getIpv4Address()).thenReturn(ip4);
+            when(ip4.getValue()).thenReturn(ipAddress);
+            listUni.add(uniAug);
+        }
+    }
+
+    @Override
+    public UniAugmentation getUni(IpAddress ipAddress) {
+        return (listUni.isEmpty()) ? null : listUni.get(0);
+    }
+
+    @Override
+    public boolean addEvc(EvcAugmentation evc) {
+        return (listEvc.contains(evc)) ? false : listEvc.add(evc);
+    }
+
+    @Override
+    public Evc getEvc(String uuid) {
+        return (listEvc.isEmpty()) ? null : listEvc.get(0);
+    }
+
+    @Override
+    public boolean removeEvc(String uuid) {
+        firstTest = !firstTest;
+        return !firstTest;
+    }
+
+    @Override
+    public void close() throws Exception { }
+
+}