Set default and check existance of 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 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);
98         }).hasMessage("dao is marked @NonNull but is null");
99
100         assertThatThrownBy(() -> {
101             new LegacyProvider().getOperationalPolicy(null, "");
102         }).hasMessage("dao is marked @NonNull but is null");
103
104         assertThatThrownBy(() -> {
105             new LegacyProvider().getOperationalPolicy(pfDao, null);
106         }).hasMessage("policyId is marked @NonNull but is null");
107
108         assertThatThrownBy(() -> {
109             new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist");
110         }).hasMessage("no policy found for policy ID: I Dont Exist");
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 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
125
126         assertEquals(gotLop, originalLop);
127
128         String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
129         String actualJsonOutput = standardCoder.encode(gotLop);
130
131         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
132
133         LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
134         LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
135         assertEquals(gotLopV2, createdLopV2);
136     }
137
138     @Test
139     public void testPolicyCreate() throws Exception {
140         assertThatThrownBy(() -> {
141             new LegacyProvider().createOperationalPolicy(null, null);
142         }).hasMessage("dao is marked @NonNull but is null");
143
144         assertThatThrownBy(() -> {
145             new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
146         }).hasMessage("dao is marked @NonNull but is null");
147
148         assertThatThrownBy(() -> {
149             new LegacyProvider().createOperationalPolicy(pfDao, null);
150         }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
151
152         createPolicyTypes();
153
154         LegacyOperationalPolicy originalLop =
155                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
156                         LegacyOperationalPolicy.class);
157
158         assertNotNull(originalLop);
159
160         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
161
162         assertEquals(originalLop, createdLop);
163
164         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
165
166         assertEquals(gotLop, originalLop);
167
168         String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
169         String actualJsonOutput = standardCoder.encode(gotLop);
170
171         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
172     }
173
174     @Test
175     public void testPolicyUpdate() throws Exception {
176         assertThatThrownBy(() -> {
177             new LegacyProvider().updateOperationalPolicy(null, null);
178         }).hasMessage("dao is marked @NonNull but is null");
179
180         assertThatThrownBy(() -> {
181             new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
182         }).hasMessage("dao is marked @NonNull but is null");
183
184         assertThatThrownBy(() -> {
185             new LegacyProvider().updateOperationalPolicy(pfDao, null);
186         }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
187
188         assertThatThrownBy(() -> {
189             new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
190         }).hasMessage("no policy found for policy ID: null");
191
192         createPolicyTypes();
193
194         LegacyOperationalPolicy originalLop =
195                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
196                         LegacyOperationalPolicy.class);
197
198         assertNotNull(originalLop);
199
200         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
201         assertEquals(originalLop, createdLop);
202
203         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
204         assertEquals(gotLop, originalLop);
205
206         originalLop.setContent("Some New Content");
207         LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
208         assertEquals(originalLop, updatedLop);
209
210         LegacyOperationalPolicy gotUpdatedLop =
211                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
212         assertEquals(gotUpdatedLop, originalLop);
213         assertEquals("Some New Content", gotUpdatedLop.getContent());
214     }
215
216     @Test
217     public void testPoliciesDelete() throws Exception {
218         assertThatThrownBy(() -> {
219             new LegacyProvider().deleteOperationalPolicy(null, null);
220         }).hasMessage("dao is marked @NonNull but is null");
221
222         assertThatThrownBy(() -> {
223             new LegacyProvider().deleteOperationalPolicy(null, "");
224
225         }).hasMessage("dao is marked @NonNull but is null");
226
227         assertThatThrownBy(() -> {
228             new LegacyProvider().deleteOperationalPolicy(pfDao, null);
229         }).hasMessage("policyId is marked @NonNull but is null");
230
231         assertThatThrownBy(() -> {
232             new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist");
233         }).hasMessage("no policy found for policy ID: I Dont Exist");
234
235         createPolicyTypes();
236
237         LegacyOperationalPolicy originalLop =
238                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
239                         LegacyOperationalPolicy.class);
240
241         assertNotNull(originalLop);
242
243         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
244         assertEquals(originalLop, createdLop);
245
246         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
247
248         assertEquals(gotLop, originalLop);
249
250         String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
251         String actualJsonOutput = standardCoder.encode(gotLop);
252
253         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
254
255         LegacyOperationalPolicy deletedLop =
256                 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId());
257         assertEquals(originalLop, deletedLop);
258
259         assertThatThrownBy(() -> {
260             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
261         }).hasMessage("no policy found for policy ID: operational.restart");
262
263         LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
264         otherLop.setPolicyId("another-policy");
265         otherLop.setPolicyVersion("1");
266         otherLop.setContent("content");
267
268         LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
269         assertEquals(otherLop, createdOtherLop);
270
271         assertThatThrownBy(() -> {
272             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
273         }).hasMessage("no policy found for policy ID: operational.restart");
274     }
275
276     private void createPolicyTypes() throws CoderException, PfModelException {
277         Object yamlObject = new Yaml().load(
278                 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
279         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
280
281         ToscaServiceTemplate toscaServiceTemplatePolicyType =
282                 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
283
284         assertNotNull(toscaServiceTemplatePolicyType);
285         new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
286     }
287 }