SfcProviderRpc unit tests 04/19804/12
authorAndrej Kincel <[email protected]>
Thu, 7 May 2015 13:53:38 +0000 (15:53 +0200)
committerAndrej Kincel <[email protected]>
Thu, 14 May 2015 12:53:27 +0000 (12:53 +0000)
Change-Id: Id1a30fb6098b87e610936bb84f92f7e680d740d1
Signed-off-by: Igor Bartak <[email protected]>
Signed-off-by: Andrej Kincel <[email protected]>
sfc-provider/pom.xml
sfc-provider/src/test/java/org/opendaylight/sfc/provider/api/SfcProviderRpcTest.java [new file with mode: 0644]

index 6aa4ddeaa4044ffb0f733df2ddc513079fde83ea..5b0110364883e21113e23541a7e3d0bcbe551741 100755 (executable)
             <type>jar</type>
             <scope>compile</scope>
         </dependency>
-        <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
-            <!-- version>4.8.1</version -->
-            <version>[4.8.1, 4.11]</version>
-            <!-- version>4.11</version -->
-            <scope>test</scope>
-        </dependency>
         <dependency>
             <groupId>org.opendaylight.controller</groupId>
             <artifactId>sal-core-api</artifactId>
             <artifactId>yang-data-api</artifactId>
             <version>${yangtools.version}</version>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <!-- version>4.8.1</version -->
+            <version>[4.8.1, 4.11]</version>
+            <!-- version>4.11</version -->
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.mockito</groupId>
-            <artifactId>mockito-core</artifactId>
+            <artifactId>mockito-all</artifactId>
             <version>${mockito.version}</version>
             <scope>test</scope>
         </dependency>
