Incorporate the ECOMP SDC Artefact Generator code
[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.apache.commons.io.IOUtils;
31 import org.junit.Test;
32 import org.onap.aai.babel.util.ArtifactTestUtils;
33 import org.onap.aai.babel.xml.generator.data.Artifact;
34
35 /**
36  * Tests @see YamlExtractor
37  */
38 public class YamlExtractorTest {
39
40     private static final String FOO = "foo";
41     private static final String SOME_BYTES = "just some bytes that will pass the firsts validation";
42     private static final String SUPPLY_AN_ARCHIVE = "An archive must be supplied for processing.";
43     private static final String SUPPLY_NAME = "The name of the archive must be supplied for processing.";
44     private static final String SUPPLY_VERSION = "The version must be supplied for processing.";
45
46     @Test
47     public void extract_nullContentSupplied() {
48         invalidArgumentsTest(null, FOO, FOO, SUPPLY_AN_ARCHIVE);
49     }
50
51     private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) {
52         try {
53             YamlExtractor.extract(archive, name, version);
54             fail("An instance of InvalidArchiveException should have been thrown");
55         } catch (Exception ex) {
56             assertTrue(ex instanceof InvalidArchiveException);
57             assertEquals(expectedErrorMessage, ex.getLocalizedMessage());
58         }
59     }
60
61     @Test
62     public void extract_emptyContentSupplied() {
63         invalidArgumentsTest(new byte[0], FOO, FOO, SUPPLY_AN_ARCHIVE);
64     }
65
66     @Test
67     public void extract_nullNameSupplied() {
68         invalidArgumentsTest(SOME_BYTES.getBytes(), null, FOO, SUPPLY_NAME);
69     }
70
71     @Test
72     public void extract_blankNameSupplied() {
73         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "  \t  ", FOO,
74                 SUPPLY_NAME);
75     }
76
77     @Test
78     public void extract_emptyNameSupplied() {
79         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", FOO, SUPPLY_NAME);
80     }
81
82     @Test
83     public void extract_nullVersionSupplied() {
84         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, null,
85                 SUPPLY_VERSION);
86     }
87
88     @Test
89     public void extract_blankVersionSupplied() {
90         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "  \t  ",
91                 SUPPLY_VERSION);
92     }
93
94     @Test
95     public void extract_emptyVersionSupplied() {
96         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), FOO, "",
97                 SUPPLY_VERSION);
98     }
99
100     @Test
101     public void extract_invalidContentSupplied() {
102         invalidArgumentsTest("This is a piece of nonsense and not a zip file".getBytes(), FOO, FOO,
103                 "An error occurred trying to create a ZipFile. Is the content being converted really a csar file?");
104     }
105
106     @Test
107     public void extract_archiveContainsNoYmlFiles() throws IOException {
108         try {
109             YamlExtractor.extract(loadResource("compressedArtifacts/noYmlFilesArchive.zip"), "noYmlFilesArchive.zip",
110                     "v1");
111             fail("An instance of InvalidArchiveException should have been thrown.");
112         } catch (Exception e) {
113             assertTrue("An instance of InvalidArchiveException should have been thrown.",
114                     e instanceof InvalidArchiveException);
115             assertEquals("Incorrect message was returned", "No valid yml files were found in the csar file.",
116                     e.getMessage());
117         }
118     }
119
120     private byte[] loadResource(final String archiveName) throws IOException {
121         return IOUtils.toByteArray(YamlExtractor.class.getClassLoader().getResource(archiveName));
122     }
123
124     @Test
125     public void extract_archiveContainsOnlyTheExpectedYmlFilesFromSdWanService()
126             throws IOException, InvalidArchiveException {
127         List<Artifact> ymlFiles =
128                 YamlExtractor.extract(loadResource("compressedArtifacts/service-SdWanServiceTest-csar.csar"),
129                         "service-SdWanServiceTest-csar.csar", "v1");
130
131         List<String> payloads = new ArrayList<>();
132         payloads.add("ymlFiles/resource-SdWanTestVsp-template.yml");
133         payloads.add("ymlFiles/resource-TunnelXconntest-template.yml");
134         payloads.add("ymlFiles/service-SdWanServiceTest-template.yml");
135         payloads.add("ymlFiles/artifacts.yml");
136         payloads.add("ymlFiles/data.yml");
137
138         new ArtifactTestUtils().performYmlAsserts(ymlFiles, payloads);
139     }
140 }
141