/*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ package org.onap.policy.models.tosca.authorative.concepts; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.gson.GsonBuilder; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; /** * Test of the {@link ToscaPolicyTypeFilter} class. * * @author Liam Fallon (liam.fallon@est.tech) */ public class ToscaPolicyTypeFilterTest { private static final String VERSION_100 = "1.0.0"; private static final String VERSION_000 = "0.0.0"; // Logger for this class private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypeFilterTest.class); // @formatter:off private static final String[] policyTypeResourceNames = { "policytypes/onap.policies.controlloop.Operational.yaml", "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml", "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml", "policytypes/onap.policies.optimization.resource.PciPolicy.yaml", "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml", "policytypes/onap.policies.controlloop.guard.common.Blacklist.yaml", "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml", "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml", "policytypes/onap.policies.optimization.resource.Vim_fit.yaml", "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml", "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml", "policytypes/onap.policies.optimization.service.QueryPolicy.yaml", "policytypes/onap.policies.controlloop.guard.common.MinMax.yaml", "policytypes/onap.policies.controlloop.guard.common.FrequencyLimiter.yaml", "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml", "policytypes/onap.policies.Optimization.yaml", "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml" }; // @formatter:on private static List typeList = new ArrayList<>(); /** * Set up a Tosca Policy type list for filtering. * * @throws CoderException on JSON decoding errors */ @BeforeClass public static void setupTypeList() throws CoderException { for (String policyTypeResourceName : policyTypeResourceNames) { String policyTypeString = ResourceUtils.getResourceAsString(policyTypeResourceName); Object yamlObject = new Yaml().load(policyTypeString); String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(yamlAsJsonString, ToscaServiceTemplate.class); assertNotNull(serviceTemplate); addPolicyTypes(serviceTemplate.getPolicyTypes()); } for (ToscaPolicyType type : typeList) { LOGGER.info("using policy type-" + type.getName() + ":" + type.getVersion()); } } private static void addPolicyTypes(Map foundPolicyTypeMap) { for (Entry policyTypeEntry : foundPolicyTypeMap.entrySet()) { ToscaPolicyType policyType = policyTypeEntry.getValue(); if (policyType.getName() == null) { policyType.setName(policyTypeEntry.getKey()); } if (policyType.getVersion() == null) { policyType.setVersion(PfKey.NULL_KEY_VERSION); } if (!typeList.contains(policyType)) { typeList.add(policyType); } } } @Test public void testNullList() { ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build(); assertThatThrownBy(() -> { filter.filter(null); }).hasMessageMatching("originalList is marked .*on.*ull but is null"); } @Test public void testFilterNothing() { ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build(); List filteredList = filter.filter(typeList); assertTrue(filteredList.containsAll(typeList)); } @Test public void testFilterLatestVersion() { ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().version(ToscaPolicyTypeFilter.LATEST_VERSION).build(); List filteredList = filter.filter(typeList); assertEquals(20, filteredList.size()); assertEquals(VERSION_100, filteredList.get(0).getVersion()); assertEquals(VERSION_100, filteredList.get(11).getVersion()); typeList.get(12).setVersion("2.0.0"); filteredList = filter.filter(typeList); assertEquals(20, filteredList.size()); // // This seems to change around as to where this policy type // got changed - perhaps we change this test to find a specific name // to test for vs an index which never remains consistent? // assertEquals("2.0.0", filteredList.get(18).getVersion()); // // And now this index changes again?? // assertEquals(VERSION_100, filteredList.get(17).getVersion()); typeList.get(12).setVersion(VERSION_100); filteredList = filter.filter(typeList); assertEquals(20, filteredList.size()); assertEquals(VERSION_100, filteredList.get(0).getVersion()); assertEquals(VERSION_100, filteredList.get(18).getVersion()); } @Test public void testFilterNameVersion() { ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().name("onap.policies.Monitoring").build(); List filteredList = filter.filter(typeList); assertEquals(1, filteredList.size()); filter = ToscaPolicyTypeFilter.builder().name("onap.policies.monitoring.cdap.tca.hi.lo.app").build(); filteredList = filter.filter(typeList); assertEquals(1, filteredList.size()); filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.LpaPolicy").build(); filteredList = filter.filter(typeList); assertEquals(0, filteredList.size()); filter = ToscaPolicyTypeFilter.builder().version(VERSION_100).build(); filteredList = filter.filter(typeList); assertEquals(20, filteredList.size()); filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version(VERSION_000) .build(); filteredList = filter.filter(typeList); assertEquals(0, filteredList.size()); filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version("0.0.1").build(); filteredList = filter.filter(typeList); assertEquals(0, filteredList.size()); } }