2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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;
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_NAME = "operational.restart";
82 private static final String POLICY_VERSION = "1";
83 private static final String POLICY_TYPE_NAME = "onap.policies.controlloop.Operational";
84 private static final String POLICY_TYPE_VERSION = "1.0.0";
85 private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
88 * Initializes parameters.
90 * @throws PfModelException the PfModel parsing exception
93 public void setupParameters() throws PfModelException {
95 standardCoder = new StandardCoder();
96 standardYamlCoder = new StandardYamlCoder();
97 providerParams = new PolicyModelsProviderParameters();
98 providerParams.setDatabaseDriver("org.h2.Driver");
99 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
100 providerParams.setDatabaseUser("policy");
101 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
102 providerParams.setPersistenceUnit("ToscaConceptTest");
103 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList());
104 ParameterService.register(apiParamGroup, true);
105 operationalPolicyProvider = new LegacyOperationalPolicyProvider();
106 policyTypeProvider = new PolicyTypeProvider();
110 * Closes up DB connections and deregisters API parameter group.
112 * @throws PfModelException the PfModel parsing exception
115 public void tearDown() throws PfModelException {
117 operationalPolicyProvider.close();
118 policyTypeProvider.close();
119 ParameterService.deregister(apiParamGroup);
123 public void testFetchOperationalPolicy() throws Exception {
125 assertThatThrownBy(() -> {
126 operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
127 }).hasMessage("no policy found for policy: dummy:null");
129 assertThatThrownBy(() -> {
130 operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
131 }).hasMessage("legacy policy version is not an integer");
133 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
134 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
135 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
137 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
138 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
139 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
140 assertNotNull(createdPolicy);
142 LegacyOperationalPolicy firstVersion =
143 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1");
144 assertNotNull(firstVersion);
145 assertEquals("1", firstVersion.getPolicyVersion());
147 LegacyOperationalPolicy latestVersion =
148 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", null);
149 assertNotNull(latestVersion);
150 assertEquals("1", latestVersion.getPolicyVersion());
152 assertThatThrownBy(() -> {
153 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1.0.0");
154 }).hasMessage("legacy policy version is not an integer");
156 assertThatThrownBy(() -> {
157 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
212 .decode(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");
234 // Update pdpSubGroup
235 pdpSubGroup.setPolicies(new ArrayList<>());
236 pdpSubGroup.getPolicies()
237 .add(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
239 databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
241 // Test fetchDeployedPolicies
242 assertThatCode(() -> {
243 operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME);
244 }).doesNotThrowAnyException();
246 // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
247 assertThatThrownBy(() -> {
248 operationalPolicyProvider.deleteOperationalPolicy(POLICY_NAME, POLICY_VERSION);
249 }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
250 + " cannot be deleted as it is deployed in pdp groups");
251 } catch (Exception exc) {
252 fail("Test should not throw an exception");
257 public void testCreateOperationalPolicy() throws Exception {
259 assertThatThrownBy(() -> {
260 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
261 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
262 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
263 }).hasMessageContaining(
264 "no policy types are defined on the service template for the policies in the topology template");
266 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
267 .decode(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
339 .decode(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
379 .decode(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();