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