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.sql.Connection;
28 import java.sql.DriverManager;
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 {
47 private Connection connection;
49 private StandardCoder standardCoder;
52 * Set up the DAO towards the database.
54 * @throws Exception on database errors
57 public void setupDao() throws Exception {
58 // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
59 // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
60 connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
62 final DaoParameters daoParameters = new DaoParameters();
63 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
65 // Use the persistence unit ToscaConceptTest to test towards the h2 database
66 // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
67 daoParameters.setPersistenceUnit("ToscaConceptTest");
69 pfDao = new PfDaoFactory().createPfDao(daoParameters);
70 pfDao.init(daoParameters);
74 * Set up standard coder.
77 public void setupStandardCoder() {
78 standardCoder = new StandardCoder();
82 public void teardown() throws Exception {
88 public void testPoliciesGet() throws Exception {
89 assertThatThrownBy(() -> {
90 new LegacyProvider().getOperationalPolicy(null, null);
91 }).hasMessage("dao is marked @NonNull but is null");
93 assertThatThrownBy(() -> {
94 new LegacyProvider().getOperationalPolicy(null, "");
95 }).hasMessage("dao is marked @NonNull but is null");
97 assertThatThrownBy(() -> {
98 new LegacyProvider().getOperationalPolicy(pfDao, null);
99 }).hasMessage("policyId is marked @NonNull but is null");
101 assertThatThrownBy(() -> {
102 new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist");
103 }).hasMessage("no policy found for policy ID: I Dont Exist");
105 LegacyOperationalPolicy originalLop =
106 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
107 LegacyOperationalPolicy.class);
109 assertNotNull(originalLop);
111 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
113 assertEquals(originalLop, createdLop);
115 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
117 assertEquals(gotLop, originalLop);
119 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
120 String actualJsonOutput = standardCoder.encode(gotLop);
122 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
124 LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
125 LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
126 assertEquals(gotLopV2, createdLopV2);
130 public void testPolicyCreate() throws Exception {
131 assertThatThrownBy(() -> {
132 new LegacyProvider().createOperationalPolicy(null, null);
133 }).hasMessage("dao is marked @NonNull but is null");
135 assertThatThrownBy(() -> {
136 new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
137 }).hasMessage("dao is marked @NonNull but is null");
139 assertThatThrownBy(() -> {
140 new LegacyProvider().createOperationalPolicy(pfDao, null);
141 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
143 LegacyOperationalPolicy originalLop =
144 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
145 LegacyOperationalPolicy.class);
147 assertNotNull(originalLop);
149 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
151 assertEquals(originalLop, createdLop);
153 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
155 assertEquals(gotLop, originalLop);
157 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
158 String actualJsonOutput = standardCoder.encode(gotLop);
160 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
165 public void testPolicyUpdate() throws Exception {
166 assertThatThrownBy(() -> {
167 new LegacyProvider().updateOperationalPolicy(null, null);
168 }).hasMessage("dao is marked @NonNull but is null");
170 assertThatThrownBy(() -> {
171 new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
172 }).hasMessage("dao is marked @NonNull but is null");
174 assertThatThrownBy(() -> {
175 new LegacyProvider().updateOperationalPolicy(pfDao, null);
176 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
178 assertThatThrownBy(() -> {
179 new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
180 }).hasMessage("no policy found for policy ID: null");
182 LegacyOperationalPolicy originalLop =
183 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
184 LegacyOperationalPolicy.class);
186 assertNotNull(originalLop);
188 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
189 assertEquals(originalLop, createdLop);
191 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
192 assertEquals(gotLop, originalLop);
194 originalLop.setContent("Some New Content");
195 LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
196 assertEquals(originalLop, updatedLop);
198 LegacyOperationalPolicy gotUpdatedLop =
199 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
200 assertEquals(gotUpdatedLop, originalLop);
201 assertEquals("Some New Content", gotUpdatedLop.getContent());
206 public void testPoliciesDelete() throws Exception {
207 assertThatThrownBy(() -> {
208 new LegacyProvider().deleteOperationalPolicy(null, null);
209 }).hasMessage("dao is marked @NonNull but is null");
211 assertThatThrownBy(() -> {
212 new LegacyProvider().deleteOperationalPolicy(null, "");
213 }).hasMessage("dao is marked @NonNull but is null");
215 assertThatThrownBy(() -> {
216 new LegacyProvider().deleteOperationalPolicy(pfDao, null);
217 }).hasMessage("policyId is marked @NonNull but is null");
220 assertThatThrownBy(() -> {
221 new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist");
222 }).hasMessage("no policy found for policy ID: I Dont Exist");
224 LegacyOperationalPolicy originalLop =
225 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
226 LegacyOperationalPolicy.class);
228 assertNotNull(originalLop);
230 LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
231 assertEquals(originalLop, createdLop);
233 LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
235 assertEquals(gotLop, originalLop);
237 String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
238 String actualJsonOutput = standardCoder.encode(gotLop);
240 assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
242 LegacyOperationalPolicy deletedLop =
243 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId());
244 assertEquals(originalLop, deletedLop);
246 assertThatThrownBy(() -> {
247 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
248 }).hasMessage("no policy found for policy ID: operational.restart");
250 LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
251 otherLop.setPolicyId("another-policy");
252 otherLop.setPolicyVersion("1");
253 otherLop.setContent("content");
255 LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
256 assertEquals(otherLop, createdOtherLop);
258 assertThatThrownBy(() -> {
259 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
260 }).hasMessage("no policy found for policy ID: operational.restart");