Merge "Fix more sonar issues in models: yaml to dao"
[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 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
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
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;
46
47 /**
48  * Test the {@link LegacyProvider} class for legacy operational policies.
49  *
50  * @author Liam Fallon (liam.fallon@est.tech)
51  */
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";
57     private PfDao pfDao;
58     private StandardCoder standardCoder;
59
60     /**
61      * Set up the DAO towards the database.
62      *
63      * @throws Exception on database errors
64      */
65     @Before
66     public void setupDao() throws Exception {
67         final DaoParameters daoParameters = new DaoParameters();
68         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
69
70         daoParameters.setPersistenceUnit("ToscaConceptTest");
71
72         Properties jdbcProperties = new Properties();
73         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
74         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
75
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");
79
80         daoParameters.setJdbcProperties(jdbcProperties);
81
82         pfDao = new PfDaoFactory().createPfDao(daoParameters);
83         pfDao.init(daoParameters);
84     }
85
86     /**
87      * Set up standard coder.
88      */
89     @Before
90     public void setupStandardCoder() {
91         standardCoder = new StandardCoder();
92     }
93
94     @After
95     public void teardown() {
96         pfDao.close();
97     }
98
99     @Test
100     public void testPoliciesGet() throws Exception {
101         assertThatThrownBy(() -> {
102             new LegacyProvider().getOperationalPolicy(null, null, null);
103         }).hasMessage(DAO_IS_NULL);
104
105         assertThatThrownBy(() -> {
106             new LegacyProvider().getOperationalPolicy(null, "", null);
107         }).hasMessage(DAO_IS_NULL);
108
109         assertThatThrownBy(() -> {
110             new LegacyProvider().getOperationalPolicy(pfDao, null, null);
111         }).hasMessage(POLICY_ID_IS_NULL);
112
113         assertThatThrownBy(() -> {
114             new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist", null);
115         }).hasMessage("no policy found for policy: I Dont Exist:null");
116
117         createPolicyTypes();
118
119         LegacyOperationalPolicy originalLop =
120                 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
121                         LegacyOperationalPolicy.class);
122
123         assertNotNull(originalLop);
124
125         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
126
127         assertEquals(originalLop, createdLop);
128
129         LegacyOperationalPolicy gotLop =
130                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
131
132         assertEquals(gotLop, originalLop);
133
134         String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
135         String actualJsonOutput = standardCoder.encode(gotLop);
136
137         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
138
139         LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
140         LegacyOperationalPolicy gotLopV2 =
141                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
142         assertEquals(gotLopV2, createdLopV2);
143     }
144
145     @Test
146     public void testPolicyCreate() throws Exception {
147         assertThatThrownBy(() -> {
148             new LegacyProvider().createOperationalPolicy(null, null);
149         }).hasMessage(DAO_IS_NULL);
150
151         assertThatThrownBy(() -> {
152             new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
153         }).hasMessage(DAO_IS_NULL);
154
155         assertThatThrownBy(() -> {
156             new LegacyProvider().createOperationalPolicy(pfDao, null);
157         }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
158
159         createPolicyTypes();
160
161         LegacyOperationalPolicy originalLop =
162                 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
163                         LegacyOperationalPolicy.class);
164
165         assertNotNull(originalLop);
166
167         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
168
169         assertEquals(originalLop, createdLop);
170
171         LegacyOperationalPolicy gotLop =
172                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
173
174         assertEquals(gotLop, originalLop);
175
176         String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
177         String actualJsonOutput = standardCoder.encode(gotLop);
178
179         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
180     }
181
182     @Test
183     public void testPolicyUpdate() throws Exception {
184         assertThatThrownBy(() -> {
185             new LegacyProvider().updateOperationalPolicy(null, null);
186         }).hasMessage(DAO_IS_NULL);
187
188         assertThatThrownBy(() -> {
189             new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
190         }).hasMessage(DAO_IS_NULL);
191
192         assertThatThrownBy(() -> {
193             new LegacyProvider().updateOperationalPolicy(pfDao, null);
194         }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
195
196         assertThatThrownBy(() -> {
197             new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
198         }).hasMessage("name is marked @NonNull but is null");
199
200         createPolicyTypes();
201
202         LegacyOperationalPolicy originalLop =
203                 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
204                         LegacyOperationalPolicy.class);
205
206         assertNotNull(originalLop);
207
208         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
209         assertEquals(originalLop, createdLop);
210
211         LegacyOperationalPolicy gotLop =
212                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
213         assertEquals(gotLop, originalLop);
214
215         originalLop.setContent("Some New Content");
216         LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
217         assertEquals(originalLop, updatedLop);
218
219         LegacyOperationalPolicy gotUpdatedLop =
220                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
221         assertEquals(gotUpdatedLop, originalLop);
222         assertEquals("Some New Content", gotUpdatedLop.getContent());
223     }
224
225     @Test
226     public void testPoliciesDelete() throws Exception {
227         assertThatThrownBy(() -> {
228             new LegacyProvider().deleteOperationalPolicy(null, null, null);
229         }).hasMessage(DAO_IS_NULL);
230
231         assertThatThrownBy(() -> {
232             new LegacyProvider().deleteOperationalPolicy(null, null, "");
233
234         }).hasMessage(DAO_IS_NULL);
235
236         assertThatThrownBy(() -> {
237             new LegacyProvider().deleteOperationalPolicy(null, "", null);
238         }).hasMessage(DAO_IS_NULL);
239
240         assertThatThrownBy(() -> {
241             new LegacyProvider().deleteOperationalPolicy(null, "", "");
242
243         }).hasMessage(DAO_IS_NULL);
244
245         assertThatThrownBy(() -> {
246             new LegacyProvider().deleteOperationalPolicy(pfDao, null, null);
247         }).hasMessage(POLICY_ID_IS_NULL);
248
249         assertThatThrownBy(() -> {
250             new LegacyProvider().deleteOperationalPolicy(pfDao, null, "");
251         }).hasMessage(POLICY_ID_IS_NULL);
252
253         assertThatThrownBy(() -> {
254             new LegacyProvider().deleteOperationalPolicy(pfDao, "", null);
255         }).hasMessage("policyVersion is marked @NonNull but is null");
256
257         assertThatThrownBy(() -> {
258             new LegacyProvider().deleteOperationalPolicy(pfDao, "IDontExist", "0");
259         }).hasMessage("no policy found for policy: IDontExist:0");
260
261         createPolicyTypes();
262
263         LegacyOperationalPolicy originalLop =
264                 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
265                         LegacyOperationalPolicy.class);
266
267         assertNotNull(originalLop);
268
269         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
270         assertEquals(originalLop, createdLop);
271
272         LegacyOperationalPolicy gotLop =
273                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
274
275         assertEquals(gotLop, originalLop);
276
277         String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
278         String actualJsonOutput = standardCoder.encode(gotLop);
279
280         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
281
282         assertThatThrownBy(() -> {
283             new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
284         }).hasMessage("policyVersion is marked @NonNull but is null");
285
286         LegacyOperationalPolicy deletedLop =
287                 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), "1");
288         assertEquals(originalLop, deletedLop);
289
290         assertThatThrownBy(() -> {
291             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
292         }).hasMessage("no policy found for policy: operational.restart:null");
293
294         LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
295         otherLop.setPolicyId("another-policy");
296         otherLop.setPolicyVersion("1");
297         otherLop.setContent("content");
298
299         LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
300         assertEquals(otherLop, createdOtherLop);
301
302         assertThatThrownBy(() -> {
303             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
304         }).hasMessage("no policy found for policy: operational.restart:null");
305     }
306
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);
311
312         ToscaServiceTemplate toscaServiceTemplatePolicyType =
313                 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
314
315         assertNotNull(toscaServiceTemplatePolicyType);
316         new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
317     }
318 }