2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.authorative.concepts;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
29 import com.google.gson.GsonBuilder;
30 import java.util.ArrayList;
31 import java.util.List;
33 import java.util.Map.Entry;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.base.PfKey;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.yaml.snakeyaml.Yaml;
45 * Test of the {@link ToscaPolicyTypeFilter} class.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class ToscaPolicyTypeFilterTest {
50 private static final String VERSION_100 = "1.0.0";
52 private static final String VERSION_000 = "0.0.0";
54 // Logger for this class
55 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypeFilterTest.class);
58 private static final String[] policyTypeResourceNames = {
59 "policytypes/onap.policies.controlloop.Operational.yaml",
60 "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml",
61 "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml",
62 "policytypes/onap.policies.optimization.resource.PciPolicy.yaml",
63 "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml",
64 "policytypes/onap.policies.controlloop.guard.common.Blacklist.yaml",
65 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
66 "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml",
67 "policytypes/onap.policies.optimization.resource.Vim_fit.yaml",
68 "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml",
69 "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml",
70 "policytypes/onap.policies.optimization.service.QueryPolicy.yaml",
71 "policytypes/onap.policies.controlloop.guard.common.MinMax.yaml",
72 "policytypes/onap.policies.controlloop.guard.common.FrequencyLimiter.yaml",
73 "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
74 "policytypes/onap.policies.Optimization.yaml",
75 "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"
79 private static List<ToscaPolicyType> typeList = new ArrayList<>();
82 * Set up a Tosca Policy type list for filtering.
84 * @throws CoderException on JSON decoding errors
87 public static void setupTypeList() throws CoderException {
88 for (String policyTypeResourceName : policyTypeResourceNames) {
89 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeResourceName);
90 Object yamlObject = new Yaml().load(policyTypeString);
91 String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
93 ToscaServiceTemplate serviceTemplate =
94 new StandardCoder().decode(yamlAsJsonString, ToscaServiceTemplate.class);
95 assertNotNull(serviceTemplate);
97 addPolicyTypes(serviceTemplate.getPolicyTypes());
100 for (ToscaPolicyType type : typeList) {
101 LOGGER.info("using policy type-" + type.getName() + ":" + type.getVersion());
105 private static void addPolicyTypes(Map<String, ToscaPolicyType> foundPolicyTypeMap) {
106 for (Entry<String, ToscaPolicyType> policyTypeEntry : foundPolicyTypeMap.entrySet()) {
107 ToscaPolicyType policyType = policyTypeEntry.getValue();
108 if (policyType.getName() == null) {
109 policyType.setName(policyTypeEntry.getKey());
111 if (policyType.getVersion() == null) {
112 policyType.setVersion(PfKey.NULL_KEY_VERSION);
114 if (!typeList.contains(policyType)) {
115 typeList.add(policyType);
121 public void testNullList() {
122 ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build();
124 assertThatThrownBy(() -> {
126 }).hasMessageMatching("originalList is marked .*on.*ull but is null");
130 public void testFilterNothing() {
131 ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build();
133 List<ToscaPolicyType> filteredList = filter.filter(typeList);
134 assertTrue(filteredList.containsAll(typeList));
138 public void testFilterLatestVersion() {
139 ToscaPolicyTypeFilter filter =
140 ToscaPolicyTypeFilter.builder().version(ToscaPolicyTypeFilter.LATEST_VERSION).build();
142 List<ToscaPolicyType> filteredList = filter.filter(typeList);
143 assertEquals(20, filteredList.size());
144 assertEquals(VERSION_100, filteredList.get(0).getVersion());
145 assertEquals(VERSION_100, filteredList.get(11).getVersion());
147 typeList.get(12).setVersion("2.0.0");
148 filteredList = filter.filter(typeList);
149 assertEquals(20, filteredList.size());
151 // This seems to change around as to where this policy type
152 // got changed - perhaps we change this test to find a specific name
153 // to test for vs an index which never remains consistent?
155 assertEquals("2.0.0", filteredList.get(18).getVersion());
157 // And now this index changes again??
159 assertEquals(VERSION_100, filteredList.get(17).getVersion());
161 typeList.get(12).setVersion(VERSION_100);
162 filteredList = filter.filter(typeList);
163 assertEquals(20, filteredList.size());
164 assertEquals(VERSION_100, filteredList.get(0).getVersion());
165 assertEquals(VERSION_100, filteredList.get(18).getVersion());
169 public void testFilterNameVersion() {
170 ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().name("onap.policies.Monitoring").build();
171 List<ToscaPolicyType> filteredList = filter.filter(typeList);
172 assertEquals(1, filteredList.size());
174 filter = ToscaPolicyTypeFilter.builder().name("onap.policies.monitoring.cdap.tca.hi.lo.app").build();
175 filteredList = filter.filter(typeList);
176 assertEquals(1, filteredList.size());
178 filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.LpaPolicy").build();
179 filteredList = filter.filter(typeList);
180 assertEquals(0, filteredList.size());
182 filter = ToscaPolicyTypeFilter.builder().version(VERSION_100).build();
183 filteredList = filter.filter(typeList);
184 assertEquals(20, filteredList.size());
186 filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version(VERSION_000)
188 filteredList = filter.filter(typeList);
189 assertEquals(0, filteredList.size());
191 filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version("0.0.1").build();
192 filteredList = filter.filter(typeList);
193 assertEquals(0, filteredList.size());