2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.legacy.provider;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
28 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 {
52 private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$";
53 private static final String VCPE_OUTPUT_JSON = "policies/vCPE.policy.operational.legacy.output.json";
54 private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.operational.legacy.input.json";
55 private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
57 private StandardCoder standardCoder;
60 * Set up the DAO towards the database.
62 * @throws Exception on database errors
65 public void setupDao() throws Exception {
66 final DaoParameters daoParameters = new DaoParameters();
67 daoParameters.setPluginClass(DefaultPfDao.class.getName());
69 daoParameters.setPersistenceUnit("ToscaConceptTest");
71 Properties jdbcProperties = new Properties();
72 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
73 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
75 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
76 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
77 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
79 daoParameters.setJdbcProperties(jdbcProperties);
81 pfDao = new PfDaoFactory().createPfDao(daoParameters);
82 pfDao.init(daoParameters);
86 * Set up standard coder.
89 public void setupStandardCoder() {
90 standardCoder = new StandardCoder();
94 public void teardown() {
99 public void testPoliciesGet() throws Exception {
100 assertThatThrownBy(() -> {
101 new LegacyProvider().getOperationalPolicy(null, null, null);
102 }).hasMessageMatching(DAO_IS_NULL);
104 assertThatThrownBy(() -> {
105 new LegacyProvider().getOperationalPolicy(null, "", null);
106 }).hasMessageMatching(DAO_IS_NULL);
108 assertThatThrownBy(() -> {
109 new LegacyProvider().getOperationalPolicy(pfDao, null, null);
110 }).hasMessageMatching(POLICY_ID_IS_NULL);
112 assertThatThrownBy(() -> {
113 new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist", null);
114 }).hasMessage("service template not found in database");
118 LegacyOperationalPolicy originalLop =
119 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
121 assertNotNull(originalLop);
123 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
125 assertEquals(originalLop, createdLop);
127 LegacyOperationalPolicy gotLop =
128 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
130 assertEquals(gotLop, originalLop);
132 String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
133 String actualJsonOutput = standardCoder.encode(gotLop);
135 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
137 LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
138 LegacyOperationalPolicy gotLopV2 =
139 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
140 assertEquals(gotLopV2, createdLopV2);
144 public void testPolicyCreate() throws Exception {
145 assertThatThrownBy(() -> {
146 new LegacyProvider().createOperationalPolicy(null, null);
147 }).hasMessageMatching(DAO_IS_NULL);
149 assertThatThrownBy(() -> {
150 new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
151 }).hasMessageMatching(DAO_IS_NULL);
153 assertThatThrownBy(() -> {
154 new LegacyProvider().createOperationalPolicy(pfDao, null);
155 }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
159 LegacyOperationalPolicy originalLop =
160 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
162 assertNotNull(originalLop);
164 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
166 assertEquals(originalLop, createdLop);
168 LegacyOperationalPolicy gotLop =
169 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
171 assertEquals(gotLop, originalLop);
173 String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
174 String actualJsonOutput = standardCoder.encode(gotLop);
176 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
180 public void testPolicyUpdate() throws Exception {
181 assertThatThrownBy(() -> {
182 new LegacyProvider().updateOperationalPolicy(null, null);
183 }).hasMessageMatching(DAO_IS_NULL);
185 assertThatThrownBy(() -> {
186 new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
187 }).hasMessageMatching(DAO_IS_NULL);
189 assertThatThrownBy(() -> {
190 new LegacyProvider().updateOperationalPolicy(pfDao, null);
191 }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
193 assertThatThrownBy(() -> {
194 new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
195 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
199 LegacyOperationalPolicy originalLop =
200 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
202 assertNotNull(originalLop);
204 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
205 assertEquals(originalLop, createdLop);
207 LegacyOperationalPolicy gotLop =
208 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
209 assertEquals(gotLop, originalLop);
211 originalLop.setContent("Some New Content");
212 LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
213 assertEquals(originalLop, updatedLop);
215 LegacyOperationalPolicy gotUpdatedLop =
216 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
217 assertEquals(gotUpdatedLop, originalLop);
218 assertEquals("Some New Content", gotUpdatedLop.getContent());
222 public void testPoliciesDelete() throws Exception {
223 assertThatThrownBy(() -> {
224 new LegacyProvider().deleteOperationalPolicy(null, null, null);
225 }).hasMessageMatching(DAO_IS_NULL);
227 assertThatThrownBy(() -> {
228 new LegacyProvider().deleteOperationalPolicy(null, null, "");
230 }).hasMessageMatching(DAO_IS_NULL);
232 assertThatThrownBy(() -> {
233 new LegacyProvider().deleteOperationalPolicy(null, "", null);
234 }).hasMessageMatching(DAO_IS_NULL);
236 assertThatThrownBy(() -> {
237 new LegacyProvider().deleteOperationalPolicy(null, "", "");
238 }).hasMessageMatching(DAO_IS_NULL);
240 assertThatThrownBy(() -> {
241 new LegacyProvider().deleteOperationalPolicy(pfDao, null, null);
242 }).hasMessageMatching(POLICY_ID_IS_NULL);
244 assertThatThrownBy(() -> {
245 new LegacyProvider().deleteOperationalPolicy(pfDao, null, "");
246 }).hasMessageMatching(POLICY_ID_IS_NULL);
248 assertThatThrownBy(() -> {
249 new LegacyProvider().deleteOperationalPolicy(pfDao, "", null);
250 }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
252 assertThatThrownBy(() -> {
253 new LegacyProvider().deleteOperationalPolicy(pfDao, "IDontExist", "0");
254 }).hasMessage("service template not found in database");
258 LegacyOperationalPolicy originalLop =
259 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
261 assertNotNull(originalLop);
263 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
264 assertEquals(originalLop, createdLop);
266 LegacyOperationalPolicy gotLop =
267 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
269 assertEquals(gotLop, originalLop);
271 String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
272 String actualJsonOutput = standardCoder.encode(gotLop);
274 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
276 assertThatThrownBy(() -> {
277 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
278 }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
280 LegacyOperationalPolicy deletedLop =
281 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), "1");
282 assertEquals(originalLop, deletedLop);
284 assertThatThrownBy(() -> {
285 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
286 }).hasMessage("policies for operational.restart:null do not exist");
288 LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
289 otherLop.setPolicyId("another-policy");
290 otherLop.setPolicyVersion("1");
291 otherLop.setContent("content");
293 LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
294 assertEquals(otherLop, createdOtherLop);
296 assertThatThrownBy(() -> {
297 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
298 }).hasMessage("policies for operational.restart:null do not exist");
301 private void createPolicyTypes() throws CoderException, PfModelException {
302 Object yamlObject = new Yaml()
303 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
304 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
306 ToscaServiceTemplate toscaServiceTemplatePolicyType =
307 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
309 assertNotNull(toscaServiceTemplatePolicyType);
310 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);