UNit test and minor fixes for DB pars
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.legacy.provider;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26
27 import java.util.Properties;
28
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;
40
41 /**
42  * Test the {@link LegacyProvider} class for legacy operational policies.
43  *
44  * @author Liam Fallon (liam.fallon@est.tech)
45  */
46 public class LegacyProvider4LegacyOperationalTest {
47     private PfDao pfDao;
48     private StandardCoder standardCoder;
49
50     /**
51      * Set up the DAO towards the database.
52      *
53      * @throws Exception on database errors
54      */
55     @Before
56     public void setupDao() throws Exception {
57         final DaoParameters daoParameters = new DaoParameters();
58         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
59
60         daoParameters.setPersistenceUnit("ToscaConceptTest");
61
62         Properties jdbcProperties = new Properties();
63         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
64         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
65
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");
69
70         daoParameters.setJdbcProperties(jdbcProperties );
71
72         pfDao = new PfDaoFactory().createPfDao(daoParameters);
73         pfDao.init(daoParameters);
74     }
75
76     /**
77      * Set up standard coder.
78      */
79     @Before
80     public void setupStandardCoder() {
81         standardCoder = new StandardCoder();
82     }
83
84     @After
85     public void teardown() throws Exception {
86         pfDao.close();
87     }
88
89     @Test
90     public void testPoliciesGet() throws Exception {
91         assertThatThrownBy(() -> {
92             new LegacyProvider().getOperationalPolicy(null, null);
93         }).hasMessage("dao is marked @NonNull but is null");
94
95         assertThatThrownBy(() -> {
96             new LegacyProvider().getOperationalPolicy(null, "");
97         }).hasMessage("dao is marked @NonNull but is null");
98
99         assertThatThrownBy(() -> {
100             new LegacyProvider().getOperationalPolicy(pfDao, null);
101         }).hasMessage("policyId is marked @NonNull but is null");
102
103         assertThatThrownBy(() -> {
104             new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist");
105         }).hasMessage("no policy found for policy ID: I Dont Exist");
106
107         LegacyOperationalPolicy originalLop =
108                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
109                         LegacyOperationalPolicy.class);
110
111         assertNotNull(originalLop);
112
113         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
114
115         assertEquals(originalLop, createdLop);
116
117         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
118
119         assertEquals(gotLop, originalLop);
120
121         String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
122         String actualJsonOutput = standardCoder.encode(gotLop);
123
124         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
125
126         LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
127         LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
128         assertEquals(gotLopV2, createdLopV2);
129     }
130
131     @Test
132     public void testPolicyCreate() throws Exception {
133         assertThatThrownBy(() -> {
134             new LegacyProvider().createOperationalPolicy(null, null);
135         }).hasMessage("dao is marked @NonNull but is null");
136
137         assertThatThrownBy(() -> {
138             new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
139         }).hasMessage("dao is marked @NonNull but is null");
140
141         assertThatThrownBy(() -> {
142             new LegacyProvider().createOperationalPolicy(pfDao, null);
143         }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
144
145         LegacyOperationalPolicy originalLop =
146                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
147                         LegacyOperationalPolicy.class);
148
149         assertNotNull(originalLop);
150
151         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
152
153         assertEquals(originalLop, createdLop);
154
155         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
156
157         assertEquals(gotLop, originalLop);
158
159         String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
160         String actualJsonOutput = standardCoder.encode(gotLop);
161
162         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
163     }
164
165
166     @Test
167     public void testPolicyUpdate() throws Exception {
168         assertThatThrownBy(() -> {
169             new LegacyProvider().updateOperationalPolicy(null, null);
170         }).hasMessage("dao is marked @NonNull but is null");
171
172         assertThatThrownBy(() -> {
173             new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
174         }).hasMessage("dao is marked @NonNull but is null");
175
176         assertThatThrownBy(() -> {
177             new LegacyProvider().updateOperationalPolicy(pfDao, null);
178         }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
179
180         assertThatThrownBy(() -> {
181             new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
182         }).hasMessage("no policy found for policy ID: null");
183
184         LegacyOperationalPolicy originalLop =
185                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
186                         LegacyOperationalPolicy.class);
187
188         assertNotNull(originalLop);
189
190         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
191         assertEquals(originalLop, createdLop);
192
193         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
194         assertEquals(gotLop, originalLop);
195
196         originalLop.setContent("Some New Content");
197         LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
198         assertEquals(originalLop, updatedLop);
199
200         LegacyOperationalPolicy gotUpdatedLop =
201                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
202         assertEquals(gotUpdatedLop, originalLop);
203         assertEquals("Some New Content", gotUpdatedLop.getContent());
204     }
205
206
207     @Test
208     public void testPoliciesDelete() throws Exception {
209         assertThatThrownBy(() -> {
210             new LegacyProvider().deleteOperationalPolicy(null, null);
211         }).hasMessage("dao is marked @NonNull but is null");
212
213         assertThatThrownBy(() -> {
214             new LegacyProvider().deleteOperationalPolicy(null, "");
215         }).hasMessage("dao is marked @NonNull but is null");
216
217         assertThatThrownBy(() -> {
218             new LegacyProvider().deleteOperationalPolicy(pfDao, null);
219         }).hasMessage("policyId is marked @NonNull but is null");
220
221
222         assertThatThrownBy(() -> {
223             new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist");
224         }).hasMessage("no policy found for policy ID: I Dont Exist");
225
226         LegacyOperationalPolicy originalLop =
227                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
228                         LegacyOperationalPolicy.class);
229
230         assertNotNull(originalLop);
231
232         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
233         assertEquals(originalLop, createdLop);
234
235         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
236
237         assertEquals(gotLop, originalLop);
238
239         String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
240         String actualJsonOutput = standardCoder.encode(gotLop);
241
242         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
243
244         LegacyOperationalPolicy deletedLop =
245                 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId());
246         assertEquals(originalLop, deletedLop);
247
248         assertThatThrownBy(() -> {
249             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
250         }).hasMessage("no policy found for policy ID: operational.restart");
251
252         LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
253         otherLop.setPolicyId("another-policy");
254         otherLop.setPolicyVersion("1");
255         otherLop.setContent("content");
256
257         LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
258         assertEquals(otherLop, createdOtherLop);
259
260         assertThatThrownBy(() -> {
261             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
262         }).hasMessage("no policy found for policy ID: operational.restart");
263
264     }
265 }