2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.legacy.provider;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
29 import java.util.Properties;
31 import org.eclipse.persistence.config.PersistenceUnitProperties;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.dao.DaoParameters;
40 import org.onap.policy.models.dao.PfDao;
41 import org.onap.policy.models.dao.PfDaoFactory;
42 import org.onap.policy.models.dao.impl.DefaultPfDao;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
45 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent;
46 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
47 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
48 import org.yaml.snakeyaml.Yaml;
51 * Test the {@link LegacyProvider} class for legacy guard policies.
53 * @author Liam Fallon (liam.fallon@est.tech)
55 public class LegacyProvider4LegacyGuardTest {
56 private static final String POLICY_ID_IS_NULL = "policyId is marked @NonNull but is null";
57 private static final String VDNS_OUTPUT_JSON = "policies/vDNS.policy.guard.frequency.output.json";
58 private static final String VDNS_INPUT_JSON = "policies/vDNS.policy.guard.frequency.input.json";
59 private static final String LEGACY_POLICY_IS_NULL = "legacyGuardPolicy is marked @NonNull but is null";
60 private static final String DAO_IS_NULL = "dao is marked @NonNull but is null";
62 private StandardCoder standardCoder;
66 * Set up the DAO towards the database.
68 * @throws Exception on database errors
71 public void setupDao() throws Exception {
72 final DaoParameters daoParameters = new DaoParameters();
73 daoParameters.setPluginClass(DefaultPfDao.class.getName());
75 daoParameters.setPersistenceUnit("ToscaConceptTest");
77 Properties jdbcProperties = new Properties();
78 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
79 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
81 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
82 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
83 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
85 daoParameters.setJdbcProperties(jdbcProperties);
87 pfDao = new PfDaoFactory().createPfDao(daoParameters);
88 pfDao.init(daoParameters);
92 * Set up standard coder.
95 public void setupStandardCoder() {
96 standardCoder = new StandardCoder();
100 public void teardown() {
105 public void testPoliciesGet() throws Exception {
106 assertThatThrownBy(() -> {
107 new LegacyProvider().getGuardPolicy(null, null, null);
108 }).hasMessage(DAO_IS_NULL);
110 assertThatThrownBy(() -> {
111 new LegacyProvider().getGuardPolicy(null, null, "");
112 }).hasMessage(DAO_IS_NULL);
114 assertThatThrownBy(() -> {
115 new LegacyProvider().getGuardPolicy(pfDao, null, null);
116 }).hasMessage(POLICY_ID_IS_NULL);
118 assertThatThrownBy(() -> {
119 new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist", null);
120 }).hasMessage("no policy found for policy: I Dont Exist:null");
124 LegacyGuardPolicyInput originalGip = standardCoder.decode(
125 ResourceUtils.getResourceAsString(VDNS_INPUT_JSON),
126 LegacyGuardPolicyInput.class);
128 assertNotNull(originalGip);
130 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
132 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
133 assertEquals(originalGip.getContent(),
134 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
136 Map<String, LegacyGuardPolicyOutput> gotGopm =
137 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
139 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
140 assertEquals(originalGip.getContent(),
141 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
143 String expectedJsonOutput =
144 ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON);
145 String actualJsonOutput = standardCoder.encode(gotGopm);
147 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
149 gotGopm = new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "1");
151 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
152 assertEquals(originalGip.getContent(),
153 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
155 actualJsonOutput = standardCoder.encode(gotGopm);
157 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
159 assertThatThrownBy(() -> {
160 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "2");
161 }).hasMessage("no policy found for policy: guard.frequency.scaleout:2");
165 public void testPolicyCreate() throws Exception {
166 assertThatThrownBy(() -> {
167 new LegacyProvider().createGuardPolicy(null, null);
168 }).hasMessage(DAO_IS_NULL);
170 assertThatThrownBy(() -> {
171 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
172 }).hasMessage(DAO_IS_NULL);
174 assertThatThrownBy(() -> {
175 new LegacyProvider().createGuardPolicy(pfDao, null);
176 }).hasMessage(LEGACY_POLICY_IS_NULL);
180 LegacyGuardPolicyInput originalGip = standardCoder.decode(
181 ResourceUtils.getResourceAsString(VDNS_INPUT_JSON),
182 LegacyGuardPolicyInput.class);
184 assertNotNull(originalGip);
186 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
188 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
189 assertEquals(originalGip.getContent(),
190 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
192 Map<String, LegacyGuardPolicyOutput> gotGopm =
193 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
195 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
196 assertEquals(originalGip.getContent(),
197 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
199 String expectedJsonOutput =
200 ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON);
201 String actualJsonOutput = standardCoder.encode(gotGopm);
203 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
207 public void testPolicyCreateBad() throws Exception {
208 assertThatThrownBy(() -> {
209 new LegacyProvider().createGuardPolicy(null, null);
210 }).hasMessage(DAO_IS_NULL);
212 assertThatThrownBy(() -> {
213 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
214 }).hasMessage(DAO_IS_NULL);
216 assertThatThrownBy(() -> {
217 new LegacyProvider().createGuardPolicy(pfDao, null);
218 }).hasMessage(LEGACY_POLICY_IS_NULL);
222 LegacyGuardPolicyInput originalGip = standardCoder.decode(
223 ResourceUtils.getResourceAsString(VDNS_INPUT_JSON),
224 LegacyGuardPolicyInput.class);
226 assertNotNull(originalGip);
228 originalGip.setPolicyId("i.do.not.exist");
230 assertThatThrownBy(() -> {
231 new LegacyProvider().createGuardPolicy(pfDao, originalGip);
232 }).hasMessage("policy type for guard policy \"i.do.not.exist\" unknown");
236 public void testPolicyUpdate() throws Exception {
237 assertThatThrownBy(() -> {
238 new LegacyProvider().updateGuardPolicy(null, null);
239 }).hasMessage(DAO_IS_NULL);
241 assertThatThrownBy(() -> {
242 new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
243 }).hasMessage(DAO_IS_NULL);
245 assertThatThrownBy(() -> {
246 new LegacyProvider().updateGuardPolicy(pfDao, null);
247 }).hasMessage(LEGACY_POLICY_IS_NULL);
249 assertThatThrownBy(() -> {
250 new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
251 }).hasMessage("policy type for guard policy \"null\" unknown");
255 LegacyGuardPolicyInput originalGip = standardCoder.decode(
256 ResourceUtils.getResourceAsString(VDNS_INPUT_JSON),
257 LegacyGuardPolicyInput.class);
259 assertNotNull(originalGip);
261 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
262 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
263 assertEquals(originalGip.getContent(),
264 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
266 Map<String, LegacyGuardPolicyOutput> gotGopm =
267 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
269 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
270 assertEquals(originalGip.getContent(),
271 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
273 originalGip.getContent().setRecipe("Roast Turkey");
274 Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
275 assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
276 assertEquals(originalGip.getContent(),
277 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
279 Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
280 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
281 assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
282 assertEquals(originalGip.getContent(),
283 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
284 assertEquals("Roast Turkey",
285 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
290 public void testPoliciesDelete() throws Exception {
291 assertThatThrownBy(() -> {
292 new LegacyProvider().deleteGuardPolicy(null, null, null);
293 }).hasMessage(DAO_IS_NULL);
295 assertThatThrownBy(() -> {
296 new LegacyProvider().deleteGuardPolicy(null, null, "");
297 }).hasMessage(DAO_IS_NULL);
299 assertThatThrownBy(() -> {
300 new LegacyProvider().deleteGuardPolicy(null, "", null);
301 }).hasMessage(DAO_IS_NULL);
303 assertThatThrownBy(() -> {
304 new LegacyProvider().deleteGuardPolicy(null, "", "");
305 }).hasMessage(DAO_IS_NULL);
307 assertThatThrownBy(() -> {
308 new LegacyProvider().deleteGuardPolicy(pfDao, null, null);
309 }).hasMessage(POLICY_ID_IS_NULL);
311 assertThatThrownBy(() -> {
312 new LegacyProvider().deleteGuardPolicy(pfDao, null, "");
313 }).hasMessage(POLICY_ID_IS_NULL);
315 assertThatThrownBy(() -> {
316 new LegacyProvider().deleteGuardPolicy(pfDao, "", null);
317 }).hasMessage("policyVersion is marked @NonNull but is null");
319 assertThatThrownBy(() -> {
320 new LegacyProvider().deleteGuardPolicy(pfDao, "IDontExist", "0");
321 }).hasMessage("no policy found for policy: IDontExist:0");
325 LegacyGuardPolicyInput originalGip = standardCoder.decode(
326 ResourceUtils.getResourceAsString(VDNS_INPUT_JSON),
327 LegacyGuardPolicyInput.class);
329 assertNotNull(originalGip);
331 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
332 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
333 assertEquals(originalGip.getContent(),
334 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
336 Map<String, LegacyGuardPolicyOutput> gotGopm =
337 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
339 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
340 assertEquals(originalGip.getContent(),
341 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
343 String expectedJsonOutput =
344 ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON);
345 String actualJsonOutput = standardCoder.encode(gotGopm);
347 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
349 assertThatThrownBy(() -> {
350 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), null);
351 }).hasMessage("policyVersion is marked @NonNull but is null");
353 Map<String, LegacyGuardPolicyOutput> deletedGopm =
354 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), "1");
355 assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
356 assertEquals(originalGip.getContent(),
357 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
359 assertThatThrownBy(() -> {
360 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
361 }).hasMessage("no policy found for policy: guard.frequency.scaleout:null");
363 LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
364 otherGip.setPolicyId("guard.blacklist.b0");
365 otherGip.setPolicyVersion("1");
366 otherGip.setContent(new LegacyGuardPolicyContent());
368 Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
369 assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
370 assertEquals(otherGip.getContent(),
371 createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next());
373 assertThatThrownBy(() -> {
374 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
375 }).hasMessage("no policy found for policy: guard.frequency.scaleout:null");
378 private void createPolicyTypes() throws CoderException, PfModelException {
379 Object yamlObject = new Yaml().load(
380 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
381 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
383 ToscaServiceTemplate toscaServiceTemplatePolicyType =
384 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
386 assertNotNull(toscaServiceTemplatePolicyType);
387 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
389 yamlObject = new Yaml()
390 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.Blacklist.yaml"));
391 yamlAsJsonString = new StandardCoder().encode(yamlObject);
393 toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
395 assertNotNull(toscaServiceTemplatePolicyType);
396 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);