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.util.Properties;
29 import org.eclipse.persistence.config.PersistenceUnitProperties;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.dao.DaoParameters;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.dao.PfDaoFactory;
40 import org.onap.policy.models.dao.impl.DefaultPfDao;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
43 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
44 import org.yaml.snakeyaml.Yaml;
47 * Test the {@link LegacyProvider} class for legacy operational policies.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class LegacyProvider4LegacyOperationalTest {
53 private StandardCoder standardCoder;
56 * Set up the DAO towards the database.
58 * @throws Exception on database errors
61 public void setupDao() throws Exception {
62 final DaoParameters daoParameters = new DaoParameters();
63 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
65 daoParameters.setPersistenceUnit("ToscaConceptTest");
67 Properties jdbcProperties = new Properties();
68 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
69 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
71 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
72 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
73 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
75 daoParameters.setJdbcProperties(jdbcProperties);
77 pfDao = new PfDaoFactory().createPfDao(daoParameters);
78 pfDao.init(daoParameters);
82 * Set up standard coder.
85 public void setupStandardCoder() {
86 standardCoder = new StandardCoder();
90 public void teardown() throws Exception {
95 public void testPoliciesGet() throws Exception {
96 assertThatThrownBy(() -> {
97 new LegacyProvider().getOperationalPolicy(null, null, null);
98 }).hasMessage("dao is marked @NonNull but is null");
100 assertThatThrownBy(() -> {
101 new LegacyProvider().getOperationalPolicy(null, "", null);
102 }).hasMessage("dao is marked @NonNull but is null");
104 assertThatThrownBy(() -> {
105 new LegacyProvider().getOperationalPolicy(pfDao, null, null);
106 }).hasMessage("policyId is marked @NonNull but is null");
108 assertThatThrownBy(() -> {
109 new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist", null);
110 }).hasMessage("no policy found for policy: I Dont Exist:null");
114 LegacyOperationalPolicy originalLop =
115 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
116 LegacyOperationalPolicy.class);
118 assertNotNull(originalLop);
120 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
122 assertEquals(originalLop, createdLop);
124 LegacyOperationalPolicy gotLop =
125 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
127 assertEquals(gotLop, originalLop);
129 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
130 String actualJsonOutput = standardCoder.encode(gotLop);
132 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
134 LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
135 LegacyOperationalPolicy gotLopV2 =
136 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
137 assertEquals(gotLopV2, createdLopV2);
141 public void testPolicyCreate() throws Exception {
142 assertThatThrownBy(() -> {
143 new LegacyProvider().createOperationalPolicy(null, null);
144 }).hasMessage("dao is marked @NonNull but is null");
146 assertThatThrownBy(() -> {
147 new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
148 }).hasMessage("dao is marked @NonNull but is null");
150 assertThatThrownBy(() -> {
151 new LegacyProvider().createOperationalPolicy(pfDao, null);
152 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
156 LegacyOperationalPolicy originalLop =
157 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
158 LegacyOperationalPolicy.class);
160 assertNotNull(originalLop);
162 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
164 assertEquals(originalLop, createdLop);
166 LegacyOperationalPolicy gotLop =
167 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
169 assertEquals(gotLop, originalLop);
171 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
172 String actualJsonOutput = standardCoder.encode(gotLop);
174 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
178 public void testPolicyUpdate() throws Exception {
179 assertThatThrownBy(() -> {
180 new LegacyProvider().updateOperationalPolicy(null, null);
181 }).hasMessage("dao is marked @NonNull but is null");
183 assertThatThrownBy(() -> {
184 new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
185 }).hasMessage("dao is marked @NonNull but is null");
187 assertThatThrownBy(() -> {
188 new LegacyProvider().updateOperationalPolicy(pfDao, null);
189 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
191 assertThatThrownBy(() -> {
192 new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
193 }).hasMessage("name is marked @NonNull but is null");
197 LegacyOperationalPolicy originalLop =
198 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
199 LegacyOperationalPolicy.class);
201 assertNotNull(originalLop);
203 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
204 assertEquals(originalLop, createdLop);
206 LegacyOperationalPolicy gotLop =
207 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
208 assertEquals(gotLop, originalLop);
210 originalLop.setContent("Some New Content");
211 LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
212 assertEquals(originalLop, updatedLop);
214 LegacyOperationalPolicy gotUpdatedLop =
215 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
216 assertEquals(gotUpdatedLop, originalLop);
217 assertEquals("Some New Content", gotUpdatedLop.getContent());
221 public void testPoliciesDelete() throws Exception {
222 assertThatThrownBy(() -> {
223 new LegacyProvider().deleteOperationalPolicy(null, null, null);
224 }).hasMessage("dao is marked @NonNull but is null");
226 assertThatThrownBy(() -> {
227 new LegacyProvider().deleteOperationalPolicy(null, null, "");
229 }).hasMessage("dao is marked @NonNull but is null");
231 assertThatThrownBy(() -> {
232 new LegacyProvider().deleteOperationalPolicy(null, "", null);
233 }).hasMessage("dao is marked @NonNull but is null");
235 assertThatThrownBy(() -> {
236 new LegacyProvider().deleteOperationalPolicy(null, "", "");
238 }).hasMessage("dao is marked @NonNull but is null");
240 assertThatThrownBy(() -> {
241 new LegacyProvider().deleteOperationalPolicy(pfDao, null, null);
242 }).hasMessage("policyId is marked @NonNull but is null");
244 assertThatThrownBy(() -> {
245 new LegacyProvider().deleteOperationalPolicy(pfDao, null, "");
246 }).hasMessage("policyId is marked @NonNull but is null");
248 assertThatThrownBy(() -> {
249 new LegacyProvider().deleteOperationalPolicy(pfDao, "", null);
250 }).hasMessage("policyVersion is marked @NonNull but is null");
252 assertThatThrownBy(() -> {
253 new LegacyProvider().deleteOperationalPolicy(pfDao, "IDontExist", "");
254 }).hasMessage("no policy found for policy: IDontExist:");
258 LegacyOperationalPolicy originalLop =
259 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
260 LegacyOperationalPolicy.class);
262 assertNotNull(originalLop);
264 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
265 assertEquals(originalLop, createdLop);
267 LegacyOperationalPolicy gotLop =
268 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
270 assertEquals(gotLop, originalLop);
272 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
273 String actualJsonOutput = standardCoder.encode(gotLop);
275 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
277 assertThatThrownBy(() -> {
278 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
279 }).hasMessage("policyVersion is marked @NonNull but is null");
281 LegacyOperationalPolicy deletedLop =
282 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), "1");
283 assertEquals(originalLop, deletedLop);
285 assertThatThrownBy(() -> {
286 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
287 }).hasMessage("no policy found for policy: operational.restart:null");
289 LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
290 otherLop.setPolicyId("another-policy");
291 otherLop.setPolicyVersion("1");
292 otherLop.setContent("content");
294 LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
295 assertEquals(otherLop, createdOtherLop);
297 assertThatThrownBy(() -> {
298 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
299 }).hasMessage("no policy found for policy: operational.restart:null");
302 private void createPolicyTypes() throws CoderException, PfModelException {
303 Object yamlObject = new Yaml()
304 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
305 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
307 ToscaServiceTemplate toscaServiceTemplatePolicyType =
308 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
310 assertNotNull(toscaServiceTemplatePolicyType);
311 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);