2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.toscaparser.api;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.containsInAnyOrder;
25 import static org.hamcrest.Matchers.hasSize;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.LinkedHashMap;
36 import org.junit.jupiter.api.Test;
37 import org.onap.sdc.toscaparser.api.common.JToscaException;
38 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
39 import org.onap.sdc.toscaparser.api.utils.JToscaErrorCodes;
40 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
42 public class JToscaMetadataParse {
45 public void testMetadataParsedCorrectly() throws JToscaException {
46 final File file = loadCsar("csars/csar_hello_world.csar");
47 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
48 LinkedHashMap<String, Object> metadataProperties = toscaTemplate.getMetaProperties("TOSCA.meta");
49 assertNotNull(metadataProperties);
50 Object entryDefinition = metadataProperties.get("Entry-Definitions");
51 assertNotNull(entryDefinition);
52 assertEquals("tosca_helloworld.yaml", entryDefinition);
56 public void noWarningsAfterParse() throws JToscaException {
57 final File file = loadCsar("csars/tmpCSAR_Huawei_vSPGW_fixed.csar");
58 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
59 int validationIssuesCaught = ThreadLocalsHolder.getCollector().validationIssuesCaught();
60 assertTrue(validationIssuesCaught == 0);
64 public void requiredInputErrorsAfterParse() throws JToscaException {
65 final File file = loadCsar("csars/tmpCSAR_Huawei_vSPGW_without_required_inputs.csar");
66 new ToscaTemplate(file.getAbsolutePath(), null, true, null);
68 final Map<String, JToscaValidationIssue> validationIssues = ThreadLocalsHolder.getCollector()
69 .getValidationIssues();
70 final Collection<JToscaValidationIssue> actualValidationIssueList = validationIssues.values();
72 final Collection<JToscaValidationIssue> expectedValidationIssueList = new ArrayList<>();
73 final String errorCode = "JE003";
74 final String errorFormat = "MissingRequiredFieldError: The required input \"%s\" was not provided";
75 expectedValidationIssueList.add(new JToscaValidationIssue(errorCode
76 , String.format(errorFormat, "nf_naming_code")));
77 expectedValidationIssueList.add(new JToscaValidationIssue(errorCode
78 , String.format(errorFormat, "nf_type")));
79 expectedValidationIssueList.add(new JToscaValidationIssue(errorCode
80 , String.format(errorFormat, "nf_role")));
81 expectedValidationIssueList.add(new JToscaValidationIssue(errorCode
82 , String.format(errorFormat, "min_instances")));
83 expectedValidationIssueList.add(new JToscaValidationIssue(errorCode
84 , String.format(errorFormat, "max_instances")));
85 expectedValidationIssueList.add(new JToscaValidationIssue(errorCode
86 , String.format(errorFormat, "nf_function")));
88 assertThat("The actual and the expected validation issue lists should have the same size"
89 , actualValidationIssueList, hasSize(expectedValidationIssueList.size())
92 assertThat("The actual and the expected validation issue lists should be the same"
93 , actualValidationIssueList, containsInAnyOrder(expectedValidationIssueList.toArray(new JToscaValidationIssue[0]))
98 public void testEmptyCsar() throws JToscaException {
99 final File file = loadCsar("csars/emptyCsar.csar");
101 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
102 } catch (JToscaException e) {
103 assertTrue(e.getCode().equals(JToscaErrorCodes.INVALID_CSAR_FORMAT.getValue()));
105 int validationIssuesCaught = ThreadLocalsHolder.getCollector().validationIssuesCaught();
106 assertTrue(validationIssuesCaught == 0);
110 public void testEmptyPath() throws JToscaException {
111 String fileStr = JToscaMetadataParse.class.getClassLoader().getResource("").getFile();
112 File file = new File(fileStr);
114 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
115 } catch (JToscaException e) {
116 assertTrue(e.getCode().equals(JToscaErrorCodes.PATH_NOT_VALID.getValue()));
120 private File loadCsar(final String csarFilePath) {
121 final URL resourceUrl = JToscaMetadataParse.class.getClassLoader().getResource(csarFilePath);
122 assertNotNull(resourceUrl, String.format("Could not load CSAR file '%s'", csarFilePath));
124 return new File(resourceUrl.getFile());