2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.legacy.provider;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
28 import java.util.Properties;
30 import org.eclipse.persistence.config.PersistenceUnitProperties;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.dao.DaoParameters;
39 import org.onap.policy.models.dao.PfDao;
40 import org.onap.policy.models.dao.PfDaoFactory;
41 import org.onap.policy.models.dao.impl.DefaultPfDao;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
44 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent;
45 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
46 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
47 import org.yaml.snakeyaml.Yaml;
50 * Test the {@link LegacyProvider} class for legacy guard policies.
52 * @author Liam Fallon (liam.fallon@est.tech)
54 public class LegacyProvider4LegacyGuardTest {
56 private StandardCoder standardCoder;
60 * Set up the DAO towards the database.
62 * @throws Exception on database errors
65 public void setupDao() throws Exception {
66 final DaoParameters daoParameters = new DaoParameters();
67 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
69 daoParameters.setPersistenceUnit("ToscaConceptTest");
71 Properties jdbcProperties = new Properties();
72 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
73 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
75 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
76 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
77 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
79 daoParameters.setJdbcProperties(jdbcProperties);
81 pfDao = new PfDaoFactory().createPfDao(daoParameters);
82 pfDao.init(daoParameters);
86 * Set up standard coder.
89 public void setupStandardCoder() {
90 standardCoder = new StandardCoder();
94 public void teardown() throws Exception {
99 public void testPoliciesGet() throws Exception {
100 assertThatThrownBy(() -> {
101 new LegacyProvider().getGuardPolicy(null, null, null);
102 }).hasMessage("dao is marked @NonNull but is null");
104 assertThatThrownBy(() -> {
105 new LegacyProvider().getGuardPolicy(null, null, "");
106 }).hasMessage("dao is marked @NonNull but is null");
108 assertThatThrownBy(() -> {
109 new LegacyProvider().getGuardPolicy(pfDao, null, null);
110 }).hasMessage("policyId is marked @NonNull but is null");
112 assertThatThrownBy(() -> {
113 new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist", null);
114 }).hasMessage("no policy found for policy: I Dont Exist:null");
118 LegacyGuardPolicyInput originalGip = standardCoder.decode(
119 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
120 LegacyGuardPolicyInput.class);
122 assertNotNull(originalGip);
124 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
126 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
127 assertEquals(originalGip.getContent(),
128 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
130 Map<String, LegacyGuardPolicyOutput> gotGopm =
131 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
133 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
134 assertEquals(originalGip.getContent(),
135 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
137 String expectedJsonOutput =
138 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
139 String actualJsonOutput = standardCoder.encode(gotGopm);
141 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
143 gotGopm = new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "1");
145 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
146 assertEquals(originalGip.getContent(),
147 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
149 actualJsonOutput = standardCoder.encode(gotGopm);
151 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
153 assertThatThrownBy(() -> {
154 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "2");
155 }).hasMessage("no policy found for policy: guard.frequency.scaleout:2");
159 public void testPolicyCreate() throws Exception {
160 assertThatThrownBy(() -> {
161 new LegacyProvider().createGuardPolicy(null, null);
162 }).hasMessage("dao is marked @NonNull but is null");
164 assertThatThrownBy(() -> {
165 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
166 }).hasMessage("dao is marked @NonNull but is null");
168 assertThatThrownBy(() -> {
169 new LegacyProvider().createGuardPolicy(pfDao, null);
170 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
174 LegacyGuardPolicyInput originalGip = standardCoder.decode(
175 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
176 LegacyGuardPolicyInput.class);
178 assertNotNull(originalGip);
180 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
182 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
183 assertEquals(originalGip.getContent(),
184 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
186 Map<String, LegacyGuardPolicyOutput> gotGopm =
187 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
189 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
190 assertEquals(originalGip.getContent(),
191 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
193 String expectedJsonOutput =
194 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
195 String actualJsonOutput = standardCoder.encode(gotGopm);
197 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
201 public void testPolicyCreateBad() throws Exception {
202 assertThatThrownBy(() -> {
203 new LegacyProvider().createGuardPolicy(null, null);
204 }).hasMessage("dao is marked @NonNull but is null");
206 assertThatThrownBy(() -> {
207 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
208 }).hasMessage("dao is marked @NonNull but is null");
210 assertThatThrownBy(() -> {
211 new LegacyProvider().createGuardPolicy(pfDao, null);
212 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
216 LegacyGuardPolicyInput originalGip = standardCoder.decode(
217 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
218 LegacyGuardPolicyInput.class);
220 assertNotNull(originalGip);
222 originalGip.setPolicyId("i.do.not.exist");
224 assertThatThrownBy(() -> {
225 new LegacyProvider().createGuardPolicy(pfDao, originalGip);
226 }).hasMessage("policy type for guard policy \"i.do.not.exist\" unknown");
230 public void testPolicyUpdate() throws Exception {
231 assertThatThrownBy(() -> {
232 new LegacyProvider().updateGuardPolicy(null, null);
233 }).hasMessage("dao is marked @NonNull but is null");
235 assertThatThrownBy(() -> {
236 new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
237 }).hasMessage("dao is marked @NonNull but is null");
239 assertThatThrownBy(() -> {
240 new LegacyProvider().updateGuardPolicy(pfDao, null);
241 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
243 assertThatThrownBy(() -> {
244 new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
245 }).hasMessage("policy type for guard policy \"null\" unknown");
249 LegacyGuardPolicyInput originalGip = standardCoder.decode(
250 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
251 LegacyGuardPolicyInput.class);
253 assertNotNull(originalGip);
255 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
256 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
257 assertEquals(originalGip.getContent(),
258 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
260 Map<String, LegacyGuardPolicyOutput> gotGopm =
261 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
263 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
264 assertEquals(originalGip.getContent(),
265 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
267 originalGip.getContent().setRecipe("Roast Turkey");
268 Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
269 assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
270 assertEquals(originalGip.getContent(),
271 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
273 Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
274 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
275 assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
276 assertEquals(originalGip.getContent(),
277 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
278 assertEquals("Roast Turkey",
279 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
284 public void testPoliciesDelete() throws Exception {
285 assertThatThrownBy(() -> {
286 new LegacyProvider().deleteGuardPolicy(null, null, null);
287 }).hasMessage("dao is marked @NonNull but is null");
289 assertThatThrownBy(() -> {
290 new LegacyProvider().deleteGuardPolicy(null, null, "");
291 }).hasMessage("dao is marked @NonNull but is null");
293 assertThatThrownBy(() -> {
294 new LegacyProvider().deleteGuardPolicy(null, "", null);
295 }).hasMessage("dao is marked @NonNull but is null");
297 assertThatThrownBy(() -> {
298 new LegacyProvider().deleteGuardPolicy(null, "", "");
299 }).hasMessage("dao is marked @NonNull but is null");
301 assertThatThrownBy(() -> {
302 new LegacyProvider().deleteGuardPolicy(pfDao, null, null);
303 }).hasMessage("policyId is marked @NonNull but is null");
305 assertThatThrownBy(() -> {
306 new LegacyProvider().deleteGuardPolicy(pfDao, null, "");
307 }).hasMessage("policyId is marked @NonNull but is null");
309 assertThatThrownBy(() -> {
310 new LegacyProvider().deleteGuardPolicy(pfDao, "", null);
311 }).hasMessage("policyVersion is marked @NonNull but is null");
313 assertThatThrownBy(() -> {
314 new LegacyProvider().deleteGuardPolicy(pfDao, "IDontExist", "");
315 }).hasMessage("no policy found for policy: IDontExist:");
319 LegacyGuardPolicyInput originalGip = standardCoder.decode(
320 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
321 LegacyGuardPolicyInput.class);
323 assertNotNull(originalGip);
325 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
326 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
327 assertEquals(originalGip.getContent(),
328 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
330 Map<String, LegacyGuardPolicyOutput> gotGopm =
331 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
333 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
334 assertEquals(originalGip.getContent(),
335 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
337 String expectedJsonOutput =
338 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
339 String actualJsonOutput = standardCoder.encode(gotGopm);
341 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
343 assertThatThrownBy(() -> {
344 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), null);
345 }).hasMessage("policyVersion is marked @NonNull but is null");
347 Map<String, LegacyGuardPolicyOutput> deletedGopm =
348 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), "1");
349 assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
350 assertEquals(originalGip.getContent(),
351 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
353 assertThatThrownBy(() -> {
354 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
355 }).hasMessage("no policy found for policy: guard.frequency.scaleout:null");
357 LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
358 otherGip.setPolicyId("guard.blacklist.b0");
359 otherGip.setPolicyVersion("1");
360 otherGip.setContent(new LegacyGuardPolicyContent());
362 Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
363 assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
364 assertEquals(otherGip.getContent(),
365 createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next());
367 assertThatThrownBy(() -> {
368 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
369 }).hasMessage("no policy found for policy: guard.frequency.scaleout:null");
372 private void createPolicyTypes() throws CoderException, PfModelException {
373 Object yamlObject = new Yaml().load(
374 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
375 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
377 ToscaServiceTemplate toscaServiceTemplatePolicyType =
378 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
380 assertNotNull(toscaServiceTemplatePolicyType);
381 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
383 yamlObject = new Yaml()
384 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.Blacklist.yaml"));
385 yamlAsJsonString = new StandardCoder().encode(yamlObject);
387 toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
389 assertNotNull(toscaServiceTemplatePolicyType);
390 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);