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.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.dao.DaoParameters;
36 import org.onap.policy.models.dao.PfDao;
37 import org.onap.policy.models.dao.PfDaoFactory;
38 import org.onap.policy.models.dao.impl.DefaultPfDao;
39 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent;
40 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
41 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
44 * Test the {@link LegacyProvider} class for legacy guard policies.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class LegacyProvider4LegacyGuardTest {
50 private StandardCoder standardCoder;
54 * Set up the DAO towards the database.
56 * @throws Exception on database errors
59 public void setupDao() throws Exception {
60 final DaoParameters daoParameters = new DaoParameters();
61 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
63 daoParameters.setPersistenceUnit("ToscaConceptTest");
65 Properties jdbcProperties = new Properties();
66 jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
67 jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
70 jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
71 jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:testdb");
74 //jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
75 //jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
77 daoParameters.setJdbcProperties(jdbcProperties);
79 pfDao = new PfDaoFactory().createPfDao(daoParameters);
80 pfDao.init(daoParameters);
84 * Set up standard coder.
87 public void setupStandardCoder() {
88 standardCoder = new StandardCoder();
92 public void teardown() throws Exception {
97 public void testPoliciesGet() throws Exception {
98 assertThatThrownBy(() -> {
99 new LegacyProvider().getGuardPolicy(null, null);
100 }).hasMessage("dao is marked @NonNull but is null");
102 assertThatThrownBy(() -> {
103 new LegacyProvider().getGuardPolicy(null, "");
104 }).hasMessage("dao is marked @NonNull but is null");
106 assertThatThrownBy(() -> {
107 new LegacyProvider().getGuardPolicy(pfDao, null);
108 }).hasMessage("policyId is marked @NonNull but is null");
110 assertThatThrownBy(() -> {
111 new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist");
112 }).hasMessage("no policy found for policy ID: I Dont Exist");
114 LegacyGuardPolicyInput originalGip = standardCoder.decode(
115 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
116 LegacyGuardPolicyInput.class);
118 assertNotNull(originalGip);
120 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
122 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
123 assertEquals(originalGip.getContent(),
124 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
126 Map<String, LegacyGuardPolicyOutput> gotGopm =
127 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
129 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
130 assertEquals(originalGip.getContent(),
131 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
133 String expectedJsonOutput =
134 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
135 String actualJsonOutput = standardCoder.encode(gotGopm);
137 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
141 public void testPolicyCreate() throws Exception {
142 assertThatThrownBy(() -> {
143 new LegacyProvider().createGuardPolicy(null, null);
144 }).hasMessage("dao is marked @NonNull but is null");
146 assertThatThrownBy(() -> {
147 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
148 }).hasMessage("dao is marked @NonNull but is null");
150 assertThatThrownBy(() -> {
151 new LegacyProvider().createGuardPolicy(pfDao, null);
152 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
154 LegacyGuardPolicyInput originalGip = standardCoder.decode(
155 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
156 LegacyGuardPolicyInput.class);
158 assertNotNull(originalGip);
160 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
162 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
163 assertEquals(originalGip.getContent(),
164 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
166 Map<String, LegacyGuardPolicyOutput> gotGopm =
167 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
169 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
170 assertEquals(originalGip.getContent(),
171 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
173 String expectedJsonOutput =
174 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
175 String actualJsonOutput = standardCoder.encode(gotGopm);
177 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
182 public void testPolicyUpdate() throws Exception {
183 assertThatThrownBy(() -> {
184 new LegacyProvider().updateGuardPolicy(null, null);
185 }).hasMessage("dao is marked @NonNull but is null");
187 assertThatThrownBy(() -> {
188 new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
189 }).hasMessage("dao is marked @NonNull but is null");
191 assertThatThrownBy(() -> {
192 new LegacyProvider().updateGuardPolicy(pfDao, null);
193 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
195 assertThatThrownBy(() -> {
196 new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
197 }).hasMessage("policy type for guard policy \"null\" unknown");
199 LegacyGuardPolicyInput originalGip = standardCoder.decode(
200 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
201 LegacyGuardPolicyInput.class);
203 assertNotNull(originalGip);
205 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
206 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
207 assertEquals(originalGip.getContent(),
208 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
210 Map<String, LegacyGuardPolicyOutput> gotGopm =
211 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
213 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
214 assertEquals(originalGip.getContent(),
215 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
217 originalGip.getContent().setRecipe("Roast Turkey");
218 Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
219 assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
220 assertEquals(originalGip.getContent(),
221 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
223 Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
224 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
225 assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
226 assertEquals(originalGip.getContent(),
227 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
228 assertEquals("Roast Turkey",
229 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
234 public void testPoliciesDelete() throws Exception {
235 assertThatThrownBy(() -> {
236 new LegacyProvider().deleteGuardPolicy(null, null);
237 }).hasMessage("dao is marked @NonNull but is null");
239 assertThatThrownBy(() -> {
240 new LegacyProvider().deleteGuardPolicy(null, "");
241 }).hasMessage("dao is marked @NonNull but is null");
243 assertThatThrownBy(() -> {
244 new LegacyProvider().deleteGuardPolicy(pfDao, null);
245 }).hasMessage("policyId is marked @NonNull but is null");
248 assertThatThrownBy(() -> {
249 new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist");
250 }).hasMessage("no policy found for policy ID: I Dont Exist");
252 LegacyGuardPolicyInput originalGip = standardCoder.decode(
253 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
254 LegacyGuardPolicyInput.class);
256 assertNotNull(originalGip);
258 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
259 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
260 assertEquals(originalGip.getContent(),
261 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
263 Map<String, LegacyGuardPolicyOutput> gotGopm =
264 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
266 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
267 assertEquals(originalGip.getContent(),
268 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
270 String expectedJsonOutput =
271 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
272 String actualJsonOutput = standardCoder.encode(gotGopm);
274 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
276 Map<String, LegacyGuardPolicyOutput> deletedGopm =
277 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId());
278 assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
279 assertEquals(originalGip.getContent(),
280 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
282 assertThatThrownBy(() -> {
283 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
284 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
286 LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
287 otherGip.setPolicyId("guard.blacklist");
288 otherGip.setPolicyVersion("1");
289 otherGip.setContent(new LegacyGuardPolicyContent());
291 Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
292 assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
293 assertEquals(otherGip.getContent(),
294 createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next());
296 assertThatThrownBy(() -> {
297 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
298 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");