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);
98 }).hasMessage("dao is marked @NonNull but is null");
100 assertThatThrownBy(() -> {
101 new LegacyProvider().getOperationalPolicy(null, "");
102 }).hasMessage("dao is marked @NonNull but is null");
104 assertThatThrownBy(() -> {
105 new LegacyProvider().getOperationalPolicy(pfDao, null);
106 }).hasMessage("policyId is marked @NonNull but is null");
108 assertThatThrownBy(() -> {
109 new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist");
110 }).hasMessage("no policy found for policy ID: I Dont Exist");
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 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
126 assertEquals(gotLop, originalLop);
128 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
129 String actualJsonOutput = standardCoder.encode(gotLop);
131 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
133 LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
134 LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
135 assertEquals(gotLopV2, createdLopV2);
139 public void testPolicyCreate() throws Exception {
140 assertThatThrownBy(() -> {
141 new LegacyProvider().createOperationalPolicy(null, null);
142 }).hasMessage("dao is marked @NonNull but is null");
144 assertThatThrownBy(() -> {
145 new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
146 }).hasMessage("dao is marked @NonNull but is null");
148 assertThatThrownBy(() -> {
149 new LegacyProvider().createOperationalPolicy(pfDao, null);
150 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
154 LegacyOperationalPolicy originalLop =
155 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
156 LegacyOperationalPolicy.class);
158 assertNotNull(originalLop);
160 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
162 assertEquals(originalLop, createdLop);
164 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
166 assertEquals(gotLop, originalLop);
168 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
169 String actualJsonOutput = standardCoder.encode(gotLop);
171 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
175 public void testPolicyUpdate() throws Exception {
176 assertThatThrownBy(() -> {
177 new LegacyProvider().updateOperationalPolicy(null, null);
178 }).hasMessage("dao is marked @NonNull but is null");
180 assertThatThrownBy(() -> {
181 new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
182 }).hasMessage("dao is marked @NonNull but is null");
184 assertThatThrownBy(() -> {
185 new LegacyProvider().updateOperationalPolicy(pfDao, null);
186 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
188 assertThatThrownBy(() -> {
189 new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
190 }).hasMessage("no policy found for policy ID: null");
194 LegacyOperationalPolicy originalLop =
195 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
196 LegacyOperationalPolicy.class);
198 assertNotNull(originalLop);
200 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
201 assertEquals(originalLop, createdLop);
203 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
204 assertEquals(gotLop, originalLop);
206 originalLop.setContent("Some New Content");
207 LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
208 assertEquals(originalLop, updatedLop);
210 LegacyOperationalPolicy gotUpdatedLop =
211 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
212 assertEquals(gotUpdatedLop, originalLop);
213 assertEquals("Some New Content", gotUpdatedLop.getContent());
217 public void testPoliciesDelete() throws Exception {
218 assertThatThrownBy(() -> {
219 new LegacyProvider().deleteOperationalPolicy(null, null);
220 }).hasMessage("dao is marked @NonNull but is null");
222 assertThatThrownBy(() -> {
223 new LegacyProvider().deleteOperationalPolicy(null, "");
225 }).hasMessage("dao is marked @NonNull but is null");
227 assertThatThrownBy(() -> {
228 new LegacyProvider().deleteOperationalPolicy(pfDao, null);
229 }).hasMessage("policyId is marked @NonNull but is null");
231 assertThatThrownBy(() -> {
232 new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist");
233 }).hasMessage("no policy found for policy ID: I Dont Exist");
237 LegacyOperationalPolicy originalLop =
238 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
239 LegacyOperationalPolicy.class);
241 assertNotNull(originalLop);
243 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
244 assertEquals(originalLop, createdLop);
246 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
248 assertEquals(gotLop, originalLop);
250 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
251 String actualJsonOutput = standardCoder.encode(gotLop);
253 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
255 LegacyOperationalPolicy deletedLop =
256 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId());
257 assertEquals(originalLop, deletedLop);
259 assertThatThrownBy(() -> {
260 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
261 }).hasMessage("no policy found for policy ID: operational.restart");
263 LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
264 otherLop.setPolicyId("another-policy");
265 otherLop.setPolicyVersion("1");
266 otherLop.setContent("content");
268 LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
269 assertEquals(otherLop, createdOtherLop);
271 assertThatThrownBy(() -> {
272 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
273 }).hasMessage("no policy found for policy ID: operational.restart");
276 private void createPolicyTypes() throws CoderException, PfModelException {
277 Object yamlObject = new Yaml().load(
278 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
279 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
281 ToscaServiceTemplate toscaServiceTemplatePolicyType =
282 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
284 assertNotNull(toscaServiceTemplatePolicyType);
285 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);