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;
27 import java.sql.Connection;
28 import java.sql.DriverManager;
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 {
50 private Connection connection;
52 private StandardCoder standardCoder;
56 * Set up the DAO towards the database.
58 * @throws Exception on database errors
61 public void setupDao() throws Exception {
62 // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
63 // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
64 connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
66 final DaoParameters daoParameters = new DaoParameters();
67 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
69 // Use the persistence unit ToscaConceptTest to test towards the h2 database
70 // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
71 daoParameters.setPersistenceUnit("ToscaConceptTest");
73 pfDao = new PfDaoFactory().createPfDao(daoParameters);
74 pfDao.init(daoParameters);
78 * Set up standard coder.
81 public void setupStandardCoder() {
82 standardCoder = new StandardCoder();
86 public void teardown() throws Exception {
92 public void testPoliciesGet() throws Exception {
93 assertThatThrownBy(() -> {
94 new LegacyProvider().getGuardPolicy(null, null);
95 }).hasMessage("dao is marked @NonNull but is null");
97 assertThatThrownBy(() -> {
98 new LegacyProvider().getGuardPolicy(null, "");
99 }).hasMessage("dao is marked @NonNull but is null");
101 assertThatThrownBy(() -> {
102 new LegacyProvider().getGuardPolicy(pfDao, null);
103 }).hasMessage("policyId is marked @NonNull but is null");
105 assertThatThrownBy(() -> {
106 new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist");
107 }).hasMessage("no policy found for policy ID: I Dont Exist");
109 LegacyGuardPolicyInput originalGip = standardCoder.decode(
110 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
111 LegacyGuardPolicyInput.class);
113 assertNotNull(originalGip);
115 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
117 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
118 assertEquals(originalGip.getContent(),
119 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
121 Map<String, LegacyGuardPolicyOutput> gotGopm =
122 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
124 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
125 assertEquals(originalGip.getContent(),
126 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
128 String expectedJsonOutput =
129 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
130 String actualJsonOutput = standardCoder.encode(gotGopm);
132 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
136 public void testPolicyCreate() throws Exception {
137 assertThatThrownBy(() -> {
138 new LegacyProvider().createGuardPolicy(null, null);
139 }).hasMessage("dao is marked @NonNull but is null");
141 assertThatThrownBy(() -> {
142 new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
143 }).hasMessage("dao is marked @NonNull but is null");
145 assertThatThrownBy(() -> {
146 new LegacyProvider().createGuardPolicy(pfDao, null);
147 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
149 LegacyGuardPolicyInput originalGip = standardCoder.decode(
150 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
151 LegacyGuardPolicyInput.class);
153 assertNotNull(originalGip);
155 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
157 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
158 assertEquals(originalGip.getContent(),
159 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
161 Map<String, LegacyGuardPolicyOutput> gotGopm =
162 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
164 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
165 assertEquals(originalGip.getContent(),
166 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
168 String expectedJsonOutput =
169 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
170 String actualJsonOutput = standardCoder.encode(gotGopm);
172 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
177 public void testPolicyUpdate() throws Exception {
178 assertThatThrownBy(() -> {
179 new LegacyProvider().updateGuardPolicy(null, null);
180 }).hasMessage("dao is marked @NonNull but is null");
182 assertThatThrownBy(() -> {
183 new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
184 }).hasMessage("dao is marked @NonNull but is null");
186 assertThatThrownBy(() -> {
187 new LegacyProvider().updateGuardPolicy(pfDao, null);
188 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
190 assertThatThrownBy(() -> {
191 new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
192 }).hasMessage("policy type for guard policy \"null\" unknown");
194 LegacyGuardPolicyInput originalGip = standardCoder.decode(
195 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
196 LegacyGuardPolicyInput.class);
198 assertNotNull(originalGip);
200 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
201 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
202 assertEquals(originalGip.getContent(),
203 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
205 Map<String, LegacyGuardPolicyOutput> gotGopm =
206 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
208 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
209 assertEquals(originalGip.getContent(),
210 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
212 originalGip.getContent().setRecipe("Roast Turkey");
213 Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
214 assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
215 assertEquals(originalGip.getContent(),
216 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
218 Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
219 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
220 assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
221 assertEquals(originalGip.getContent(),
222 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
223 assertEquals("Roast Turkey",
224 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
229 public void testPoliciesDelete() throws Exception {
230 assertThatThrownBy(() -> {
231 new LegacyProvider().deleteGuardPolicy(null, null);
232 }).hasMessage("dao is marked @NonNull but is null");
234 assertThatThrownBy(() -> {
235 new LegacyProvider().deleteGuardPolicy(null, "");
236 }).hasMessage("dao is marked @NonNull but is null");
238 assertThatThrownBy(() -> {
239 new LegacyProvider().deleteGuardPolicy(pfDao, null);
240 }).hasMessage("policyId is marked @NonNull but is null");
243 assertThatThrownBy(() -> {
244 new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist");
245 }).hasMessage("no policy found for policy ID: I Dont Exist");
247 LegacyGuardPolicyInput originalGip = standardCoder.decode(
248 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
249 LegacyGuardPolicyInput.class);
251 assertNotNull(originalGip);
253 Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
254 assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
255 assertEquals(originalGip.getContent(),
256 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
258 Map<String, LegacyGuardPolicyOutput> gotGopm =
259 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
261 assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
262 assertEquals(originalGip.getContent(),
263 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
265 String expectedJsonOutput =
266 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
267 String actualJsonOutput = standardCoder.encode(gotGopm);
269 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
271 Map<String, LegacyGuardPolicyOutput> deletedGopm =
272 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId());
273 assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
274 assertEquals(originalGip.getContent(),
275 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
277 assertThatThrownBy(() -> {
278 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
279 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
281 LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
282 otherGip.setPolicyId("guard.blacklist");
283 otherGip.setPolicyVersion("1");
284 otherGip.setContent(new LegacyGuardPolicyContent());
286 Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
287 assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
288 assertEquals(otherGip.getContent(),
289 createdOtherGopm.get(otherGip.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");