2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.List;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.api.main.parameters.ApiParameterGroup;
42 import org.onap.policy.common.parameters.ParameterService;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.common.utils.coder.StandardYamlCoder;
45 import org.onap.policy.common.utils.resources.ResourceUtils;
46 import org.onap.policy.models.base.PfModelException;
47 import org.onap.policy.models.pdp.concepts.Pdp;
48 import org.onap.policy.models.pdp.concepts.PdpGroup;
49 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
50 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
51 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
52 import org.onap.policy.models.pdp.enums.PdpState;
53 import org.onap.policy.models.provider.PolicyModelsProvider;
54 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
55 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
59 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
62 * This class performs unit test of {@link LegacyOperationalPolicyProvider}.
64 * @author Chenfei Gao (cgao@research.att.com)
66 public class TestLegacyOperationalPolicyProvider {
68 private static LegacyOperationalPolicyProvider operationalPolicyProvider;
69 private static PolicyTypeProvider policyTypeProvider;
70 private static PolicyModelsProviderParameters providerParams;
71 private static ApiParameterGroup apiParamGroup;
72 private static StandardCoder standardCoder;
73 private static StandardYamlCoder standardYamlCoder;
75 private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.input.json";
76 private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.controlloop.Operational.yaml";
77 private static final String POLICY_TYPE_ID = "onap.policies.controlloop.Operational:1.0.0";
78 private static final String POLICY_ID = "operational.restart:1.0.0";
79 private static final String POLICY_NAME = "operational.restart";
80 private static final String POLICY_VERSION = "1";
81 private static final String POLICY_TYPE_NAME = "onap.policies.controlloop.Operational";
82 private static final String POLICY_TYPE_VERSION = "1.0.0";
83 private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
86 * Initializes parameters.
88 * @throws PfModelException the PfModel parsing exception
91 public void setupParameters() throws PfModelException {
93 standardCoder = new StandardCoder();
94 standardYamlCoder = new StandardYamlCoder();
95 providerParams = new PolicyModelsProviderParameters();
96 providerParams.setDatabaseDriver("org.h2.Driver");
97 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
98 providerParams.setDatabaseUser("policy");
99 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
100 providerParams.setPersistenceUnit("ToscaConceptTest");
101 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
102 ParameterService.register(apiParamGroup, true);
103 operationalPolicyProvider = new LegacyOperationalPolicyProvider();
104 policyTypeProvider = new PolicyTypeProvider();
108 * Closes up DB connections and deregisters API parameter group.
110 * @throws PfModelException the PfModel parsing exception
113 public void tearDown() throws PfModelException {
115 operationalPolicyProvider.close();
116 policyTypeProvider.close();
117 ParameterService.deregister(apiParamGroup);
121 public void testFetchOperationalPolicy() {
123 assertThatThrownBy(() -> {
124 operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
125 }).hasMessage("no policy found for policy: dummy:null");
127 assertThatThrownBy(() -> {
128 operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
129 }).hasMessage("legacy policy version is not an integer");
131 assertThatCode(() -> {
132 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
133 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
134 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
136 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
137 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
138 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
139 assertNotNull(createdPolicy);
141 policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
142 policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
143 createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
144 assertNotNull(createdPolicy);
146 LegacyOperationalPolicy firstVersion =
147 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1");
148 assertNotNull(firstVersion);
149 assertEquals("1", firstVersion.getPolicyVersion());
151 LegacyOperationalPolicy latestVersion =
152 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", null);
153 assertNotNull(latestVersion);
154 assertEquals("2", latestVersion.getPolicyVersion());
155 }).doesNotThrowAnyException();
157 assertThatThrownBy(() -> {
158 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1.0.0");
159 }).hasMessage("legacy policy version is not an integer");
161 assertThatThrownBy(() -> {
162 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "latest");;
163 }).hasMessage("legacy policy version is not an integer");
165 assertThatCode(() -> {
166 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
167 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "2");
168 policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
169 }).doesNotThrowAnyException();
173 public void testFetchDeployedOperationalPolicies() {
175 assertThatThrownBy(() -> {
176 operationalPolicyProvider.fetchDeployedOperationalPolicies("dummy");
177 }).hasMessage("could not find policy with ID dummy and type " + POLICY_TYPE_ID + " deployed in any pdp group");
179 try (PolicyModelsProvider databaseProvider =
180 new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
181 assertEquals(0, databaseProvider.getPdpGroups("name").size());
182 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
184 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
185 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
187 PdpGroup pdpGroup = new PdpGroup();
188 pdpGroup.setName("group");
189 pdpGroup.setVersion("1.2.3");
190 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
191 pdpGroup.setPdpSubgroups(new ArrayList<>());
192 List<PdpGroup> groupList = new ArrayList<>();
193 groupList.add(pdpGroup);
195 PdpSubGroup pdpSubGroup = new PdpSubGroup();
196 pdpSubGroup.setPdpType("type");
197 pdpSubGroup.setDesiredInstanceCount(123);
198 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
199 pdpSubGroup.getSupportedPolicyTypes()
200 .add(new ToscaPolicyTypeIdentifier(POLICY_TYPE_NAME, POLICY_TYPE_VERSION));
201 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
204 pdp.setInstanceId("type-0");
205 pdp.setMessage("Hello");
206 pdp.setPdpState(PdpState.ACTIVE);
207 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
208 pdpSubGroup.setPdpInstances(new ArrayList<>());
209 pdpSubGroup.getPdpInstances().add(pdp);
212 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
213 .getDesiredInstanceCount());
214 assertEquals(1, databaseProvider.getPdpGroups("group").size());
216 // Create Policy Type
217 assertThatCode(() -> {
218 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
219 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
220 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
221 }).doesNotThrowAnyException();
224 assertThatCode(() -> {
225 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
226 LegacyOperationalPolicy policyToCreate =
227 standardCoder.decode(policyString, LegacyOperationalPolicy.class);
228 LegacyOperationalPolicy policyCreated =
229 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
230 assertEquals("operational.restart", policyCreated.getPolicyId());
231 assertEquals("1", policyCreated.getPolicyVersion());
232 assertFalse(policyCreated.getContent() == null);
233 }).doesNotThrowAnyException();
235 // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true
236 assertThatThrownBy(() -> {
237 operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME);
238 }) .hasMessage("could not find policy with ID " + POLICY_NAME + " and type " + POLICY_TYPE_ID
239 + " deployed in any pdp group");
242 // Update pdpSubGroup
243 pdpSubGroup.setPolicies(new ArrayList<>());
244 pdpSubGroup.getPolicies()
245 .add(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
247 databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
249 // Test fetchDeployedPolicies
250 assertThatCode(() -> {
251 operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME);
252 }).doesNotThrowAnyException();
254 // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
255 assertThatThrownBy(() -> {
256 operationalPolicyProvider.deleteOperationalPolicy(POLICY_NAME, POLICY_VERSION);
257 }) .hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
258 + " cannot be deleted as it is deployed in pdp groups");
259 } catch (Exception exc) {
260 fail("Test should not throw an exception");
265 public void testCreateOperationalPolicy() {
267 assertThatThrownBy(() -> {
268 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
269 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
270 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
271 }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist");
273 assertThatCode(() -> {
274 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
275 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
276 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
278 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
279 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
280 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
281 assertNotNull(createdPolicy);
282 assertEquals("operational.restart", createdPolicy.getPolicyId());
283 assertTrue(createdPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
284 }).doesNotThrowAnyException();
288 public void testDeleteOperationalPolicyException() {
289 String policyId = "operational.restart";
290 String policyVersion = "1";
291 String policyTypeVersion = "1.0.0";
292 String policyTypeId = "onap.policies.controlloop.Operational";
293 String legacyMinorPatchSuffix = ".0.0";
295 try (PolicyModelsProvider databaseProvider =
296 new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
297 assertEquals(0, databaseProvider.getPdpGroups("name").size());
298 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
300 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
301 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
303 PdpGroup pdpGroup = new PdpGroup();
304 pdpGroup.setName("group");
305 pdpGroup.setVersion("1.2.3");
306 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
307 pdpGroup.setPdpSubgroups(new ArrayList<>());
308 List<PdpGroup> groupList = new ArrayList<>();
309 groupList.add(pdpGroup);
311 PdpSubGroup pdpSubGroup = new PdpSubGroup();
312 pdpSubGroup.setPdpType("type");
313 pdpSubGroup.setDesiredInstanceCount(123);
314 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
315 pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));
316 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
319 pdp.setInstanceId("type-0");
320 pdp.setMessage("Hello");
321 pdp.setPdpState(PdpState.ACTIVE);
322 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
323 pdpSubGroup.setPdpInstances(new ArrayList<>());
324 pdpSubGroup.getPdpInstances().add(pdp);
327 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
328 .getDesiredInstanceCount());
329 assertEquals(1, databaseProvider.getPdpGroups("group").size());
331 // Create Policy Type
332 assertThatCode(() -> {
333 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
334 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
335 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
336 }).doesNotThrowAnyException();
339 assertThatCode(() -> {
340 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
341 LegacyOperationalPolicy policyToCreate =
342 standardCoder.decode(policyString, LegacyOperationalPolicy.class);
343 LegacyOperationalPolicy createdPolicy =
344 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
345 assertNotNull(createdPolicy);
346 }).doesNotThrowAnyException();
348 // Update pdpSubGroup
349 pdpSubGroup.setPolicies(new ArrayList<>());
350 pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion + legacyMinorPatchSuffix));
352 databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
353 assertThatThrownBy(() -> {
354 operationalPolicyProvider.deleteOperationalPolicy(policyId, policyVersion);
355 }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups");
356 } catch (Exception exc) {
357 fail("Test should not throw an exception");
362 public void testDeleteOperationalPolicy() {
364 assertThatThrownBy(() -> {
365 operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
366 }).hasMessage("legacy policy version is not an integer");
368 assertThatThrownBy(() -> {
369 operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
370 }).hasMessage("legacy policy version is not an integer");
372 assertThatCode(() -> {
373 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
374 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
375 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
377 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
378 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
379 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
380 assertNotNull(createdPolicy);
382 LegacyOperationalPolicy deletedPolicy =
383 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
384 assertNotNull(deletedPolicy);
385 assertEquals("operational.restart", deletedPolicy.getPolicyId());
386 assertTrue(deletedPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
387 }).doesNotThrowAnyException();
389 assertThatThrownBy(() -> {
390 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
391 }).hasMessage("no policy found for policy: operational.restart:1");
393 assertThatCode(() -> {
394 policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
395 }).doesNotThrowAnyException();