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=========================================================
22 package org.onap.aai.babel.csar.vnfcatalog;
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;
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;
45 * Tests {@link VnfVendorImageExtractor}.
47 public class TestVnfVendorImageExtractor {
49 @Test(expected = NullPointerException.class)
50 public void createVendorImageMappingsNullCsarSupplied() throws ToscaToCatalogException, IOException {
51 new VnfVendorImageExtractor().extract(null);
54 @Test(expected = ToscaToCatalogException.class)
55 public void createVendorImageMappingsEmptyCsarSupplied() throws ToscaToCatalogException, IOException {
56 new VnfVendorImageExtractor().extract(new byte[0]);
59 @Test(expected = ToscaToCatalogException.class)
60 public void createVendorImageMappingsInvalidCsarFile() throws IOException, ToscaToCatalogException {
61 CsarTest.NO_YAML_FILES.extractVnfVendorImages();
64 @Test(expected = ToscaToCatalogException.class)
65 public void createVendorImageMappingsInvalidFile() throws IOException, ToscaToCatalogException {
66 new VnfVendorImageExtractor().extract("not a real file".getBytes());
70 public void createVendorImageMappingsMoreThanOneVnfConfigurationExists() throws IOException {
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.")));
82 public void createVendorImageMappingsNoVnfConfigurationExists() throws IOException, ToscaToCatalogException {
83 assertThat(CsarTest.NO_VNF_CONFIG_CSAR.extractVnfVendorImages(), is(nullValue()));
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"))));
96 * Test that an Exception is created when there are no software versions defined for a VF.
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);
108 public void testSoftwareVersions() throws ToscaToCatalogException {
109 VnfVendorImageExtractor extractor = new VnfVendorImageExtractor();
110 SdcToscaHelper helper = new SdcToscaHelper();
112 List<String> versions;
113 versions = extractor.extractSoftwareVersions(helper.buildMappings().getNodeTemplates());
114 assertThat(versions.size(), is(0));
116 helper.addNodeTemplate();
117 versions = extractor.extractSoftwareVersions(helper.buildMappings().getNodeTemplates());
118 assertThat(versions.size(), is(0));
120 helper.addNodeTemplate("string");
122 versions = extractor.extractSoftwareVersions(helper.buildMappings().getNodeTemplates());
123 assertThat(versions.size(), is(0));
124 } catch (ClassCastException e) {
125 assertThat(e.getMessage(), containsString("java.lang.String"));
128 HashMap<String, Object> images = new LinkedHashMap<>();
129 images.put("image", "string");
130 helper.addNodeTemplate(images);
132 versions = extractor.extractSoftwareVersions(helper.buildMappings().getNodeTemplates());
133 assertThat(versions.size(), is(1));
134 } catch (ClassCastException e) {
135 assertThat(e.getMessage(), containsString("java.lang.String"));
138 HashMap<String, Object> image = new LinkedHashMap<>();
139 image.put("software_version", "1.2.3");
140 images.put("image", image);
141 helper = new SdcToscaHelper();
142 helper.addNodeTemplate(images);
143 versions = extractor.extractSoftwareVersions(helper.buildMappings().getNodeTemplates());
144 assertThat(versions.size(), is(1));
145 assertThat(versions.get(0), is("1.2.3"));