Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / test / java / org / onap / sdc / toscaparser / api / elements / CalculatePropertyByPathTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api.elements;
22
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.onap.sdc.toscaparser.api.JToscaImportTest;
26 import org.onap.sdc.toscaparser.api.NodeTemplate;
27 import org.onap.sdc.toscaparser.api.Property;
28 import org.onap.sdc.toscaparser.api.ToscaTemplate;
29 import org.onap.sdc.toscaparser.api.common.JToscaException;
30
31 import java.io.File;
32 import java.net.URL;
33 import java.util.List;
34
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertTrue;
37
38 public class CalculatePropertyByPathTest {
39     private static ToscaTemplate toscaTemplate;
40
41     @BeforeClass
42     public static void setUpClass() throws JToscaException {
43         URL scarUrl = JToscaImportTest.class.getClassLoader().getResource("csars/service-NetworkCloudVnfServiceMock-csar.csar");
44         if (scarUrl != null) {
45             File file = new File(scarUrl.getFile());
46             toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
47         }
48
49     }
50
51     @Test
52     public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsNotEmpty() throws JToscaException {
53         NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0)               //Network Cloud VNF MOCK 0
54                 .getSubMappingToscaTemplate().getNodeTemplates().get(0)         //abstract_testVM
55                 .getSubMappingToscaTemplate().getNodeTemplates().get(0);       //testVM_testVM_SRIOVtrunk1_port
56
57         Property property = cp.getProperties().get("related_networks");
58         List<String> propertyValueList = property.getLeafPropertyValue("related_network_role");
59         assertEquals(3, propertyValueList.size());
60         assertTrue(propertyValueList.contains("cor_direct_2"));
61         assertTrue(propertyValueList.contains("sgi_direct_2"));
62         assertTrue(propertyValueList.contains("int_imbl_2"));
63     }
64
65     @Test
66     public void testGetPropertyWhenPropertyHasDataTypeAndPathIsEmpty() {
67         NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0)               //Network Cloud VNF MOCK 0
68                 .getSubMappingToscaTemplate().getNodeTemplates().get(0)         //abstract_testVM
69                 .getSubMappingToscaTemplate().getNodeTemplates().get(1);       //testVM_testVM_SRIOVNonTrunk0_port
70
71         Property property = cp.getProperties().get("exCP_naming");
72         List<String> propertyValueList = property.getLeafPropertyValue("");
73         assertTrue(propertyValueList.isEmpty());
74     }
75
76     @Test
77     public void testGetPropertyWhenPropertyHasSimpleTypeAndValueAsGetInputIsNotResolvedCorrectlyAndPathIsEmpty() {
78         NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0)               //Network Cloud VNF MOCK 0
79                 .getSubMappingToscaTemplate().getNodeTemplates().get(0)         //abstract_testVM
80                 .getSubMappingToscaTemplate().getNodeTemplates().get(1);       //testVM_testVM_SRIOVNonTrunk0_port
81
82         Property property = cp.getProperties().get("network");
83         List<String> propertyValueList = property.getLeafPropertyValue("");
84         assertTrue(propertyValueList.isEmpty());
85     }
86
87     @Test
88     public void testGetPropertyWhenPropertyHasSimpleTypeAndPathIsEmpty() {
89         NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0)               //Network Cloud VNF MOCK 0
90                 .getSubMappingToscaTemplate().getNodeTemplates().get(0)         //abstract_testVM
91                 .getSubMappingToscaTemplate().getNodeTemplates().get(1);       //testVM_testVM_SRIOVNonTrunk0_port
92
93         Property property = cp.getProperties().get("subinterface_indicator");
94         List<String> propertyValueList = property.getLeafPropertyValue("");
95         assertEquals(1, propertyValueList.size());
96         assertEquals("false", propertyValueList.get(0));
97     }
98
99
100     @Test
101     public void testGetPropertyWhenPropertyHasDataTypeAndPathIsNotEmpty() {
102         NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0)               //Network Cloud VNF MOCK 0
103                 .getSubMappingToscaTemplate().getNodeTemplates().get(0)        //abstract_testVM
104                 .getSubMappingToscaTemplate().getNodeTemplates().get(2);       //testVM_testVM_OVS_port
105
106         Property property = cp.getProperties().get("ip_requirements");
107         List<String> propertyValueList = property.getLeafPropertyValue("ip_version");
108         assertEquals(1, propertyValueList.size());
109         assertEquals("4", propertyValueList.get(0));
110     }
111
112     @Test
113     public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsNull() {
114         NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0)               //Network Cloud VNF MOCK 0
115                 .getSubMappingToscaTemplate().getNodeTemplates().get(0)        //abstract_testVM
116                 .getSubMappingToscaTemplate().getNodeTemplates().get(2);       //testVM_testVM_OVS_port
117
118         Property property = cp.getProperties().get("ip_requirements");
119         assertTrue(property.getLeafPropertyValue(null).isEmpty());
120     }
121
122     @Test
123     public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsComplex() {
124         NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0)               //Network Cloud VNF MOCK 0
125                 .getSubMappingToscaTemplate().getNodeTemplates().get(0)        //abstract_testVM
126                 .getSubMappingToscaTemplate().getNodeTemplates().get(0);       //testVM_testVM_SRIOVtrunk1_port
127
128         Property property = cp.getProperties().get("ip_requirements");
129         List<String> propertyValueList = property.getLeafPropertyValue("ip_count_required#is_required");
130         assertEquals(1, propertyValueList.size());
131         assertEquals("false", propertyValueList.get(0));
132     }
133
134     @Test
135     public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsWrong() {
136         NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0)               //Network Cloud VNF MOCK 0
137                 .getSubMappingToscaTemplate().getNodeTemplates().get(0)        //abstract_testVM
138                 .getSubMappingToscaTemplate().getNodeTemplates().get(0);       //testVM_testVM_SRIOVtrunk1_port
139
140         Property property = cp.getProperties().get("ip_requirements");
141         List<String> propertyValueList = property.getLeafPropertyValue("ip_count_required#is_required_1");
142         assertEquals(0, propertyValueList.size());
143     }
144
145     @Test
146     public void testGetPropertyWhenPropertyHasDataTypeWithoutSchemaAndComplexPath() {
147         NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0)               //Network Cloud VNF MOCK 0
148                 .getSubMappingToscaTemplate().getNodeTemplates().get(0)        //abstract_testVM
149                 .getSubMappingToscaTemplate().getNodeTemplates().get(0);       //testVM_testVM_SRIOVtrunk1_port
150
151         Property property = cp.getProperties().get("mac_requirements");
152         List<String> propertyValueList = property.getLeafPropertyValue("mac_count_required#is_required");
153         assertEquals(1, propertyValueList.size());
154         assertEquals("false", propertyValueList.get(0));
155     }
156
157     @Test
158     public void testGetPropertyWhenPropertyHasDataTypeWithoutSchemaAndSimplePath() {
159         NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0)               //Network Cloud VNF MOCK 0
160                 .getSubMappingToscaTemplate().getNodeTemplates().get(0)        //abstract_testVM
161                 .getSubMappingToscaTemplate().getNodeTemplates().get(0);       //testVM_testVM_SRIOVtrunk1_port
162
163         Property property = cp.getProperties().get("mac_requirements");
164         List<String> propertyValueList = property.getLeafPropertyValue("mac_count_required");
165         assertEquals(0, propertyValueList.size());
166     }
167 }