2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.modelloader.csar.extractor;
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;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
33 import java.util.stream.Collectors;
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;
44 * Tests {@link VnfCatalogExtractor}.
46 public class VnfCatalogExtractorTest {
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.";
54 public void nullContentSupplied() {
55 invalidArgumentsTest(null, FOO, SUPPLY_AN_ARCHIVE);
59 public void emptyContentSupplied() {
60 invalidArgumentsTest(new byte[0], FOO, SUPPLY_AN_ARCHIVE);
64 public void nullNameSupplied() {
65 invalidArgumentsTest(SOME_BYTES.getBytes(), null, SUPPLY_NAME);
69 public void blankNameSupplied() {
70 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), " \t ", SUPPLY_NAME);
74 public void emptyNameSupplied() {
75 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", SUPPLY_NAME);
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?");
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());
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),
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()));
109 for (Artifact artifact : actualVnfcArtifacts) {
110 assertThat("Unexpected artifact type found.", artifact.getType(), is(ArtifactType.VNF_CATALOG_XML));
113 Set<String> expectedVnfcPayloads = expectedVnfcPayloadsToLoad.stream().map(s -> {
115 return ArtifactTestUtils.loadResourceAsString(s);
116 } catch (IOException e) {
117 throw new RuntimeException(e);
119 }).collect(Collectors.toSet());
121 Set<String> actualVnfcPayloads =
122 actualVnfcArtifacts.stream().map(s -> s.getPayload()).collect(Collectors.toSet());
124 assertThat("Unexpected VNF Catalog file(s) found.", expectedVnfcPayloads.containsAll(actualVnfcPayloads),
128 private void invalidArgumentsTest(byte[] archive, String name, String expectedErrorMessage) {
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());