abe6f4d88ae789bc914ea3631ff8fb03829adc4c
[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-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
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.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import org.junit.Test;
32 import org.onap.aai.babel.testdata.CsarTest;
33 import org.onap.aai.babel.util.ArtifactTestUtils;
34 import org.onap.aai.babel.xml.generator.data.Artifact;
35
36 /**
37  * Tests {@link YamlExtractor}.
38  */
39 public class YamlExtractorTest {
40
41     private static final String FOO = "foo";
42     private static final String SOME_BYTES = "just some bytes that will pass the firsts validation";
43     private static final String SUPPLY_AN_ARCHIVE = "An archive must be supplied for processing.";
44     private static final String SUPPLY_NAME = "The name of the archive must be supplied for processing.";
45     private static final String SUPPLY_VERSION = "The version must be supplied for processing.";
46
47     @Test
48     public void testNullContentSupplied() {
49         invalidArgumentsTest(null, FOO, FOO, SUPPLY_AN_ARCHIVE);
50     }
51
52     @Test
53     public void testEmptyContentSupplied() {
54         invalidArgumentsTest(new byte[0], FOO, FOO, SUPPLY_AN_ARCHIVE);
55     }
56
57     @Test
58     public void testNullNameSupplied() {
59         invalidArgumentsTest(SOME_BYTES.getBytes(), null, FOO, SUPPLY_NAME);
60     }
61
62     @Test
63     public void testBlankNameSupplied() {
64         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "  \t  ", FOO,
65                 SUPPLY_NAME);
66     }
67
68     @Test
69     public void testEmptyNameSupplied() {
70         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", FOO, SUPPLY_NAME);
71     }
72
73     @Test
74     public void testNullVersionSupplied() {
75         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, null,
76                 SUPPLY_VERSION);
77     }
78
79     @Test
80     public void testBlankVersionSupplied() {
81         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "  \t  ",
82                 SUPPLY_VERSION);
83     }
84
85     @Test
86     public void testEmptyVersionSupplied() {
87         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "",
88                 SUPPLY_VERSION);
89     }
90
91     @Test
92     public void testInvalidContentSupplied() {
93         invalidArgumentsTest("This is a piece of nonsense and not a zip file".getBytes(), FOO, FOO,
94                 "An error occurred trying to create a ZipFile. Is the content being converted really a csar file?");
95     }
96
97     @Test
98     public void testArchiveContainsNoYmlFiles() throws IOException {
99         try {
100             CsarTest.NO_YAML_FILES.extractArtifacts();
101             fail("An instance of InvalidArchiveException should have been thrown.");
102         } catch (Exception e) {
103             assertTrue("An instance of InvalidArchiveException should have been thrown.",
104                     e instanceof InvalidArchiveException);
105             assertEquals("Incorrect message was returned", "No valid YAML files were found in the csar file.",
106                     e.getMessage());
107         }
108     }
109
110     @Test
111     public void testArchiveContainsOnlyTheExpectedYmlFilesFromSdWanService()
112             throws IOException, InvalidArchiveException {
113         final List<Artifact> ymlFiles = CsarTest.SD_WAN_CSAR_FILE.extractArtifacts();
114         List<String> payloads = new ArrayList<>();
115         payloads.add("ymlFiles/resource-SdWanTestVsp-template.yml");
116         payloads.add("ymlFiles/resource-SdWanTestVsp-template-interface.yml");
117         payloads.add("ymlFiles/resource-TunnelXconntest-template.yml");
118         payloads.add("ymlFiles/resource-TunnelXconntest-template-interface.yml");
119         payloads.add("ymlFiles/service-SdWanServiceTest-template.yml");
120         payloads.add("ymlFiles/service-SdWanServiceTest-template-interface.yml");
121         payloads.add("ymlFiles/resource-Allotedresource-template.yml");
122         payloads.add("ymlFiles/resource-SdwantestvspNodesDummyServer-template.yml");
123         payloads.add("ymlFiles/nodes.yml");
124         payloads.add("ymlFiles/capabilities.yml");
125         payloads.add("ymlFiles/artifacts.yml");
126         payloads.add("ymlFiles/data.yml");
127         payloads.add("ymlFiles/groups.yml");
128
129         new ArtifactTestUtils().performYmlAsserts(ymlFiles, payloads);
130     }
131
132     /**
133      * Call the extractor with the specified arguments and assert that an exception is thrown.
134      *
135      * @param archive the compressed archive in the form of a byte array, expected to contain one or more YAML files
136      * @param name the name of the archive
137      * @param version the version of the archive
138      * @param expectedErrorMessage the text of the InvalidArchiveException thrown by the extractor
139      */
140     private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) {
141         try {
142             new YamlExtractor().extract(archive, name, version);
143             fail("An instance of InvalidArchiveException should have been thrown");
144         } catch (InvalidArchiveException ex) {
145             assertTrue(ex instanceof InvalidArchiveException);
146             assertEquals(expectedErrorMessage, ex.getLocalizedMessage());
147         }
148     }
149 }