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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.core.converter.impl.pnfd;
22 import static org.hamcrest.Matchers.is;
23 import static org.junit.Assert.assertThat;
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;
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;
38 public class PnfdQueryExecutorTest {
40 public ExpectedException expectedExceptionRule = ExpectedException.none();
42 private Map<String, Object> yamlToQuery;
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"
51 "type", "tosca.nodes.nfv.PNF",
52 "properties", ImmutableMap.of(
53 "layers_protocol", "",
54 "provider", "Mycompany",
60 final Map nodeTemplate2 = ImmutableMap.of("nodeTemplate2"
62 "type", "tosca.nodes.nfv.PnfExtCp",
63 "properties", ImmutableMap.of(
64 "trunk_mode", "false",
66 "description", "External connection point to access this pnf",
67 "layers_protocol", ImmutableList.of("ipv4", "ipv6", "otherProtocol")
72 nodeTemplateMap.putAll(nodeTemplate1);
73 nodeTemplateMap.putAll(nodeTemplate2);
78 public void queryNestedYaml() {
80 final ImmutableMap<String, Object> query =
81 ImmutableMap.of("topology_template",
82 ImmutableMap.of("nodeTemplate2",
83 ImmutableMap.of("type", "tosca.nodes.nfv.PnfExtCp")
87 final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
89 assertThat("Element should be found", queryResult, is(true));
93 public void andQueryWithPropertiesInSameLevel() {
95 final ImmutableMap<String, Object> query =
101 "type", "tosca.nodes.nfv.PnfExtCp",
102 "properties", ImmutableMap.of(
104 "description", "External connection point to access this pnf"
110 final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
112 assertThat("Element should be found", queryResult, is(true));
116 public void andQueryWithPropertiesDifferentLevel() {
118 final ImmutableMap<String, Object> query =
124 "type", "tosca.nodes.nfv.PnfExtCp"
128 "type", "tosca.nodes.nfv.PNF"
133 final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
135 assertThat("Element should be found", queryResult, is(true));
139 public void queryListPropertyNotSupported() {
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 =
150 "type", "tosca.nodes.nfv.PnfExtCp",
151 "properties", ImmutableMap.of(
152 "layers_protocol", ImmutableList.of("ipv4", "ipv6", "otherProtocol")
158 final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
162 public void queryStartingListPropertyNotSupported() {
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");
169 PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
173 public void queryWithUnsupportedClassInstance() {
174 //given a query with unsupported class instance
175 final Object query = ImmutableMap.of("topology_template", new Object());
177 expectedExceptionRule.expect(QueryOperationNotSupportedException.class);
178 expectedExceptionRule.expectMessage(String.format("Yaml query operation for '%s' is not supported yet", Object.class));
180 PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
184 public void queryAString() {
185 //given a query with string
186 final Object yaml = "query";
187 final Object query = "query";
189 final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yaml);
192 assertThat("Element should be found", queryResult, is(true));
197 public void queryWithNullPropertyValue() {
198 //given a query with string
199 final Map<String, Object> query1 = new HashMap<>();
200 query1.put("topology_template", null);
202 final Map<String, Object> topologyTemplate = new HashMap<>();
203 topologyTemplate.put("nodeTemplate1", null);
204 final Object query2 = ImmutableMap.of("topology_template", topologyTemplate);
206 final Map<String, Object> query3 = new HashMap<>();
207 query3.put("topology_template1", null);
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);
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));
219 public void nullQuery() {
221 final Object query = null;
223 final boolean queryResult = PnfdQueryExecutor.find(new ConversionQuery(query), yamlToQuery);
225 assertThat("Element should be found", queryResult, is(true));