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 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;
42 * Tests {@link VnfVendorImageExtractor}.
44 public class VnfVendorImageExtractorTest {
46 @Test(expected = NullPointerException.class)
47 public void createVendorImageMappingsNullCsarSupplied() throws ToscaToCatalogException, IOException {
48 new VnfVendorImageExtractor().extract(null);
51 @Test(expected = ToscaToCatalogException.class)
52 public void createVendorImageMappingsEmptyCsarSupplied() throws ToscaToCatalogException, IOException {
53 new VnfVendorImageExtractor().extract(new byte[0]);
56 @Test(expected = ToscaToCatalogException.class)
57 public void createVendorImageMappingsInvalidCsarFile() throws IOException, ToscaToCatalogException {
58 CsarTest.NO_YAML_FILES.extractVnfVendorImages();
61 @Test(expected = ToscaToCatalogException.class)
62 public void createVendorImageMappingsInvalidFile() throws IOException, ToscaToCatalogException {
63 new VnfVendorImageExtractor().extract("not a real file".getBytes());
67 public void createVendorImageMappingsMoreThanOneVnfConfigurationExists() throws IOException {
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.")));
79 public void createVendorImageMappingsNoVnfConfigurationExists() throws IOException, ToscaToCatalogException {
80 assertThat(CsarTest.NO_VNF_CONFIG_CSAR.extractVnfVendorImages(), is(nullValue()));
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"))));
93 public void testSoftwareVersions() throws ToscaToCatalogException {
94 VnfVendorImageExtractor extractor = new VnfVendorImageExtractor();
95 SdcToscaHelper helper = new SdcToscaHelper();
97 List<String> versions;
98 versions = extractor.extractSoftwareVersions(helper.buildMappings().getNodeTemplates());
99 assertThat(versions.size(), is(0));
101 helper.addNodeTemplate();
102 versions = extractor.extractSoftwareVersions(helper.buildMappings().getNodeTemplates());
103 assertThat(versions.size(), is(0));
105 helper.addNodeTemplate("string");
107 versions = extractor.extractSoftwareVersions(helper.buildMappings().getNodeTemplates());
108 assertThat(versions.size(), is(0));
109 } catch (ClassCastException e) {
110 assertThat(e.getMessage(), containsString("java.lang.String"));
113 HashMap<String, Object> images = new LinkedHashMap<>();
114 images.put("image", "string");
115 helper.addNodeTemplate(images);
117 versions = extractor.extractSoftwareVersions(helper.buildMappings().getNodeTemplates());
118 assertThat(versions.size(), is(1));
119 } catch (ClassCastException e) {
120 assertThat(e.getMessage(), containsString("java.lang.String"));
123 HashMap<String, Object> image = new LinkedHashMap<>();
124 image.put("software_version", "1.2.3");
125 images.put("image", image);
126 helper = new SdcToscaHelper();
127 helper.addNodeTemplate(images);
128 versions = extractor.extractSoftwareVersions(helper.buildMappings().getNodeTemplates());
129 assertThat(versions.size(), is(1));
130 assertThat(versions.get(0), is("1.2.3"));