2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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 .*on.*ull 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 .*on.*ull but is null$";
60 private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
62 private StandardCoder standardCoder;
65 * Set up the DAO towards the database.
67 * @throws Exception on database errors
70 public void setupDao() throws Exception {
71 final DaoParameters daoParameters = new DaoParameters();
72 daoParameters.setPluginClass(DefaultPfDao.class.getName());
74 daoParameters.setPersistenceUnit("ToscaConceptTest");
76 Properties jdbcProperties = new Properties();
77 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
78 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
80 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
81 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
82 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
84 daoParameters.setJdbcProperties(jdbcProperties);
86 pfDao = new PfDaoFactory().createPfDao(daoParameters);
87 pfDao.init(daoParameters);
91 * Set up standard coder.
94 public void setupStandardCoder() {
95 standardCoder = new StandardCoder();
99 public void teardown() {
104 public void testPoliciesGet() throws Exception {
105 assertThatThrownBy(() -> {
106 new LegacyProvider().getGuardPolicy(null, null, null);
107 }).hasMessageMatching(DAO_IS_NULL);
109 assertThatThrownBy(() -> {
110 new LegacyProvider().getGuardPolicy(null, null, "");
111 }).hasMessageMatching(DAO_IS_NULL);
113 assertThatThrownBy(() -> {
114 new LegacyProvider().getGuardPolicy(pfDao, null, null);
115 }).hasMessageMatching(POLICY_ID_IS_NULL);
117 assertThatThrownBy(() -> {
118 new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist", null);
119 }).hasMessage("no policy found for policy: I Dont Exist:null");
123 LegacyGuardPolicyInput originalGip =
124 standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class);
126 assertNotNull(originalGip);
128 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
130 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
131 assertEquals(originalGip.getContent(),
132 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
134 Map<String, LegacyGuardPolicyOutput> gotGopm =
135 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
137 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
138 assertEquals(originalGip.getContent(),
139 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
141 String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON);
142 String actualJsonOutput = standardCoder.encode(gotGopm);
144 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
146 gotGopm = new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "1");
148 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
149 assertEquals(originalGip.getContent(),
150 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
152 actualJsonOutput = standardCoder.encode(gotGopm);
154 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
156 assertThatThrownBy(() -> {
157 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "2");
158 }).hasMessage("no policy found for policy: guard.frequency.scaleout:2");
162 public void testPolicyCreate() throws Exception {
163 assertThatThrownBy(() -> {
164 new LegacyProvider().createGuardPolicy(null, null);
165 }).hasMessageMatching(DAO_IS_NULL);
167 assertThatThrownBy(() -> {
168 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
169 }).hasMessageMatching(DAO_IS_NULL);
171 assertThatThrownBy(() -> {
172 new LegacyProvider().createGuardPolicy(pfDao, null);
173 }).hasMessageMatching(LEGACY_POLICY_IS_NULL);
177 LegacyGuardPolicyInput originalGip =
178 standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class);
180 assertNotNull(originalGip);
182 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
184 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
185 assertEquals(originalGip.getContent(),
186 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
188 Map<String, LegacyGuardPolicyOutput> gotGopm =
189 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
191 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
192 assertEquals(originalGip.getContent(),
193 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
195 String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON);
196 String actualJsonOutput = standardCoder.encode(gotGopm);
198 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
202 public void testPolicyCreateBad() throws Exception {
203 assertThatThrownBy(() -> {
204 new LegacyProvider().createGuardPolicy(null, null);
205 }).hasMessageMatching(DAO_IS_NULL);
207 assertThatThrownBy(() -> {
208 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
209 }).hasMessageMatching(DAO_IS_NULL);
211 assertThatThrownBy(() -> {
212 new LegacyProvider().createGuardPolicy(pfDao, null);
213 }).hasMessageMatching(LEGACY_POLICY_IS_NULL);
217 LegacyGuardPolicyInput originalGip =
218 standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), 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 }).hasMessageMatching(DAO_IS_NULL);
235 assertThatThrownBy(() -> {
236 new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
237 }).hasMessageMatching(DAO_IS_NULL);
239 assertThatThrownBy(() -> {
240 new LegacyProvider().updateGuardPolicy(pfDao, null);
241 }).hasMessageMatching(LEGACY_POLICY_IS_NULL);
243 assertThatThrownBy(() -> {
244 new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
245 }).hasMessage("policy type for guard policy \"null\" unknown");
249 LegacyGuardPolicyInput originalGip =
250 standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class);
252 assertNotNull(originalGip);
254 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
255 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
256 assertEquals(originalGip.getContent(),
257 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
259 Map<String, LegacyGuardPolicyOutput> gotGopm =
260 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
262 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
263 assertEquals(originalGip.getContent(),
264 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
266 originalGip.getContent().setRecipe("Roast Turkey");
267 Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
268 assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
269 assertEquals(originalGip.getContent(),
270 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
272 Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
273 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
274 assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
275 assertEquals(originalGip.getContent(),
276 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
277 assertEquals("Roast Turkey",
278 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
282 public void testPoliciesDelete() throws Exception {
283 assertThatThrownBy(() -> {
284 new LegacyProvider().deleteGuardPolicy(null, null, null);
285 }).hasMessageMatching(DAO_IS_NULL);
287 assertThatThrownBy(() -> {
288 new LegacyProvider().deleteGuardPolicy(null, null, "");
289 }).hasMessageMatching(DAO_IS_NULL);
291 assertThatThrownBy(() -> {
292 new LegacyProvider().deleteGuardPolicy(null, "", null);
293 }).hasMessageMatching(DAO_IS_NULL);
295 assertThatThrownBy(() -> {
296 new LegacyProvider().deleteGuardPolicy(null, "", "");
297 }).hasMessageMatching(DAO_IS_NULL);
299 assertThatThrownBy(() -> {
300 new LegacyProvider().deleteGuardPolicy(pfDao, null, null);
301 }).hasMessageMatching(POLICY_ID_IS_NULL);
303 assertThatThrownBy(() -> {
304 new LegacyProvider().deleteGuardPolicy(pfDao, null, "");
305 }).hasMessageMatching(POLICY_ID_IS_NULL);
307 assertThatThrownBy(() -> {
308 new LegacyProvider().deleteGuardPolicy(pfDao, "", null);
309 }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
311 assertThatThrownBy(() -> {
312 new LegacyProvider().deleteGuardPolicy(pfDao, "IDontExist", "0");
313 }).hasMessage("no policy found for policy: IDontExist:0");
317 LegacyGuardPolicyInput originalGip =
318 standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class);
320 assertNotNull(originalGip);
322 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
323 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
324 assertEquals(originalGip.getContent(),
325 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
327 Map<String, LegacyGuardPolicyOutput> gotGopm =
328 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
330 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
331 assertEquals(originalGip.getContent(),
332 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
334 String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON);
335 String actualJsonOutput = standardCoder.encode(gotGopm);
337 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
339 assertThatThrownBy(() -> {
340 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), null);
341 }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
343 Map<String, LegacyGuardPolicyOutput> deletedGopm =
344 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), "1");
345 assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
346 assertEquals(originalGip.getContent(),
347 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
349 assertThatThrownBy(() -> {
350 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
351 }).hasMessage("no policy found for policy: guard.frequency.scaleout:null");
353 LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
354 otherGip.setPolicyId("guard.blacklist.b0");
355 otherGip.setPolicyVersion("1");
356 otherGip.setContent(new LegacyGuardPolicyContent());
358 Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
359 assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
360 assertEquals(otherGip.getContent(),
361 createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next());
363 assertThatThrownBy(() -> {
364 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
365 }).hasMessage("no policy found for policy: guard.frequency.scaleout:null");
368 private void createPolicyTypes() throws CoderException, PfModelException {
369 Object yamlObject = new Yaml().load(
370 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
371 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
373 ToscaServiceTemplate toscaServiceTemplatePolicyType =
374 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
376 assertNotNull(toscaServiceTemplatePolicyType);
377 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
379 yamlObject = new Yaml()
380 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.Blacklist.yaml"));
381 yamlAsJsonString = new StandardCoder().encode(yamlObject);
383 toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
385 assertNotNull(toscaServiceTemplatePolicyType);
386 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);