Refactor Junit test code to remove duplication
[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.testdata.CsarTest;
35 import org.onap.aai.babel.util.ArtifactTestUtils;
36
37 /**
38  * Tests {@link VnfVendorImageExtractor}.
39  */
40 public class VnfVendorImageExtractorTest {
41
42     @Test(expected = NullPointerException.class)
43     public void createVendorImageMappingsNullCsarSupplied() throws ToscaToCatalogException, IOException {
44         new VnfVendorImageExtractor().extract(null);
45     }
46
47     @Test(expected = ToscaToCatalogException.class)
48     public void createVendorImageMappingsEmptyCsarSupplied() throws ToscaToCatalogException, IOException {
49         new VnfVendorImageExtractor().extract(new byte[0]);
50     }
51
52     @Test(expected = ToscaToCatalogException.class)
53     public void createVendorImageMappingsInvalidCsarFile() throws IOException, ToscaToCatalogException {
54         CsarTest.NO_YAML_FILES.extractVnfVendorImages();
55     }
56
57     @Test(expected = ToscaToCatalogException.class)
58     public void createVendorImageMappingsInvalidFile() throws IOException, ToscaToCatalogException {
59         new VnfVendorImageExtractor().extract("not a real file".getBytes());
60     }
61
62     @Test
63     public void createVendorImageMappingsMoreThanOneVnfConfigurationExists() throws IOException {
64         try {
65             CsarTest.MULTIPLE_VNF_CSAR.extractArtifacts();
66         } catch (Exception e) {
67             assertThat(e, is(instanceOf(ToscaToCatalogException.class)));
68             assertThat(e.getLocalizedMessage(),
69                     is(equalTo("An error occurred trying to get the vnf catalog from a csar file. "
70                             + "2 vnfConfigurations were found in the csar file and only one is allowed.")));
71         }
72     }
73
74     @Test
75     public void createVendorImageMappingsNoVnfConfigurationExists() throws IOException, ToscaToCatalogException {
76         assertThat(CsarTest.NO_VNF_CONFIG_CSAR.extractVnfVendorImages(), is(nullValue()));
77     }
78
79     @Test
80     public void createVendorImageMappingsValidFile() throws IOException, ToscaToCatalogException {
81         BabelArtifact artifact = CsarTest.VNF_VENDOR_CSAR.extractVnfVendorImages();
82         assertThat(artifact.getName(), is(equalTo("vnfVendorImageConfigurations")));
83         assertThat(artifact.getType(), is(equalTo(ArtifactType.VNFCATALOG)));
84         assertThat(artifact.getPayload(),
85                 is(equalTo(new ArtifactTestUtils().getRequestJson("vnfVendorImageConfigurations.json"))));
86     }
87 }