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;
30 import org.eclipse.persistence.config.PersistenceUnitProperties;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.dao.DaoParameters;
39 import org.onap.policy.models.dao.PfDao;
40 import org.onap.policy.models.dao.PfDaoFactory;
41 import org.onap.policy.models.dao.impl.DefaultPfDao;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
44 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
45 import org.yaml.snakeyaml.Yaml;
48 * Test the {@link LegacyProvider} class for legacy operational policies.
50 * @author Liam Fallon (liam.fallon@est.tech)
52 public class LegacyProvider4LegacyOperationalTest {
53 private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$";
54 private static final String VCPE_OUTPUT_JSON = "policies/vCPE.policy.operational.legacy.output.json";
55 private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.operational.legacy.input.json";
56 private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
58 private StandardCoder standardCoder;
61 * Set up the DAO towards the database.
63 * @throws Exception on database errors
66 public void setupDao() throws Exception {
67 final DaoParameters daoParameters = new DaoParameters();
68 daoParameters.setPluginClass(DefaultPfDao.class.getName());
70 daoParameters.setPersistenceUnit("ToscaConceptTest");
72 Properties jdbcProperties = new Properties();
73 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
74 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
76 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
77 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
78 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
80 daoParameters.setJdbcProperties(jdbcProperties);
82 pfDao = new PfDaoFactory().createPfDao(daoParameters);
83 pfDao.init(daoParameters);
87 * Set up standard coder.
90 public void setupStandardCoder() {
91 standardCoder = new StandardCoder();
95 public void teardown() {
100 public void testPoliciesGet() throws Exception {
101 assertThatThrownBy(() -> {
102 new LegacyProvider().getOperationalPolicy(null, null, null);
103 }).hasMessageMatching(DAO_IS_NULL);
105 assertThatThrownBy(() -> {
106 new LegacyProvider().getOperationalPolicy(null, "", null);
107 }).hasMessageMatching(DAO_IS_NULL);
109 assertThatThrownBy(() -> {
110 new LegacyProvider().getOperationalPolicy(pfDao, null, null);
111 }).hasMessageMatching(POLICY_ID_IS_NULL);
113 assertThatThrownBy(() -> {
114 new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist", null);
115 }).hasMessage("no policy found for policy: I Dont Exist:null");
119 LegacyOperationalPolicy originalLop =
120 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
122 assertNotNull(originalLop);
124 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
126 assertEquals(originalLop, createdLop);
128 LegacyOperationalPolicy gotLop =
129 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
131 assertEquals(gotLop, originalLop);
133 String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
134 String actualJsonOutput = standardCoder.encode(gotLop);
136 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
138 LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
139 LegacyOperationalPolicy gotLopV2 =
140 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
141 assertEquals(gotLopV2, createdLopV2);
145 public void testPolicyCreate() throws Exception {
146 assertThatThrownBy(() -> {
147 new LegacyProvider().createOperationalPolicy(null, null);
148 }).hasMessageMatching(DAO_IS_NULL);
150 assertThatThrownBy(() -> {
151 new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
152 }).hasMessageMatching(DAO_IS_NULL);
154 assertThatThrownBy(() -> {
155 new LegacyProvider().createOperationalPolicy(pfDao, null);
156 }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
160 LegacyOperationalPolicy originalLop =
161 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
163 assertNotNull(originalLop);
165 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
167 assertEquals(originalLop, createdLop);
169 LegacyOperationalPolicy gotLop =
170 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
172 assertEquals(gotLop, originalLop);
174 String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
175 String actualJsonOutput = standardCoder.encode(gotLop);
177 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
181 public void testPolicyUpdate() throws Exception {
182 assertThatThrownBy(() -> {
183 new LegacyProvider().updateOperationalPolicy(null, null);
184 }).hasMessageMatching(DAO_IS_NULL);
186 assertThatThrownBy(() -> {
187 new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
188 }).hasMessageMatching(DAO_IS_NULL);
190 assertThatThrownBy(() -> {
191 new LegacyProvider().updateOperationalPolicy(pfDao, null);
192 }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
194 assertThatThrownBy(() -> {
195 new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
196 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
200 LegacyOperationalPolicy originalLop =
201 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
203 assertNotNull(originalLop);
205 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
206 assertEquals(originalLop, createdLop);
208 LegacyOperationalPolicy gotLop =
209 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
210 assertEquals(gotLop, originalLop);
212 originalLop.setContent("Some New Content");
213 LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
214 assertEquals(originalLop, updatedLop);
216 LegacyOperationalPolicy gotUpdatedLop =
217 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
218 assertEquals(gotUpdatedLop, originalLop);
219 assertEquals("Some New Content", gotUpdatedLop.getContent());
223 public void testPoliciesDelete() throws Exception {
224 assertThatThrownBy(() -> {
225 new LegacyProvider().deleteOperationalPolicy(null, null, null);
226 }).hasMessageMatching(DAO_IS_NULL);
228 assertThatThrownBy(() -> {
229 new LegacyProvider().deleteOperationalPolicy(null, null, "");
231 }).hasMessageMatching(DAO_IS_NULL);
233 assertThatThrownBy(() -> {
234 new LegacyProvider().deleteOperationalPolicy(null, "", null);
235 }).hasMessageMatching(DAO_IS_NULL);
237 assertThatThrownBy(() -> {
238 new LegacyProvider().deleteOperationalPolicy(null, "", "");
239 }).hasMessageMatching(DAO_IS_NULL);
241 assertThatThrownBy(() -> {
242 new LegacyProvider().deleteOperationalPolicy(pfDao, null, null);
243 }).hasMessageMatching(POLICY_ID_IS_NULL);
245 assertThatThrownBy(() -> {
246 new LegacyProvider().deleteOperationalPolicy(pfDao, null, "");
247 }).hasMessageMatching(POLICY_ID_IS_NULL);
249 assertThatThrownBy(() -> {
250 new LegacyProvider().deleteOperationalPolicy(pfDao, "", null);
251 }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
253 assertThatThrownBy(() -> {
254 new LegacyProvider().deleteOperationalPolicy(pfDao, "IDontExist", "0");
255 }).hasMessage("no policy found for policy: IDontExist:0");
259 LegacyOperationalPolicy originalLop =
260 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), 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(VCPE_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 }).hasMessageMatching("^policyVersion is marked .*on.*ull 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);