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