From 32b6e452ab34939da6eb7ee23ea18244cff147ac Mon Sep 17 00:00:00 2001 From: "Smokowski, Steven" Date: Mon, 30 Mar 2020 13:22:32 -0400 Subject: Add validations to prevent out of order deletes Add validations to prevent out of order deletes Add additional junits to all validations Remove tests that are not longer needed Issue-ID: SO-2772 Signed-off-by: Benjamin, Max (mb388a) Change-Id: I00b8490691181bc1e95da25c73e0f215e1c2b0e2 --- .../onap/so/apihandlerinfra/ServiceInstances.java | 4 +- .../infra/rest/AAIDataRetrieval.java | 35 +++++++++ .../rest/validators/NetworkDeleteValidator.java | 34 +++++++++ .../infra/rest/validators/RequestValidator.java | 3 +- .../validators/RequestValidatorListenerRunner.java | 9 ++- .../validators/ServiceInstanceDeleteValidator.java | 38 ++++++++++ .../infra/rest/validators/VnfDeleteValidator.java | 34 +++++++++ .../validators/VolumeGroupDeleteValidator.java | 33 ++++++++ .../rest/validator/NetworkDeleteValidatorTest.java | 73 ++++++++++++++++++ .../ServiceInstanceDeleteValidatorTest.java | 88 ++++++++++++++++++++++ .../rest/validator/VnfDeleteValidatorTest.java | 67 ++++++++++++++++ .../validator/VolumeGroupDeleteValidatorTest.java | 69 +++++++++++++++++ 12 files changed, 480 insertions(+), 7 deletions(-) create mode 100644 mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/NetworkDeleteValidator.java create mode 100644 mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/ServiceInstanceDeleteValidator.java create mode 100644 mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VnfDeleteValidator.java create mode 100644 mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VolumeGroupDeleteValidator.java create mode 100644 mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/NetworkDeleteValidatorTest.java create mode 100644 mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/ServiceInstanceDeleteValidatorTest.java create mode 100644 mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VnfDeleteValidatorTest.java create mode 100644 mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VolumeGroupDeleteValidatorTest.java diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ServiceInstances.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ServiceInstances.java index f1e8e711c4..418ba05abf 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ServiceInstances.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ServiceInstances.java @@ -40,6 +40,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.apache.commons.lang.StringUtils; import org.apache.http.HttpStatus; +import org.onap.logging.filter.base.ErrorCode; import org.onap.so.apihandler.common.CommonConstants; import org.onap.so.apihandler.common.ErrorNumbers; import org.onap.so.apihandler.common.RequestClientParameter; @@ -56,7 +57,6 @@ import org.onap.so.db.catalog.client.CatalogDbClient; import org.onap.so.db.request.beans.InfraActiveRequests; import org.onap.so.db.request.client.RequestsDbClient; import org.onap.so.exceptions.ValidationException; -import org.onap.logging.filter.base.ErrorCode; import org.onap.so.logger.MessageEnum; import org.onap.so.serviceinstancebeans.CloudConfiguration; import org.onap.so.serviceinstancebeans.ModelInfo; @@ -823,7 +823,7 @@ public class ServiceInstances extends AbstractRestHandler { sir = requestHandlerUtils.convertJsonToServiceInstanceRequest(requestJSON, action, requestId, requestUri); action = handleReplaceInstance(action, sir); - requestValidatorListenerRunner.runValidations(requestUri, instanceIdMap, sir, queryParams); + requestValidatorListenerRunner.runValidations(requestUri, instanceIdMap, sir, queryParams, action); String requestScope = requestHandlerUtils.deriveRequestScope(action, sir, requestUri); InfraActiveRequests currentActiveReq = diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrieval.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrieval.java index fee7a3a8f4..4e3d8736c2 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrieval.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrieval.java @@ -9,6 +9,7 @@ import org.onap.aai.domain.yang.Tenant; import org.onap.aai.domain.yang.VfModule; import org.onap.aai.domain.yang.VolumeGroup; import org.onap.so.apihandlerinfra.infra.rest.exception.AAIEntityNotFound; +import org.onap.so.client.aai.AAIObjectPlurals; import org.onap.so.client.aai.AAIObjectType; import org.onap.so.client.aai.AAIResourcesClient; import org.onap.so.client.aai.entities.AAIResultWrapper; @@ -76,6 +77,40 @@ public class AAIDataRetrieval { }); } + + public boolean isVolumeGroupRelatedToVFModule(String volumeGroupId) { + return this.getAaiResourcesClient().exists(AAIUriFactory + .createResourceUri(AAIObjectType.VOLUME_GROUP, volumeGroupId).relatedTo(AAIObjectPlurals.VF_MODULE)); + } + + public boolean isVnfRelatedToVolumes(String vnfId) { + return this.getAaiResourcesClient().exists(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId) + .relatedTo(AAIObjectPlurals.VOLUME_GROUP)); + } + + public boolean isNetworkRelatedToModules(String networkId) { + return this.getAaiResourcesClient().exists(AAIUriFactory.createResourceUri(AAIObjectType.L3_NETWORK, networkId) + .relatedTo(AAIObjectPlurals.VF_MODULE)); + } + + public boolean isServiceRelatedToNetworks(String serviceInstanceId) { + return this.getAaiResourcesClient() + .exists(AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId) + .relatedTo(AAIObjectPlurals.L3_NETWORK)); + } + + public boolean isServiceRelatedToGenericVnf(String serviceInstanceId) { + return this.getAaiResourcesClient() + .exists(AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId) + .relatedTo(AAIObjectPlurals.GENERIC_VNF)); + } + + public boolean isServiceRelatedToConfiguration(String serviceInstanceId) { + return this.getAaiResourcesClient() + .exists(AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId) + .relatedTo(AAIObjectPlurals.CONFIGURATION)); + } + public Service getService(String serviceId) { return this.getAaiResourcesClient() .get(Service.class, AAIUriFactory.createResourceUri(AAIObjectType.SERVICE, serviceId)).orElseGet(() -> { diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/NetworkDeleteValidator.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/NetworkDeleteValidator.java new file mode 100644 index 0000000000..f75897dda1 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/NetworkDeleteValidator.java @@ -0,0 +1,34 @@ +package org.onap.so.apihandlerinfra.infra.rest.validators; + +import java.util.Map; +import java.util.Optional; +import java.util.regex.Pattern; +import org.onap.so.apihandlerinfra.Action; +import org.onap.so.apihandlerinfra.Actions; +import org.onap.so.apihandlerinfra.infra.rest.AAIDataRetrieval; +import org.onap.so.serviceinstancebeans.ServiceInstancesRequest; +import org.springframework.beans.factory.annotation.Autowired; + + +public class NetworkDeleteValidator implements RequestValidator { + + @Autowired + AAIDataRetrieval aaiDataRetrieval; + + @Override + public boolean shouldRunFor(String requestUri, ServiceInstancesRequest request, Actions action) { + return Pattern.compile("[Vv][5-8]/serviceInstances/[^/]+/networks/[^/]+").matcher(requestUri).matches() + && action.equals(Action.deleteInstance); + + } + + @Override + public Optional validate(Map instanceIdMap, ServiceInstancesRequest request, + Map queryParams) { + if (aaiDataRetrieval.isNetworkRelatedToModules(instanceIdMap.get("networkInstanceId"))) { + return Optional.of("Cannot delete network it is still related to existing vf-modules"); + } else { + return Optional.empty(); + } + } +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/RequestValidator.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/RequestValidator.java index 4aa60152dd..1dea57ad9c 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/RequestValidator.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/RequestValidator.java @@ -22,6 +22,7 @@ package org.onap.so.apihandlerinfra.infra.rest.validators; import java.util.Map; import java.util.Optional; +import org.onap.so.apihandlerinfra.Actions; import org.onap.so.serviceinstancebeans.ServiceInstancesRequest; public interface RequestValidator { @@ -32,7 +33,7 @@ public interface RequestValidator { * * @return */ - public boolean shouldRunFor(String uri, ServiceInstancesRequest request); + public boolean shouldRunFor(String uri, ServiceInstancesRequest request, Actions action); public Optional validate(Map instanceIdMap, ServiceInstancesRequest request, diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/RequestValidatorListenerRunner.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/RequestValidatorListenerRunner.java index d689c6b7a5..3aba39b501 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/RequestValidatorListenerRunner.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/RequestValidatorListenerRunner.java @@ -28,6 +28,7 @@ import java.util.Optional; import java.util.stream.Collectors; import javax.annotation.PostConstruct; import org.javatuples.Pair; +import org.onap.so.apihandlerinfra.Actions; import org.onap.so.apihandlerinfra.exceptions.ApiException; import org.onap.so.apihandlerinfra.exceptions.ValidateException; import org.onap.so.listener.ListenerRunner; @@ -50,10 +51,10 @@ public class RequestValidatorListenerRunner extends ListenerRunner { } public boolean runValidations(String requestURI, Map instanceIdMap, ServiceInstancesRequest request, - Map queryParams) throws ApiException { + Map queryParams, Actions action) throws ApiException { logger.info("Running local validations"); List>> results = - runValidations(requestValidators, instanceIdMap, request, queryParams, requestURI); + runValidations(requestValidators, instanceIdMap, request, queryParams, requestURI, action); if (!results.isEmpty()) { throw new ValidateException("Failed Validations:\n" + results.stream().map(item -> String.format("%s: %s", item.getValue0(), item.getValue1().get())) @@ -66,10 +67,10 @@ public class RequestValidatorListenerRunner extends ListenerRunner { protected List>> runValidations(List validators, Map instanceIdMap, ServiceInstancesRequest request, Map queryParams, - String requestURI) { + String requestURI, Actions action) { List filtered = - filterListeners(validators, (item -> item.shouldRunFor(requestURI, request))); + filterListeners(validators, (item -> item.shouldRunFor(requestURI, request, action))); List>> results = new ArrayList<>(); filtered.forEach(item -> results diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/ServiceInstanceDeleteValidator.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/ServiceInstanceDeleteValidator.java new file mode 100644 index 0000000000..0c7ba65cf4 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/ServiceInstanceDeleteValidator.java @@ -0,0 +1,38 @@ +package org.onap.so.apihandlerinfra.infra.rest.validators; + +import java.util.Map; +import java.util.Optional; +import java.util.regex.Pattern; +import org.onap.so.apihandlerinfra.Action; +import org.onap.so.apihandlerinfra.Actions; +import org.onap.so.apihandlerinfra.infra.rest.AAIDataRetrieval; +import org.onap.so.listener.Skip; +import org.onap.so.serviceinstancebeans.ServiceInstancesRequest; +import org.springframework.beans.factory.annotation.Autowired; + +@Skip +public class ServiceInstanceDeleteValidator implements RequestValidator { + + @Autowired + AAIDataRetrieval aaiDataRetrieval; + + @Override + public boolean shouldRunFor(String requestUri, ServiceInstancesRequest request, Actions action) { + return Pattern.compile("[Vv][5-8]/serviceInstances/[^/]+").matcher(requestUri).matches() + && action.equals(Action.deleteInstance); + } + + @Override + public Optional validate(Map instanceIdMap, ServiceInstancesRequest request, + Map queryParams) { + if (aaiDataRetrieval.isServiceRelatedToGenericVnf(instanceIdMap.get("serviceInstanceId"))) { + return Optional.of("Cannot delete service it is still related to existing vf-modules"); + } else if (aaiDataRetrieval.isServiceRelatedToNetworks(instanceIdMap.get("serviceInstanceId"))) { + return Optional.of("Cannot delete service it is still related to existing networks"); + } else if (aaiDataRetrieval.isServiceRelatedToConfiguration(instanceIdMap.get("serviceInstanceId"))) { + return Optional.of("Cannot delete service it is still related to existing configurations"); + } else { + return Optional.empty(); + } + } +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VnfDeleteValidator.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VnfDeleteValidator.java new file mode 100644 index 0000000000..d54e60d153 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VnfDeleteValidator.java @@ -0,0 +1,34 @@ +package org.onap.so.apihandlerinfra.infra.rest.validators; + +import java.util.Map; +import java.util.Optional; +import java.util.regex.Pattern; +import org.onap.so.apihandlerinfra.Action; +import org.onap.so.apihandlerinfra.Actions; +import org.onap.so.apihandlerinfra.infra.rest.AAIDataRetrieval; +import org.onap.so.serviceinstancebeans.ServiceInstancesRequest; +import org.springframework.beans.factory.annotation.Autowired; + + +public class VnfDeleteValidator implements RequestValidator { + + @Autowired + AAIDataRetrieval aaiDataRetrieval; + + @Override + public boolean shouldRunFor(String requestUri, ServiceInstancesRequest request, Actions action) { + return Pattern.compile("[Vv][5-8]/serviceInstances/[^/]+/vnfs/[^/]+").matcher(requestUri).matches() + && action.equals(Action.deleteInstance); + } + + @Override + public Optional validate(Map instanceIdMap, ServiceInstancesRequest request, + Map queryParams) { + if (aaiDataRetrieval.isVnfRelatedToVolumes(instanceIdMap.get("vnfInstanceId"))) { + return Optional.of("Cannot delete vnf it is still related to existing volume groups"); + } else { + return Optional.empty(); + } + } + +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VolumeGroupDeleteValidator.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VolumeGroupDeleteValidator.java new file mode 100644 index 0000000000..f010d47f70 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VolumeGroupDeleteValidator.java @@ -0,0 +1,33 @@ +package org.onap.so.apihandlerinfra.infra.rest.validators; + +import java.util.Map; +import java.util.Optional; +import java.util.regex.Pattern; +import org.onap.so.apihandlerinfra.Action; +import org.onap.so.apihandlerinfra.Actions; +import org.onap.so.apihandlerinfra.infra.rest.AAIDataRetrieval; +import org.onap.so.serviceinstancebeans.ServiceInstancesRequest; +import org.springframework.beans.factory.annotation.Autowired; + +public class VolumeGroupDeleteValidator implements RequestValidator { + + @Autowired + AAIDataRetrieval aaiDataRetrieval; + + @Override + public boolean shouldRunFor(String requestUri, ServiceInstancesRequest request, Actions action) { + return Pattern.compile("[Vv][5-8]/serviceInstances/[^/]+/volumeGroups/[^/]+").matcher(requestUri).matches() + && action.equals(Action.deleteInstance); + } + + @Override + public Optional validate(Map instanceIdMap, ServiceInstancesRequest request, + Map queryParams) { + if (aaiDataRetrieval.isVolumeGroupRelatedToVFModule(instanceIdMap.get("volumeGroupInstanceId"))) { + return Optional.of("Cannot delete volume group it is related to existing vf-modules"); + } else { + return Optional.empty(); + } + } + +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/NetworkDeleteValidatorTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/NetworkDeleteValidatorTest.java new file mode 100644 index 0000000000..7780f0ee2b --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/NetworkDeleteValidatorTest.java @@ -0,0 +1,73 @@ +package org.onap.so.apihandlerinfra.infra.rest.validator; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.apihandlerinfra.Action; +import org.onap.so.apihandlerinfra.infra.rest.AAIDataRetrieval; +import org.onap.so.apihandlerinfra.infra.rest.validators.NetworkDeleteValidator; +import org.onap.so.serviceinstancebeans.ServiceInstancesRequest; + + +@RunWith(MockitoJUnitRunner.class) +public class NetworkDeleteValidatorTest { + + + @InjectMocks + @Spy + private NetworkDeleteValidator networkValidator; + + @Mock + private AAIDataRetrieval aaiDataRetrieval; + + private Map instanceIdMap = new HashMap<>(); + + @Test + public void validateURIMatchTest() { + assertEquals(true, networkValidator.shouldRunFor("v8/serviceInstances/uasdfasdf/networks/asdfasdf", + new ServiceInstancesRequest(), Action.deleteInstance)); + } + + @Test + public void validateURINotMatchTest() { + assertEquals(false, networkValidator.shouldRunFor("v8/serviceInstances/uasdfasdf/vnfs/asdfasdf", + new ServiceInstancesRequest(), Action.deleteInstance)); + } + + @Test + public void validateURINotMatch2Test() { + assertEquals(false, networkValidator.shouldRunFor("v8/serviceInstances/uasdfasdf/networks/asdfasdf/update", + new ServiceInstancesRequest(), Action.deleteInstance)); + } + + @Test + public void validateURINotMatchActionTest() { + assertEquals(false, networkValidator.shouldRunFor("v8/serviceInstances/uasdfasdf/networks/asdfasdf", + new ServiceInstancesRequest(), Action.createInstance)); + } + + @Test + public void validateSuccessTest() { + instanceIdMap.put("networkInstanceId", "1"); + when(aaiDataRetrieval.isNetworkRelatedToModules("1")).thenReturn(false); + Optional result = networkValidator.validate(instanceIdMap, null, null); + assertEquals(false, result.isPresent()); + } + + @Test + public void validateFailureTest() { + instanceIdMap.put("networkInstanceId", "1"); + when(aaiDataRetrieval.isNetworkRelatedToModules("1")).thenReturn(true); + Optional result = networkValidator.validate(instanceIdMap, null, null); + assertEquals(true, result.isPresent()); + } + +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/ServiceInstanceDeleteValidatorTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/ServiceInstanceDeleteValidatorTest.java new file mode 100644 index 0000000000..c334d522b6 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/ServiceInstanceDeleteValidatorTest.java @@ -0,0 +1,88 @@ +package org.onap.so.apihandlerinfra.infra.rest.validator; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.apihandlerinfra.Action; +import org.onap.so.apihandlerinfra.infra.rest.AAIDataRetrieval; +import org.onap.so.apihandlerinfra.infra.rest.validators.ServiceInstanceDeleteValidator; +import org.onap.so.serviceinstancebeans.ServiceInstancesRequest; + + +@RunWith(MockitoJUnitRunner.class) +public class ServiceInstanceDeleteValidatorTest { + + + @InjectMocks + @Spy + private ServiceInstanceDeleteValidator serviceValidator; + + @Mock + private AAIDataRetrieval aaiDataRetrieval; + + private Map instanceIdMap = new HashMap<>(); + + @Test + public void validateURIMatchTest() { + assertEquals(true, serviceValidator.shouldRunFor("v8/serviceInstances/uasdfasdf", new ServiceInstancesRequest(), + Action.deleteInstance)); + } + + @Test + public void validateURINotMatchTest() { + assertEquals(false, serviceValidator.shouldRunFor("v8/serviceInstances/uasdfasdf/vnfs/asdfasdf", + new ServiceInstancesRequest(), Action.deleteInstance)); + } + + @Test + public void validateURINotMatch2Test() { + assertEquals(false, serviceValidator.shouldRunFor("v8/serviceInstances/uasdfasdf/update", + new ServiceInstancesRequest(), Action.deleteInstance)); + } + + @Test + public void validateSuccessTest() { + instanceIdMap.put("serviceInstanceId", "1"); + when(aaiDataRetrieval.isServiceRelatedToGenericVnf("1")).thenReturn(false); + when(aaiDataRetrieval.isServiceRelatedToNetworks("1")).thenReturn(false); + when(aaiDataRetrieval.isServiceRelatedToConfiguration("1")).thenReturn(false); + Optional result = serviceValidator.validate(instanceIdMap, null, null); + assertEquals(false, result.isPresent()); + } + + @Test + public void validateFailureVnfTest() { + instanceIdMap.put("serviceInstanceId", "1"); + when(aaiDataRetrieval.isServiceRelatedToGenericVnf("1")).thenReturn(true); + Optional result = serviceValidator.validate(instanceIdMap, null, null); + assertEquals(true, result.isPresent()); + } + + @Test + public void validateFailureNetworksTest() { + instanceIdMap.put("serviceInstanceId", "1"); + when(aaiDataRetrieval.isServiceRelatedToGenericVnf("1")).thenReturn(false); + when(aaiDataRetrieval.isServiceRelatedToNetworks("1")).thenReturn(true); + Optional result = serviceValidator.validate(instanceIdMap, null, null); + assertEquals(true, result.isPresent()); + } + + @Test + public void validateFailureConfigurationTest() { + instanceIdMap.put("serviceInstanceId", "1"); + when(aaiDataRetrieval.isServiceRelatedToGenericVnf("1")).thenReturn(false); + when(aaiDataRetrieval.isServiceRelatedToNetworks("1")).thenReturn(false); + when(aaiDataRetrieval.isServiceRelatedToConfiguration("1")).thenReturn(true); + Optional result = serviceValidator.validate(instanceIdMap, null, null); + assertEquals(true, result.isPresent()); + } + +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VnfDeleteValidatorTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VnfDeleteValidatorTest.java new file mode 100644 index 0000000000..d4f57d3146 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VnfDeleteValidatorTest.java @@ -0,0 +1,67 @@ +package org.onap.so.apihandlerinfra.infra.rest.validator; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.apihandlerinfra.Action; +import org.onap.so.apihandlerinfra.infra.rest.AAIDataRetrieval; +import org.onap.so.apihandlerinfra.infra.rest.validators.VnfDeleteValidator; +import org.onap.so.serviceinstancebeans.ServiceInstancesRequest; + + +@RunWith(MockitoJUnitRunner.class) +public class VnfDeleteValidatorTest { + + + @InjectMocks + @Spy + private VnfDeleteValidator vnfValidator; + + @Mock + private AAIDataRetrieval aaiDataRetrieval; + + private Map instanceIdMap = new HashMap<>(); + + @Test + public void validateURIMatchTest() { + assertEquals(true, vnfValidator.shouldRunFor("v8/serviceInstances/uasdfasdf/vnfs/asdfasdf", + new ServiceInstancesRequest(), Action.deleteInstance)); + } + + @Test + public void validateURINotMatchTest() { + assertEquals(false, vnfValidator.shouldRunFor("v8/serviceInstances/uasdfasdf/vnfs/asdfasdf/replace", + new ServiceInstancesRequest(), Action.deleteInstance)); + } + + @Test + public void validateURINotMatch2Test() { + assertEquals(false, vnfValidator.shouldRunFor("v8/serviceInstances/uasdfasdf", new ServiceInstancesRequest(), + Action.deleteInstance)); + } + + @Test + public void validateSuccessTest() { + instanceIdMap.put("vnfInstanceId", "1"); + when(aaiDataRetrieval.isVnfRelatedToVolumes("1")).thenReturn(false); + Optional result = vnfValidator.validate(instanceIdMap, null, null); + assertEquals(false, result.isPresent()); + } + + @Test + public void validateFailureVnfTest() { + instanceIdMap.put("vnfInstanceId", "1"); + when(aaiDataRetrieval.isVnfRelatedToVolumes("1")).thenReturn(true); + Optional result = vnfValidator.validate(instanceIdMap, null, null); + assertEquals(true, result.isPresent()); + } + +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VolumeGroupDeleteValidatorTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VolumeGroupDeleteValidatorTest.java new file mode 100644 index 0000000000..3d81ee66c9 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VolumeGroupDeleteValidatorTest.java @@ -0,0 +1,69 @@ +package org.onap.so.apihandlerinfra.infra.rest.validator; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.apihandlerinfra.Action; +import org.onap.so.apihandlerinfra.infra.rest.AAIDataRetrieval; +import org.onap.so.apihandlerinfra.infra.rest.validators.VolumeGroupDeleteValidator; +import org.onap.so.serviceinstancebeans.ServiceInstancesRequest; + + +@RunWith(MockitoJUnitRunner.class) +public class VolumeGroupDeleteValidatorTest { + + + @InjectMocks + @Spy + private VolumeGroupDeleteValidator volumeGroupDeleteValidator; + + @Mock + private AAIDataRetrieval aaiDataRetrieval; + + private Map instanceIdMap = new HashMap<>(); + + @Test + public void validateURIMatchTest() { + assertEquals(true, volumeGroupDeleteValidator.shouldRunFor("v8/serviceInstances/uasdfasdf/volumeGroups/uuid", + new ServiceInstancesRequest(), Action.deleteInstance)); + } + + @Test + public void validateURINotMatchTest() { + assertEquals(false, + volumeGroupDeleteValidator.shouldRunFor( + "v8/serviceInstances/uasdfasdf/vnfs/asdfasdf/volumeGroups/uuid/replace", + new ServiceInstancesRequest(), Action.deleteInstance)); + } + + @Test + public void validateURINotMatch2Test() { + assertEquals(false, volumeGroupDeleteValidator.shouldRunFor("v8/serviceInstances/uasdfasdf/vnfs/uuid", + new ServiceInstancesRequest(), Action.deleteInstance)); + } + + @Test + public void validateSuccessTest() { + instanceIdMap.put("volumeGroupInstanceId", "1"); + when(aaiDataRetrieval.isVolumeGroupRelatedToVFModule("1")).thenReturn(false); + Optional result = volumeGroupDeleteValidator.validate(instanceIdMap, null, null); + assertEquals(false, result.isPresent()); + } + + @Test + public void validateFailureVnfTest() { + instanceIdMap.put("volumeGroupInstanceId", "1"); + when(aaiDataRetrieval.isVolumeGroupRelatedToVFModule("1")).thenReturn(true); + Optional result = volumeGroupDeleteValidator.validate(instanceIdMap, null, null); + assertEquals(true, result.isPresent()); + } + +} -- cgit 1.2.3-korg