2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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 @NonNull but is null";
54 private static final String VCPE_OUTPUT_JSON = "policies/vCPE.policy.operational.output.json";
55 private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.operational.input.json";
56 private static final String DAO_IS_NULL = "dao is marked @NonNull 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.getCanonicalName());
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 }).hasMessage(DAO_IS_NULL);
105 assertThatThrownBy(() -> {
106 new LegacyProvider().getOperationalPolicy(null, "", null);
107 }).hasMessage(DAO_IS_NULL);
109 assertThatThrownBy(() -> {
110 new LegacyProvider().getOperationalPolicy(pfDao, null, null);
111 }).hasMessage(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),
121 LegacyOperationalPolicy.class);
123 assertNotNull(originalLop);
125 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
127 assertEquals(originalLop, createdLop);
129 LegacyOperationalPolicy gotLop =
130 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
132 assertEquals(gotLop, originalLop);
134 String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
135 String actualJsonOutput = standardCoder.encode(gotLop);
137 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
139 LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
140 LegacyOperationalPolicy gotLopV2 =
141 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
142 assertEquals(gotLopV2, createdLopV2);
146 public void testPolicyCreate() throws Exception {
147 assertThatThrownBy(() -> {
148 new LegacyProvider().createOperationalPolicy(null, null);
149 }).hasMessage(DAO_IS_NULL);
151 assertThatThrownBy(() -> {
152 new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
153 }).hasMessage(DAO_IS_NULL);
155 assertThatThrownBy(() -> {
156 new LegacyProvider().createOperationalPolicy(pfDao, null);
157 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
161 LegacyOperationalPolicy originalLop =
162 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
163 LegacyOperationalPolicy.class);
165 assertNotNull(originalLop);
167 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
169 assertEquals(originalLop, createdLop);
171 LegacyOperationalPolicy gotLop =
172 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
174 assertEquals(gotLop, originalLop);
176 String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
177 String actualJsonOutput = standardCoder.encode(gotLop);
179 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
183 public void testPolicyUpdate() throws Exception {
184 assertThatThrownBy(() -> {
185 new LegacyProvider().updateOperationalPolicy(null, null);
186 }).hasMessage(DAO_IS_NULL);
188 assertThatThrownBy(() -> {
189 new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
190 }).hasMessage(DAO_IS_NULL);
192 assertThatThrownBy(() -> {
193 new LegacyProvider().updateOperationalPolicy(pfDao, null);
194 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
196 assertThatThrownBy(() -> {
197 new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
198 }).hasMessage("name is marked @NonNull but is null");
202 LegacyOperationalPolicy originalLop =
203 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
204 LegacyOperationalPolicy.class);
206 assertNotNull(originalLop);
208 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
209 assertEquals(originalLop, createdLop);
211 LegacyOperationalPolicy gotLop =
212 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
213 assertEquals(gotLop, originalLop);
215 originalLop.setContent("Some New Content");
216 LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
217 assertEquals(originalLop, updatedLop);
219 LegacyOperationalPolicy gotUpdatedLop =
220 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
221 assertEquals(gotUpdatedLop, originalLop);
222 assertEquals("Some New Content", gotUpdatedLop.getContent());
226 public void testPoliciesDelete() throws Exception {
227 assertThatThrownBy(() -> {
228 new LegacyProvider().deleteOperationalPolicy(null, null, null);
229 }).hasMessage(DAO_IS_NULL);
231 assertThatThrownBy(() -> {
232 new LegacyProvider().deleteOperationalPolicy(null, null, "");
234 }).hasMessage(DAO_IS_NULL);
236 assertThatThrownBy(() -> {
237 new LegacyProvider().deleteOperationalPolicy(null, "", null);
238 }).hasMessage(DAO_IS_NULL);
240 assertThatThrownBy(() -> {
241 new LegacyProvider().deleteOperationalPolicy(null, "", "");
243 }).hasMessage(DAO_IS_NULL);
245 assertThatThrownBy(() -> {
246 new LegacyProvider().deleteOperationalPolicy(pfDao, null, null);
247 }).hasMessage(POLICY_ID_IS_NULL);
249 assertThatThrownBy(() -> {
250 new LegacyProvider().deleteOperationalPolicy(pfDao, null, "");
251 }).hasMessage(POLICY_ID_IS_NULL);
253 assertThatThrownBy(() -> {
254 new LegacyProvider().deleteOperationalPolicy(pfDao, "", null);
255 }).hasMessage("policyVersion is marked @NonNull but is null");
257 assertThatThrownBy(() -> {
258 new LegacyProvider().deleteOperationalPolicy(pfDao, "IDontExist", "0");
259 }).hasMessage("no policy found for policy: IDontExist:0");
263 LegacyOperationalPolicy originalLop =
264 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
265 LegacyOperationalPolicy.class);
267 assertNotNull(originalLop);
269 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
270 assertEquals(originalLop, createdLop);
272 LegacyOperationalPolicy gotLop =
273 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
275 assertEquals(gotLop, originalLop);
277 String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
278 String actualJsonOutput = standardCoder.encode(gotLop);
280 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
282 assertThatThrownBy(() -> {
283 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
284 }).hasMessage("policyVersion is marked @NonNull but is null");
286 LegacyOperationalPolicy deletedLop =
287 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), "1");
288 assertEquals(originalLop, deletedLop);
290 assertThatThrownBy(() -> {
291 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
292 }).hasMessage("no policy found for policy: operational.restart:null");
294 LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
295 otherLop.setPolicyId("another-policy");
296 otherLop.setPolicyVersion("1");
297 otherLop.setContent("content");
299 LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
300 assertEquals(otherLop, createdOtherLop);
302 assertThatThrownBy(() -> {
303 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
304 }).hasMessage("no policy found for policy: operational.restart:null");
307 private void createPolicyTypes() throws CoderException, PfModelException {
308 Object yamlObject = new Yaml()
309 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
310 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
312 ToscaServiceTemplate toscaServiceTemplatePolicyType =
313 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
315 assertNotNull(toscaServiceTemplatePolicyType);
316 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);