X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fbabel%2Fcsar%2Fextractor%2FYamlExtractorTest.java;h=5bb7763804fc5392e96c981134da188195717ee8;hb=be30876e3a3872a7274c944995544836ff31913c;hp=9024efaece8e77130436665b85820adbfe7c90fd;hpb=66b3afa06776e9944ad515206d281d67747c9770;p=aai%2Fbabel.git diff --git a/src/test/java/org/onap/aai/babel/csar/extractor/YamlExtractorTest.java b/src/test/java/org/onap/aai/babel/csar/extractor/YamlExtractorTest.java index 9024efa..5bb7763 100644 --- a/src/test/java/org/onap/aai/babel/csar/extractor/YamlExtractorTest.java +++ b/src/test/java/org/onap/aai/babel/csar/extractor/YamlExtractorTest.java @@ -18,6 +18,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.aai.babel.csar.extractor; import static org.junit.Assert.assertEquals; @@ -27,13 +28,13 @@ import static org.junit.Assert.fail; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.apache.commons.io.IOUtils; import org.junit.Test; +import org.onap.aai.babel.testdata.CsarTest; import org.onap.aai.babel.util.ArtifactTestUtils; import org.onap.aai.babel.xml.generator.data.Artifact; /** - * Tests @see YamlExtractor + * Tests {@link YamlExtractor}. */ public class YamlExtractorTest { @@ -44,98 +45,109 @@ public class YamlExtractorTest { private static final String SUPPLY_VERSION = "The version must be supplied for processing."; @Test - public void extract_nullContentSupplied() { + public void testNullContentSupplied() { invalidArgumentsTest(null, FOO, FOO, SUPPLY_AN_ARCHIVE); } - private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) { - try { - YamlExtractor.extract(archive, name, version); - fail("An instance of InvalidArchiveException should have been thrown"); - } catch (Exception ex) { - assertTrue(ex instanceof InvalidArchiveException); - assertEquals(expectedErrorMessage, ex.getLocalizedMessage()); - } - } - @Test - public void extract_emptyContentSupplied() { + public void testEmptyContentSupplied() { invalidArgumentsTest(new byte[0], FOO, FOO, SUPPLY_AN_ARCHIVE); } @Test - public void extract_nullNameSupplied() { + public void testNullNameSupplied() { invalidArgumentsTest(SOME_BYTES.getBytes(), null, FOO, SUPPLY_NAME); } @Test - public void extract_blankNameSupplied() { + public void testBlankNameSupplied() { invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), " \t ", FOO, SUPPLY_NAME); } @Test - public void extract_emptyNameSupplied() { + public void testEmptyNameSupplied() { invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", FOO, SUPPLY_NAME); } @Test - public void extract_nullVersionSupplied() { + public void testNullVersionSupplied() { invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, null, SUPPLY_VERSION); } @Test - public void extract_blankVersionSupplied() { + public void testBlankVersionSupplied() { invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, " \t ", SUPPLY_VERSION); } @Test - public void extract_emptyVersionSupplied() { + public void testEmptyVersionSupplied() { invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "", SUPPLY_VERSION); } @Test - public void extract_invalidContentSupplied() { + public void testInvalidContentSupplied() { invalidArgumentsTest("This is a piece of nonsense and not a zip file".getBytes(), FOO, FOO, "An error occurred trying to create a ZipFile. Is the content being converted really a csar file?"); } @Test - public void extract_archiveContainsNoYmlFiles() throws IOException { + public void testArchiveContainsNoYmlFiles() throws IOException { try { - YamlExtractor.extract(loadResource("compressedArtifacts/noYmlFilesArchive.zip"), "noYmlFilesArchive.zip", - "v1"); + CsarTest.NO_YAML_FILES.extractArtifacts(); fail("An instance of InvalidArchiveException should have been thrown."); } catch (Exception e) { assertTrue("An instance of InvalidArchiveException should have been thrown.", e instanceof InvalidArchiveException); - assertEquals("Incorrect message was returned", "No valid yml files were found in the csar file.", + assertEquals("Incorrect message was returned", "No valid YAML files were found in the csar file.", e.getMessage()); } } - private byte[] loadResource(final String archiveName) throws IOException { - return IOUtils.toByteArray(YamlExtractor.class.getClassLoader().getResource(archiveName)); - } - @Test - public void extract_archiveContainsOnlyTheExpectedYmlFilesFromSdWanService() + public void testArchiveContainsOnlyTheExpectedYmlFilesFromSdWanService() throws IOException, InvalidArchiveException { - List ymlFiles = - YamlExtractor.extract(loadResource("compressedArtifacts/service-SdWanServiceTest-csar.csar"), - "service-SdWanServiceTest-csar.csar", "v1"); - + final List ymlFiles = CsarTest.SD_WAN_CSAR_FILE.extractArtifacts(); List payloads = new ArrayList<>(); payloads.add("ymlFiles/resource-SdWanTestVsp-template.yml"); + payloads.add("ymlFiles/resource-SdWanTestVsp-template-interface.yml"); payloads.add("ymlFiles/resource-TunnelXconntest-template.yml"); + payloads.add("ymlFiles/resource-TunnelXconntest-template-interface.yml"); payloads.add("ymlFiles/service-SdWanServiceTest-template.yml"); + payloads.add("ymlFiles/service-SdWanServiceTest-template-interface.yml"); + payloads.add("ymlFiles/resource-Allotedresource-template.yml"); + payloads.add("ymlFiles/resource-SdwantestvspNodesDummyServer-template.yml"); + payloads.add("ymlFiles/nodes.yml"); + payloads.add("ymlFiles/capabilities.yml"); payloads.add("ymlFiles/artifacts.yml"); payloads.add("ymlFiles/data.yml"); + payloads.add("ymlFiles/groups.yml"); new ArtifactTestUtils().performYmlAsserts(ymlFiles, payloads); } -} + /** + * Call the extractor with the specified arguments and assert that an exception is thrown. + * + * @param archive + * the compressed archive in the form of a byte array, expected to contain one or more YAML files + * @param name + * the name of the archive + * @param version + * the version of the archive + * @param expectedErrorMessage + * the text of the InvalidArchiveException thrown by the extractor + */ + private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) { + try { + new YamlExtractor().extract(archive, name, version); + fail("An instance of InvalidArchiveException should have been thrown"); + } catch (InvalidArchiveException ex) { + assertTrue(ex instanceof InvalidArchiveException); + assertEquals(expectedErrorMessage, ex.getLocalizedMessage()); + } + } +}