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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
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;
43 * Tests {@link VnfCatalogExtractor}.
45 public class VnfCatalogExtractorTest {
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.";
53 public void nullContentSupplied() {
54 invalidArgumentsTest(null, FOO, SUPPLY_AN_ARCHIVE);
58 public void emptyContentSupplied() {
59 invalidArgumentsTest(new byte[0], FOO, SUPPLY_AN_ARCHIVE);
63 public void nullNameSupplied() {
64 invalidArgumentsTest(SOME_BYTES.getBytes(), null, SUPPLY_NAME);
68 public void blankNameSupplied() {
69 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), " \t ", SUPPLY_NAME);
73 public void emptyNameSupplied() {
74 invalidArgumentsTest("just some bytes that will pass the firsts validation".getBytes(), "", SUPPLY_NAME);
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?");
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());
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),
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()));
108 for (Artifact artifact : actualVnfcArtifacts) {
109 assertThat("Unexpected artifact type found.", artifact.getType(), is(ArtifactType.VNF_CATALOG_XML));
112 Set<String> expectedVnfcPayloads = expectedVnfcPayloadsToLoad.stream().map(s -> {
114 return ArtifactTestUtils.loadResourceAsString(s);
115 } catch (IOException e) {
116 throw new RuntimeException(e);
118 }).collect(Collectors.toSet());
120 Set<String> actualVnfcPayloads =
121 actualVnfcArtifacts.stream().map(s -> s.getPayload()).collect(Collectors.toSet());
123 assertThat("Unexpected VNF Catalog file(s) found.", expectedVnfcPayloads.containsAll(actualVnfcPayloads),
127 private void invalidArgumentsTest(byte[] archive, String name, String expectedErrorMessage) {
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());