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