Additional tests for VNF image extraction
[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.containsString;
25 import static org.hamcrest.CoreMatchers.equalTo;
26 import static org.hamcrest.CoreMatchers.instanceOf;
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.hamcrest.CoreMatchers.nullValue;
29 import static org.junit.Assert.assertThat;
30
31 import java.io.IOException;
32 import java.util.HashMap;
33 import java.util.LinkedHashMap;
34 import java.util.List;
35 import org.junit.Test;
36 import org.onap.aai.babel.service.data.BabelArtifact;
37 import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType;
38 import org.onap.aai.babel.testdata.CsarTest;
39 import org.onap.aai.babel.util.ArtifactTestUtils;
40
41 /**
42  * Tests {@link VnfVendorImageExtractor}.
43  */
44 public class VnfVendorImageExtractorTest {
45
46     @Test(expected = NullPointerException.class)
47     public void createVendorImageMappingsNullCsarSupplied() throws ToscaToCatalogException, IOException {
48         new VnfVendorImageExtractor().extract(null);
49     }
50
51     @Test(expected = ToscaToCatalogException.class)
52     public void createVendorImageMappingsEmptyCsarSupplied() throws ToscaToCatalogException, IOException {
53         new VnfVendorImageExtractor().extract(new byte[0]);
54     }
55
56     @Test(expected = ToscaToCatalogException.class)
57     public void createVendorImageMappingsInvalidCsarFile() throws IOException, ToscaToCatalogException {
58         CsarTest.NO_YAML_FILES.extractVnfVendorImages();
59     }
60
61     @Test(expected = ToscaToCatalogException.class)
62     public void createVendorImageMappingsInvalidFile() throws IOException, ToscaToCatalogException {
63         new VnfVendorImageExtractor().extract("not a real file".getBytes());
64     }
65
66     @Test
67     public void createVendorImageMappingsMoreThanOneVnfConfigurationExists() throws IOException {
68         try {
69             CsarTest.MULTIPLE_VNF_CSAR.extractArtifacts();
70         } catch (Exception e) {
71             assertThat(e, is(instanceOf(ToscaToCatalogException.class)));
72             assertThat(e.getLocalizedMessage(),
73                     is(equalTo("An error occurred trying to get the vnf catalog from a csar file. "
74                             + "2 vnfConfigurations were found in the csar file and only one is allowed.")));
75         }
76     }
77
78     @Test
79     public void createVendorImageMappingsNoVnfConfigurationExists() throws IOException, ToscaToCatalogException {
80         assertThat(CsarTest.NO_VNF_CONFIG_CSAR.extractVnfVendorImages(), is(nullValue()));
81     }
82
83     @Test
84     public void createVendorImageMappingsValidFile() throws IOException, ToscaToCatalogException {
85         BabelArtifact artifact = CsarTest.VNF_VENDOR_CSAR.extractVnfVendorImages();
86         assertThat(artifact.getName(), is(equalTo("vnfVendorImageConfigurations")));
87         assertThat(artifact.getType(), is(equalTo(ArtifactType.VNFCATALOG)));
88         assertThat(artifact.getPayload(),
89                 is(equalTo(new ArtifactTestUtils().getRequestJson("vnfVendorImageConfigurations.json"))));
90     }
91
92     @Test
93     public void testSoftwareVersions() throws ToscaToCatalogException {
94         VnfVendorImageExtractor extractor = new VnfVendorImageExtractor();
95         SdcToscaHelper helper = new SdcToscaHelper();
96
97         List<String> versions;
98         try {
99             versions = extractor.extractSoftwareVersions(helper.buildMappings());
100             assertThat(versions.size(), is(0));
101         } catch (ToscaToCatalogException e) {
102             assertThat(e.getMessage(), containsString("No software versions"));
103         }
104
105         helper.addNodeTemplate();
106         try {
107             versions = extractor.extractSoftwareVersions(helper.buildMappings());
108             assertThat(versions.size(), is(0));
109         } catch (ToscaToCatalogException e) {
110             assertThat(e.getMessage(), containsString("No software versions"));
111         }
112
113         helper.addNodeTemplate("string");
114         try {
115             versions = extractor.extractSoftwareVersions(helper.buildMappings());
116             assertThat(versions.size(), is(0));
117         } catch (ClassCastException e) {
118             assertThat(e.getMessage(), containsString("java.lang.String"));
119         }
120
121         HashMap<String, Object> images = new LinkedHashMap<>();
122         images.put("image", "string");
123         helper.addNodeTemplate(images);
124         try {
125             versions = extractor.extractSoftwareVersions(helper.buildMappings());
126             assertThat(versions.size(), is(1));
127         } catch (ClassCastException e) {
128             assertThat(e.getMessage(), containsString("java.lang.String"));
129         }
130
131         HashMap<String, Object> image = new LinkedHashMap<>();
132         image.put("software_version", "1.2.3");
133         images.put("image", image);
134         helper = new SdcToscaHelper();
135         helper.addNodeTemplate(images);
136         versions = extractor.extractSoftwareVersions(helper.buildMappings());
137         assertThat(versions.size(), is(1));
138         assertThat(versions.get(0), is("1.2.3"));
139     }
140 }