2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.babel.csar.extractor;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import org.apache.commons.io.IOUtils;
33 import org.junit.Test;
34 import org.onap.aai.babel.util.ArtifactTestUtils;
35 import org.openecomp.sdc.generator.data.Artifact;
38 * Tests @see YamlExtractor
40 public class YamlExtractorTest {
42 private static final String FOO = "foo";
43 private static final String SOME_BYTES = "just some bytes that will pass the firsts validation";
44 private static final String SUPPLY_AN_ARCHIVE = "An archive must be supplied for processing.";
45 private static final String SUPPLY_NAME = "The name of the archive must be supplied for processing.";
46 private static final String SUPPLY_VERSION = "The version must be supplied for processing.";
49 public void extract_nullContentSupplied() {
50 invalidArgumentsTest(null, FOO, FOO, SUPPLY_AN_ARCHIVE);
53 private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) {
55 YamlExtractor.extract(archive, name, version);
56 fail("An instance of InvalidArchiveException should have been thrown");
57 } catch (Exception ex) {
58 assertTrue(ex instanceof InvalidArchiveException);
59 assertEquals(expectedErrorMessage, ex.getLocalizedMessage());
64 public void extract_emptyContentSupplied() {
65 invalidArgumentsTest(new byte[0], FOO, FOO, SUPPLY_AN_ARCHIVE);
69 public void extract_nullNameSupplied() {
70 invalidArgumentsTest(SOME_BYTES.getBytes(), null, FOO, SUPPLY_NAME);
74 public void extract_blankNameSupplied() {
75 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), " \t ", FOO,
80 public void extract_emptyNameSupplied() {
81 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", FOO, SUPPLY_NAME);
85 public void extract_nullVersionSupplied() {
86 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, null,
91 public void extract_blankVersionSupplied() {
92 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, " \t ",
97 public void extract_emptyVersionSupplied() {
98 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "",
103 public void extract_invalidContentSupplied() {
104 invalidArgumentsTest("This is a piece of nonsense and not a zip file".getBytes(), FOO, FOO,
105 "An error occurred trying to create a ZipFile. Is the content being converted really a csar file?");
109 public void extract_archiveContainsNoYmlFiles() throws IOException {
111 YamlExtractor.extract(loadResource("compressedArtifacts/noYmlFilesArchive.zip"), "noYmlFilesArchive.zip",
113 fail("An instance of InvalidArchiveException should have been thrown.");
114 } catch (Exception e) {
115 assertTrue("An instance of InvalidArchiveException should have been thrown.",
116 e instanceof InvalidArchiveException);
117 assertEquals("Incorrect message was returned", "No valid yml files were found in the csar file.",
122 private byte[] loadResource(final String archiveName) throws IOException {
123 return IOUtils.toByteArray(YamlExtractor.class.getClassLoader().getResource(archiveName));
127 public void extract_archiveContainsThreeRelevantYmlFilesFromSdWanService()
128 throws IOException, InvalidArchiveException {
129 List<Artifact> ymlFiles =
130 YamlExtractor.extract(loadResource("compressedArtifacts/service-SdWanServiceTest-csar.csar"),
131 "service-SdWanServiceTest-csar.csar", "v1");
133 List<String> payloads = new ArrayList<>();
134 payloads.add("ymlFiles/resource-SdWanTestVsp-template.yml");
135 payloads.add("ymlFiles/resource-TunnelXconntest-template.yml");
136 payloads.add("ymlFiles/service-SdWanServiceTest-template.yml");
138 new ArtifactTestUtils().performYmlAsserts(ymlFiles, payloads);