Changes for Checkstyle 8.32
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / legacy / provider / LegacyProvider4LegacyOperationalTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.legacy.provider;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 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.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.dao.DaoParameters;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.dao.PfDaoFactory;
40 import org.onap.policy.models.dao.impl.DefaultPfDao;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
43 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
44 import org.yaml.snakeyaml.Yaml;
45
46 /**
47  * Test the {@link LegacyProvider} class for legacy operational policies.
48  *
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51 public class LegacyProvider4LegacyOperationalTest {
52     private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$";
53     private static final String VCPE_OUTPUT_JSON = "policies/vCPE.policy.operational.legacy.output.json";
54     private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.operational.legacy.input.json";
55     private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
56     private PfDao pfDao;
57     private StandardCoder standardCoder;
58
59     /**
60      * Set up the DAO towards the database.
61      *
62      * @throws Exception on database errors
63      */
64     @Before
65     public void setupDao() throws Exception {
66         final DaoParameters daoParameters = new DaoParameters();
67         daoParameters.setPluginClass(DefaultPfDao.class.getName());
68
69         daoParameters.setPersistenceUnit("ToscaConceptTest");
70
71         Properties jdbcProperties = new Properties();
72         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
73         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
74
75         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
76         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
77         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
78
79         daoParameters.setJdbcProperties(jdbcProperties);
80
81         pfDao = new PfDaoFactory().createPfDao(daoParameters);
82         pfDao.init(daoParameters);
83     }
84
85     /**
86      * Set up standard coder.
87      */
88     @Before
89     public void setupStandardCoder() {
90         standardCoder = new StandardCoder();
91     }
92
93     @After
94     public void teardown() {
95         pfDao.close();
96     }
97
98     @Test
99     public void testPoliciesGet() throws Exception {
100         assertThatThrownBy(() -> {
101             new LegacyProvider().getOperationalPolicy(null, null, null);
102         }).hasMessageMatching(DAO_IS_NULL);
103
104         assertThatThrownBy(() -> {
105             new LegacyProvider().getOperationalPolicy(null, "", null);
106         }).hasMessageMatching(DAO_IS_NULL);
107
108         assertThatThrownBy(() -> {
109             new LegacyProvider().getOperationalPolicy(pfDao, null, null);
110         }).hasMessageMatching(POLICY_ID_IS_NULL);
111
112         assertThatThrownBy(() -> {
113             new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist", null);
114         }).hasMessage("service template not found in database");
115
116         createPolicyTypes();
117
118         LegacyOperationalPolicy originalLop =
119             standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
120
121         assertNotNull(originalLop);
122
123         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
124
125         assertEquals(originalLop, createdLop);
126
127         LegacyOperationalPolicy gotLop =
128             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
129
130         assertEquals(gotLop, originalLop);
131
132         String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
133         String actualJsonOutput = standardCoder.encode(gotLop);
134
135         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
136
137         LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
138         LegacyOperationalPolicy gotLopV2 =
139             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
140         assertEquals(gotLopV2, createdLopV2);
141     }
142
143     @Test
144     public void testPolicyCreate() throws Exception {
145         assertThatThrownBy(() -> {
146             new LegacyProvider().createOperationalPolicy(null, null);
147         }).hasMessageMatching(DAO_IS_NULL);
148
149         assertThatThrownBy(() -> {
150             new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
151         }).hasMessageMatching(DAO_IS_NULL);
152
153         assertThatThrownBy(() -> {
154             new LegacyProvider().createOperationalPolicy(pfDao, null);
155         }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
156
157         createPolicyTypes();
158
159         LegacyOperationalPolicy originalLop =
160             standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
161
162         assertNotNull(originalLop);
163
164         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
165
166         assertEquals(originalLop, createdLop);
167
168         LegacyOperationalPolicy gotLop =
169             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
170
171         assertEquals(gotLop, originalLop);
172
173         String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
174         String actualJsonOutput = standardCoder.encode(gotLop);
175
176         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
177     }
178
179     @Test
180     public void testPolicyUpdate() throws Exception {
181         assertThatThrownBy(() -> {
182             new LegacyProvider().updateOperationalPolicy(null, null);
183         }).hasMessageMatching(DAO_IS_NULL);
184
185         assertThatThrownBy(() -> {
186             new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
187         }).hasMessageMatching(DAO_IS_NULL);
188
189         assertThatThrownBy(() -> {
190             new LegacyProvider().updateOperationalPolicy(pfDao, null);
191         }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
192
193         assertThatThrownBy(() -> {
194             new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
195         }).hasMessageMatching("^name is marked .*on.*ull but is null$");
196
197         createPolicyTypes();
198
199         LegacyOperationalPolicy originalLop =
200             standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
201
202         assertNotNull(originalLop);
203
204         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
205         assertEquals(originalLop, createdLop);
206
207         LegacyOperationalPolicy gotLop =
208             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
209         assertEquals(gotLop, originalLop);
210
211         originalLop.setContent("Some New Content");
212         LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
213         assertEquals(originalLop, updatedLop);
214
215         LegacyOperationalPolicy gotUpdatedLop =
216             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
217         assertEquals(gotUpdatedLop, originalLop);
218         assertEquals("Some New Content", gotUpdatedLop.getContent());
219     }
220
221     @Test
222     public void testPoliciesDelete() throws Exception {
223         assertThatThrownBy(() -> {
224             new LegacyProvider().deleteOperationalPolicy(null, null, null);
225         }).hasMessageMatching(DAO_IS_NULL);
226
227         assertThatThrownBy(() -> {
228             new LegacyProvider().deleteOperationalPolicy(null, null, "");
229
230         }).hasMessageMatching(DAO_IS_NULL);
231
232         assertThatThrownBy(() -> {
233             new LegacyProvider().deleteOperationalPolicy(null, "", null);
234         }).hasMessageMatching(DAO_IS_NULL);
235
236         assertThatThrownBy(() -> {
237             new LegacyProvider().deleteOperationalPolicy(null, "", "");
238         }).hasMessageMatching(DAO_IS_NULL);
239
240         assertThatThrownBy(() -> {
241             new LegacyProvider().deleteOperationalPolicy(pfDao, null, null);
242         }).hasMessageMatching(POLICY_ID_IS_NULL);
243
244         assertThatThrownBy(() -> {
245             new LegacyProvider().deleteOperationalPolicy(pfDao, null, "");
246         }).hasMessageMatching(POLICY_ID_IS_NULL);
247
248         assertThatThrownBy(() -> {
249             new LegacyProvider().deleteOperationalPolicy(pfDao, "", null);
250         }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
251
252         assertThatThrownBy(() -> {
253             new LegacyProvider().deleteOperationalPolicy(pfDao, "IDontExist", "0");
254         }).hasMessage("service template not found in database");
255
256         createPolicyTypes();
257
258         LegacyOperationalPolicy originalLop =
259             standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
260
261         assertNotNull(originalLop);
262
263         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
264         assertEquals(originalLop, createdLop);
265
266         LegacyOperationalPolicy gotLop =
267             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
268
269         assertEquals(gotLop, originalLop);
270
271         String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
272         String actualJsonOutput = standardCoder.encode(gotLop);
273
274         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
275
276         assertThatThrownBy(() -> {
277             new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
278         }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
279
280         LegacyOperationalPolicy deletedLop =
281             new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), "1");
282         assertEquals(originalLop, deletedLop);
283
284         assertThatThrownBy(() -> {
285             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
286         }).hasMessage("policies for operational.restart:null do not exist");
287
288         LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
289         otherLop.setPolicyId("another-policy");
290         otherLop.setPolicyVersion("1");
291         otherLop.setContent("content");
292
293         LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
294         assertEquals(otherLop, createdOtherLop);
295
296         assertThatThrownBy(() -> {
297             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
298         }).hasMessage("policies for operational.restart:null do not exist");
299     }
300
301     private void createPolicyTypes() throws CoderException, PfModelException {
302         Object yamlObject = new Yaml()
303             .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
304         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
305
306         ToscaServiceTemplate toscaServiceTemplatePolicyType =
307             standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
308
309         assertNotNull(toscaServiceTemplatePolicyType);
310         new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
311     }
312 }