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