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