Add missing entry_schema for operational policy type
[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
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 .*on.*ull but is null$";
54     private static final String VCPE_OUTPUT_JSON = "policies/vCPE.policy.operational.legacy.output.json";
55     private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.operational.legacy.input.json";
56     private static final String DAO_IS_NULL = "^dao is marked .*on.*ull 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.getName());
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         }).hasMessageMatching(DAO_IS_NULL);
104
105         assertThatThrownBy(() -> {
106             new LegacyProvider().getOperationalPolicy(null, "", null);
107         }).hasMessageMatching(DAO_IS_NULL);
108
109         assertThatThrownBy(() -> {
110             new LegacyProvider().getOperationalPolicy(pfDao, null, null);
111         }).hasMessageMatching(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), LegacyOperationalPolicy.class);
121
122         assertNotNull(originalLop);
123
124         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
125
126         assertEquals(originalLop, createdLop);
127
128         LegacyOperationalPolicy gotLop =
129                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
130
131         assertEquals(gotLop, originalLop);
132
133         String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
134         String actualJsonOutput = standardCoder.encode(gotLop);
135
136         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
137
138         LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
139         LegacyOperationalPolicy gotLopV2 =
140                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
141         assertEquals(gotLopV2, createdLopV2);
142     }
143
144     @Test
145     public void testPolicyCreate() throws Exception {
146         assertThatThrownBy(() -> {
147             new LegacyProvider().createOperationalPolicy(null, null);
148         }).hasMessageMatching(DAO_IS_NULL);
149
150         assertThatThrownBy(() -> {
151             new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
152         }).hasMessageMatching(DAO_IS_NULL);
153
154         assertThatThrownBy(() -> {
155             new LegacyProvider().createOperationalPolicy(pfDao, null);
156         }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
157
158         createPolicyTypes();
159
160         LegacyOperationalPolicy originalLop =
161                 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
162
163         assertNotNull(originalLop);
164
165         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
166
167         assertEquals(originalLop, createdLop);
168
169         LegacyOperationalPolicy gotLop =
170                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
171
172         assertEquals(gotLop, originalLop);
173
174         String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
175         String actualJsonOutput = standardCoder.encode(gotLop);
176
177         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
178     }
179
180     @Test
181     public void testPolicyUpdate() throws Exception {
182         assertThatThrownBy(() -> {
183             new LegacyProvider().updateOperationalPolicy(null, null);
184         }).hasMessageMatching(DAO_IS_NULL);
185
186         assertThatThrownBy(() -> {
187             new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
188         }).hasMessageMatching(DAO_IS_NULL);
189
190         assertThatThrownBy(() -> {
191             new LegacyProvider().updateOperationalPolicy(pfDao, null);
192         }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
193
194         assertThatThrownBy(() -> {
195             new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
196         }).hasMessageMatching("^name is marked .*on.*ull but is null$");
197
198         createPolicyTypes();
199
200         LegacyOperationalPolicy originalLop =
201                 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class);
202
203         assertNotNull(originalLop);
204
205         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
206         assertEquals(originalLop, createdLop);
207
208         LegacyOperationalPolicy gotLop =
209                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
210         assertEquals(gotLop, originalLop);
211
212         originalLop.setContent("Some New Content");
213         LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
214         assertEquals(originalLop, updatedLop);
215
216         LegacyOperationalPolicy gotUpdatedLop =
217                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
218         assertEquals(gotUpdatedLop, originalLop);
219         assertEquals("Some New Content", gotUpdatedLop.getContent());
220     }
221
222     @Test
223     public void testPoliciesDelete() throws Exception {
224         assertThatThrownBy(() -> {
225             new LegacyProvider().deleteOperationalPolicy(null, null, null);
226         }).hasMessageMatching(DAO_IS_NULL);
227
228         assertThatThrownBy(() -> {
229             new LegacyProvider().deleteOperationalPolicy(null, null, "");
230
231         }).hasMessageMatching(DAO_IS_NULL);
232
233         assertThatThrownBy(() -> {
234             new LegacyProvider().deleteOperationalPolicy(null, "", null);
235         }).hasMessageMatching(DAO_IS_NULL);
236
237         assertThatThrownBy(() -> {
238             new LegacyProvider().deleteOperationalPolicy(null, "", "");
239         }).hasMessageMatching(DAO_IS_NULL);
240
241         assertThatThrownBy(() -> {
242             new LegacyProvider().deleteOperationalPolicy(pfDao, null, null);
243         }).hasMessageMatching(POLICY_ID_IS_NULL);
244
245         assertThatThrownBy(() -> {
246             new LegacyProvider().deleteOperationalPolicy(pfDao, null, "");
247         }).hasMessageMatching(POLICY_ID_IS_NULL);
248
249         assertThatThrownBy(() -> {
250             new LegacyProvider().deleteOperationalPolicy(pfDao, "", null);
251         }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
252
253         assertThatThrownBy(() -> {
254             new LegacyProvider().deleteOperationalPolicy(pfDao, "IDontExist", "0");
255         }).hasMessage("no policy found for policy: IDontExist:0");
256
257         createPolicyTypes();
258
259         LegacyOperationalPolicy originalLop =
260                 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), 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(VCPE_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         }).hasMessageMatching("^policyVersion is marked .*on.*ull 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 }