2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.rest.provider;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
32 import java.util.ArrayList;
33 import java.util.Base64;
34 import java.util.List;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.api.main.parameters.ApiParameterGroup;
40 import org.onap.policy.common.parameters.ParameterService;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.base.PfModelException;
44 import org.onap.policy.models.pdp.concepts.Pdp;
45 import org.onap.policy.models.pdp.concepts.PdpGroup;
46 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
47 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
48 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
49 import org.onap.policy.models.pdp.enums.PdpState;
50 import org.onap.policy.models.provider.PolicyModelsProvider;
51 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
52 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
56 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
59 * This class performs unit test of {@link LegacyOperationalPolicyProvider}
61 * @author Chenfei Gao (cgao@research.att.com)
63 public class TestLegacyOperationalPolicyProvider {
65 private static LegacyOperationalPolicyProvider operationalPolicyProvider;
66 private static PolicyTypeProvider policyTypeProvider;
67 private static PolicyModelsProviderParameters providerParams;
68 private static ApiParameterGroup apiParamGroup;
69 private static StandardCoder standardCoder;
71 private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.input.json";
72 private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.controlloop.Operational.json";
73 private static final String POLICY_TYPE_ID = "onap.policies.controlloop.Operational:1.0.0";
74 private static final String POLICY_ID = "operational.restart:1.0.0";
77 * Initializes parameters.
79 * @throws PfModelException the PfModel parsing exception
82 public void setupParameters() throws PfModelException {
84 standardCoder = new StandardCoder();
85 providerParams = new PolicyModelsProviderParameters();
86 providerParams.setDatabaseDriver("org.h2.Driver");
87 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
88 providerParams.setDatabaseUser("policy");
89 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
90 providerParams.setPersistenceUnit("ToscaConceptTest");
91 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
92 ParameterService.register(apiParamGroup, true);
93 operationalPolicyProvider = new LegacyOperationalPolicyProvider();
94 policyTypeProvider = new PolicyTypeProvider();
98 * Closes up DB connections and deregisters API parameter group.
100 * @throws PfModelException the PfModel parsing exception
103 public void tearDown() throws PfModelException {
105 operationalPolicyProvider.close();
106 policyTypeProvider.close();
107 ParameterService.deregister(apiParamGroup);
111 public void testFetchOperationalPolicy() {
113 assertThatThrownBy(() -> {
114 operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
115 }).hasMessage("no policy found for policy: dummy:null");
117 assertThatThrownBy(() -> {
118 operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
119 }).hasMessage("legacy policy version is not an integer");
121 assertThatCode(() -> {
122 String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
123 ToscaServiceTemplate policyTypeServiceTemplate =
124 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
125 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
127 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
128 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
129 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
130 assertNotNull(createdPolicy);
132 policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
133 policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
134 createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
135 assertNotNull(createdPolicy);
137 LegacyOperationalPolicy firstVersion =
138 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1");
139 assertNotNull(firstVersion);
141 firstVersion.getPolicyVersion());
143 LegacyOperationalPolicy latestVersion =
144 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", null);
145 assertNotNull(latestVersion);
147 latestVersion.getPolicyVersion());
148 }).doesNotThrowAnyException();
150 assertThatThrownBy(() -> {
151 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1.0.0");
152 }).hasMessage("legacy policy version is not an integer");
154 assertThatThrownBy(() -> {
155 operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "latest");;
156 }).hasMessage("legacy policy version is not an integer");
158 assertThatCode(() -> {
159 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
160 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "2");
161 policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
162 }).doesNotThrowAnyException();
166 public void testCreateOperationalPolicy() {
168 assertThatThrownBy(() -> {
169 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
170 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
171 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
172 }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist");
174 assertThatCode(() -> {
175 String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
176 ToscaServiceTemplate policyTypeServiceTemplate =
177 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
178 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
180 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
181 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
182 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
183 assertNotNull(createdPolicy);
184 assertEquals("operational.restart", createdPolicy.getPolicyId());
185 assertTrue(createdPolicy.getContent()
186 .startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
187 }).doesNotThrowAnyException();
191 public void testDeleteOperationalPolicyException() {
192 String policyId = "operational.restart";
193 String policyVersion = "1";
194 String policyTypeVersion = "1.0.0";
195 String policyTypeId = "onap.policies.controlloop.Operational";
196 String legacyMinorPatchSuffix = ".0.0";
198 try (PolicyModelsProvider databaseProvider =
199 new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
200 assertEquals(0, databaseProvider.getPdpGroups("name").size());
201 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
203 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
204 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
206 PdpGroup pdpGroup = new PdpGroup();
207 pdpGroup.setName("group");
208 pdpGroup.setVersion("1.2.3");
209 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
210 pdpGroup.setPdpSubgroups(new ArrayList<>());
211 List<PdpGroup> groupList = new ArrayList<>();
212 groupList.add(pdpGroup);
214 PdpSubGroup pdpSubGroup = new PdpSubGroup();
215 pdpSubGroup.setPdpType("type");
216 pdpSubGroup.setDesiredInstanceCount(123);
217 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
218 pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(
219 policyTypeId, policyTypeVersion));
220 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
223 pdp.setInstanceId("type-0");
224 pdp.setMessage("Hello");
225 pdp.setPdpState(PdpState.ACTIVE);
226 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
227 pdpSubGroup.setPdpInstances(new ArrayList<>());
228 pdpSubGroup.getPdpInstances().add(pdp);
231 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
232 .getDesiredInstanceCount());
233 assertEquals(1, databaseProvider.getPdpGroups("group").size());
235 // Create Policy Type
236 assertThatCode(() -> {
237 String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
238 ToscaServiceTemplate policyTypeServiceTemplate =
239 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
240 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
241 }).doesNotThrowAnyException();
244 assertThatCode(() -> {
245 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
246 LegacyOperationalPolicy policyToCreate =
247 standardCoder.decode(policyString, LegacyOperationalPolicy.class);
248 LegacyOperationalPolicy createdPolicy =
249 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
250 assertNotNull(createdPolicy);
251 }).doesNotThrowAnyException();
253 // Update pdpSubGroup
254 pdpSubGroup.setPolicies(new ArrayList<>());
255 pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion + legacyMinorPatchSuffix));
256 assertEquals(1, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
257 .getPolicies().size());
258 assertThatThrownBy(() -> {
259 operationalPolicyProvider.deleteOperationalPolicy(policyId, policyVersion);
260 }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups");
262 catch (Exception exc) {
263 fail("Test should not throw an exception");
268 public void testDeleteOperationalPolicy() {
270 assertThatThrownBy(() -> {
271 operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
272 }).hasMessage("legacy policy version is not an integer");
274 assertThatThrownBy(() -> {
275 operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
276 }).hasMessage("legacy policy version is not an integer");
278 assertThatCode(() -> {
279 String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
280 ToscaServiceTemplate policyTypeServiceTemplate =
281 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
282 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
284 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
285 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
286 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
287 assertNotNull(createdPolicy);
289 LegacyOperationalPolicy deletedPolicy = operationalPolicyProvider
290 .deleteOperationalPolicy("operational.restart", "1");
291 assertNotNull(deletedPolicy);
292 assertEquals("operational.restart", deletedPolicy.getPolicyId());
293 assertTrue(deletedPolicy.getContent()
294 .startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
295 }).doesNotThrowAnyException();
297 assertThatThrownBy(() -> {
298 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
299 }).hasMessage("no policy found for policy: operational.restart:1");
301 assertThatCode(() -> {
302 policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
303 }).doesNotThrowAnyException();