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.legacy.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,
104 Collections.emptyList(), 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 }).hasMessageContaining("parameter \"version\": value \"dummy.0.0\", does not match regular expression");
134 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
135 .decode(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 }).hasMessageContaining("parameter \"version\": value \"1.0.0.0.0\", does not match regular expression");
157 assertThatThrownBy(() -> {
158 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "latest");
160 }).hasMessageContaining("parameter \"version\": value \"latest.0.0\", does not match regular expression");
162 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
163 policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
167 public void testFetchDeployedOperationalPolicies() {
169 assertThatThrownBy(() -> {
170 operationalPolicyProvider.fetchDeployedOperationalPolicies("dummy");
171 }).hasMessage("could not find policy with ID dummy and type " + POLICY_TYPE_ID + " deployed in any pdp group");
173 try (PolicyModelsProvider databaseProvider =
174 new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
175 assertEquals(0, databaseProvider.getPdpGroups("name").size());
176 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
178 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
179 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
181 PdpGroup pdpGroup = new PdpGroup();
182 pdpGroup.setName("group");
183 pdpGroup.setVersion("1.2.3");
184 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
185 pdpGroup.setPdpSubgroups(new ArrayList<>());
186 List<PdpGroup> groupList = new ArrayList<>();
187 groupList.add(pdpGroup);
189 PdpSubGroup pdpSubGroup = new PdpSubGroup();
190 pdpSubGroup.setPdpType("type");
191 pdpSubGroup.setDesiredInstanceCount(123);
192 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
193 pdpSubGroup.getSupportedPolicyTypes()
194 .add(new ToscaPolicyTypeIdentifier(POLICY_TYPE_NAME, POLICY_TYPE_VERSION));
195 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
198 pdp.setInstanceId("type-0");
199 pdp.setMessage("Hello");
200 pdp.setPdpState(PdpState.ACTIVE);
201 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
202 pdpSubGroup.setPdpInstances(new ArrayList<>());
203 pdpSubGroup.getPdpInstances().add(pdp);
206 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
207 .getDesiredInstanceCount());
208 assertEquals(1, databaseProvider.getPdpGroups("group").size());
210 // Create Policy Type
211 assertThatCode(() -> {
212 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
213 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
214 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
215 }).doesNotThrowAnyException();
218 assertThatCode(() -> {
219 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
220 LegacyOperationalPolicy policyToCreate =
221 standardCoder.decode(policyString, LegacyOperationalPolicy.class);
222 LegacyOperationalPolicy policyCreated =
223 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
224 assertEquals("operational.restart", policyCreated.getPolicyId());
225 assertEquals("1", policyCreated.getPolicyVersion());
226 assertFalse(policyCreated.getContent() == null);
227 }).doesNotThrowAnyException();
229 // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true
230 assertThatThrownBy(() -> {
231 operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME);
232 }).hasMessage("could not find policy with ID " + POLICY_NAME + " and type " + POLICY_TYPE_ID
233 + " 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 is in use, it is deployed in PDP group group subgroup type");
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 String defaultPolicyVersionString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION);
278 LegacyOperationalPolicy defaultPolicyVersionPolicy =
279 standardCoder.decode(defaultPolicyVersionString, LegacyOperationalPolicy.class);
280 createdPolicy = operationalPolicyProvider.createOperationalPolicy(defaultPolicyVersionPolicy);
281 assertEquals("1", createdPolicy.getPolicyVersion());
283 assertThatCode(() -> {
284 String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
285 LegacyOperationalPolicy duplicatePolicyToCreate =
286 standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class);
287 operationalPolicyProvider.createOperationalPolicy(duplicatePolicyToCreate);
288 }).doesNotThrowAnyException();
290 assertThatThrownBy(() -> {
291 String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
292 LegacyOperationalPolicy duplicatePolicyToCreate =
293 standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class);
294 duplicatePolicyToCreate.setContent("some other content");
295 operationalPolicyProvider.createOperationalPolicy(duplicatePolicyToCreate);
296 }).hasMessageContaining("INVALID:entity in incoming fragment does not equal existing entity");
300 public void testDeleteOperationalPolicyException() {
301 String policyId = "operational.restart";
302 String policyVersion = "1";
303 String policyTypeVersion = "1.0.0";
304 String policyTypeId = "onap.policies.controlloop.Operational";
305 String legacyMinorPatchSuffix = ".0.0";
307 try (PolicyModelsProvider databaseProvider =
308 new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
309 assertEquals(0, databaseProvider.getPdpGroups("name").size());
310 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
312 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
313 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
315 PdpGroup pdpGroup = new PdpGroup();
316 pdpGroup.setName("group");
317 pdpGroup.setVersion("1.2.3");
318 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
319 pdpGroup.setPdpSubgroups(new ArrayList<>());
320 List<PdpGroup> groupList = new ArrayList<>();
321 groupList.add(pdpGroup);
323 PdpSubGroup pdpSubGroup = new PdpSubGroup();
324 pdpSubGroup.setPdpType("type");
325 pdpSubGroup.setDesiredInstanceCount(123);
326 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
327 pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));
328 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
331 pdp.setInstanceId("type-0");
332 pdp.setMessage("Hello");
333 pdp.setPdpState(PdpState.ACTIVE);
334 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
335 pdpSubGroup.setPdpInstances(new ArrayList<>());
336 pdpSubGroup.getPdpInstances().add(pdp);
339 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
340 .getDesiredInstanceCount());
341 assertEquals(1, databaseProvider.getPdpGroups("group").size());
343 // Create Policy Type
344 assertThatCode(() -> {
345 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
346 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
347 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
348 }).doesNotThrowAnyException();
351 assertThatCode(() -> {
352 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
353 LegacyOperationalPolicy policyToCreate =
354 standardCoder.decode(policyString, LegacyOperationalPolicy.class);
355 LegacyOperationalPolicy createdPolicy =
356 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
357 assertNotNull(createdPolicy);
358 }).doesNotThrowAnyException();
360 // Update pdpSubGroup
361 pdpSubGroup.setPolicies(new ArrayList<>());
362 pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion + legacyMinorPatchSuffix));
364 databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
365 assertThatThrownBy(() -> {
366 operationalPolicyProvider.deleteOperationalPolicy(policyId, policyVersion);
367 }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type");
368 } catch (Exception exc) {
369 fail("Test should not throw an exception");
374 public void testDeleteOperationalPolicy() {
376 assertThatThrownBy(() -> {
377 operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
378 }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
380 assertThatThrownBy(() -> {
381 operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
382 }).hasMessageContaining("parameter \"version\": value \"dummy.0.0\", does not match regular expression");
384 assertThatCode(() -> {
385 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
386 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
387 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
389 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
390 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
391 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
392 assertNotNull(createdPolicy);
394 LegacyOperationalPolicy deletedPolicy =
395 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
396 assertNotNull(deletedPolicy);
397 assertEquals("operational.restart", deletedPolicy.getPolicyId());
398 assertTrue(deletedPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
399 }).doesNotThrowAnyException();
401 assertThatThrownBy(() -> {
402 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
403 }).hasMessage("no policy found for policy: operational.restart:1");
405 assertThatCode(() -> {
406 policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
407 }).doesNotThrowAnyException();