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.junit.Test;
31 import org.onap.aai.babel.util.ArtifactTestUtils;
32 import org.onap.aai.babel.xml.generator.data.Artifact;
35 * Tests {@link YamlExtractor}.
37 public class YamlExtractorTest {
39 private static final String FOO = "foo";
40 private static final String SOME_BYTES = "just some bytes that will pass the firsts validation";
41 private static final String SUPPLY_AN_ARCHIVE = "An archive must be supplied for processing.";
42 private static final String SUPPLY_NAME = "The name of the archive must be supplied for processing.";
43 private static final String SUPPLY_VERSION = "The version must be supplied for processing.";
46 public void testNullContentSupplied() {
47 invalidArgumentsTest(null, FOO, FOO, SUPPLY_AN_ARCHIVE);
51 public void testEmptyContentSupplied() {
52 invalidArgumentsTest(new byte[0], FOO, FOO, SUPPLY_AN_ARCHIVE);
56 public void testNullNameSupplied() {
57 invalidArgumentsTest(SOME_BYTES.getBytes(), null, FOO, SUPPLY_NAME);
61 public void testBlankNameSupplied() {
62 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), " \t ", FOO,
67 public void testEmptyNameSupplied() {
68 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", FOO, SUPPLY_NAME);
72 public void testNullVersionSupplied() {
73 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, null,
78 public void testBlankVersionSupplied() {
79 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, " \t ",
84 public void testEmptyVersionSupplied() {
85 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "",
90 public void testInvalidContentSupplied() {
91 invalidArgumentsTest("This is a piece of nonsense and not a zip file".getBytes(), FOO, FOO,
92 "An error occurred trying to create a ZipFile. Is the content being converted really a csar file?");
96 public void testArchiveContainsNoYmlFiles() throws IOException {
98 extractArchive("noYmlFilesArchive.zip");
99 fail("An instance of InvalidArchiveException should have been thrown.");
100 } catch (Exception e) {
101 assertTrue("An instance of InvalidArchiveException should have been thrown.",
102 e instanceof InvalidArchiveException);
103 assertEquals("Incorrect message was returned", "No valid YAML files were found in the csar file.",
109 public void testArchiveContainsOnlyTheExpectedYmlFilesFromSdWanService()
110 throws IOException, InvalidArchiveException {
111 final List<Artifact> ymlFiles = extractArchive("service-SdWanServiceTest-csar.csar");
112 List<String> payloads = new ArrayList<>();
113 payloads.add("ymlFiles/resource-SdWanTestVsp-template.yml");
114 payloads.add("ymlFiles/resource-SdWanTestVsp-template-interface.yml");
115 payloads.add("ymlFiles/resource-TunnelXconntest-template.yml");
116 payloads.add("ymlFiles/resource-TunnelXconntest-template-interface.yml");
117 payloads.add("ymlFiles/service-SdWanServiceTest-template.yml");
118 payloads.add("ymlFiles/service-SdWanServiceTest-template-interface.yml");
119 payloads.add("ymlFiles/resource-Allotedresource-template.yml");
120 payloads.add("ymlFiles/resource-SdwantestvspNodesDummyServer-template.yml");
121 payloads.add("ymlFiles/nodes.yml");
122 payloads.add("ymlFiles/capabilities.yml");
123 payloads.add("ymlFiles/artifacts.yml");
124 payloads.add("ymlFiles/data.yml");
125 payloads.add("ymlFiles/groups.yml");
127 new ArtifactTestUtils().performYmlAsserts(ymlFiles, payloads);
131 * Call the extractor with the specified arguments and assert that an exception is thrown.
136 * @param expectedErrorMessage
138 private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) {
140 new YamlExtractor().extract(archive, name, version);
141 fail("An instance of InvalidArchiveException should have been thrown");
142 } catch (InvalidArchiveException ex) {
143 assertTrue(ex instanceof InvalidArchiveException);
144 assertEquals(expectedErrorMessage, ex.getLocalizedMessage());
149 * Extract Artifacts from the specified CSAR resource.
151 * @param resourceName
153 * @return the extracted artifacts
154 * @throws InvalidArchiveException
155 * @throws IOException
158 private List<Artifact> extractArchive(String resourceName) throws InvalidArchiveException, IOException {
159 byte[] csar = new ArtifactTestUtils().getCompressedArtifact(resourceName);
160 return new YamlExtractor().extract(csar, resourceName, "v1");