e01a8f049859b2fbbe57d96e8d23b9e41c30777a
[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 package org.onap.aai.babel.csar.extractor;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
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;
33
34 /**
35  * Tests {@link YamlExtractor}.
36  */
37 public class YamlExtractorTest {
38
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.";
44
45     @Test
46     public void testNullContentSupplied() {
47         invalidArgumentsTest(null, FOO, FOO, SUPPLY_AN_ARCHIVE);
48     }
49
50     @Test
51     public void testEmptyContentSupplied() {
52         invalidArgumentsTest(new byte[0], FOO, FOO, SUPPLY_AN_ARCHIVE);
53     }
54
55     @Test
56     public void testNullNameSupplied() {
57         invalidArgumentsTest(SOME_BYTES.getBytes(), null, FOO, SUPPLY_NAME);
58     }
59
60     @Test
61     public void testBlankNameSupplied() {
62         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "  \t  ", FOO,
63                 SUPPLY_NAME);
64     }
65
66     @Test
67     public void testEmptyNameSupplied() {
68         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", FOO, SUPPLY_NAME);
69     }
70
71     @Test
72     public void testNullVersionSupplied() {
73         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, null,
74                 SUPPLY_VERSION);
75     }
76
77     @Test
78     public void testBlankVersionSupplied() {
79         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "  \t  ",
80                 SUPPLY_VERSION);
81     }
82
83     @Test
84     public void testEmptyVersionSupplied() {
85         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "",
86                 SUPPLY_VERSION);
87     }
88
89     @Test
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?");
93     }
94
95     @Test
96     public void testArchiveContainsNoYmlFiles() throws IOException {
97         try {
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.",
104                     e.getMessage());
105         }
106     }
107
108     @Test
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");
126
127         new ArtifactTestUtils().performYmlAsserts(ymlFiles, payloads);
128     }
129
130     /**
131      * Call the extractor with the specified arguments and assert that an exception is thrown.
132      *
133      * @param archive
134      * @param name
135      * @param version
136      * @param expectedErrorMessage
137      */
138     private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) {
139         try {
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());
145         }
146     }
147
148     /**
149      * Extract Artifacts from the specified CSAR resource.
150      *
151      * @param resourceName
152      *            the CSAR file
153      * @return the extracted artifacts
154      * @throws InvalidArchiveException
155      * @throws IOException
156      *             for I/O errors
157      */
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");
161     }
162 }