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.ApiTestSupportUtilities;
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.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;
74 private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.input.json";
75 private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.controlloop.Operational.yaml";
76 private static final String POLICY_TYPE_ID = "onap.policies.controlloop.Operational:1.0.0";
77 private static final String POLICY_ID = "operational.restart:1.0.0";
78 private static final String POLICY_NAME = "operational.restart";
79 private static final String POLICY_VERSION = "1";
80 private static final String POLICY_TYPE_NAME = "onap.policies.controlloop.Operational";
81 private static final String POLICY_TYPE_VERSION = "1.0.0";
82 private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
85 * Initializes parameters.
87 * @throws PfModelException the PfModel parsing exception
90 public void setupParameters() throws PfModelException {
92 standardCoder = new StandardCoder();
93 providerParams = new PolicyModelsProviderParameters();
94 providerParams.setDatabaseDriver("org.h2.Driver");
95 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
96 providerParams.setDatabaseUser("policy");
97 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
98 providerParams.setPersistenceUnit("ToscaConceptTest");
99 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
100 ParameterService.register(apiParamGroup, true);
101 operationalPolicyProvider = new LegacyOperationalPolicyProvider();
102 policyTypeProvider = new PolicyTypeProvider();
106 * Closes up DB connections and deregisters API parameter group.
108 * @throws PfModelException the PfModel parsing exception
111 public void tearDown() throws PfModelException {
113 operationalPolicyProvider.close();
114 policyTypeProvider.close();
115 ParameterService.deregister(apiParamGroup);
119 public void testFetchOperationalPolicy() {
121 assertThatThrownBy(() -> {
122 operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
123 }).hasMessage("no policy found for policy: dummy:null");
125 assertThatThrownBy(() -> {
126 operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
127 }).hasMessage("legacy policy version is not an integer");
129 assertThatCode(() -> {
130 String policyTypeString =
131 ApiTestSupportUtilities.yaml2Json(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE));
132 ToscaServiceTemplate policyTypeServiceTemplate =
133 standardCoder.decode(policyTypeString, 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 String policyTypeString =
219 ApiTestSupportUtilities.yaml2Json(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE));
220 ToscaServiceTemplate policyTypeServiceTemplate =
221 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
222 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
223 }).doesNotThrowAnyException();
226 assertThatCode(() -> {
227 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
228 LegacyOperationalPolicy policyToCreate =
229 standardCoder.decode(policyString, LegacyOperationalPolicy.class);
230 LegacyOperationalPolicy policyCreated =
231 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
232 assertEquals("operational.restart", policyCreated.getPolicyId());
233 assertEquals("1", policyCreated.getPolicyVersion());
234 assertFalse(policyCreated.getContent() == null);
235 }).doesNotThrowAnyException();
237 // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true
238 assertThatThrownBy(() -> {
239 operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME);
240 }) .hasMessage("could not find policy with ID " + POLICY_NAME + " and type " + POLICY_TYPE_ID
241 + " deployed in any pdp group");
244 // Update pdpSubGroup
245 pdpSubGroup.setPolicies(new ArrayList<>());
246 pdpSubGroup.getPolicies()
247 .add(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
249 databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
251 // Test fetchDeployedPolicies
252 assertThatCode(() -> {
253 operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME);
254 }).doesNotThrowAnyException();
256 // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
257 assertThatThrownBy(() -> {
258 operationalPolicyProvider.deleteOperationalPolicy(POLICY_NAME, POLICY_VERSION);
259 }) .hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
260 + " cannot be deleted as it is deployed in pdp groups");
261 } catch (Exception exc) {
262 fail("Test should not throw an exception");
267 public void testCreateOperationalPolicy() {
269 assertThatThrownBy(() -> {
270 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
271 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
272 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
273 }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist");
275 assertThatCode(() -> {
276 String policyTypeString =
277 ApiTestSupportUtilities.yaml2Json(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE));
278 ToscaServiceTemplate policyTypeServiceTemplate =
279 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
280 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
282 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
283 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
284 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
285 assertNotNull(createdPolicy);
286 assertEquals("operational.restart", createdPolicy.getPolicyId());
287 assertTrue(createdPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
288 }).doesNotThrowAnyException();
292 public void testDeleteOperationalPolicyException() {
293 String policyId = "operational.restart";
294 String policyVersion = "1";
295 String policyTypeVersion = "1.0.0";
296 String policyTypeId = "onap.policies.controlloop.Operational";
297 String legacyMinorPatchSuffix = ".0.0";
299 try (PolicyModelsProvider databaseProvider =
300 new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
301 assertEquals(0, databaseProvider.getPdpGroups("name").size());
302 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
304 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
305 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
307 PdpGroup pdpGroup = new PdpGroup();
308 pdpGroup.setName("group");
309 pdpGroup.setVersion("1.2.3");
310 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
311 pdpGroup.setPdpSubgroups(new ArrayList<>());
312 List<PdpGroup> groupList = new ArrayList<>();
313 groupList.add(pdpGroup);
315 PdpSubGroup pdpSubGroup = new PdpSubGroup();
316 pdpSubGroup.setPdpType("type");
317 pdpSubGroup.setDesiredInstanceCount(123);
318 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
319 pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));
320 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
323 pdp.setInstanceId("type-0");
324 pdp.setMessage("Hello");
325 pdp.setPdpState(PdpState.ACTIVE);
326 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
327 pdpSubGroup.setPdpInstances(new ArrayList<>());
328 pdpSubGroup.getPdpInstances().add(pdp);
331 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
332 .getDesiredInstanceCount());
333 assertEquals(1, databaseProvider.getPdpGroups("group").size());
335 // Create Policy Type
336 assertThatCode(() -> {
337 String policyTypeString =
338 ApiTestSupportUtilities.yaml2Json(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE));
339 ToscaServiceTemplate policyTypeServiceTemplate =
340 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
341 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
342 }).doesNotThrowAnyException();
345 assertThatCode(() -> {
346 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
347 LegacyOperationalPolicy policyToCreate =
348 standardCoder.decode(policyString, LegacyOperationalPolicy.class);
349 LegacyOperationalPolicy createdPolicy =
350 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
351 assertNotNull(createdPolicy);
352 }).doesNotThrowAnyException();
354 // Update pdpSubGroup
355 pdpSubGroup.setPolicies(new ArrayList<>());
356 pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion + legacyMinorPatchSuffix));
358 databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
359 assertThatThrownBy(() -> {
360 operationalPolicyProvider.deleteOperationalPolicy(policyId, policyVersion);
361 }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups");
362 } catch (Exception exc) {
363 fail("Test should not throw an exception");
368 public void testDeleteOperationalPolicy() {
370 assertThatThrownBy(() -> {
371 operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
372 }).hasMessage("legacy policy version is not an integer");
374 assertThatThrownBy(() -> {
375 operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
376 }).hasMessage("legacy policy version is not an integer");
378 assertThatCode(() -> {
379 String policyTypeString =
380 ApiTestSupportUtilities.yaml2Json(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE));
381 ToscaServiceTemplate policyTypeServiceTemplate =
382 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
383 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
385 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
386 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
387 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
388 assertNotNull(createdPolicy);
390 LegacyOperationalPolicy deletedPolicy =
391 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
392 assertNotNull(deletedPolicy);
393 assertEquals("operational.restart", deletedPolicy.getPolicyId());
394 assertTrue(deletedPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
395 }).doesNotThrowAnyException();
397 assertThatThrownBy(() -> {
398 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
399 }).hasMessage("no policy found for policy: operational.restart:1");
401 assertThatCode(() -> {
402 policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
403 }).doesNotThrowAnyException();