Added test case to increase SONAR coverage
[sdnc/northbound.git] / generic-resource-api / provider / src / test / java / org / onap / sdnc / northbound / ServiceTopologyOperationRPCTest.java
index b4ce214..bbd0afd 100644 (file)
@@ -22,7 +22,6 @@
 package org.onap.sdnc.northbound;
 
 import static org.junit.Assert.assertEquals;
-import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.onap.sdnc.northbound.GenericResourceApiProvider.APP_NAME;
@@ -40,8 +39,6 @@ import static org.onap.sdnc.northbound.util.MDSALUtil.serviceStatus;
 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationInput;
 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationOutput;
 
-import javax.xml.crypto.Data;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -60,8 +57,6 @@ import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.re
 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.information.ServiceInformation;
 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.Service;
 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatus;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 
 
@@ -71,17 +66,14 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 @RunWith(MockitoJUnitRunner.class)
 public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderTest {
 
-
     final String SVC_OPERATION = "service-topology-operation";
 
-
     @Before
     public void setUp() throws Exception {
         super.setUp();
         svcClient.setScvOperation(SVC_OPERATION);
     }
 
-
     /**
      * Verify  ServiceTopologyOperation RPC executes a DG then produces the expected {@link
      * ServiceTopologyOperationOutput} and persisted the expected {@link Service} in the {@link DataBroker}
@@ -95,26 +87,30 @@ public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderT
         svcClient.mockExecute(svcResultProp);
 
         // create the ServiceTopologyOperationInput from the template
-        ServiceTopologyOperationInput serviceTopologyOperationInput = createSTOI();
+        ServiceTopologyOperationInput input = createSTOI();
 
         //execute the mdsal exec
-        ServiceTopologyOperationOutput actualServiceTopologyOperationOutput = exec(
+        ServiceTopologyOperationOutput output = exec(
             genericResourceApiProvider::serviceTopologyOperation
-            , serviceTopologyOperationInput
+            , input
             , RpcResult::getResult
         );
 
+        assertEquals("200", output.getResponseCode());
+        assertEquals("OK", output.getResponseMessage());
+        assertEquals("Y", output.getAckFinalIndicator());
+
         //verify the returned ServiceTopologyOperationOutput
         ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput = createExpectedSTOO(svcResultProp,
-            serviceTopologyOperationInput);
-        assertEquals(expectedServiceTopologyOperationOutput, actualServiceTopologyOperationOutput);
+            input);
+        assertEquals(expectedServiceTopologyOperationOutput, output);
 
         //verify the persisted Service
-        Service actualService = db.read(serviceTopologyOperationInput.getServiceInformation().getServiceInstanceId(),
+        Service actualService = db.read(input.getServiceInformation().getServiceInstanceId(),
             LogicalDatastoreType.CONFIGURATION);
         Service expectedService = createExpectedService(
             expectedServiceTopologyOperationOutput,
-            serviceTopologyOperationInput,
+            input,
             actualService);
         assertEquals(expectedService, actualService);
 
@@ -122,18 +118,18 @@ public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderT
     }
 
     @Test
-    public void serviceTopologyOperation_should_fail_when_service_info_not_present() throws Exception {
+    public void should_fail_when_service_info_not_present() throws Exception {
         // create the ServiceTopologyOperationInput from the template
         ServiceTopologyOperationInput input = build(
             serviceTopologyOperationInput()
                 .setSdncRequestHeader(build(sdncRequestHeader()
-                .setSvcRequestId("svc-request-id: xyz")
-                .setSvcAction(SvcAction.Assign)
-            ))
-            .setRequestInformation(build(requestInformation()
-                .setRequestId("request-id: xyz")
-                .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
-            )));
+                    .setSvcRequestId("svc-request-id: xyz")
+                    .setSvcAction(SvcAction.Assign)
+                ))
+                .setRequestInformation(build(requestInformation()
+                    .setRequestId("request-id: xyz")
+                    .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
+                )));
 
         //execute the mdsal exec
         ServiceTopologyOperationOutput output = exec(
@@ -149,7 +145,7 @@ public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderT
 
 
     @Test
-    public void serviceTopologyOperation_should_fail_when_client_execution_failed() throws Exception {
+    public void should_fail_when_client_execution_failed() throws Exception {
         svcClient.mockHasGraph(true);
         svcClient.mockExecute(new RuntimeException("test exception"));
 
@@ -168,7 +164,50 @@ public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderT
     }
 
     @Test
-    public void serviceTopologyOperation_should_fail_when_client_has_no_graph() throws Exception {
+    public void delete_fail_when_client_execution_failed() throws Exception {
+
+        //mock svcClient to perform a successful execution with the expected parameters
+        svcClient.mockHasGraph(true);
+        PropBuilder svcResultProp = svcClient.createExecuteOKResult();
+        svcClient.mockExecute(svcResultProp);
+
+        ServiceTopologyOperationInput input = deleteSTOI();
+
+        //execute the mdsal exec
+        ServiceTopologyOperationOutput output = exec(
+                genericResourceApiProvider::serviceTopologyOperation
+                , input
+                , RpcResult::getResult
+        );
+
+        assertEquals("200", output.getResponseCode());
+        assertEquals("OK", output.getResponseMessage());
+        assertEquals("Y", output.getAckFinalIndicator());
+    }
+
+    @Test
+    public void delete_service_fail_when_client_execution_failed() throws Exception {
+
+        //mock svcClient to perform a successful execution with the expected parameters
+        svcClient.mockHasGraph(true);
+        PropBuilder svcResultProp = svcClient.createExecuteOKResult();
+        svcClient.mockExecute(svcResultProp);
+
+        ServiceTopologyOperationInput input = deleteServiceSTOI();
+
+        //execute the mdsal exec
+        ServiceTopologyOperationOutput output = exec(
+                genericResourceApiProvider::serviceTopologyOperation
+                , input
+                , RpcResult::getResult
+        );
+
+        assertEquals("500", output.getResponseCode());
+        assertEquals("Y", output.getAckFinalIndicator());
+    }
+
+    @Test
+    public void should_fail_when_client_has_no_graph() throws Exception {
         svcClient.mockHasGraph(false);
 
         ServiceTopologyOperationInput input = createSTOI();
@@ -187,7 +226,7 @@ public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderT
 
 
     @Test
-    public void serviceTopologyOperation_should_fail_when_failed_to_update_mdsal() throws Exception {
+    public void should_fail_when_failed_to_update_mdsal() throws Exception {
 
         svcClient.mockHasGraph(true);
         WriteTransaction mockWriteTransaction = mock(WriteTransaction.class);
@@ -211,8 +250,6 @@ public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderT
         assertEquals("Y", output.getAckFinalIndicator());
     }
 
-
-
     private ServiceTopologyOperationInput createSTOI() {
 
         return build(
@@ -231,6 +268,42 @@ public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderT
         );
     }
 
+    private ServiceTopologyOperationInput deleteSTOI() {
+
+        return build(
+                serviceTopologyOperationInput()
+                        .setSdncRequestHeader(build(sdncRequestHeader()
+                                .setSvcRequestId("svc-request-id: xyz")
+                                .setSvcAction(SvcAction.Unassign)
+                        ))
+                        .setRequestInformation(build(requestInformation()
+                                .setRequestId("request-id: xyz")
+                                .setRequestAction(RequestInformation.RequestAction.DeleteServiceInstance)
+                        ))
+                        .setServiceInformation(build(serviceInformationBuilder()
+                                .setServiceInstanceId("service-instance-id: xyz")
+                        ))
+        );
+    }
+
+    private ServiceTopologyOperationInput deleteServiceSTOI() {
+
+        return build(
+                serviceTopologyOperationInput()
+                        .setSdncRequestHeader(build(sdncRequestHeader()
+                                .setSvcRequestId("svc-request-id: xyz")
+                                .setSvcAction(SvcAction.Delete)
+                        ))
+                        .setRequestInformation(build(requestInformation()
+                                .setRequestId("request-id: xyz")
+                                .setRequestAction(RequestInformation.RequestAction.DeleteServiceInstance)
+                        ))
+                        .setServiceInformation(build(serviceInformationBuilder()
+                                .setServiceInstanceId("service-instance-id: xyz")
+                        ))
+        );
+    }
+
 
     private ServiceTopologyOperationOutput createExpectedSTOO(PropBuilder expectedSvcResultProp,
         ServiceTopologyOperationInput expectedServiceTopologyOperationInput) {