68414a8e0eff7fc3fcb3ef8c73142f75a36acaff
[aai/babel.git] / src / test / java / org / onap / aai / babel / csar / extractor / YamlExtractorTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.aai.babel.csar.extractor;
23
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;
28
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;
36
37 /**
38  * Tests {@link YamlExtractor}.
39  */
40 public class YamlExtractorTest {
41
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.";
47
48     @Test
49     public void testNullContentSupplied() {
50         invalidArgumentsTest(null, FOO, FOO, SUPPLY_AN_ARCHIVE);
51     }
52
53     @Test
54     public void testEmptyContentSupplied() {
55         invalidArgumentsTest(new byte[0], FOO, FOO, SUPPLY_AN_ARCHIVE);
56     }
57
58     @Test
59     public void testNullNameSupplied() {
60         invalidArgumentsTest(SOME_BYTES.getBytes(), null, FOO, SUPPLY_NAME);
61     }
62
63     @Test
64     public void testBlankNameSupplied() {
65         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "  \t  ", FOO,
66                 SUPPLY_NAME);
67     }
68
69     @Test
70     public void testEmptyNameSupplied() {
71         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", FOO, SUPPLY_NAME);
72     }
73
74     @Test
75     public void testNullVersionSupplied() {
76         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, null,
77                 SUPPLY_VERSION);
78     }
79
80     @Test
81     public void testBlankVersionSupplied() {
82         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "  \t  ",
83                 SUPPLY_VERSION);
84     }
85
86     @Test
87     public void testEmptyVersionSupplied() {
88         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "",
89                 SUPPLY_VERSION);
90     }
91
92     @Test
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?");
96     }
97
98     @Test
99     public void testArchiveContainsNoYmlFiles() throws IOException {
100         try {
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.",
107                     e.getMessage(),
108                     "Incorrect message was returned");
109         }
110     }
111
112     @Test
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");
131
132             new ArtifactTestUtils().performYmlAsserts(ymlFiles, payloads);
133         });
134     }
135
136     /**
137      * Call the extractor with the specified arguments and assert that an exception is thrown.
138      *
139      * @param archive
140      *            the compressed archive in the form of a byte array, expected to contain one or more YAML files
141      * @param name
142      *            the name of the archive
143      * @param version
144      *            the version of the archive
145      * @param expectedErrorMessage
146      *            the text of the InvalidArchiveException thrown by the extractor
147      */
148     private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) {
149         try {
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());
155         }
156     }
157 }