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);
102 }).hasMessage("dao is marked @NonNull but is null");
104 assertThatThrownBy(() -> {
105 new LegacyProvider().getGuardPolicy(null, "");
106 }).hasMessage("dao is marked @NonNull but is null");
108 assertThatThrownBy(() -> {
109 new LegacyProvider().getGuardPolicy(pfDao, null);
110 }).hasMessage("policyId is marked @NonNull but is null");
112 assertThatThrownBy(() -> {
113 new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist");
114 }).hasMessage("no policy found for policy ID: I Dont Exist");
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());
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+", ""));
145 public void testPolicyCreate() throws Exception {
146 assertThatThrownBy(() -> {
147 new LegacyProvider().createGuardPolicy(null, null);
148 }).hasMessage("dao is marked @NonNull but is null");
150 assertThatThrownBy(() -> {
151 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
152 }).hasMessage("dao is marked @NonNull but is null");
154 assertThatThrownBy(() -> {
155 new LegacyProvider().createGuardPolicy(pfDao, null);
156 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
160 LegacyGuardPolicyInput originalGip = standardCoder.decode(
161 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
162 LegacyGuardPolicyInput.class);
164 assertNotNull(originalGip);
166 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
168 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
169 assertEquals(originalGip.getContent(),
170 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
172 Map<String, LegacyGuardPolicyOutput> gotGopm =
173 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
175 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
176 assertEquals(originalGip.getContent(),
177 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
179 String expectedJsonOutput =
180 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
181 String actualJsonOutput = standardCoder.encode(gotGopm);
183 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
187 public void testPolicyCreateBad() throws Exception {
188 assertThatThrownBy(() -> {
189 new LegacyProvider().createGuardPolicy(null, null);
190 }).hasMessage("dao is marked @NonNull but is null");
192 assertThatThrownBy(() -> {
193 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
194 }).hasMessage("dao is marked @NonNull but is null");
196 assertThatThrownBy(() -> {
197 new LegacyProvider().createGuardPolicy(pfDao, null);
198 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
202 LegacyGuardPolicyInput originalGip = standardCoder.decode(
203 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
204 LegacyGuardPolicyInput.class);
206 assertNotNull(originalGip);
208 originalGip.setPolicyId("i.do.not.exist");
210 assertThatThrownBy(() -> {
211 new LegacyProvider().createGuardPolicy(pfDao, originalGip);
212 }).hasMessage("policy type for guard policy \"i.do.not.exist\" unknown");
216 public void testPolicyUpdate() throws Exception {
217 assertThatThrownBy(() -> {
218 new LegacyProvider().updateGuardPolicy(null, null);
219 }).hasMessage("dao is marked @NonNull but is null");
221 assertThatThrownBy(() -> {
222 new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
223 }).hasMessage("dao is marked @NonNull but is null");
225 assertThatThrownBy(() -> {
226 new LegacyProvider().updateGuardPolicy(pfDao, null);
227 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
229 assertThatThrownBy(() -> {
230 new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
231 }).hasMessage("policy type for guard policy \"null\" unknown");
235 LegacyGuardPolicyInput originalGip = standardCoder.decode(
236 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
237 LegacyGuardPolicyInput.class);
239 assertNotNull(originalGip);
241 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
242 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
243 assertEquals(originalGip.getContent(),
244 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
246 Map<String, LegacyGuardPolicyOutput> gotGopm =
247 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
249 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
250 assertEquals(originalGip.getContent(),
251 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
253 originalGip.getContent().setRecipe("Roast Turkey");
254 Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
255 assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
256 assertEquals(originalGip.getContent(),
257 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
259 Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
260 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
261 assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
262 assertEquals(originalGip.getContent(),
263 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
264 assertEquals("Roast Turkey",
265 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
270 public void testPoliciesDelete() throws Exception {
271 assertThatThrownBy(() -> {
272 new LegacyProvider().deleteGuardPolicy(null, null);
273 }).hasMessage("dao is marked @NonNull but is null");
275 assertThatThrownBy(() -> {
276 new LegacyProvider().deleteGuardPolicy(null, "");
277 }).hasMessage("dao is marked @NonNull but is null");
279 assertThatThrownBy(() -> {
280 new LegacyProvider().deleteGuardPolicy(pfDao, null);
281 }).hasMessage("policyId is marked @NonNull but is null");
284 assertThatThrownBy(() -> {
285 new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist");
286 }).hasMessage("no policy found for policy ID: I Dont Exist");
290 LegacyGuardPolicyInput originalGip = standardCoder.decode(
291 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
292 LegacyGuardPolicyInput.class);
294 assertNotNull(originalGip);
296 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
297 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
298 assertEquals(originalGip.getContent(),
299 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
301 Map<String, LegacyGuardPolicyOutput> gotGopm =
302 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
304 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
305 assertEquals(originalGip.getContent(),
306 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
308 String expectedJsonOutput =
309 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
310 String actualJsonOutput = standardCoder.encode(gotGopm);
312 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
314 Map<String, LegacyGuardPolicyOutput> deletedGopm =
315 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId());
316 assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
317 assertEquals(originalGip.getContent(),
318 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
320 assertThatThrownBy(() -> {
321 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
322 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
324 LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
325 otherGip.setPolicyId("guard.blacklist.b0");
326 otherGip.setPolicyVersion("1");
327 otherGip.setContent(new LegacyGuardPolicyContent());
329 Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
330 assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
331 assertEquals(otherGip.getContent(),
332 createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next());
334 assertThatThrownBy(() -> {
335 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
336 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
339 private void createPolicyTypes() throws CoderException, PfModelException {
340 Object yamlObject = new Yaml().load(
341 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
342 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
344 ToscaServiceTemplate toscaServiceTemplatePolicyType =
345 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
347 assertNotNull(toscaServiceTemplatePolicyType);
348 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
350 yamlObject = new Yaml()
351 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.Blacklist.yaml"));
352 yamlAsJsonString = new StandardCoder().encode(yamlObject);
354 toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
356 assertNotNull(toscaServiceTemplatePolicyType);
357 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);