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