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.StandardCoder;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.dao.DaoParameters;
36 import org.onap.policy.models.dao.PfDao;
37 import org.onap.policy.models.dao.PfDaoFactory;
38 import org.onap.policy.models.dao.impl.DefaultPfDao;
39 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
42 * Test the {@link LegacyProvider} class for legacy operational policies.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class LegacyProvider4LegacyOperationalTest {
48 private StandardCoder standardCoder;
51 * Set up the DAO towards the database.
53 * @throws Exception on database errors
56 public void setupDao() throws Exception {
57 final DaoParameters daoParameters = new DaoParameters();
58 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
60 daoParameters.setPersistenceUnit("ToscaConceptTest");
62 Properties jdbcProperties = new Properties();
63 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
64 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
66 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
67 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
68 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
70 daoParameters.setJdbcProperties(jdbcProperties );
72 pfDao = new PfDaoFactory().createPfDao(daoParameters);
73 pfDao.init(daoParameters);
77 * Set up standard coder.
80 public void setupStandardCoder() {
81 standardCoder = new StandardCoder();
85 public void teardown() throws Exception {
90 public void testPoliciesGet() throws Exception {
91 assertThatThrownBy(() -> {
92 new LegacyProvider().getOperationalPolicy(null, null);
93 }).hasMessage("dao is marked @NonNull but is null");
95 assertThatThrownBy(() -> {
96 new LegacyProvider().getOperationalPolicy(null, "");
97 }).hasMessage("dao is marked @NonNull but is null");
99 assertThatThrownBy(() -> {
100 new LegacyProvider().getOperationalPolicy(pfDao, null);
101 }).hasMessage("policyId is marked @NonNull but is null");
103 assertThatThrownBy(() -> {
104 new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist");
105 }).hasMessage("no policy found for policy ID: I Dont Exist");
107 LegacyOperationalPolicy originalLop =
108 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
109 LegacyOperationalPolicy.class);
111 assertNotNull(originalLop);
113 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
115 assertEquals(originalLop, createdLop);
117 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
119 assertEquals(gotLop, originalLop);
121 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
122 String actualJsonOutput = standardCoder.encode(gotLop);
124 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
126 LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
127 LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
128 assertEquals(gotLopV2, createdLopV2);
132 public void testPolicyCreate() throws Exception {
133 assertThatThrownBy(() -> {
134 new LegacyProvider().createOperationalPolicy(null, null);
135 }).hasMessage("dao is marked @NonNull but is null");
137 assertThatThrownBy(() -> {
138 new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
139 }).hasMessage("dao is marked @NonNull but is null");
141 assertThatThrownBy(() -> {
142 new LegacyProvider().createOperationalPolicy(pfDao, null);
143 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
145 LegacyOperationalPolicy originalLop =
146 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
147 LegacyOperationalPolicy.class);
149 assertNotNull(originalLop);
151 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
153 assertEquals(originalLop, createdLop);
155 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
157 assertEquals(gotLop, originalLop);
159 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
160 String actualJsonOutput = standardCoder.encode(gotLop);
162 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
167 public void testPolicyUpdate() throws Exception {
168 assertThatThrownBy(() -> {
169 new LegacyProvider().updateOperationalPolicy(null, null);
170 }).hasMessage("dao is marked @NonNull but is null");
172 assertThatThrownBy(() -> {
173 new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
174 }).hasMessage("dao is marked @NonNull but is null");
176 assertThatThrownBy(() -> {
177 new LegacyProvider().updateOperationalPolicy(pfDao, null);
178 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
180 assertThatThrownBy(() -> {
181 new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
182 }).hasMessage("no policy found for policy ID: null");
184 LegacyOperationalPolicy originalLop =
185 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
186 LegacyOperationalPolicy.class);
188 assertNotNull(originalLop);
190 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
191 assertEquals(originalLop, createdLop);
193 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
194 assertEquals(gotLop, originalLop);
196 originalLop.setContent("Some New Content");
197 LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
198 assertEquals(originalLop, updatedLop);
200 LegacyOperationalPolicy gotUpdatedLop =
201 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
202 assertEquals(gotUpdatedLop, originalLop);
203 assertEquals("Some New Content", gotUpdatedLop.getContent());
208 public void testPoliciesDelete() throws Exception {
209 assertThatThrownBy(() -> {
210 new LegacyProvider().deleteOperationalPolicy(null, null);
211 }).hasMessage("dao is marked @NonNull but is null");
213 assertThatThrownBy(() -> {
214 new LegacyProvider().deleteOperationalPolicy(null, "");
215 }).hasMessage("dao is marked @NonNull but is null");
217 assertThatThrownBy(() -> {
218 new LegacyProvider().deleteOperationalPolicy(pfDao, null);
219 }).hasMessage("policyId is marked @NonNull but is null");
222 assertThatThrownBy(() -> {
223 new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist");
224 }).hasMessage("no policy found for policy ID: I Dont Exist");
226 LegacyOperationalPolicy originalLop =
227 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
228 LegacyOperationalPolicy.class);
230 assertNotNull(originalLop);
232 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
233 assertEquals(originalLop, createdLop);
235 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
237 assertEquals(gotLop, originalLop);
239 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
240 String actualJsonOutput = standardCoder.encode(gotLop);
242 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
244 LegacyOperationalPolicy deletedLop =
245 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId());
246 assertEquals(originalLop, deletedLop);
248 assertThatThrownBy(() -> {
249 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
250 }).hasMessage("no policy found for policy ID: operational.restart");
252 LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
253 otherLop.setPolicyId("another-policy");
254 otherLop.setPolicyVersion("1");
255 otherLop.setContent("content");
257 LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
258 assertEquals(otherLop, createdOtherLop);
260 assertThatThrownBy(() -> {
261 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
262 }).hasMessage("no policy found for policy ID: operational.restart");