2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2019 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=========================================================
22 package org.onap.aai.babel.csar.extractor;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.junit.jupiter.api.Assertions.fail;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import org.junit.jupiter.api.Test;
33 import org.onap.aai.babel.testdata.CsarTest;
34 import org.onap.aai.babel.util.ArtifactTestUtils;
35 import org.onap.aai.babel.xml.generator.data.Artifact;
38 * Tests {@link 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 testNullContentSupplied() {
50 invalidArgumentsTest(null, FOO, FOO, SUPPLY_AN_ARCHIVE);
54 public void testEmptyContentSupplied() {
55 invalidArgumentsTest(new byte[0], FOO, FOO, SUPPLY_AN_ARCHIVE);
59 public void testNullNameSupplied() {
60 invalidArgumentsTest(SOME_BYTES.getBytes(), null, FOO, SUPPLY_NAME);
64 public void testBlankNameSupplied() {
65 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), " \t ", FOO,
70 public void testEmptyNameSupplied() {
71 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", FOO, SUPPLY_NAME);
75 public void testNullVersionSupplied() {
76 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, null,
81 public void testBlankVersionSupplied() {
82 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, " \t ",
87 public void testEmptyVersionSupplied() {
88 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "",
93 public void testInvalidContentSupplied() {
94 invalidArgumentsTest("This is a piece of nonsense and not a zip file".getBytes(), FOO, FOO,
95 "An error occurred trying to create a ZipFile. Is the content being converted really a CSAR file?");
99 public void testArchiveContainsNoYmlFiles() throws IOException {
101 CsarTest.NO_YAML_FILES.extractArtifacts();
102 fail("An instance of InvalidArchiveException should have been thrown.");
103 } catch (Exception e) {
104 assertTrue(e instanceof InvalidArchiveException,
105 "An instance of InvalidArchiveException should have been thrown.");
106 assertEquals("No valid YAML files were found in the CSAR file.",
108 "Incorrect message was returned");
113 public void testArchiveContainsOnlyTheExpectedYmlFilesFromSdWanService()
114 throws IOException, InvalidArchiveException {
115 assertDoesNotThrow(() -> {
116 final List<Artifact> ymlFiles = CsarTest.SD_WAN_CSAR_FILE.extractArtifacts();
117 List<String> payloads = new ArrayList<>();
118 payloads.add("ymlFiles/resource-SdWanTestVsp-template.yml");
119 payloads.add("ymlFiles/resource-SdWanTestVsp-template-interface.yml");
120 payloads.add("ymlFiles/resource-TunnelXconntest-template.yml");
121 payloads.add("ymlFiles/resource-TunnelXconntest-template-interface.yml");
122 payloads.add("ymlFiles/service-SdWanServiceTest-template.yml");
123 payloads.add("ymlFiles/service-SdWanServiceTest-template-interface.yml");
124 payloads.add("ymlFiles/resource-Allotedresource-template.yml");
125 payloads.add("ymlFiles/resource-SdwantestvspNodesDummyServer-template.yml");
126 payloads.add("ymlFiles/nodes.yml");
127 payloads.add("ymlFiles/capabilities.yml");
128 payloads.add("ymlFiles/artifacts.yml");
129 payloads.add("ymlFiles/data.yml");
130 payloads.add("ymlFiles/groups.yml");
132 new ArtifactTestUtils().performYmlAsserts(ymlFiles, payloads);
137 * Call the extractor with the specified arguments and assert that an exception is thrown.
140 * the compressed archive in the form of a byte array, expected to contain one or more YAML files
142 * the name of the archive
144 * the version of the archive
145 * @param expectedErrorMessage
146 * the text of the InvalidArchiveException thrown by the extractor
148 private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) {
150 new YamlExtractor().extract(archive, name, version);
151 fail("An instance of InvalidArchiveException should have been thrown");
152 } catch (InvalidArchiveException ex) {
153 assertTrue(ex instanceof InvalidArchiveException);
154 assertEquals(expectedErrorMessage, ex.getLocalizedMessage());