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.StandardCoder;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.models.dao.DaoParameters;
37 import org.onap.policy.models.dao.PfDao;
38 import org.onap.policy.models.dao.PfDaoFactory;
39 import org.onap.policy.models.dao.impl.DefaultPfDao;
40 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent;
41 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
42 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
45 * Test the {@link LegacyProvider} class for legacy guard policies.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class LegacyProvider4LegacyGuardTest {
51 private StandardCoder standardCoder;
55 * Set up the DAO towards the database.
57 * @throws Exception on database errors
60 public void setupDao() throws Exception {
61 final DaoParameters daoParameters = new DaoParameters();
62 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
64 daoParameters.setPersistenceUnit("ToscaConceptTest");
66 Properties jdbcProperties = new Properties();
67 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
68 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
70 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
71 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
72 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
74 daoParameters.setJdbcProperties(jdbcProperties);
76 pfDao = new PfDaoFactory().createPfDao(daoParameters);
77 pfDao.init(daoParameters);
81 * Set up standard coder.
84 public void setupStandardCoder() {
85 standardCoder = new StandardCoder();
89 public void teardown() throws Exception {
94 public void testPoliciesGet() throws Exception {
95 assertThatThrownBy(() -> {
96 new LegacyProvider().getGuardPolicy(null, null);
97 }).hasMessage("dao is marked @NonNull but is null");
99 assertThatThrownBy(() -> {
100 new LegacyProvider().getGuardPolicy(null, "");
101 }).hasMessage("dao is marked @NonNull but is null");
103 assertThatThrownBy(() -> {
104 new LegacyProvider().getGuardPolicy(pfDao, null);
105 }).hasMessage("policyId is marked @NonNull but is null");
107 assertThatThrownBy(() -> {
108 new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist");
109 }).hasMessage("no policy found for policy ID: I Dont Exist");
111 LegacyGuardPolicyInput originalGip = standardCoder.decode(
112 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
113 LegacyGuardPolicyInput.class);
115 assertNotNull(originalGip);
117 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
119 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
120 assertEquals(originalGip.getContent(),
121 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
123 Map<String, LegacyGuardPolicyOutput> gotGopm =
124 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
126 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
127 assertEquals(originalGip.getContent(),
128 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
130 String expectedJsonOutput =
131 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
132 String actualJsonOutput = standardCoder.encode(gotGopm);
134 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
138 public void testPolicyCreate() throws Exception {
139 assertThatThrownBy(() -> {
140 new LegacyProvider().createGuardPolicy(null, null);
141 }).hasMessage("dao is marked @NonNull but is null");
143 assertThatThrownBy(() -> {
144 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
145 }).hasMessage("dao is marked @NonNull but is null");
147 assertThatThrownBy(() -> {
148 new LegacyProvider().createGuardPolicy(pfDao, null);
149 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
151 LegacyGuardPolicyInput originalGip = standardCoder.decode(
152 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
153 LegacyGuardPolicyInput.class);
155 assertNotNull(originalGip);
157 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
159 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
160 assertEquals(originalGip.getContent(),
161 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
163 Map<String, LegacyGuardPolicyOutput> gotGopm =
164 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
166 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
167 assertEquals(originalGip.getContent(),
168 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
170 String expectedJsonOutput =
171 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
172 String actualJsonOutput = standardCoder.encode(gotGopm);
174 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
179 public void testPolicyUpdate() throws Exception {
180 assertThatThrownBy(() -> {
181 new LegacyProvider().updateGuardPolicy(null, null);
182 }).hasMessage("dao is marked @NonNull but is null");
184 assertThatThrownBy(() -> {
185 new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
186 }).hasMessage("dao is marked @NonNull but is null");
188 assertThatThrownBy(() -> {
189 new LegacyProvider().updateGuardPolicy(pfDao, null);
190 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
192 assertThatThrownBy(() -> {
193 new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
194 }).hasMessage("policy type for guard policy \"null\" unknown");
196 LegacyGuardPolicyInput originalGip = standardCoder.decode(
197 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
198 LegacyGuardPolicyInput.class);
200 assertNotNull(originalGip);
202 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
203 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
204 assertEquals(originalGip.getContent(),
205 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
207 Map<String, LegacyGuardPolicyOutput> gotGopm =
208 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
210 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
211 assertEquals(originalGip.getContent(),
212 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
214 originalGip.getContent().setRecipe("Roast Turkey");
215 Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
216 assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
217 assertEquals(originalGip.getContent(),
218 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
220 Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
221 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
222 assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
223 assertEquals(originalGip.getContent(),
224 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
225 assertEquals("Roast Turkey",
226 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
231 public void testPoliciesDelete() throws Exception {
232 assertThatThrownBy(() -> {
233 new LegacyProvider().deleteGuardPolicy(null, null);
234 }).hasMessage("dao is marked @NonNull but is null");
236 assertThatThrownBy(() -> {
237 new LegacyProvider().deleteGuardPolicy(null, "");
238 }).hasMessage("dao is marked @NonNull but is null");
240 assertThatThrownBy(() -> {
241 new LegacyProvider().deleteGuardPolicy(pfDao, null);
242 }).hasMessage("policyId is marked @NonNull but is null");
245 assertThatThrownBy(() -> {
246 new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist");
247 }).hasMessage("no policy found for policy ID: I Dont Exist");
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());
263 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
264 assertEquals(originalGip.getContent(),
265 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
267 String expectedJsonOutput =
268 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
269 String actualJsonOutput = standardCoder.encode(gotGopm);
271 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
273 Map<String, LegacyGuardPolicyOutput> deletedGopm =
274 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId());
275 assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
276 assertEquals(originalGip.getContent(),
277 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
279 assertThatThrownBy(() -> {
280 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
281 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
283 LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
284 otherGip.setPolicyId("guard.blacklist");
285 otherGip.setPolicyVersion("1");
286 otherGip.setContent(new LegacyGuardPolicyContent());
288 Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
289 assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
290 assertEquals(otherGip.getContent(),
291 createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next());
293 assertThatThrownBy(() -> {
294 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
295 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");