Implement PNFD Model driven conversion
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / test / java / org / openecomp / core / converter / impl / pnfd / PnfdQueryExecutorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.core.converter.impl.pnfd;
21
22 import static org.hamcrest.Matchers.is;
23 import static org.junit.Assert.assertThat;
24
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.ImmutableMap;
27 import com.google.common.collect.ImmutableSet;
28 import java.util.HashMap;
29 import java.util.LinkedHashMap;
30 import java.util.Map;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35 import org.openecomp.core.converter.pnfd.exception.QueryOperationNotSupportedException;
36 import org.openecomp.core.converter.pnfd.model.ConversionQuery;
37
38 public class PnfdQueryExecutorTest {
39     @Rule
40     public ExpectedException expectedExceptionRule = ExpectedException.none();
41
42     private Map<String, Object> yamlToQuery;
43
44     @Before
45     public void setup() {
46         yamlToQuery = new HashMap<>();
47         final LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
48         yamlToQuery.put("topology_template", nodeTemplateMap);
49         final Map nodeTemplate1 = ImmutableMap.of("nodeTemplate1"
50             , ImmutableMap.of(
51                 "type", "tosca.nodes.nfv.PNF",
52                 "properties", ImmutableMap.of(
53                     "layers_protocol", "",
54                     "provider", "Mycompany",
55                     "version", "1.0"
56                 )
57             )
58         );
59
60         final Map nodeTemplate2 = ImmutableMap.of("nodeTemplate2"
61             , ImmutableMap.of(
62                 "type", "tosca.nodes.nfv.PnfExtCp",
63                 "properties", ImmutableMap.of(
64                     "trunk_mode", "false",
65                     "role", "leaf",
66                     "description", "External connection point to access this pnf",
67                     "layers_protocol", ImmutableList.of("ipv4", "ipv6", "otherProtocol")
68                 )
69             )
70         );
71
72         nodeTemplateMap.putAll(nodeTemplate1);
73         nodeTemplateMap.putAll(nodeTemplate2);
74     }
75
76
77     @Test
78     public void queryNestedYaml() {
79         //given
80         final ImmutableMap<String, Object> query =
81             ImmutableMap.of("topology_template",
82                 ImmutableMap.of("nodeTemplate2",
83                     ImmutableMap.of("type", "tosca.nodes.nfv.PnfExtCp")
84                 )
85             );
86         //when
87         final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
88         //then
89         assertThat("Element should be found", queryResult, is(true));
90     }
91
92     @Test
93     public void andQueryWithPropertiesInSameLevel() {
94         //given
95         final ImmutableMap<String, Object> query =
96             ImmutableMap.of(
97                 "topology_template",
98                 ImmutableMap.of(
99                     "nodeTemplate2",
100                     ImmutableMap.of(
101                         "type", "tosca.nodes.nfv.PnfExtCp",
102                         "properties", ImmutableMap.of(
103                             "role", "leaf",
104                             "description", "External connection point to access this pnf"
105                         )
106                     )
107                 )
108             );
109         //when
110         final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
111         //then
112         assertThat("Element should be found", queryResult, is(true));
113     }
114
115     @Test
116     public void andQueryWithPropertiesDifferentLevel() {
117         //given
118         final ImmutableMap<String, Object> query =
119             ImmutableMap.of(
120                 "topology_template",
121                 ImmutableMap.of(
122                     "nodeTemplate2",
123                     ImmutableMap.of(
124                         "type", "tosca.nodes.nfv.PnfExtCp"
125                     ),
126                     "nodeTemplate1",
127                     ImmutableMap.of(
128                         "type", "tosca.nodes.nfv.PNF"
129                     )
130                 )
131             );
132         //when
133         final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
134         //then
135         assertThat("Element should be found", queryResult, is(true));
136     }
137
138     @Test
139     public void queryListPropertyNotSupported() {
140         //then
141         expectedExceptionRule.expect(QueryOperationNotSupportedException.class);
142         expectedExceptionRule.expectMessage("Yaml list query is not supported yet");
143         //given query with a list instance
144         final ImmutableMap<String, Object> query =
145             ImmutableMap.of(
146                 "topology_template",
147                 ImmutableMap.of(
148                     "nodeTemplate2",
149                     ImmutableMap.of(
150                         "type", "tosca.nodes.nfv.PnfExtCp",
151                         "properties", ImmutableMap.of(
152                             "layers_protocol", ImmutableList.of("ipv4", "ipv6", "otherProtocol")
153                         )
154                     )
155                 )
156             );
157         //when
158         final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
159     }
160
161     @Test
162     public void queryStartingListPropertyNotSupported() {
163         //then
164         expectedExceptionRule.expect(QueryOperationNotSupportedException.class);
165         expectedExceptionRule.expectMessage("Yaml list query is not supported yet");
166         //given query with a list instance
167         final Object query = ImmutableSet.of("test", "test", "test");
168         //when
169         PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
170     }
171
172     @Test
173     public void queryWithUnsupportedClassInstance() {
174         //given a query with unsupported class instance
175         final Object query = ImmutableMap.of("topology_template", new Object());
176         //then
177         expectedExceptionRule.expect(QueryOperationNotSupportedException.class);
178         expectedExceptionRule.expectMessage(String.format("Yaml query operation for '%s' is not supported yet", Object.class));
179         //when
180         PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
181     }
182
183     @Test
184     public void queryAString() {
185         //given a query with string
186         final Object yaml = "query";
187         final Object query = "query";
188         //when
189         final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yaml);
190
191         //then
192         assertThat("Element should be found", queryResult, is(true));
193
194     }
195
196     @Test
197     public void queryWithNullPropertyValue() {
198         //given a query with string
199         final Map<String, Object> query1 = new HashMap<>();
200         query1.put("topology_template", null);
201
202         final Map<String, Object> topologyTemplate = new HashMap<>();
203         topologyTemplate.put("nodeTemplate1", null);
204         final Object query2 = ImmutableMap.of("topology_template", topologyTemplate);
205
206         final Map<String, Object> query3 = new HashMap<>();
207         query3.put("topology_template1", null);
208         //when
209         final boolean queryResult1 = PnfdQueryExecutor.find(new ConversionQuery(query1), yamlToQuery);
210         final boolean queryResult2 = PnfdQueryExecutor.find(new ConversionQuery(query2), yamlToQuery);
211         final boolean queryResult3 = PnfdQueryExecutor.find(new ConversionQuery(query3), yamlToQuery);
212         //then
213         assertThat("Element should be found", queryResult1, is(true));
214         assertThat("Element should be found", queryResult2, is(true));
215         assertThat("Element should be found", queryResult3, is(false));
216     }
217
218     @Test
219     public void nullQuery() {
220         //given a null query
221         final Object query = null;
222         //when
223         final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
224         //then
225         assertThat("Element should be found", queryResult, is(true));
226
227     }
228
229 }