49784c16a2e0e8204909cd8dd10b2e488eb68179
[aai/babel.git] / src / test / java / org / onap / aai / babel / csar / vnfcatalog / VnfVendorImageExtractorTest.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
22 package org.onap.aai.babel.csar.vnfcatalog;
23
24 import static org.hamcrest.CoreMatchers.equalTo;
25 import static org.hamcrest.CoreMatchers.instanceOf;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.hamcrest.CoreMatchers.nullValue;
28 import static org.junit.Assert.assertThat;
29
30 import java.io.IOException;
31 import org.junit.Test;
32 import org.onap.aai.babel.service.data.BabelArtifact;
33 import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType;
34 import org.onap.aai.babel.util.ArtifactTestUtils;
35
36 /**
37  * Tests {@link VnfVendorImageExtractor}
38  */
39 public class VnfVendorImageExtractorTest {
40
41     @Test(expected = NullPointerException.class)
42     public void createVendorImageMappingsNullCsarSupplied() throws ToscaToCatalogException, IOException {
43         new VnfVendorImageExtractor().extract(null);
44     }
45
46     @Test(expected = ToscaToCatalogException.class)
47     public void createVendorImageMappingsEmptyCsarSupplied() throws ToscaToCatalogException, IOException {
48         new VnfVendorImageExtractor().extract(new byte[0]);
49     }
50
51     @Test(expected = ToscaToCatalogException.class)
52     public void createVendorImageMappingsInvalidCsarFile() throws IOException, ToscaToCatalogException {
53         extractArtifact("noYmlFilesArchive.zip");
54     }
55
56     @Test(expected = ToscaToCatalogException.class)
57     public void createVendorImageMappingsInvalidFile() throws IOException, ToscaToCatalogException {
58         extractArtifact("Duff.txt");
59     }
60
61     @Test
62     public void createVendorImageMappingsMoreThanOneVnfConfigurationExists() throws IOException {
63         try {
64             extractArtifact("catalog_csar_too_many_vnfConfigurations.csar");
65         } catch (Exception e) {
66             assertThat(e, is(instanceOf(ToscaToCatalogException.class)));
67             assertThat(e.getLocalizedMessage(),
68                     is(equalTo("An error occurred trying to get the vnf catalog from a csar file. "
69                             + "2 vnfConfigurations were found in the csar file and only one is allowed.")));
70         }
71     }
72
73     @Test
74     public void createVendorImageMappingsNoVnfConfigurationExists() throws IOException, ToscaToCatalogException {
75         assertThat(extractArtifact("noVnfConfiguration.csar"), is(nullValue()));
76     }
77
78     @Test
79     public void createVendorImageMappingsValidFile() throws IOException, ToscaToCatalogException {
80         BabelArtifact artifact = extractArtifact("catalog_csar.csar");
81         assertThat(artifact.getName(), is(equalTo("vnfVendorImageConfigurations")));
82         assertThat(artifact.getType(), is(equalTo(ArtifactType.VNFCATALOG)));
83         assertThat(artifact.getPayload(),
84                 is(equalTo(new ArtifactTestUtils().getRequestJson("vnfVendorImageConfigurations.json"))));
85     }
86
87     private BabelArtifact extractArtifact(String artifactName) throws ToscaToCatalogException, IOException {
88         return new VnfVendorImageExtractor().extract(new ArtifactTestUtils().getCompressedArtifact(artifactName));
89     }
90 }