From 7e2a79d09429101a16fa1011770651a1880afd74 Mon Sep 17 00:00:00 2001 From: Jakub Dudycz Date: Mon, 19 Mar 2018 15:40:51 +0100 Subject: GenericResourceApiProvider unit tests part 2. Unit tests for vnfTopologyOperation method Change-Id: I60c73f31eb070cf554e55779d1d18631ce9b7147 Issue-ID: SDNC-275 Signed-off-by: Jakub Dudycz --- .../ServiceTopologyOperationRPCTest.java | 32 ++-- .../northbound/VnfTopologyOperationRPCTest.java | 194 +++++++++++++++++++++ .../org/onap/sdnc/northbound/util/MDSALUtil.java | 77 +++++--- 3 files changed, 259 insertions(+), 44 deletions(-) create mode 100644 generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/VnfTopologyOperationRPCTest.java (limited to 'generic-resource-api/provider/src/test/java/org') diff --git a/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/ServiceTopologyOperationRPCTest.java b/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/ServiceTopologyOperationRPCTest.java index b4ce214a..ee0bdb50 100644 --- a/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/ServiceTopologyOperationRPCTest.java +++ b/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/ServiceTopologyOperationRPCTest.java @@ -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} @@ -122,18 +114,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 +141,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 +160,7 @@ public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderT } @Test - public void serviceTopologyOperation_should_fail_when_client_has_no_graph() throws Exception { + public void should_fail_when_client_has_no_graph() throws Exception { svcClient.mockHasGraph(false); ServiceTopologyOperationInput input = createSTOI(); @@ -187,7 +179,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 +203,6 @@ public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderT assertEquals("Y", output.getAckFinalIndicator()); } - - private ServiceTopologyOperationInput createSTOI() { return build( diff --git a/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/VnfTopologyOperationRPCTest.java b/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/VnfTopologyOperationRPCTest.java new file mode 100644 index 00000000..a39146b2 --- /dev/null +++ b/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/VnfTopologyOperationRPCTest.java @@ -0,0 +1,194 @@ +package org.onap.sdnc.northbound; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.onap.sdnc.northbound.GenericResourceApiProvider.APP_NAME; +import static org.onap.sdnc.northbound.GenericResourceApiProvider.NO_SERVICE_LOGIC_ACTIVE; +import static org.onap.sdnc.northbound.GenericResourceApiProvider.NULL_OR_EMPTY_ERROR_PARAM; +import static org.onap.sdnc.northbound.util.MDSALUtil.build; +import static org.onap.sdnc.northbound.util.MDSALUtil.exec; +import static org.onap.sdnc.northbound.util.MDSALUtil.requestInformation; +import static org.onap.sdnc.northbound.util.MDSALUtil.sdncRequestHeader; +import static org.onap.sdnc.northbound.util.MDSALUtil.serviceInformationBuilder; +import static org.onap.sdnc.northbound.util.MDSALUtil.serviceResponseInformation; +import static org.onap.sdnc.northbound.util.MDSALUtil.vnfInformationBuilder; +import static org.onap.sdnc.northbound.util.MDSALUtil.vnfTopologyOperationInput; +import static org.onap.sdnc.northbound.util.MDSALUtil.vnfTopologyOperationOutput; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.sdnc.northbound.util.PropBuilder; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.VnfTopologyOperationInput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.VnfTopologyOperationOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformation; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader.SvcAction; +import org.opendaylight.yangtools.yang.common.RpcResult; + +@RunWith(MockitoJUnitRunner.class) +public class VnfTopologyOperationRPCTest extends GenericResourceApiProviderTest { + + private static final String SVC_OPERATION = "vnf-topology-operation"; + + @Before + public void setUp() throws Exception { + super.setUp(); + svcClient.setScvOperation(SVC_OPERATION); + } + + @Test + public void should_fail_when_service_info_not_present() throws Exception { + + VnfTopologyOperationInput input = build(vnfTopologyOperationInput()); + + VnfTopologyOperationOutput output = + exec(genericResourceApiProvider::vnfTopologyOperation, input, RpcResult::getResult); + + assertEquals("404", output.getResponseCode()); + assertEquals(NULL_OR_EMPTY_ERROR_PARAM, output.getResponseMessage()); + assertEquals("Y", output.getAckFinalIndicator()); + } + + + @Test + public void should_fail_when_invalid_vnf_id() throws Exception { + + VnfTopologyOperationInput input = build(vnfTopologyOperationInput() + .setServiceInformation(build(serviceInformationBuilder(). + setServiceInstanceId("test-service-instance-id"))) + ); + + VnfTopologyOperationOutput output = + exec(genericResourceApiProvider::vnfTopologyOperation, input, RpcResult::getResult); + + assertEquals("404", output.getResponseCode()); + assertEquals("invalid input, null or empty vnf-id", output.getResponseMessage()); + assertEquals("Y", output.getAckFinalIndicator()); + } + + + @Test + public void should_fail_when_client_execution_failed() throws Exception { + + svcClient.mockHasGraph(true); + svcClient.mockExecute(new RuntimeException("test exception")); + + VnfTopologyOperationInput input = build(vnfTopologyOperationInput() + .setServiceInformation(build(serviceInformationBuilder() + .setServiceInstanceId("test-service-instance-id"))) + .setVnfInformation(build(vnfInformationBuilder() + .setVnfId("test-vnf-id"))) + ); + + VnfTopologyOperationOutput output = + exec(genericResourceApiProvider::vnfTopologyOperation, input, RpcResult::getResult); + + assertEquals("500", output.getResponseCode()); + assertEquals("test exception", output.getResponseMessage()); + assertEquals("Y", output.getAckFinalIndicator()); + } + + @Test + public void should_fail_when_client_has_no_graph() throws Exception { + + svcClient.mockHasGraph(false); + + VnfTopologyOperationInput input = build(vnfTopologyOperationInput() + .setServiceInformation(build(serviceInformationBuilder() + .setServiceInstanceId("test-service-instance-id"))) + .setVnfInformation(build(vnfInformationBuilder() + .setVnfId("test-vnf-id"))) + ); + + VnfTopologyOperationOutput output = + exec(genericResourceApiProvider::vnfTopologyOperation, input, RpcResult::getResult); + + assertEquals("503", output.getResponseCode()); + assertEquals(NO_SERVICE_LOGIC_ACTIVE + APP_NAME + ": '" + SVC_OPERATION + "'", output.getResponseMessage()); + assertEquals("Y", output.getAckFinalIndicator()); + } + + @Test + public void should_fail_when_failed_to_update_mdsal() throws Exception { + + PropBuilder svcResultProp = svcClient.createExecuteOKResult(); + svcClient.mockExecute(svcResultProp); + svcClient.mockHasGraph(true); + WriteTransaction mockWriteTransaction = mock(WriteTransaction.class); + when(mockWriteTransaction.submit()).thenThrow(new TransactionChainClosedException("test exception")); + + DataBroker spyDataBroker = Mockito.spy(dataBroker); + when(spyDataBroker.newWriteOnlyTransaction()).thenReturn(mockWriteTransaction); + genericResourceApiProvider.setDataBroker(spyDataBroker); + + VnfTopologyOperationInput input = build(vnfTopologyOperationInput() + .setServiceInformation(build(serviceInformationBuilder() + .setServiceInstanceId("test-service-instance-id"))) + .setVnfInformation(build(vnfInformationBuilder() + .setVnfId("test-vnf-id"))) + ); + + VnfTopologyOperationOutput output = + exec(genericResourceApiProvider::vnfTopologyOperation, input, RpcResult::getResult); + + assertEquals("500", output.getResponseCode()); + assertEquals("test exception", output.getResponseMessage()); + assertEquals("Y", output.getAckFinalIndicator()); + } + + @Test + public void should_complete_with_success_when_no_errors() throws Exception { + + svcClient.mockHasGraph(true); + PropBuilder svcResultProp = svcClient.createExecuteOKResult(); + svcClient.mockExecute(svcResultProp); + + VnfTopologyOperationInput input = build(vnfTopologyOperationInput() + .setSdncRequestHeader(build(sdncRequestHeader() + .setSvcRequestId("test-svc-request-id") + .setSvcAction(SvcAction.Assign) + )) + .setRequestInformation(build(requestInformation() + .setRequestId("test-request-id") + .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance) + )) + .setServiceInformation(build(serviceInformationBuilder() + .setServiceInstanceId("test-service-instance-id"))) + .setVnfInformation(build(vnfInformationBuilder() + .setVnfId("test-vnf-id"))) + + ); + + VnfTopologyOperationOutput output = + exec(genericResourceApiProvider::vnfTopologyOperation, input, RpcResult::getResult); + + assertEquals("200", output.getResponseCode()); + assertEquals("OK", output.getResponseMessage()); + assertEquals("Y", output.getAckFinalIndicator()); + + VnfTopologyOperationOutput expectedVnfTopologyOperationOutput = createExpectedVTOO(svcResultProp, + input); + assertEquals(expectedVnfTopologyOperationOutput, output); + } + + private VnfTopologyOperationOutput createExpectedVTOO(PropBuilder expectedSvcResultProp, + VnfTopologyOperationInput vnfTopologyOperationInput) { + return build( + vnfTopologyOperationOutput() + .setSvcRequestId(vnfTopologyOperationInput.getSdncRequestHeader().getSvcRequestId()) + .setResponseCode(expectedSvcResultProp.get(svcClient.errorCode)) + .setAckFinalIndicator(expectedSvcResultProp.get(svcClient.ackFinal)) + .setResponseMessage(expectedSvcResultProp.get(svcClient.errorMessage)) + .setServiceResponseInformation(build(serviceResponseInformation() + .setInstanceId(vnfTopologyOperationInput.getServiceInformation().getServiceInstanceId()) + .setObjectPath(expectedSvcResultProp.get("vnf-object-path")) + )) + ); + } +} diff --git a/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/util/MDSALUtil.java b/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/util/MDSALUtil.java index 11610659..8474eab3 100644 --- a/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/util/MDSALUtil.java +++ b/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/util/MDSALUtil.java @@ -21,10 +21,15 @@ package org.onap.sdnc.northbound.util; +import java.util.concurrent.Future; +import java.util.function.Consumer; +import java.util.function.Function; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.NetworkTopologyOperationInputBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.NetworkTopologyOperationOutputBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationInputBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationOutputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.VnfTopologyOperationInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.VnfTopologyOperationOutputBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.network.information.NetworkInformationBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.network.response.information.NetworkResponseInformationBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformationBuilder; @@ -35,13 +40,10 @@ 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.model.infrastructure.ServiceBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.response.information.ServiceResponseInformationBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatusBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.vnf.information.VnfInformationBuilder; import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.common.RpcResult; -import java.util.concurrent.Future; -import java.util.function.Consumer; -import java.util.function.Function; - /** * This uill class provides utility to build yang objects using a recursive syntax that resembles the tree structure @@ -70,12 +72,19 @@ import java.util.function.Function; */ public class MDSALUtil { + public static VnfTopologyOperationInputBuilder vnfTopologyOperationInput() { + return new VnfTopologyOperationInputBuilder(); + } + + public static VnfTopologyOperationOutputBuilder vnfTopologyOperationOutput() { + return new VnfTopologyOperationOutputBuilder(); + } + public static ServiceTopologyOperationInputBuilder serviceTopologyOperationInput() { return new ServiceTopologyOperationInputBuilder(); } - - public static ServiceTopologyOperationOutputBuilder serviceTopologyOperationOutput(){ + public static ServiceTopologyOperationOutputBuilder serviceTopologyOperationOutput() { return new ServiceTopologyOperationOutputBuilder(); } @@ -89,47 +98,68 @@ public class MDSALUtil { return new RequestInformationBuilder(); } - public static ServiceResponseInformationBuilder serviceResponseInformation(){ - return new ServiceResponseInformationBuilder(); + public static ServiceResponseInformationBuilder serviceResponseInformation() { + return new ServiceResponseInformationBuilder(); } public static ServiceInformationBuilder serviceInformationBuilder() { - return new ServiceInformationBuilder(); + return new ServiceInformationBuilder(); } + public static VnfInformationBuilder vnfInformationBuilder() { + return new VnfInformationBuilder(); + } - public static ServiceBuilder service(){return new ServiceBuilder();} + public static ServiceBuilder service() { + return new ServiceBuilder(); + } - public static ServiceDataBuilder serviceData(){return new ServiceDataBuilder();} + public static ServiceDataBuilder serviceData() { + return new ServiceDataBuilder(); + } - public static ServiceStatusBuilder serviceStatus(){return new ServiceStatusBuilder();} - public static NetworkInformationBuilder networkInformation(){return new NetworkInformationBuilder();} + public static ServiceStatusBuilder serviceStatus() { + return new ServiceStatusBuilder(); + } - public static NetworkTopologyOperationInputBuilder networkTopologyOperationInput() {return new NetworkTopologyOperationInputBuilder();} + public static NetworkInformationBuilder networkInformation() { + return new NetworkInformationBuilder(); + } - public static NetworkTopologyOperationOutputBuilder networkTopologyOperationOutput() {return new NetworkTopologyOperationOutputBuilder();} + public static NetworkTopologyOperationInputBuilder networkTopologyOperationInput() { + return new NetworkTopologyOperationInputBuilder(); + } - public static NetworkResponseInformationBuilder networkResponseInformation(){return new NetworkResponseInformationBuilder();} + public static NetworkTopologyOperationOutputBuilder networkTopologyOperationOutput() { + return new NetworkTopologyOperationOutputBuilder(); + } - public static ServiceLevelOperStatusBuilder serviceLevelOperStatus() {return new ServiceLevelOperStatusBuilder();} + public static NetworkResponseInformationBuilder networkResponseInformation() { + return new NetworkResponseInformationBuilder(); + } + + public static ServiceLevelOperStatusBuilder serviceLevelOperStatus() { + return new ServiceLevelOperStatusBuilder(); + } public static

P build(Builder

b) { - return b == null? null :b.build(); + return b == null ? null : b.build(); } - public static > P build(Function builderConstructor,P sourceDataObject){ - if(sourceDataObject == null){ + public static > P build(Function builderConstructor, P sourceDataObject) { + if (sourceDataObject == null) { return null; } B bp = builderConstructor.apply(sourceDataObject); return bp.build(); } - public static > P build(Function builderConstructor,P sourceDataObject,Consumer builder){ - if(sourceDataObject == null){ + public static > P build(Function builderConstructor, P sourceDataObject, + Consumer builder) { + if (sourceDataObject == null) { return null; } B bp = builderConstructor.apply(sourceDataObject); @@ -137,7 +167,8 @@ public class MDSALUtil { return bp.build(); } - public static O exec(Function>> rpc,I rpcParameter,Function,O> rpcResult) throws Exception { + public static O exec(Function>> rpc, I rpcParameter, + Function, O> rpcResult) throws Exception { Future> future = rpc.apply(rpcParameter); return rpcResult.apply(future.get()); } -- cgit 1.2.3-korg