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.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.coder.StandardCoder;
33 import org.onap.policy.common.utils.resources.ResourceUtils;
34 import org.onap.policy.models.dao.DaoParameters;
35 import org.onap.policy.models.dao.PfDao;
36 import org.onap.policy.models.dao.PfDaoFactory;
37 import org.onap.policy.models.dao.impl.DefaultPfDao;
38 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
41 * Test the {@link LegacyProvider} class for legacy operational policies.
43 * @author Liam Fallon (liam.fallon@est.tech)
45 public class LegacyProvider4LegacyOperationalTest {
47 private StandardCoder standardCoder;
50 * Set up the DAO towards the database.
52 * @throws Exception on database errors
55 public void setupDao() throws Exception {
56 final DaoParameters daoParameters = new DaoParameters();
57 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
59 daoParameters.setPersistenceUnit("ToscaConceptTest");
61 Properties jdbcProperties = new Properties();
62 jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
63 jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
66 jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
67 jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:testdb");
70 //jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
71 //jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
73 daoParameters.setJdbcProperties(jdbcProperties );
75 pfDao = new PfDaoFactory().createPfDao(daoParameters);
76 pfDao.init(daoParameters);
80 * Set up standard coder.
83 public void setupStandardCoder() {
84 standardCoder = new StandardCoder();
88 public void teardown() throws Exception {
93 public void testPoliciesGet() throws Exception {
94 assertThatThrownBy(() -> {
95 new LegacyProvider().getOperationalPolicy(null, null);
96 }).hasMessage("dao is marked @NonNull but is null");
98 assertThatThrownBy(() -> {
99 new LegacyProvider().getOperationalPolicy(null, "");
100 }).hasMessage("dao is marked @NonNull but is null");
102 assertThatThrownBy(() -> {
103 new LegacyProvider().getOperationalPolicy(pfDao, null);
104 }).hasMessage("policyId is marked @NonNull but is null");
106 assertThatThrownBy(() -> {
107 new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist");
108 }).hasMessage("no policy found for policy ID: I Dont Exist");
110 LegacyOperationalPolicy originalLop =
111 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
112 LegacyOperationalPolicy.class);
114 assertNotNull(originalLop);
116 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
118 assertEquals(originalLop, createdLop);
120 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
122 assertEquals(gotLop, originalLop);
124 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
125 String actualJsonOutput = standardCoder.encode(gotLop);
127 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
129 LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
130 LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
131 assertEquals(gotLopV2, createdLopV2);
135 public void testPolicyCreate() throws Exception {
136 assertThatThrownBy(() -> {
137 new LegacyProvider().createOperationalPolicy(null, null);
138 }).hasMessage("dao is marked @NonNull but is null");
140 assertThatThrownBy(() -> {
141 new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
142 }).hasMessage("dao is marked @NonNull but is null");
144 assertThatThrownBy(() -> {
145 new LegacyProvider().createOperationalPolicy(pfDao, null);
146 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
148 LegacyOperationalPolicy originalLop =
149 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
150 LegacyOperationalPolicy.class);
152 assertNotNull(originalLop);
154 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
156 assertEquals(originalLop, createdLop);
158 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
160 assertEquals(gotLop, originalLop);
162 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
163 String actualJsonOutput = standardCoder.encode(gotLop);
165 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
170 public void testPolicyUpdate() throws Exception {
171 assertThatThrownBy(() -> {
172 new LegacyProvider().updateOperationalPolicy(null, null);
173 }).hasMessage("dao is marked @NonNull but is null");
175 assertThatThrownBy(() -> {
176 new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
177 }).hasMessage("dao is marked @NonNull but is null");
179 assertThatThrownBy(() -> {
180 new LegacyProvider().updateOperationalPolicy(pfDao, null);
181 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
183 assertThatThrownBy(() -> {
184 new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
185 }).hasMessage("no policy found for policy ID: null");
187 LegacyOperationalPolicy originalLop =
188 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
189 LegacyOperationalPolicy.class);
191 assertNotNull(originalLop);
193 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
194 assertEquals(originalLop, createdLop);
196 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
197 assertEquals(gotLop, originalLop);
199 originalLop.setContent("Some New Content");
200 LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
201 assertEquals(originalLop, updatedLop);
203 LegacyOperationalPolicy gotUpdatedLop =
204 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
205 assertEquals(gotUpdatedLop, originalLop);
206 assertEquals("Some New Content", gotUpdatedLop.getContent());
211 public void testPoliciesDelete() throws Exception {
212 assertThatThrownBy(() -> {
213 new LegacyProvider().deleteOperationalPolicy(null, null);
214 }).hasMessage("dao is marked @NonNull but is null");
216 assertThatThrownBy(() -> {
217 new LegacyProvider().deleteOperationalPolicy(null, "");
218 }).hasMessage("dao is marked @NonNull but is null");
220 assertThatThrownBy(() -> {
221 new LegacyProvider().deleteOperationalPolicy(pfDao, null);
222 }).hasMessage("policyId is marked @NonNull but is null");
225 assertThatThrownBy(() -> {
226 new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist");
227 }).hasMessage("no policy found for policy ID: I Dont Exist");
229 LegacyOperationalPolicy originalLop =
230 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
231 LegacyOperationalPolicy.class);
233 assertNotNull(originalLop);
235 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
236 assertEquals(originalLop, createdLop);
238 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
240 assertEquals(gotLop, originalLop);
242 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
243 String actualJsonOutput = standardCoder.encode(gotLop);
245 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
247 LegacyOperationalPolicy deletedLop =
248 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId());
249 assertEquals(originalLop, deletedLop);
251 assertThatThrownBy(() -> {
252 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
253 }).hasMessage("no policy found for policy ID: operational.restart");
255 LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
256 otherLop.setPolicyId("another-policy");
257 otherLop.setPolicyVersion("1");
258 otherLop.setContent("content");
260 LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
261 assertEquals(otherLop, createdOtherLop);
263 assertThatThrownBy(() -> {
264 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
265 }).hasMessage("no policy found for policy ID: operational.restart");