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;
31 import java.util.ArrayList;
32 import java.util.List;
34 import java.util.Map.Entry;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.base.PfKey;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.yaml.snakeyaml.Yaml;
47 * Test of the {@link ToscaPolicyTypeFilter} class.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class ToscaPolicyTypeFilterTest {
52 private static final String VERSION_100 = "1.0.0";
54 private static final String VERSION_000 = "0.0.0";
56 // Logger for this class
57 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypeFilterTest.class);
60 private static final String[] policyTypeResourceNames = {
61 "policytypes/onap.policies.controlloop.Operational.yaml",
62 "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml",
63 "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml",
64 "policytypes/onap.policies.optimization.resource.PciPolicy.yaml",
65 "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml",
66 "policytypes/onap.policies.controlloop.guard.Blacklist.yaml",
67 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
68 "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml",
69 "policytypes/onap.policies.optimization.resource.Vim_fit.yaml",
70 "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml",
71 "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml",
72 "policytypes/onap.policies.optimization.service.QueryPolicy.yaml",
73 "policytypes/onap.policies.controlloop.guard.MinMax.yaml",
74 "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml",
75 "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
76 "policytypes/onap.policies.Optimization.yaml",
77 "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"
81 private static List<ToscaPolicyType> typeList = new ArrayList<>();
84 * Set up a Tosca Policy type list for filtering.
86 * @throws CoderException on JSON decoding errors
89 public static void setupTypeList() throws CoderException {
90 for (String policyTypeResourceName : policyTypeResourceNames) {
91 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeResourceName);
92 Object yamlObject = new Yaml().load(policyTypeString);
93 String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
95 ToscaServiceTemplate serviceTemplate =
96 new StandardCoder().decode(yamlAsJsonString, ToscaServiceTemplate.class);
97 assertNotNull(serviceTemplate);
99 addPolicyTypes(serviceTemplate.getPolicyTypes());
102 for (ToscaPolicyType type : typeList) {
103 LOGGER.info("using policy type-" + type.getName() + ":" + type.getVersion());
107 private static void addPolicyTypes(Map<String, ToscaPolicyType> foundPolicyTypeMap) {
108 for (Entry<String, ToscaPolicyType> policyTypeEntry : foundPolicyTypeMap.entrySet()) {
109 ToscaPolicyType policyType = policyTypeEntry.getValue();
110 if (policyType.getName() == null) {
111 policyType.setName(policyTypeEntry.getKey());
113 if (policyType.getVersion() == null) {
114 policyType.setVersion(PfKey.NULL_KEY_VERSION);
116 if (!typeList.contains(policyType)) {
117 typeList.add(policyType);
123 public void testNullList() {
124 ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build();
126 assertThatThrownBy(() -> {
128 }).hasMessageMatching("originalList is marked .*on.*ull but is null");
132 public void testFilterNothing() {
133 ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build();
135 List<ToscaPolicyType> filteredList = filter.filter(typeList);
136 assertTrue(filteredList.containsAll(typeList));
140 public void testFilterLatestVersion() {
141 ToscaPolicyTypeFilter filter =
142 ToscaPolicyTypeFilter.builder().version(ToscaPolicyTypeFilter.LATEST_VERSION).build();
144 List<ToscaPolicyType> filteredList = filter.filter(typeList);
145 assertEquals(20, filteredList.size());
146 assertEquals(VERSION_100, filteredList.get(0).getVersion());
147 assertEquals(VERSION_100, filteredList.get(11).getVersion());
149 typeList.get(12).setVersion("2.0.0");
150 filteredList = filter.filter(typeList);
151 assertEquals(20, filteredList.size());
152 assertEquals("2.0.0", filteredList.get(11).getVersion());
153 assertEquals(VERSION_100, filteredList.get(18).getVersion());
155 typeList.get(12).setVersion(VERSION_100);
156 filteredList = filter.filter(typeList);
157 assertEquals(20, filteredList.size());
158 assertEquals(VERSION_100, filteredList.get(0).getVersion());
159 assertEquals(VERSION_100, filteredList.get(18).getVersion());
163 public void testFilterNameVersion() {
164 ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().name("onap.policies.Monitoring").build();
165 List<ToscaPolicyType> filteredList = filter.filter(typeList);
166 assertEquals(1, filteredList.size());
168 filter = ToscaPolicyTypeFilter.builder().name("onap.policies.monitoring.cdap.tca.hi.lo.app").build();
169 filteredList = filter.filter(typeList);
170 assertEquals(1, filteredList.size());
172 filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.LpaPolicy").build();
173 filteredList = filter.filter(typeList);
174 assertEquals(0, filteredList.size());
176 filter = ToscaPolicyTypeFilter.builder().version(VERSION_100).build();
177 filteredList = filter.filter(typeList);
178 assertEquals(20, filteredList.size());
180 filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version(VERSION_000)
182 filteredList = filter.filter(typeList);
183 assertEquals(0, filteredList.size());
185 filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version("0.0.1").build();
186 filteredList = filter.filter(typeList);
187 assertEquals(0, filteredList.size());