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 testPolicyUpdate() throws Exception {
188 assertThatThrownBy(() -> {
189 new LegacyProvider().updateGuardPolicy(null, null);
190 }).hasMessage("dao is marked @NonNull but is null");
192 assertThatThrownBy(() -> {
193 new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
194 }).hasMessage("dao is marked @NonNull but is null");
196 assertThatThrownBy(() -> {
197 new LegacyProvider().updateGuardPolicy(pfDao, null);
198 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
200 assertThatThrownBy(() -> {
201 new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
202 }).hasMessage("policy type for guard policy \"null\" unknown");
206 LegacyGuardPolicyInput originalGip = standardCoder.decode(
207 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
208 LegacyGuardPolicyInput.class);
210 assertNotNull(originalGip);
212 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
213 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
214 assertEquals(originalGip.getContent(),
215 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
217 Map<String, LegacyGuardPolicyOutput> gotGopm =
218 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
220 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
221 assertEquals(originalGip.getContent(),
222 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
224 originalGip.getContent().setRecipe("Roast Turkey");
225 Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
226 assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
227 assertEquals(originalGip.getContent(),
228 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
230 Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
231 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
232 assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
233 assertEquals(originalGip.getContent(),
234 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
235 assertEquals("Roast Turkey",
236 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
241 public void testPoliciesDelete() throws Exception {
242 assertThatThrownBy(() -> {
243 new LegacyProvider().deleteGuardPolicy(null, null);
244 }).hasMessage("dao is marked @NonNull but is null");
246 assertThatThrownBy(() -> {
247 new LegacyProvider().deleteGuardPolicy(null, "");
248 }).hasMessage("dao is marked @NonNull but is null");
250 assertThatThrownBy(() -> {
251 new LegacyProvider().deleteGuardPolicy(pfDao, null);
252 }).hasMessage("policyId is marked @NonNull but is null");
255 assertThatThrownBy(() -> {
256 new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist");
257 }).hasMessage("no policy found for policy ID: I Dont Exist");
261 LegacyGuardPolicyInput originalGip = standardCoder.decode(
262 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
263 LegacyGuardPolicyInput.class);
265 assertNotNull(originalGip);
267 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
268 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
269 assertEquals(originalGip.getContent(),
270 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
272 Map<String, LegacyGuardPolicyOutput> gotGopm =
273 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
275 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
276 assertEquals(originalGip.getContent(),
277 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
279 String expectedJsonOutput =
280 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
281 String actualJsonOutput = standardCoder.encode(gotGopm);
283 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
285 Map<String, LegacyGuardPolicyOutput> deletedGopm =
286 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId());
287 assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
288 assertEquals(originalGip.getContent(),
289 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
291 assertThatThrownBy(() -> {
292 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
293 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
295 LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
296 otherGip.setPolicyId("guard.blacklist");
297 otherGip.setPolicyVersion("1");
298 otherGip.setContent(new LegacyGuardPolicyContent());
300 Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
301 assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
302 assertEquals(otherGip.getContent(),
303 createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next());
305 assertThatThrownBy(() -> {
306 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
307 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
310 private void createPolicyTypes() throws CoderException, PfModelException {
311 Object yamlObject = new Yaml().load(
312 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
313 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
315 ToscaServiceTemplate toscaServiceTemplatePolicyType =
316 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
318 assertNotNull(toscaServiceTemplatePolicyType);
319 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
321 yamlObject = new Yaml()
322 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.Blacklist.yaml"));
323 yamlAsJsonString = new StandardCoder().encode(yamlObject);
325 toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
327 assertNotNull(toscaServiceTemplatePolicyType);
328 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);