From a5c4a996f58bfff602124fd4e38c0d2ec70d68f2 Mon Sep 17 00:00:00 2001 From: vempo Date: Thu, 10 May 2018 13:28:06 +0300 Subject: Refactor HEAT tree test for error handling Change-Id: Ibd19cecc948896dcca240ab6fc65c394a1034d11 Issue-ID: SDC-836 Signed-off-by: vempo --- .../tree/HeatTreeManagerTest.java | 52 +++++++--------- .../vendorsoftwareproduct/tree/TreeBaseTest.java | 70 ++++++++++------------ .../heat/services/tree/HeatTreeManagerTest.java | 33 ++++------ 3 files changed, 65 insertions(+), 90 deletions(-) (limited to 'openecomp-be') diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/tree/HeatTreeManagerTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/tree/HeatTreeManagerTest.java index baa0d8da3f..1ee1e2a85d 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/tree/HeatTreeManagerTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/tree/HeatTreeManagerTest.java @@ -1,47 +1,42 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2016-2018 European Support Limited + * * 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 - * + * * 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. - * ============LICENSE_END========================================================= */ package org.openecomp.sdc.vendorsoftwareproduct.tree; -import org.openecomp.sdc.datatypes.error.ErrorLevel; -import org.openecomp.sdc.datatypes.error.ErrorMessage; -import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; -import org.openecomp.sdc.heat.services.tree.HeatTreeManager; -import org.testng.Assert; -import org.testng.annotations.Test; - import java.io.IOException; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; +import org.openecomp.sdc.heat.services.tree.HeatTreeManager; +import org.testng.Assert; +import org.testng.annotations.Test; public class HeatTreeManagerTest extends TreeBaseTest { @Test - public void testHeatTreeManager() { + public void testHeatTreeManager() throws IOException, URISyntaxException { - INPUT_DIR = "./tree/valid_tree/input/"; - HeatTreeManager heatTreeManager = initHeatTreeManager(); + HeatTreeManager heatTreeManager = initHeatTreeManager("./tree/valid_tree/input/"); heatTreeManager.createTree(); Map> errorMap = new HashMap<>(); @@ -57,10 +52,9 @@ public class HeatTreeManagerTest extends TreeBaseTest { } @Test - public void testHeatTreeManagerMissingManifest() { + public void testHeatTreeManagerMissingManifest() throws IOException, URISyntaxException { - INPUT_DIR = "./tree/missing_manifest/input/"; - HeatTreeManager heatTreeManager = initHeatTreeManager(); + HeatTreeManager heatTreeManager = initHeatTreeManager("./tree/missing_manifest/input/"); heatTreeManager.createTree(); Map> errorMap = new HashMap<>(); @@ -73,15 +67,15 @@ public class HeatTreeManagerTest extends TreeBaseTest { heatTreeManager.addErrors(errorMap); HeatStructureTree tree = heatTreeManager.getTree(); Assert.assertNotNull(tree); - Assert.assertEquals(tree.getHeat(), null); + Assert.assertNull(tree.getHeat()); } @Test - public void testResourceGroupShowsAsNestedFileInTree() throws IOException { - INPUT_DIR = "./tree/nested_resource_group"; - HeatTreeManager heatTreeManager = initHeatTreeManager(); + public void testResourceGroupShowsAsNestedFileInTree() throws IOException, URISyntaxException { + + HeatTreeManager heatTreeManager = initHeatTreeManager("./tree/nested_resource_group"); heatTreeManager.createTree(); HeatStructureTree tree = heatTreeManager.getTree(); @@ -102,9 +96,9 @@ public class HeatTreeManagerTest extends TreeBaseTest { @Test - public void testVolumeNestedFileIsNotUnderVolumeSubTree() { - INPUT_DIR = "./tree/nested_volume"; - HeatTreeManager heatTreeManager = initHeatTreeManager(); + public void testVolumeNestedFileIsNotUnderVolumeSubTree() throws IOException, URISyntaxException { + + HeatTreeManager heatTreeManager = initHeatTreeManager("./tree/nested_volume"); heatTreeManager.createTree(); HeatStructureTree tree = heatTreeManager.getTree(); diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/tree/TreeBaseTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/tree/TreeBaseTest.java index 2174a6894a..324260e490 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/tree/TreeBaseTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/tree/TreeBaseTest.java @@ -1,68 +1,58 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2016-2018 European Support Limited + * * 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 - * + * * 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. - * ============LICENSE_END========================================================= */ package org.openecomp.sdc.vendorsoftwareproduct.tree; -import org.openecomp.core.utilities.file.FileUtils; -import org.openecomp.sdc.heat.services.tree.HeatTreeManager; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; - import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.sdc.heat.services.tree.HeatTreeManager; /** * Created by SHALOMB on 6/8/2016. */ -public class TreeBaseTest { - - private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName()); +public abstract class TreeBaseTest { - String INPUT_DIR; + HeatTreeManager initHeatTreeManager(String inputDirectory) throws URISyntaxException, IOException { + URL url = Thread.currentThread().getContextClassLoader().getResource(inputDirectory); + if (url == null) { + throw new FileNotFoundException("Directory " + inputDirectory + " not found in classpath"); + } - HeatTreeManager initHeatTreeManager() { - HeatTreeManager heatTreeManager = new HeatTreeManager(); + File inputDir = new File(url.toURI()); - URL url = Thread.currentThread().getContextClassLoader().getResource(INPUT_DIR); + File[] files = inputDir.listFiles(); + if (files == null) { + throw new IllegalArgumentException("Directory " + inputDirectory + " does not contain files"); + } - File inputDir = null; - try { - inputDir = new File(url.toURI()); - } catch (URISyntaxException exception) { - log.debug("",exception); - } + HeatTreeManager heatTreeManager = new HeatTreeManager(); + for (File inputFile : files) { - if(inputDir != null) { - File[] files = inputDir.listFiles(); - for (File inputFile : files) { - try { - heatTreeManager.addFile(inputFile.getName(), FileUtils.loadFileToInputStream( - INPUT_DIR.replace("/", File.separator) + File.separator + inputFile.getName())); - } catch (Exception e) { - throw e; + String path = inputDirectory.replace("/", File.separator) + File.separator + inputFile.getName(); + try (InputStream inputStream = FileUtils.loadFileToInputStream(path)) { + heatTreeManager.addFile(inputFile.getName(), inputStream); + } } - } - } - return heatTreeManager; - } -} + return heatTreeManager; + } +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java index c43c55721e..6e8901c3ce 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java @@ -16,26 +16,21 @@ package org.openecomp.sdc.heat.services.tree; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; import org.testng.Assert; import org.testng.annotations.Test; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.net.URL;; - - public class HeatTreeManagerTest { - private Logger logger = LoggerFactory.getLogger(HeatTreeManagerTest.class); - @Test - public void testHeatTreeCreation() { + public void testHeatTreeCreation() throws IOException { FileContentHandler fileContentMap = new FileContentHandler(); URL url = this.getClass().getResource("/heatTreeValidationOutput"); @@ -59,7 +54,7 @@ public class HeatTreeManagerTest { } @Test - public void testHeatTreeArtifactsCreated() { + public void testHeatTreeArtifactsCreated() throws IOException { FileContentHandler fileContentMap = new FileContentHandler(); URL url = this.getClass().getResource("/heatTreeArtifactsValidationOutput"); @@ -88,23 +83,19 @@ public class HeatTreeManagerTest { private void verifyHeatArtifacts(HeatStructureTree tree, String heatName, int expectedArtifactNum ) { HeatStructureTree heat = HeatStructureTree.getHeatStructureTreeByName(tree.getHeat(), heatName); - if(expectedArtifactNum > 0) { + Assert.assertNotNull(heat); + if (expectedArtifactNum > 0) { Assert.assertNotNull(heat.getArtifacts()); Assert.assertEquals(heat.getArtifacts().size(), expectedArtifactNum); } else { Assert.assertNull(heat.getArtifacts()); } - } - private byte[] getFileContent(File file) { - try { - return FileUtils.toByteArray(new FileInputStream(file)); - } catch (IOException e) { - logger.debug("",e); + private byte[] getFileContent(File file) throws IOException { + try(InputStream inputStream = new FileInputStream(file)) { + return FileUtils.toByteArray(inputStream); } - - return new byte[0]; } } -- cgit 1.2.3-korg