Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / test / java / org / onap / sdc / toscaparser / api / functions / GetInputTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2019 Fujitsu Limited.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18 package org.onap.sdc.toscaparser.api.functions;
19
20 import org.junit.Test;
21 import org.onap.sdc.toscaparser.api.*;
22 import org.onap.sdc.toscaparser.api.common.JToscaException;
23 import org.onap.sdc.toscaparser.api.elements.constraints.Schema;
24 import org.onap.sdc.toscaparser.api.parameters.Input;
25 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31
32 import static org.hamcrest.CoreMatchers.is;
33 import static org.hamcrest.CoreMatchers.notNullValue;
34 import static org.junit.Assert.*;
35
36 public class GetInputTest {
37
38     private static final String TEST_FILENAME = "csars/listed_input.csar";
39     private static final String TEST_FILENAME_NG = "csars/listed_input_ng.csar";
40     private static final String TEST_PROPERTY_ROLE = "role";
41     private static final String TEST_PROPERTY_LONGITUDE = "longitude";
42     private static final String TEST_DEFAULT_VALUE = "dsvpn-hub";
43     private static final String TEST_DESCRIPTION_VALUE = "This is used for SDWAN only";
44     private static final String TEST_INPUT_TYPE = "type";
45     private static final String TEST_INPUT_SCHEMA_TYPE = "tosca.datatypes.siteresource.site";
46     private static final String TEST_TOSTRING = "get_input:[sites, 1, longitude]";
47     private static final String TEST_INPUT_SITES = "sites";
48
49     @Test
50     public void validate() throws JToscaException {
51         String fileStr = JToscaImportTest.class.getClassLoader().getResource(TEST_FILENAME).getFile();
52         File file = new File(fileStr);
53         ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null, false);
54         NodeTemplate nodeTemplate = toscaTemplate.getNodeTemplates().get(1).getSubMappingToscaTemplate().getNodeTemplates().get(0);
55         ArrayList<Input> inputs = toscaTemplate.getNodeTemplates().get(1).getSubMappingToscaTemplate().getInputs();
56         LinkedHashMap<String, Property> properties = nodeTemplate.getProperties();
57         assertThat(properties, notNullValue());
58         assertThat(properties.size(), is(14));
59
60         Property property = properties.get(TEST_PROPERTY_ROLE);
61         assertThat(properties, notNullValue());
62         assertThat(property.getName(), is(TEST_PROPERTY_ROLE));
63         assertThat(property.getType(), is(Schema.STRING));
64         assertThat(property.getDefault(), is(TEST_DEFAULT_VALUE));
65         assertThat(property.getDescription(), is(TEST_DESCRIPTION_VALUE));
66         GetInput getInput = (GetInput) property.getValue();
67         assertThat(getInput.getEntrySchema().get(TEST_INPUT_TYPE).toString(), is(TEST_INPUT_SCHEMA_TYPE));
68
69         property = properties.get(TEST_PROPERTY_LONGITUDE);
70         assertThat(properties, notNullValue());
71         assertThat(property.getName(), is(TEST_PROPERTY_LONGITUDE));
72         assertThat(property.getValue().toString(), is(TEST_TOSTRING));
73         getInput = (GetInput) property.getValue();
74         ArrayList<Object> getInputArguments = getInput.getArguments();
75         assertThat(getInputArguments.size(), is(3));
76         assertThat(getInputArguments.get(0).toString(), is(TEST_INPUT_SITES));
77         assertThat(getInputArguments.get(1).toString(), is("1"));
78         assertThat(getInputArguments.get(2).toString(), is(TEST_PROPERTY_LONGITUDE));
79
80         Input in = inputs.get(10);
81         assertThat(in.getEntrySchema().get(TEST_INPUT_TYPE), is(TEST_INPUT_SCHEMA_TYPE));
82         assertThat(in.getName(), is(TEST_INPUT_SITES));
83         assertThat(in.getType(), is(Input.LIST));
84     }
85
86     @Test
87     public void validate_ng() throws JToscaException {
88         //invalid file
89         String fileStr = JToscaImportTest.class.getClassLoader().getResource(TEST_FILENAME_NG).getFile();
90         File file = new File(fileStr);
91         ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null, false);
92
93         List<String> issues = ThreadLocalsHolder.getCollector().getValidationIssueReport();
94         assertTrue(issues.stream().anyMatch(x -> x.contains("JE282")));
95     }
96 }