2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.api.main.rest.provider;
26 import static org.assertj.core.api.Assertions.assertThatCode;
27 import static org.assertj.core.api.Assertions.assertThatThrownBy;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertTrue;
32 import static org.junit.Assert.fail;
34 import java.util.ArrayList;
35 import java.util.Base64;
36 import java.util.Collections;
37 import java.util.List;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.policy.api.main.parameters.ApiParameterGroup;
44 import org.onap.policy.common.parameters.ParameterService;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.common.utils.coder.StandardYamlCoder;
47 import org.onap.policy.common.utils.resources.ResourceUtils;
48 import org.onap.policy.models.base.PfModelException;
49 import org.onap.policy.models.pdp.concepts.Pdp;
50 import org.onap.policy.models.pdp.concepts.PdpGroup;
51 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
52 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
53 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
54 import org.onap.policy.models.pdp.enums.PdpState;
55 import org.onap.policy.models.provider.PolicyModelsProvider;
56 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
57 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
61 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
62 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
65 * This class performs unit test of {@link LegacyGuardPolicyProvider}.
67 * @author Chenfei Gao (cgao@research.att.com)
69 public class TestLegacyGuardPolicyProvider {
71 private static LegacyGuardPolicyProvider guardPolicyProvider;
72 private static PolicyTypeProvider policyTypeProvider;
73 private static PolicyModelsProviderParameters providerParams;
74 private static ApiParameterGroup apiParamGroup;
75 private static StandardCoder standardCoder;
76 private static StandardYamlCoder standardYamlCoder;
78 private static final String POLICY_RESOURCE = "policies/vDNS.policy.guard.frequency.input.json";
79 private static final String POLICY_RESOURCE_VER1 = "policies/vDNS.policy.guard.frequency.input.ver1.json";
80 private static final String POLICY_RESOURCE_VER2 = "policies/vDNS.policy.guard.frequency.input.ver2.json";
81 private static final String POLICY_RESOURCE_WITH_NO_VERSION =
82 "policies/vDNS.policy.guard.frequency.no.policyversion.json";
83 private static final String POLICY_TYPE_RESOURCE =
84 "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml";
85 private static final String POLICY_TYPE_ID = "onap.policies.controlloop.guard.FrequencyLimiter:1.0.0";
86 private static final String POLICY_TYPE_NAME = "onap.policies.controlloop.guard.FrequencyLimiter";
87 private static final String POLICY_TYPE_VERSION = "1.0.0";
88 private static final String POLICY_NAME = "guard.frequency.scaleout";
89 private static final String POLICY_VERSION = "1";
90 private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
93 * Initializes parameters.
95 * @throws PfModelException the PfModel parsing exception
98 public void setupParameters() throws PfModelException {
99 standardCoder = new StandardCoder();
100 standardYamlCoder = new StandardYamlCoder();
101 providerParams = new PolicyModelsProviderParameters();
102 providerParams.setDatabaseDriver("org.h2.Driver");
103 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
104 providerParams.setDatabaseUser("policy");
105 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
106 providerParams.setPersistenceUnit("ToscaConceptTest");
107 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList());
108 ParameterService.register(apiParamGroup, true);
109 guardPolicyProvider = new LegacyGuardPolicyProvider();
110 policyTypeProvider = new PolicyTypeProvider();
114 * Closes up DB connections and deregisters API parameter group.
116 * @throws PfModelException the PfModel parsing exception
119 public void tearDown() throws PfModelException {
120 guardPolicyProvider.close();
121 policyTypeProvider.close();
122 ParameterService.deregister(apiParamGroup);
126 public void testFetchGuardPolicy() {
128 assertThatThrownBy(() -> {
129 guardPolicyProvider.fetchGuardPolicy("dummy", null);
130 }).hasMessage("no policy found for policy: dummy:null");
132 assertThatThrownBy(() -> {
133 guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
134 }).hasMessage("legacy policy version is not an integer");
136 assertThatCode(() -> {
137 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
138 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
139 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
141 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_VER1);
142 LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
143 Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
144 assertNotNull(createdPolicy);
145 assertFalse(createdPolicy.isEmpty());
147 policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_VER2);
148 policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
149 createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
150 assertNotNull(createdPolicy);
151 assertFalse(createdPolicy.isEmpty());
153 Map<String, LegacyGuardPolicyOutput> firstVersion =
154 guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "1");
155 assertNotNull(firstVersion);
157 firstVersion.get("guard.frequency.scaleout").getMetadata().get("policy-version").toString());
159 Map<String, LegacyGuardPolicyOutput> latestVersion =
160 guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", null);
161 assertNotNull(latestVersion);
163 latestVersion.get("guard.frequency.scaleout").getMetadata().get("policy-version").toString());
164 }).doesNotThrowAnyException();
166 assertThatThrownBy(() -> {
167 guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "1.0.0");
168 }).hasMessage("legacy policy version is not an integer");
170 assertThatThrownBy(() -> {
171 guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "latest");
172 }).hasMessage("legacy policy version is not an integer");
174 assertThatCode(() -> {
175 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
176 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "2");
177 policyTypeProvider.deletePolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0");
178 }).doesNotThrowAnyException();
182 public void testFetchDeployedGuardPolicies() {
184 assertThatThrownBy(() -> {
185 guardPolicyProvider.fetchDeployedGuardPolicies("dummy");
186 }).hasMessage("No policy type defined for dummy");
188 try (PolicyModelsProvider databaseProvider =
189 new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
190 assertEquals(0, databaseProvider.getPdpGroups("name").size());
191 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
193 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
194 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
196 PdpGroup pdpGroup = new PdpGroup();
197 pdpGroup.setName("group");
198 pdpGroup.setVersion("1.2.3");
199 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
200 pdpGroup.setPdpSubgroups(new ArrayList<>());
201 List<PdpGroup> groupList = new ArrayList<>();
202 groupList.add(pdpGroup);
204 PdpSubGroup pdpSubGroup = new PdpSubGroup();
205 pdpSubGroup.setPdpType("type");
206 pdpSubGroup.setDesiredInstanceCount(123);
207 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
208 pdpSubGroup.getSupportedPolicyTypes()
209 .add(new ToscaPolicyTypeIdentifier(POLICY_TYPE_NAME, POLICY_TYPE_VERSION));
210 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
213 pdp.setInstanceId("type-0");
214 pdp.setMessage("Hello");
215 pdp.setPdpState(PdpState.ACTIVE);
216 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
217 pdpSubGroup.setPdpInstances(new ArrayList<>());
218 pdpSubGroup.getPdpInstances().add(pdp);
221 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
222 .getDesiredInstanceCount());
223 assertEquals(1, databaseProvider.getPdpGroups("group").size());
225 // Create Policy Type
226 assertThatCode(() -> {
227 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
228 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
229 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
230 }).doesNotThrowAnyException();
233 assertThatCode(() -> {
234 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
235 LegacyGuardPolicyInput policyToCreate =
236 standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
237 Map<String, LegacyGuardPolicyOutput> policyCreated =
238 guardPolicyProvider.createGuardPolicy(policyToCreate);
239 assertFalse(policyCreated.isEmpty());
240 }).doesNotThrowAnyException();
242 // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true
243 assertThatThrownBy(() -> {
244 guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME);
245 }).hasMessage("could not find policy with ID " + POLICY_NAME + " and type " + POLICY_TYPE_ID
246 + " deployed in any pdp group");
248 // Update pdpSubGroup
249 pdpSubGroup.setPolicies(new ArrayList<>());
250 pdpSubGroup.getPolicies()
251 .add(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
253 databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
255 // Test fetchDeployedPolicies
256 assertThatCode(() -> {
257 guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME);
258 }).doesNotThrowAnyException();
260 // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
261 assertThatThrownBy(() -> {
262 guardPolicyProvider.deleteGuardPolicy(POLICY_NAME, POLICY_VERSION);
263 }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
264 + " cannot be deleted as it is deployed in pdp groups");
265 } catch (Exception exc) {
266 fail("Test should not throw an exception");
271 public void testCreateGuardPolicy() throws Exception {
272 assertThatThrownBy(() -> {
273 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
274 LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
275 guardPolicyProvider.createGuardPolicy(policyToCreate);
276 }).hasMessageContaining(
277 "no policy types are defined on the service template for the policies in the topology template");
279 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
280 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
281 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
283 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
284 LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
285 Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
286 assertNotNull(createdPolicy);
287 assertFalse(createdPolicy.isEmpty());
288 assertTrue(createdPolicy.containsKey("guard.frequency.scaleout"));
289 assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
290 createdPolicy.get("guard.frequency.scaleout").getType());
291 assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion());
293 assertThatThrownBy(() -> {
294 String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION);
295 LegacyGuardPolicyInput badPolicyToCreate =
296 standardCoder.decode(badPolicyString, LegacyGuardPolicyInput.class);
297 guardPolicyProvider.createGuardPolicy(badPolicyToCreate);
298 }).hasMessage("mandatory field 'policy-version' is missing in the policy: guard.frequency.scaleout");
300 assertThatThrownBy(() -> {
301 String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
302 LegacyGuardPolicyInput duplicatePolicyToCreate =
303 standardCoder.decode(duplicatePolicyString, LegacyGuardPolicyInput.class);
304 guardPolicyProvider.createGuardPolicy(duplicatePolicyToCreate);
305 }).hasMessage("guard policy guard.frequency.scaleout:1 already exists; its latest version is 1");
309 public void testDeleteGuardPolicyException() {
310 String policyId = "guard.frequency.scaleout";
311 String policyVersion = "1";
312 String policyTypeVersion = "1.0.0";
313 String policyTypeId = "onap.policies.controlloop.guard.FrequencyLimiter";
314 String legacyMinorPatchSuffix = ".0.0";
316 try (PolicyModelsProvider databaseProvider =
317 new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
318 assertEquals(0, databaseProvider.getPdpGroups("name").size());
319 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
321 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
322 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
324 PdpGroup pdpGroup = new PdpGroup();
325 pdpGroup.setName("group");
326 pdpGroup.setVersion("1.2.3");
327 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
328 pdpGroup.setPdpSubgroups(new ArrayList<>());
329 List<PdpGroup> groupList = new ArrayList<>();
330 groupList.add(pdpGroup);
332 PdpSubGroup pdpSubGroup = new PdpSubGroup();
333 pdpSubGroup.setPdpType("type");
334 pdpSubGroup.setDesiredInstanceCount(123);
335 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
336 pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));
337 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
340 pdp.setInstanceId("type-0");
341 pdp.setMessage("Hello");
342 pdp.setPdpState(PdpState.ACTIVE);
343 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
344 pdpSubGroup.setPdpInstances(new ArrayList<>());
345 pdpSubGroup.getPdpInstances().add(pdp);
348 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
349 .getDesiredInstanceCount());
350 assertEquals(1, databaseProvider.getPdpGroups("group").size());
352 // Create Policy Type
353 assertThatCode(() -> {
354 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
355 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
356 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
357 }).doesNotThrowAnyException();
360 assertThatCode(() -> {
361 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
362 LegacyGuardPolicyInput policyToCreate =
363 standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
364 Map<String, LegacyGuardPolicyOutput> createdPolicy =
365 guardPolicyProvider.createGuardPolicy(policyToCreate);
366 assertNotNull(createdPolicy);
367 assertFalse(createdPolicy.isEmpty());
368 }).doesNotThrowAnyException();
370 // Update pdpSubGroup
371 pdpSubGroup.setPolicies(new ArrayList<>());
372 pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion + legacyMinorPatchSuffix));
374 databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
375 assertThatThrownBy(() -> {
376 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
377 }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups");
378 } catch (Exception exc) {
379 fail("Test should not throw an exception");
384 public void testDeleteGuardPolicy() {
385 assertThatThrownBy(() -> {
386 guardPolicyProvider.deleteGuardPolicy("dummy", null);
387 }).hasMessage("legacy policy version is not an integer");
389 assertThatThrownBy(() -> {
390 guardPolicyProvider.deleteGuardPolicy("dummy", "1.0.0");
391 }).hasMessage("legacy policy version is not an integer");
393 assertThatCode(() -> {
394 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
395 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
396 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
398 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
399 LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
400 Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
401 assertNotNull(createdPolicy);
402 assertFalse(createdPolicy.isEmpty());
404 Map<String, LegacyGuardPolicyOutput> deletedPolicy =
405 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
406 assertNotNull(deletedPolicy);
407 assertFalse(deletedPolicy.isEmpty());
408 assertTrue(deletedPolicy.containsKey("guard.frequency.scaleout"));
409 assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
410 deletedPolicy.get("guard.frequency.scaleout").getType());
412 deletedPolicy.get("guard.frequency.scaleout").getMetadata().get("policy-version").toString());
414 }).doesNotThrowAnyException();
416 assertThatThrownBy(() -> {
417 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
418 }).hasMessage("no policy found for policy: guard.frequency.scaleout:1");
420 assertThatCode(() -> {
421 policyTypeProvider.deletePolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0");
422 }).doesNotThrowAnyException();