diff --git a/sfc-provider/src/test/java/org/opendaylight/sfc/provider/api/SfcProviderRpcTest.java b/sfc-provider/src/test/java/org/opendaylight/sfc/provider/api/SfcProviderRpcTest.java
new file mode 100644 (file)
index 0000000..e6c3b93
--- /dev/null
@@ -0,0 +1,161 @@
+/*
+ * 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
+ */
+package org.opendaylight.sfc.provider.api;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.opendaylight.sfc.provider.OpendaylightSfc;
+import org.opendaylight.sfc.provider.SfcProviderRpc;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.CreateRenderedPathInput;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.DeleteRenderedPathInput;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.DeleteRenderedPathOutput;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.ReadRenderedServicePathFirstHopInput;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.ReadRenderedServicePathFirstHopOutput;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.path.first.hop.info.RenderedServicePathFirstHop;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.scf.rev140701.service.function.classifiers.ServiceFunctionClassifier;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import java.util.concurrent.ExecutionException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.when;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({OpendaylightSfc.class, SfcProviderServicePathAPI.class, SfcProviderRenderedPathAPI.class,
+        SfcProviderServiceClassifierAPI.class, SfcProviderServiceForwarderAPI.class, SfcProviderServiceFunctionAPI.class, RpcResultBuilder.class})
+public class SfcProviderRpcTest {
+
+    SfcProviderRpc sfcProviderRpc;
+
+    @Before
+    public void before() throws ExecutionException, InterruptedException {
+        OpendaylightSfc opendaylightSfcMock = mock(OpendaylightSfc.class);
+        PowerMockito.mockStatic(OpendaylightSfc.class);
+        when(OpendaylightSfc.getOpendaylightSfcObj()).thenReturn(opendaylightSfcMock);
+        sfcProviderRpc = new SfcProviderRpc();
+    }
+
+    @Test
+    public void createRenderedPathTest() throws ExecutionException, InterruptedException {
+        CreateRenderedPathInput createRenderedPathInputMock = mock(CreateRenderedPathInput.class);
+
+        ServiceFunctionPath serviceFunctionPathMock = mock(ServiceFunctionPath.class);
+        PowerMockito.mockStatic(SfcProviderServicePathAPI.class);
+        doReturn("crpiString1").when(createRenderedPathInputMock).getParentServiceFunctionPath();
+        when(SfcProviderServicePathAPI.readServiceFunctionPathExecutor("crpiString1")).thenReturn(serviceFunctionPathMock);
+
+        RenderedServicePath renderedServicePathMock = mock(RenderedServicePath.class);
+        PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
+        when(SfcProviderRenderedPathAPI.createRenderedServicePathAndState(eq(serviceFunctionPathMock), eq(createRenderedPathInputMock))).thenReturn(renderedServicePathMock);
+        doReturn("classifier1").when(serviceFunctionPathMock).getClassifier();
+
+        ServiceFunctionClassifier serviceFunctionClassifier = mock(ServiceFunctionClassifier.class);
+        PowerMockito.mockStatic(SfcProviderServiceClassifierAPI.class);
+        doReturn("classifier1").when(serviceFunctionPathMock).getSymmetricClassifier();
+        when(SfcProviderServiceClassifierAPI.readServiceClassifierExecutor("classifier1")).thenReturn(serviceFunctionClassifier);
+
+        doReturn(true).when(serviceFunctionPathMock).isSymmetric();
+
+        when(SfcProviderRenderedPathAPI.createSymmetricRenderedServicePathAndState(renderedServicePathMock)).thenReturn(renderedServicePathMock);
+        doReturn("rspName1").when(renderedServicePathMock).getName();
+
+        assertEquals("RSP name has not been set correctly.", "rspName1", sfcProviderRpc.createRenderedPath(createRenderedPathInputMock).get().getResult().getName());
+        PowerMockito.verifyStatic(Mockito.times(2));
+        SfcProviderServiceClassifierAPI.addRenderedPathToServiceClassifierStateExecutor("classifier1", "rspName1");
+        PowerMockito.verifyStatic(Mockito.times(1));
+        SfcProviderRenderedPathAPI.createSymmetricRenderedServicePathAndState(renderedServicePathMock);
+        PowerMockito.verifyStatic(Mockito.times(2));
+        SfcProviderServiceClassifierAPI.readServiceClassifierExecutor("classifier1");
+        PowerMockito.verifyStatic(Mockito.times(1));
+        SfcProviderRenderedPathAPI.createRenderedServicePathAndState(serviceFunctionPathMock, createRenderedPathInputMock);
+        PowerMockito.verifyStatic(Mockito.times(1));
+        SfcProviderServicePathAPI.readServiceFunctionPathExecutor("crpiString1");
+    }
+
+    @Test
+    public void readRenderedServicePathFirstHopTest() throws ExecutionException, InterruptedException {
+        ReadRenderedServicePathFirstHopInput readRenderedServicePathFirstHopInputMock = mock(ReadRenderedServicePathFirstHopInput.class);
+        doReturn("renderedServicePathFirstHopInputName1").when(readRenderedServicePathFirstHopInputMock).getName();
+        PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
+        RenderedServicePathFirstHop renderedServicePathFirstHopMock = mock(RenderedServicePathFirstHop.class);
+        doReturn("rspFirstHop1").when(readRenderedServicePathFirstHopInputMock).getName();
+        when(SfcProviderRenderedPathAPI.readRenderedServicePathFirstHop("rspFirstHop1")).thenReturn(renderedServicePathFirstHopMock);
+        assertNotNull("RenderedServicePathFirstHop has not been set correctly.", sfcProviderRpc.readRenderedServicePathFirstHop(readRenderedServicePathFirstHopInputMock));
+        PowerMockito.verifyStatic(Mockito.times(1));
+        SfcProviderRenderedPathAPI.readRenderedServicePathFirstHop("rspFirstHop1");
+        PowerMockito.verifyStatic(Mockito.times(1));
+        RpcResultBuilder.success(any(ReadRenderedServicePathFirstHopOutput.class));
+    }
+
+    @Test
+    public void readRenderedServicePathFirstHopElseTest() throws ExecutionException, InterruptedException {
+        ReadRenderedServicePathFirstHopInput readRenderedServicePathFirstHopInputMock = mock(ReadRenderedServicePathFirstHopInput.class);
+        doReturn("renderedServicePathFirstHopInputName1").when(readRenderedServicePathFirstHopInputMock).getName();
+        PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
+        doReturn("rspFirstHop1").when(readRenderedServicePathFirstHopInputMock).getName();
+        when(SfcProviderRenderedPathAPI.readRenderedServicePathFirstHop("rspFirstHop1")).thenReturn(null);
+        assertNotNull("RenderedServicePathFirstHop has not been set correctly.", sfcProviderRpc.readRenderedServicePathFirstHop(readRenderedServicePathFirstHopInputMock));
+        Mockito.verify(readRenderedServicePathFirstHopInputMock, times(2)).getName();
+        PowerMockito.verifyStatic(Mockito.times(1));
+        SfcProviderRenderedPathAPI.readRenderedServicePathFirstHop("rspFirstHop1");
+    }
+
+    @Test
+    public void deleteRenderedPathTest() throws ExecutionException, InterruptedException {
+        DeleteRenderedPathInput deleteRenderedPathInput = mock(DeleteRenderedPathInput.class);
+        doReturn("stringName1").when(deleteRenderedPathInput).getName();
+        PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
+        when(SfcProviderServiceForwarderAPI.deletePathFromServiceForwarderStateExecutor("stringName1")).thenReturn(true);
+        PowerMockito.mockStatic(SfcProviderServiceFunctionAPI.class);
+        when(SfcProviderServiceFunctionAPI.deleteServicePathFromServiceFunctionStateExecutor("stringName1")).thenReturn(true);
+        PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
+        when(SfcProviderRenderedPathAPI.deleteRenderedServicePathExecutor("stringName1")).thenReturn(true);
+        assertNotNull("DeleteRenderedPath has not run correctly.", sfcProviderRpc.deleteRenderedPath(deleteRenderedPathInput));
+        PowerMockito.verifyStatic();
+        SfcProviderServiceForwarderAPI.deletePathFromServiceForwarderStateExecutor("stringName1");
+        PowerMockito.verifyStatic();
+        SfcProviderServiceFunctionAPI.deleteServicePathFromServiceFunctionStateExecutor("stringName1");
+        PowerMockito.verifyStatic();
+        SfcProviderRenderedPathAPI.deleteRenderedServicePathExecutor("stringName1");
+        PowerMockito.verifyStatic();
+        RpcResultBuilder.success(any(DeleteRenderedPathOutput.class));
+    }
+
+    @Test
+    public void deleteRenderedPathElseTest() throws ExecutionException, InterruptedException {
+        DeleteRenderedPathInput deleteRenderedPathInput = mock(DeleteRenderedPathInput.class);
+        doReturn("stringName1").when(deleteRenderedPathInput).getName();
+        PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
+        when(SfcProviderServiceForwarderAPI.deletePathFromServiceForwarderStateExecutor("stringName1")).thenReturn(true);
+        PowerMockito.mockStatic(SfcProviderServiceFunctionAPI.class);
+        when(SfcProviderServiceFunctionAPI.deleteServicePathFromServiceFunctionStateExecutor("stringName1")).thenReturn(true);
+        PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
+        when(SfcProviderRenderedPathAPI.deleteRenderedServicePathExecutor("stringName1")).thenReturn(false);
+        assertNotNull("DeleteRenderedPath has not run correctly.", sfcProviderRpc.deleteRenderedPath(deleteRenderedPathInput));
+        Mockito.verify(deleteRenderedPathInput, times(4)).getName();
+        PowerMockito.verifyStatic();
+        SfcProviderServiceForwarderAPI.deletePathFromServiceForwarderStateExecutor("stringName1");
+        PowerMockito.verifyStatic();
+        SfcProviderServiceFunctionAPI.deleteServicePathFromServiceFunctionStateExecutor("stringName1");
+        PowerMockito.verifyStatic();
+        SfcProviderRenderedPathAPI.deleteRenderedServicePathExecutor("stringName1");
+    }
+}