From aea64ba99fa9c9b51112b30aeb0872c4cdb89759 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Wed, 18 Aug 2021 14:34:33 +0100 Subject: Validate the Resource Model before importing VSP Validates the Resource Model selected during the Import VSP. This model must be aligned with one of the models that the imported VSP relates to. Fix VSP package retrieval to get the exact version from the VSP chosen during the Import VSP process. Introduces a client in the Catalog to retrieve from the Onboarding backend a VSP information using its Id and version Id. Change-Id: Ic8fb52b6daadc0e7203c81a9c15c3e46d5b9fffb Issue-ID: SDC-3675 Signed-off-by: andre.schmid --- .../be/components/csar/CsarBusinessLogicTest.java | 272 +++++++++++++++------ .../components/impl/ResourceBusinessLogicTest.java | 11 +- 2 files changed, 200 insertions(+), 83 deletions(-) (limited to 'catalog-be/src/test/java/org/openecomp') diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogicTest.java index 91ec89540c..66c8a51471 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogicTest.java @@ -1,45 +1,62 @@ /* - - * Copyright (c) 2018 Huawei Intellectual Property. - - * Modifications Copyright (c) 2019 Samsung - - * Licensed under the Apache License, Version 2.0 (the "License"); - - * you may not use this file except in compliance with the License. - - * You may obtain a copy of the License at - + * - + * ============LICENSE_START======================================================= + * Copyright (c) 2018 Huawei Intellectual Property. + * Modifications Copyright (c) 2019 Samsung. + * Modifications Copyright (c) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - - * http://www.apache.org/licenses/LICENSE-2.0 - + * http://www.apache.org/licenses/LICENSE-2.0 * - - * Unless required by applicable law or agreed to in writing, software - - * distributed under the License is distributed on an "AS IS" BASIS, - - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - - * See the License for the specific language governing permissions and - - * limitations under the License. - + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= */ package org.openecomp.sdc.be.components.csar; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import fj.data.Either; -import org.junit.Before; -import org.junit.Test; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.openecomp.sdc.be.components.impl.BaseBusinessLogicMock; +import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.VendorSoftwareProduct; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.CsarOperation; @@ -48,33 +65,17 @@ import org.openecomp.sdc.common.zip.ZipUtils; import org.openecomp.sdc.common.zip.exception.ZipException; import org.openecomp.sdc.exception.ResponseFormat; -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; +class CsarBusinessLogicTest extends BaseBusinessLogicMock { -public class CsarBusinessLogicTest extends BaseBusinessLogicMock { + private final CsarOperation csarOperation = Mockito.mock(CsarOperation.class); + private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class); + private final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class); + private final User user = Mockito.mock(User.class); + private final YamlTemplateParsingHandler yamlHandler = Mockito.mock(YamlTemplateParsingHandler.class); - private CsarOperation csarOperation = Mockito.mock(CsarOperation.class); - private ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class); - private ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class); - private User user = Mockito.mock(User.class); - private YamlTemplateParsingHandler yamlHandler = Mockito.mock(YamlTemplateParsingHandler.class); - - private CsarBusinessLogic test = new CsarBusinessLogic(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, - groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation, yamlHandler, artifactToscaOperation); + private final CsarBusinessLogic csarBusinessLogic = new CsarBusinessLogic(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, + interfaceOperation, interfaceLifecycleTypeOperation, yamlHandler, artifactToscaOperation); private static final String CSAR_UUID = "csarUUID"; private static final String CSAR_ENTRY = "Definitions/tosca_mock_vf.yaml"; @@ -88,31 +89,38 @@ public class CsarBusinessLogicTest extends BaseBusinessLogicMock { private static final String RESOURCE_NAME = "resourceName"; private static final String PAYLOAD_NAME = "mock_vf.csar"; - @Before - public void setUp() throws Exception { - test.setCsarOperation(csarOperation); - test.setToscaOperationFacade(toscaOperationFacade); - test.setComponentsUtils(componentsUtils); + @BeforeEach + void setUp() throws Exception { + csarBusinessLogic.setCsarOperation(csarOperation); + csarBusinessLogic.setToscaOperationFacade(toscaOperationFacade); + csarBusinessLogic.setComponentsUtils(componentsUtils); } - @Test() - public void testGetCsarInfo() { + @Test + void testGetCsarInfo() { // given Resource resource = new Resource(); resource.setName(RESOURCE_NAME); + resource.setCsarUUID(CSAR_UUID); + resource.setCsarVersionId("csarVersionId"); Map csar_data = new HashMap<>(); csar_data.put(CSAR_METADATA, CSAR_METADATA_CONTENT.getBytes()); csar_data.put(CSAR_ENTRY, CSAR_ENTRY_CONTENT.getBytes()); - when(csarOperation.getCsar(anyString(), any(User.class))).thenReturn(Either.left(csar_data)); + final var vendorSoftwareProduct = new VendorSoftwareProduct(); + vendorSoftwareProduct.setFileMap(csar_data); + vendorSoftwareProduct.setModelList(Collections.emptyList()); + when(csarOperation.findVsp(eq(resource.getCsarUUID()), eq(resource.getCsarVersionId()), any(User.class))) + .thenReturn(Optional.of(vendorSoftwareProduct)); // when - CsarInfo csarInfo = test.getCsarInfo(resource, null, user, null, CSAR_UUID); + final CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID); // then assertNotNull(csarInfo); - assertEquals(CSAR_UUID, csarInfo.getCsarUUID()); + assertEquals(resource.getCsarUUID(), csarInfo.getCsarUUID()); + assertEquals(resource.getCsarVersionId(), csarInfo.getCsarVersionId()); assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName()); assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName()); @@ -120,8 +128,92 @@ public class CsarBusinessLogicTest extends BaseBusinessLogicMock { assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA))); } - @Test() - public void testGetCsarInfoWithPayload() throws IOException, URISyntaxException, ZipException { + @Test + void testGetCsarInfo_vspWithModelAndResourceWithInvalidModel() { + final var resource = new Resource(); + resource.setCsarUUID(CSAR_UUID); + final String csarVersionId = "csarVersionId"; + resource.setCsarVersionId(csarVersionId); + resource.setModel("model1"); + var vendorSoftwareProduct = new VendorSoftwareProduct(); + final List modelList = List.of("model2", "model3"); + vendorSoftwareProduct.setModelList(modelList); + + when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct)); + + final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class, + () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID)); + assertEquals(ActionStatus.VSP_MODEL_NOT_ALLOWED, actualException.getActionStatus()); + assertEquals(2, actualException.getParams().length); + assertEquals(resource.getModel(), actualException.getParams()[0]); + assertEquals(String.join(", ", modelList), actualException.getParams()[1]); + } + + + @Test + void testGetCsarInfo_vspWithNoModelAndResourceWithInvalidModel() { + final var resource = new Resource(); + resource.setCsarUUID(CSAR_UUID); + final String csarVersionId = "csarVersionId"; + resource.setCsarVersionId(csarVersionId); + resource.setModel("model1"); + var vendorSoftwareProduct = new VendorSoftwareProduct(); + final List modelList = new ArrayList<>(); + vendorSoftwareProduct.setModelList(modelList); + + when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct)); + + final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class, + () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID)); + assertEquals(ActionStatus.VSP_MODEL_NOT_ALLOWED, actualException.getActionStatus()); + assertEquals(2, actualException.getParams().length); + assertEquals(resource.getModel(), actualException.getParams()[0]); + assertEquals("SDC AID", actualException.getParams()[1]); + } + + @Test + void testGetCsarInfo_vspWithModelAndResourceWithNoModel() { + final var resource = new Resource(); + resource.setCsarUUID(CSAR_UUID); + final String csarVersionId = "csarVersionId"; + resource.setCsarVersionId(csarVersionId); + resource.setModel(null); + var vendorSoftwareProduct = new VendorSoftwareProduct(); + final List modelList = List.of("model2", "model3"); + vendorSoftwareProduct.setModelList(modelList); + + when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct)); + + final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class, + () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID)); + assertEquals(ActionStatus.VSP_MODEL_NOT_ALLOWED, actualException.getActionStatus()); + assertEquals(2, actualException.getParams().length); + assertEquals("SDC AID", actualException.getParams()[0]); + assertEquals(String.join(", ", modelList), actualException.getParams()[1]); + } + + @Test + void testGetCsarInfo_vspWithNoModelAndResourceWithNoModel() { + final var resource = new Resource(); + resource.setCsarUUID(CSAR_UUID); + final String csarVersionId = "csarVersionId"; + resource.setCsarVersionId(csarVersionId); + resource.setModel(null); + var vendorSoftwareProduct = new VendorSoftwareProduct(); + final List modelList = new ArrayList<>(); + vendorSoftwareProduct.setModelList(modelList); + when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenThrow(new RuntimeException()); + + final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class, + () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID)); + assertEquals(ActionStatus.VSP_FIND_ERROR, actualException.getActionStatus()); + assertEquals(2, actualException.getParams().length); + assertEquals(resource.getCsarUUID(), actualException.getParams()[0]); + assertEquals(resource.getCsarVersionId(), actualException.getParams()[1]); + } + + @Test + void testGetCsarInfoWithPayload() throws IOException, URISyntaxException, ZipException { // given Resource resource = new Resource(); resource.setName(RESOURCE_NAME); @@ -129,7 +221,7 @@ public class CsarBusinessLogicTest extends BaseBusinessLogicMock { Map payload = loadPayload(PAYLOAD_NAME); // when - CsarInfo csarInfo = test.getCsarInfo(resource, null, user, payload, CSAR_UUID); + CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, payload, CSAR_UUID); // then assertNotNull(csarInfo); @@ -142,49 +234,69 @@ public class CsarBusinessLogicTest extends BaseBusinessLogicMock { assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA))); } - @Test(expected = ComponentException.class) - public void testGetCsarInfoWithBadData(){ + @Test + void testGetCsarInfoWithBadData(){ // given Resource resource = new Resource(); resource.setName(RESOURCE_NAME); Map csar_data = new HashMap<>(); - when(csarOperation.getCsar(anyString(), any(User.class))).thenReturn(Either.left(csar_data)); + when(csarOperation.findVspLatestPackage(anyString(), any(User.class))).thenReturn(Either.left(csar_data)); - // when - test.getCsarInfo(resource, null, user, null, CSAR_UUID); + // when/then + assertThrows(ComponentException.class, () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID)); } @Test - public void testValidateCsarBeforeCreate() { + void testValidateCsarBeforeCreate() { Resource resource = new Resource(); StorageOperationStatus status = StorageOperationStatus.OK; when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status); - test.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, CSAR_UUID); + csarBusinessLogic.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, CSAR_UUID); } - @Test(expected = ComponentException.class) - public void testValidateCsarBeforeCreate_Exists() { + @Test + void testValidateCsarBeforeCreate_ResourceExists() { Resource resource = new Resource(); ResponseFormat responseFormat = new ResponseFormat(); StorageOperationStatus status = StorageOperationStatus.ENTITY_ALREADY_EXISTS; when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status); when(componentsUtils.getResponseFormat(ActionStatus.VSP_ALREADY_EXISTS, CSAR_UUID)).thenReturn(responseFormat); - test.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, "csarUUID"); + assertThrows(ComponentException.class, () -> csarBusinessLogic + .validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, CSAR_UUID)); + } + + @Test + void testValidateCsarBeforeCreate_ServiceExists() { + final var service = new Service(); + final var status = StorageOperationStatus.ENTITY_ALREADY_EXISTS; + when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status); + csarBusinessLogic.validateCsarBeforeCreate(service, CSAR_UUID); + verify(toscaOperationFacade).validateCsarUuidUniqueness(CSAR_UUID); + } + + @Test + void testValidateCsarBeforeCreate_ServiceValidateError() { + final var service = new Service(); + final var status = StorageOperationStatus.GENERAL_ERROR; + when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status); + when(componentsUtils.convertFromStorageResponse(status)).thenReturn(ActionStatus.GENERAL_ERROR); + assertThrows(ComponentException.class, () -> csarBusinessLogic.validateCsarBeforeCreate(service, CSAR_UUID)); + verify(toscaOperationFacade).validateCsarUuidUniqueness(CSAR_UUID); } - @Test(expected = ComponentException.class) - public void testValidateCsarBeforeCreate_Fail() { + @Test + void testValidateCsarBeforeCreate_Fail() { Resource resource = new Resource(); String csarUUID = "csarUUID"; when(toscaOperationFacade.validateCsarUuidUniqueness(csarUUID)).thenReturn(StorageOperationStatus.EXEUCTION_FAILED); when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.EXEUCTION_FAILED)).thenReturn(ActionStatus.GENERAL_ERROR); - test.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, "csarUUID"); + assertThrows(ComponentException.class, () -> csarBusinessLogic + .validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, "csarUUID")); } - public Map loadPayload(String payloadName) throws IOException, URISyntaxException, ZipException { - - Path path = Paths.get(getClass().getResource("/" + payloadName).toURI()); + private Map loadPayload(String payloadName) throws IOException, URISyntaxException, ZipException { + var path = Paths.get(getClass().getResource("/" + payloadName).toURI()); byte[] data = Files.readAllBytes(path); return ZipUtils.readZip(data, false); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java index 09aae40244..b369773892 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java @@ -112,6 +112,7 @@ import org.openecomp.sdc.be.model.RequirementDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.UploadComponentInstanceInfo; import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.VendorSoftwareProduct; import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation; @@ -545,6 +546,12 @@ public class ResourceBusinessLogicTest { @Test public void testUpdateUnhappyScenario() { Resource resource = createResourceObjectCsar(true); + final var csarVersionId = "csarVersionId"; + resource.setCsarVersionId(csarVersionId); + + final var vendorSoftwareProduct = new VendorSoftwareProduct(); + vendorSoftwareProduct.setFileMap(new HashMap<>()); + vendorSoftwareProduct.setModelList(Collections.emptyList()); setCanWorkOnResource(resource); validateUserRoles(Role.ADMIN, Role.DESIGNER); @@ -552,9 +559,7 @@ public class ResourceBusinessLogicTest { when(toscaOperationFacade.validateToscaResourceNameExists("tosca.nodes.Root")).thenReturn(Either.left(true)); when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(Either.left(setCanWorkOnResource(resource))); when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(Either.left(resource)); - - when(csarOperation.getCsar("valid_vf.csar", user)).thenReturn(Either.left(new HashMap<>())); - + when(csarOperation.findVsp("valid_vf.csar", csarVersionId, user)).thenReturn(Optional.of(vendorSoftwareProduct)); try { Resource createdResource = bl.validateAndUpdateResourceFromCsar(resource, user, null, "", resource.getUniqueId()); -- cgit 1.2.3-korg