2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.babel.csar.extractor;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.apache.commons.io.IOUtils;
31 import org.junit.Test;
32 import org.onap.aai.babel.util.ArtifactTestUtils;
33 import org.onap.aai.babel.xml.generator.data.Artifact;
36 * Tests @see YamlExtractor
38 public class YamlExtractorTest {
40 private static final String FOO = "foo";
41 private static final String SOME_BYTES = "just some bytes that will pass the firsts validation";
42 private static final String SUPPLY_AN_ARCHIVE = "An archive must be supplied for processing.";
43 private static final String SUPPLY_NAME = "The name of the archive must be supplied for processing.";
44 private static final String SUPPLY_VERSION = "The version must be supplied for processing.";
47 public void extract_nullContentSupplied() {
48 invalidArgumentsTest(null, FOO, FOO, SUPPLY_AN_ARCHIVE);
51 private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) {
53 YamlExtractor.extract(archive, name, version);
54 fail("An instance of InvalidArchiveException should have been thrown");
55 } catch (Exception ex) {
56 assertTrue(ex instanceof InvalidArchiveException);
57 assertEquals(expectedErrorMessage, ex.getLocalizedMessage());
62 public void extract_emptyContentSupplied() {
63 invalidArgumentsTest(new byte[0], FOO, FOO, SUPPLY_AN_ARCHIVE);
67 public void extract_nullNameSupplied() {
68 invalidArgumentsTest(SOME_BYTES.getBytes(), null, FOO, SUPPLY_NAME);
72 public void extract_blankNameSupplied() {
73 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), " \t ", FOO,
78 public void extract_emptyNameSupplied() {
79 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", FOO, SUPPLY_NAME);
83 public void extract_nullVersionSupplied() {
84 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, null,
89 public void extract_blankVersionSupplied() {
90 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, " \t ",
95 public void extract_emptyVersionSupplied() {
96 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "",
101 public void extract_invalidContentSupplied() {
102 invalidArgumentsTest("This is a piece of nonsense and not a zip file".getBytes(), FOO, FOO,
103 "An error occurred trying to create a ZipFile. Is the content being converted really a csar file?");
107 public void extract_archiveContainsNoYmlFiles() throws IOException {
109 YamlExtractor.extract(loadResource("compressedArtifacts/noYmlFilesArchive.zip"), "noYmlFilesArchive.zip",
111 fail("An instance of InvalidArchiveException should have been thrown.");
112 } catch (Exception e) {
113 assertTrue("An instance of InvalidArchiveException should have been thrown.",
114 e instanceof InvalidArchiveException);
115 assertEquals("Incorrect message was returned", "No valid yml files were found in the csar file.",
120 private byte[] loadResource(final String archiveName) throws IOException {
121 return IOUtils.toByteArray(YamlExtractor.class.getClassLoader().getResource(archiveName));
125 public void extract_archiveContainsOnlyTheExpectedYmlFilesFromSdWanService()
126 throws IOException, InvalidArchiveException {
127 List<Artifact> ymlFiles =
128 YamlExtractor.extract(loadResource("compressedArtifacts/service-SdWanServiceTest-csar.csar"),
129 "service-SdWanServiceTest-csar.csar", "v1");
131 List<String> payloads = new ArrayList<>();
132 payloads.add("ymlFiles/resource-SdWanTestVsp-template.yml");
133 payloads.add("ymlFiles/resource-TunnelXconntest-template.yml");
134 payloads.add("ymlFiles/service-SdWanServiceTest-template.yml");
135 payloads.add("ymlFiles/artifacts.yml");
136 payloads.add("ymlFiles/data.yml");
138 new ArtifactTestUtils().performYmlAsserts(ymlFiles, payloads);