2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.onap.policy.api.main.parameters.ApiParameterGroup;
43 import org.onap.policy.common.parameters.ParameterService;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.common.utils.coder.StandardYamlCoder;
46 import org.onap.policy.common.utils.resources.ResourceUtils;
47 import org.onap.policy.models.base.PfModelException;
48 import org.onap.policy.models.pdp.concepts.Pdp;
49 import org.onap.policy.models.pdp.concepts.PdpGroup;
50 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
51 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
52 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
53 import org.onap.policy.models.pdp.enums.PdpState;
54 import org.onap.policy.models.provider.PolicyModelsProvider;
55 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
56 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
60 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
63 * This class performs unit test of {@link LegacyOperationalPolicyProvider}.
65 * @author Chenfei Gao (cgao@research.att.com)
67 public class TestLegacyOperationalPolicyProvider {
69 private static LegacyOperationalPolicyProvider operationalPolicyProvider;
70 private static PolicyTypeProvider policyTypeProvider;
71 private static PolicyModelsProviderParameters providerParams;
72 private static ApiParameterGroup apiParamGroup;
73 private static StandardCoder standardCoder;
74 private static StandardYamlCoder standardYamlCoder;
76 private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.input.json";
77 private static final String POLICY_RESOURCE_WITH_NO_VERSION =
78 "policies/vDNS.policy.operational.no.policyversion.json";
79 private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.controlloop.Operational.yaml";
80 private static final String POLICY_TYPE_ID = "onap.policies.controlloop.Operational:1.0.0";
81 private static final String POLICY_ID = "operational.restart:1.0.0";
82 private static final String POLICY_NAME = "operational.restart";
83 private static final String POLICY_VERSION = "1";
84 private static final String POLICY_TYPE_NAME = "onap.policies.controlloop.Operational";
85 private static final String POLICY_TYPE_VERSION = "1.0.0";
86 private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
89 * Initializes parameters.
91 * @throws PfModelException the PfModel parsing exception
94 public void setupParameters() throws PfModelException {
96 standardCoder = new StandardCoder();
97 standardYamlCoder = new StandardYamlCoder();
98 providerParams = new PolicyModelsProviderParameters();
99 providerParams.setDatabaseDriver("org.h2.Driver");
100 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
101 providerParams.setDatabaseUser("policy");
102 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
103 providerParams.setPersistenceUnit("ToscaConceptTest");
104 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList());
105 ParameterService.register(apiParamGroup, true);
106 operationalPolicyProvider = new LegacyOperationalPolicyProvider();
107 policyTypeProvider = new PolicyTypeProvider();
111 * Closes up DB connections and deregisters API parameter group.
113 * @throws PfModelException the PfModel parsing exception
116 public void tearDown() throws PfModelException {
118 operationalPolicyProvider.close();
119 policyTypeProvider.close();
120 ParameterService.deregister(apiParamGroup);
124 public void testFetchOperationalPolicy() throws Exception {
126 assertThatThrownBy(() -> {
127 operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
128 }).hasMessage("no policy found for policy: dummy:null");
130 assertThatThrownBy(() -> {
131 operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
132 }).hasMessage("legacy policy version is not an integer");
134 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
135 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
136 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
138 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
139 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
140 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
141 assertNotNull(createdPolicy);
143 LegacyOperationalPolicy firstVersion =
144 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1");
145 assertNotNull(firstVersion);
146 assertEquals("1", firstVersion.getPolicyVersion());
148 LegacyOperationalPolicy latestVersion =
149 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", null);
150 assertNotNull(latestVersion);
151 assertEquals("1", latestVersion.getPolicyVersion());
153 assertThatThrownBy(() -> {
154 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1.0.0");
155 }).hasMessage("legacy policy version is not an integer");
157 assertThatThrownBy(() -> {
158 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "latest");;
159 }).hasMessage("legacy policy version is not an integer");
161 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
162 policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
166 public void testFetchDeployedOperationalPolicies() {
168 assertThatThrownBy(() -> {
169 operationalPolicyProvider.fetchDeployedOperationalPolicies("dummy");
170 }).hasMessage("could not find policy with ID dummy and type " + POLICY_TYPE_ID + " deployed in any pdp group");
172 try (PolicyModelsProvider databaseProvider =
173 new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
174 assertEquals(0, databaseProvider.getPdpGroups("name").size());
175 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
177 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
178 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
180 PdpGroup pdpGroup = new PdpGroup();
181 pdpGroup.setName("group");
182 pdpGroup.setVersion("1.2.3");
183 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
184 pdpGroup.setPdpSubgroups(new ArrayList<>());
185 List<PdpGroup> groupList = new ArrayList<>();
186 groupList.add(pdpGroup);
188 PdpSubGroup pdpSubGroup = new PdpSubGroup();
189 pdpSubGroup.setPdpType("type");
190 pdpSubGroup.setDesiredInstanceCount(123);
191 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
192 pdpSubGroup.getSupportedPolicyTypes()
193 .add(new ToscaPolicyTypeIdentifier(POLICY_TYPE_NAME, POLICY_TYPE_VERSION));
194 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
197 pdp.setInstanceId("type-0");
198 pdp.setMessage("Hello");
199 pdp.setPdpState(PdpState.ACTIVE);
200 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
201 pdpSubGroup.setPdpInstances(new ArrayList<>());
202 pdpSubGroup.getPdpInstances().add(pdp);
205 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
206 .getDesiredInstanceCount());
207 assertEquals(1, databaseProvider.getPdpGroups("group").size());
209 // Create Policy Type
210 assertThatCode(() -> {
211 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
212 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
213 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
214 }).doesNotThrowAnyException();
217 assertThatCode(() -> {
218 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
219 LegacyOperationalPolicy policyToCreate =
220 standardCoder.decode(policyString, LegacyOperationalPolicy.class);
221 LegacyOperationalPolicy policyCreated =
222 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
223 assertEquals("operational.restart", policyCreated.getPolicyId());
224 assertEquals("1", policyCreated.getPolicyVersion());
225 assertFalse(policyCreated.getContent() == null);
226 }).doesNotThrowAnyException();
228 // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true
229 assertThatThrownBy(() -> {
230 operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME);
231 }) .hasMessage("could not find policy with ID " + POLICY_NAME + " and type " + POLICY_TYPE_ID
232 + " deployed in any pdp group");
235 // Update pdpSubGroup
236 pdpSubGroup.setPolicies(new ArrayList<>());
237 pdpSubGroup.getPolicies()
238 .add(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
240 databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
242 // Test fetchDeployedPolicies
243 assertThatCode(() -> {
244 operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME);
245 }).doesNotThrowAnyException();
247 // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
248 assertThatThrownBy(() -> {
249 operationalPolicyProvider.deleteOperationalPolicy(POLICY_NAME, POLICY_VERSION);
250 }) .hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
251 + " cannot be deleted as it is deployed in pdp groups");
252 } catch (Exception exc) {
253 fail("Test should not throw an exception");
258 public void testCreateOperationalPolicy() throws Exception {
260 assertThatThrownBy(() -> {
261 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
262 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
263 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
264 }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist");
266 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
267 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
268 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
270 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
271 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
272 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
273 assertNotNull(createdPolicy);
274 assertEquals("operational.restart", createdPolicy.getPolicyId());
275 assertTrue(createdPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
277 assertThatThrownBy(() -> {
278 String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION);
279 LegacyOperationalPolicy badPolicyToCreate =
280 standardCoder.decode(badPolicyString, LegacyOperationalPolicy.class);
281 operationalPolicyProvider.createOperationalPolicy(badPolicyToCreate);
282 }).hasMessage("mandatory field 'policy-version' is missing in the policy: operational.scaleout");
284 assertThatThrownBy(() -> {
285 String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
286 LegacyOperationalPolicy duplicatePolicyToCreate =
287 standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class);
288 operationalPolicyProvider.createOperationalPolicy(duplicatePolicyToCreate);
289 }).hasMessage("operational policy operational.restart:1 already exists; its latest version is 1");
293 public void testDeleteOperationalPolicyException() {
294 String policyId = "operational.restart";
295 String policyVersion = "1";
296 String policyTypeVersion = "1.0.0";
297 String policyTypeId = "onap.policies.controlloop.Operational";
298 String legacyMinorPatchSuffix = ".0.0";
300 try (PolicyModelsProvider databaseProvider =
301 new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
302 assertEquals(0, databaseProvider.getPdpGroups("name").size());
303 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
305 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
306 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
308 PdpGroup pdpGroup = new PdpGroup();
309 pdpGroup.setName("group");
310 pdpGroup.setVersion("1.2.3");
311 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
312 pdpGroup.setPdpSubgroups(new ArrayList<>());
313 List<PdpGroup> groupList = new ArrayList<>();
314 groupList.add(pdpGroup);
316 PdpSubGroup pdpSubGroup = new PdpSubGroup();
317 pdpSubGroup.setPdpType("type");
318 pdpSubGroup.setDesiredInstanceCount(123);
319 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
320 pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));
321 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
324 pdp.setInstanceId("type-0");
325 pdp.setMessage("Hello");
326 pdp.setPdpState(PdpState.ACTIVE);
327 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
328 pdpSubGroup.setPdpInstances(new ArrayList<>());
329 pdpSubGroup.getPdpInstances().add(pdp);
332 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
333 .getDesiredInstanceCount());
334 assertEquals(1, databaseProvider.getPdpGroups("group").size());
336 // Create Policy Type
337 assertThatCode(() -> {
338 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
339 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
340 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
341 }).doesNotThrowAnyException();
344 assertThatCode(() -> {
345 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
346 LegacyOperationalPolicy policyToCreate =
347 standardCoder.decode(policyString, LegacyOperationalPolicy.class);
348 LegacyOperationalPolicy createdPolicy =
349 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
350 assertNotNull(createdPolicy);
351 }).doesNotThrowAnyException();
353 // Update pdpSubGroup
354 pdpSubGroup.setPolicies(new ArrayList<>());
355 pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion + legacyMinorPatchSuffix));
357 databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
358 assertThatThrownBy(() -> {
359 operationalPolicyProvider.deleteOperationalPolicy(policyId, policyVersion);
360 }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups");
361 } catch (Exception exc) {
362 fail("Test should not throw an exception");
367 public void testDeleteOperationalPolicy() {
369 assertThatThrownBy(() -> {
370 operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
371 }).hasMessage("legacy policy version is not an integer");
373 assertThatThrownBy(() -> {
374 operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
375 }).hasMessage("legacy policy version is not an integer");
377 assertThatCode(() -> {
378 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
379 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
380 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
382 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
383 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
384 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
385 assertNotNull(createdPolicy);
387 LegacyOperationalPolicy deletedPolicy =
388 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
389 assertNotNull(deletedPolicy);
390 assertEquals("operational.restart", deletedPolicy.getPolicyId());
391 assertTrue(deletedPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
392 }).doesNotThrowAnyException();
394 assertThatThrownBy(() -> {
395 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
396 }).hasMessage("no policy found for policy: operational.restart:1");
398 assertThatCode(() -> {
399 policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
400 }).doesNotThrowAnyException();