bd8c33fb1d610efc89c80f6fe6d30997723f4c3a
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / csar / extractor / VnfCatalogExtractorTest.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.modelloader.csar.extractor;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
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 java.util.Set;
33 import java.util.stream.Collectors;
34 import org.junit.Test;
35 import org.onap.aai.modelloader.entity.Artifact;
36 import org.onap.aai.modelloader.entity.ArtifactType;
37 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
38 import org.onap.aai.modelloader.extraction.VnfCatalogExtractor;
39 import org.onap.aai.modelloader.util.ArtifactTestUtils;
40
41
42 /**
43  * Tests {@link VnfCatalogExtractor}.
44  */
45 public class VnfCatalogExtractorTest {
46
47     private static final String FOO = "foo";
48     private static final String SOME_BYTES = "just some bytes that will pass the firsts validation";
49     private static final String SUPPLY_AN_ARCHIVE = "An archive must be supplied for processing.";
50     private static final String SUPPLY_NAME = "The name of the archive must be supplied for processing.";
51
52     @Test
53     public void nullContentSupplied() {
54         invalidArgumentsTest(null, FOO, SUPPLY_AN_ARCHIVE);
55     }
56
57     @Test
58     public void emptyContentSupplied() {
59         invalidArgumentsTest(new byte[0], FOO, SUPPLY_AN_ARCHIVE);
60     }
61
62     @Test
63     public void nullNameSupplied() {
64         invalidArgumentsTest(SOME_BYTES.getBytes(), null, SUPPLY_NAME);
65     }
66
67     @Test
68     public void blankNameSupplied() {
69         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "  \t  ", SUPPLY_NAME);
70     }
71
72     @Test
73     public void emptyNameSupplied() {
74         invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", SUPPLY_NAME);
75     }
76
77     @Test
78     public void invalidContentSupplied() {
79         invalidArgumentsTest("This is a piece of nonsense and not a zip file".getBytes(), FOO,
80                 "An error occurred trying to create a ZipFile. Is the content being converted really a csar file?");
81     }
82
83     @Test
84     public void archiveContainsNoVnfcFiles() throws InvalidArchiveException, IOException {
85         List<Artifact> vnfcArtifacts = new VnfCatalogExtractor().extract(
86                 new ArtifactTestUtils().loadResource("compressedArtifacts/noVnfcFilesArchive.csar"),
87                 "noVnfcFilesArchive.csar");
88         assertTrue("No VNFC files should have been extracted, but " + vnfcArtifacts.size() + " were found.",
89                 vnfcArtifacts.isEmpty());
90     }
91
92     @Test
93     public void archiveContainsThreeRelevantVnfcFiles() throws IOException, InvalidArchiveException {
94         List<String> payloads = new ArrayList<>();
95         payloads.add("xmlFiles/vnfcatalog-1.xml");
96         payloads.add("xmlFiles/vnfcatalog-2.xml");
97         payloads.add("xmlFiles/vnfcatalog-3.xml");
98         String csarArchiveFile = "threeVnfcFilesArchive.csar";
99         performVnfcAsserts(new VnfCatalogExtractor().extract(
100                 new ArtifactTestUtils().loadResource("compressedArtifacts/" + csarArchiveFile), csarArchiveFile),
101                 payloads);
102     }
103
104     public void performVnfcAsserts(List<Artifact> actualVnfcArtifacts, List<String> expectedVnfcPayloadsToLoad) {
105         assertThat("An unexpected number of VNFC files have been extracted", actualVnfcArtifacts.size(),
106                 is(expectedVnfcPayloadsToLoad.size()));
107
108         for (Artifact artifact : actualVnfcArtifacts) {
109             assertThat("Unexpected artifact type found.", artifact.getType(), is(ArtifactType.VNF_CATALOG_XML));
110         }
111
112         Set<String> expectedVnfcPayloads = expectedVnfcPayloadsToLoad.stream().map(s -> {
113             try {
114                 return ArtifactTestUtils.loadResourceAsString(s);
115             } catch (IOException e) {
116                 throw new RuntimeException(e);
117             }
118         }).collect(Collectors.toSet());
119
120         Set<String> actualVnfcPayloads =
121                 actualVnfcArtifacts.stream().map(s -> s.getPayload()).collect(Collectors.toSet());
122
123         assertThat("Unexpected VNF Catalog file(s) found.", expectedVnfcPayloads.containsAll(actualVnfcPayloads),
124                 is(true));
125     }
126
127     private void invalidArgumentsTest(byte[] archive, String name, String expectedErrorMessage) {
128         try {
129             new VnfCatalogExtractor().extract(archive, name);
130             fail("An instance of InvalidArchiveException should have been thrown");
131         } catch (Exception ex) {
132             assertTrue(ex instanceof InvalidArchiveException);
133             assertEquals(expectedErrorMessage, ex.getLocalizedMessage());
134         }
135     }
136
137 }
138