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.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
32 import java.util.Base64;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.api.main.parameters.ApiParameterGroup;
38 import org.onap.policy.common.parameters.ParameterService;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.base.PfModelException;
42 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
45 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
48 * This class performs unit test of {@link LegacyGuardPolicyProvider}
50 * @author Chenfei Gao (cgao@research.att.com)
52 public class TestLegacyGuardPolicyProvider {
54 private static LegacyGuardPolicyProvider guardPolicyProvider;
55 private static PolicyTypeProvider policyTypeProvider;
56 private static PolicyModelsProviderParameters providerParams;
57 private static ApiParameterGroup apiParamGroup;
58 private static StandardCoder standardCoder;
60 private static final String POLICY_RESOURCE = "policies/vDNS.policy.guard.frequency.input.json";
61 private static final String POLICY_RESOURCE_VER1 = "policies/vDNS.policy.guard.frequency.input.ver1.json";
62 private static final String POLICY_RESOURCE_VER2 = "policies/vDNS.policy.guard.frequency.input.ver2.json";
63 private static final String POLICY_TYPE_RESOURCE =
64 "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.json";
65 private static final String POLICY_TYPE_ID = "onap.policies.controlloop.guard.FrequencyLimiter:1.0.0";
66 private static final String POLICY_ID = "guard.frequency.scaleout:1.0.0";
69 * Initializes parameters.
71 * @throws PfModelException the PfModel parsing exception
74 public static void setupParameters() throws PfModelException {
76 standardCoder = new StandardCoder();
77 providerParams = new PolicyModelsProviderParameters();
78 providerParams.setDatabaseDriver("org.h2.Driver");
79 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
80 providerParams.setDatabaseUser("policy");
81 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
82 providerParams.setPersistenceUnit("ToscaConceptTest");
83 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
84 ParameterService.register(apiParamGroup, true);
85 guardPolicyProvider = new LegacyGuardPolicyProvider();
86 policyTypeProvider = new PolicyTypeProvider();
90 * Closes up DB connections and deregisters API parameter group.
92 * @throws PfModelException the PfModel parsing exception
95 public static void tearDown() throws PfModelException {
97 guardPolicyProvider.close();
98 policyTypeProvider.close();
99 ParameterService.deregister(apiParamGroup);
104 public void testFetchGuardPolicy() {
106 assertThatThrownBy(() -> {
107 guardPolicyProvider.fetchGuardPolicy("dummy", null);
108 }).hasMessage("no policy found for policy: dummy:null");
110 assertThatThrownBy(() -> {
111 guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
112 }).hasMessage("legacy policy version is not an integer");
114 assertThatCode(() -> {
115 String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
116 ToscaServiceTemplate policyTypeServiceTemplate =
117 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
118 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
120 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_VER1);
121 LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
122 Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
123 assertNotNull(createdPolicy);
124 assertFalse(createdPolicy.isEmpty());
126 policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_VER2);
127 policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
128 createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
129 assertNotNull(createdPolicy);
130 assertFalse(createdPolicy.isEmpty());
132 Map<String, LegacyGuardPolicyOutput> firstVersion =
133 guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "1");
134 assertNotNull(firstVersion);
136 firstVersion.get("guard.frequency.scaleout").getMetadata().get("policy-version").toString());
138 Map<String, LegacyGuardPolicyOutput> latestVersion =
139 guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", null);
140 assertNotNull(latestVersion);
142 latestVersion.get("guard.frequency.scaleout").getMetadata().get("policy-version").toString());
143 }).doesNotThrowAnyException();
145 assertThatThrownBy(() -> {
146 guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "1.0.0");
147 }).hasMessage("legacy policy version is not an integer");
149 assertThatThrownBy(() -> {
150 guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "latest");
151 }).hasMessage("legacy policy version is not an integer");
153 assertThatCode(() -> {
154 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
155 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "2");
156 policyTypeProvider.deletePolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0");
157 }).doesNotThrowAnyException();
161 public void testCreateGuardPolicy() {
163 assertThatThrownBy(() -> {
164 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
165 LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
166 guardPolicyProvider.createGuardPolicy(policyToCreate);
167 }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist");
169 assertThatCode(() -> {
170 String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
171 ToscaServiceTemplate policyTypeServiceTemplate =
172 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
173 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
175 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
176 LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
177 Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
178 assertNotNull(createdPolicy);
179 assertFalse(createdPolicy.isEmpty());
180 assertTrue(createdPolicy.containsKey("guard.frequency.scaleout"));
181 assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
182 createdPolicy.get("guard.frequency.scaleout").getType());
183 assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion());
184 }).doesNotThrowAnyException();
188 public void testDeleteGuardPolicy() {
190 assertThatThrownBy(() -> {
191 guardPolicyProvider.deleteGuardPolicy("dummy", null);
192 }).hasMessage("version is marked @NonNull but is null");
194 assertThatThrownBy(() -> {
195 guardPolicyProvider.deleteGuardPolicy("dummy", "1.0.0");
196 }).hasMessage("legacy policy version is not an integer");
198 assertThatCode(() -> {
199 String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
200 ToscaServiceTemplate policyTypeServiceTemplate =
201 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
202 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
204 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
205 LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
206 Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
207 assertNotNull(createdPolicy);
208 assertFalse(createdPolicy.isEmpty());
210 Map<String, LegacyGuardPolicyOutput> deletedPolicy = guardPolicyProvider
211 .deleteGuardPolicy("guard.frequency.scaleout", "1");
212 assertNotNull(deletedPolicy);
213 assertFalse(deletedPolicy.isEmpty());
214 assertTrue(deletedPolicy.containsKey("guard.frequency.scaleout"));
215 assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
216 deletedPolicy.get("guard.frequency.scaleout").getType());
218 deletedPolicy.get("guard.frequency.scaleout").getMetadata().get("policy-version").toString());
219 }).doesNotThrowAnyException();
221 assertThatThrownBy(() -> {
222 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
223 }).hasMessage("no policy found for policy: guard.frequency.scaleout:1");
225 assertThatCode(() -> {
226 policyTypeProvider.deletePolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0");
227 }).doesNotThrowAnyException();