Remove all references to artifactgenerator config
[aai/babel.git] / src / test / java / org / onap / aai / babel / xml / generator / TestAaiArtifactGenerator.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 2017-2019 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.xml.generator;
23
24 import static org.hamcrest.CoreMatchers.equalTo;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.junit.Assert.assertThat;
27
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.aai.babel.csar.extractor.InvalidArchiveException;
34 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
35 import org.onap.aai.babel.testdata.CsarTest;
36 import org.onap.aai.babel.util.ArtifactTestUtils;
37 import org.onap.aai.babel.util.Resources;
38 import org.onap.aai.babel.xml.generator.api.AaiArtifactGenerator;
39 import org.onap.aai.babel.xml.generator.data.AdditionalParams;
40 import org.onap.aai.babel.xml.generator.data.GenerationData;
41 import org.onap.aai.babel.xml.generator.model.WidgetType;
42 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
43 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
44 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
45
46 /**
47  * Direct tests of the {@link AaiArtifactGenerator} to improve code coverage.
48  */
49 public class TestAaiArtifactGenerator {
50
51     private ArtifactTestUtils testUtils;
52
53     @Before
54     public void setup() {
55         testUtils = new ArtifactTestUtils();
56         testUtils.setGeneratorSystemProperties();
57     }
58
59     @Test
60     public void testParserWithInvalidMappings() throws IOException, InvalidArchiveException {
61         System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE,
62                 new ArtifactTestUtils().getResourcePath(Resources.INVALID_TOSCA_MAPPING_CONFIG));
63
64         GenerationData data = generateArtifactsFromCsarFile();
65         assertThat("Number of errors produced " + data.getErrorData(), data.getErrorData().size(), is(equalTo(1)));
66         assertThat("Number of resources generated", data.getResultData().size(), is(equalTo(0)));
67     }
68
69     @Test
70     public void testParserWithCsarFile() throws IOException, InvalidArchiveException {
71         GenerationData data = generateArtifactsFromCsarFile();
72         assertThat("Number of errors produced " + data.getErrorData(), data.getErrorData().size(), is(equalTo(0)));
73         assertThat("Number of resources generated", data.getResultData().size(), is(equalTo(2)));
74     }
75
76     /**
77      * Test that an Exception is thrown when a Widget Type (such as ALLOTTED_RESOURCE) required by Babel is not present
78      * in the WidgetType dynamic enumeration.
79      *
80      * @throws SdcToscaParserException
81      *             if the test CSAR file is invalid
82      * @throws IOException
83      *             if the widget mappings config cannot be loaded
84      * @throws XmlArtifactGenerationException
85      *             if the configured widget mappings do not support processed widget type(s)
86      */
87     @Test(expected = IllegalArgumentException.class)
88     public void testParserWithIncompleteMappings()
89             throws SdcToscaParserException, IOException, XmlArtifactGenerationException {
90         testUtils.loadWidgetMappings();
91
92         AaiArtifactGenerator artifactGenerator = new AaiArtifactGenerator();
93         WidgetType.clearElements(); // Remove all WidgetTypes so that the generator fails
94
95         ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance()
96                 .getSdcCsarHelper(TestAaiArtifactGenerator.class.getClassLoader()
97                         .getResource(ArtifactTestUtils.CSAR_INPUTS_FOLDER + CsarTest.VNF_VENDOR_CSAR.getName())
98                         .getFile().toString());
99         artifactGenerator.generateAllArtifacts("1.0", csarHelper);
100     }
101
102     /**
103      * Invoke the generator with a sample CSAR file.
104      *
105      * @return the generated AAI Artifacts
106      * @throws InvalidArchiveException
107      *             if the test CSAR file is invalid
108      * @throws IOException
109      *             if there are I/O errors reading the CSAR content
110      */
111     private GenerationData generateArtifactsFromCsarFile() throws InvalidArchiveException, IOException {
112         Map<String, String> additionalParams = new HashMap<>();
113         additionalParams.put(AdditionalParams.SERVICE_VERSION.getName(), "1.0");
114         return new AaiArtifactGenerator().generateArtifact(CsarTest.VNF_VENDOR_CSAR.getContent(),
115                 CsarTest.VNF_VENDOR_CSAR.extractArtifacts(), additionalParams);
116     }
117 }