Merge "Add SO VF Module Delete Operation"
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / legacy / provider / LegacyProvider4LegacyGuardTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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.Map;
29 import java.util.Properties;
30
31 import org.eclipse.persistence.config.PersistenceUnitProperties;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.dao.DaoParameters;
40 import org.onap.policy.models.dao.PfDao;
41 import org.onap.policy.models.dao.PfDaoFactory;
42 import org.onap.policy.models.dao.impl.DefaultPfDao;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
45 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent;
46 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
47 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
48 import org.yaml.snakeyaml.Yaml;
49
50 /**
51  * Test the {@link LegacyProvider} class for legacy guard policies.
52  *
53  * @author Liam Fallon (liam.fallon@est.tech)
54  */
55 public class LegacyProvider4LegacyGuardTest {
56     private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$";
57     private static final String VDNS_OUTPUT_JSON = "policies/vDNS.policy.guard.frequency.output.json";
58     private static final String VDNS_INPUT_JSON = "policies/vDNS.policy.guard.frequency.input.json";
59     private static final String LEGACY_POLICY_IS_NULL = "^legacyGuardPolicy is marked .*on.*ull but is null$";
60     private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
61     private PfDao pfDao;
62     private StandardCoder standardCoder;
63
64     /**
65      * Set up the DAO towards the database.
66      *
67      * @throws Exception on database errors
68      */
69     @Before
70     public void setupDao() throws Exception {
71         final DaoParameters daoParameters = new DaoParameters();
72         daoParameters.setPluginClass(DefaultPfDao.class.getName());
73
74         daoParameters.setPersistenceUnit("ToscaConceptTest");
75
76         Properties jdbcProperties = new Properties();
77         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
78         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
79
80         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
81         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
82         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
83
84         daoParameters.setJdbcProperties(jdbcProperties);
85
86         pfDao = new PfDaoFactory().createPfDao(daoParameters);
87         pfDao.init(daoParameters);
88     }
89
90     /**
91      * Set up standard coder.
92      */
93     @Before
94     public void setupStandardCoder() {
95         standardCoder = new StandardCoder();
96     }
97
98     @After
99     public void teardown() {
100         pfDao.close();
101     }
102
103     @Test
104     public void testPoliciesGet() throws Exception {
105         assertThatThrownBy(() -> {
106             new LegacyProvider().getGuardPolicy(null, null, null);
107         }).hasMessageMatching(DAO_IS_NULL);
108
109         assertThatThrownBy(() -> {
110             new LegacyProvider().getGuardPolicy(null, null, "");
111         }).hasMessageMatching(DAO_IS_NULL);
112
113         assertThatThrownBy(() -> {
114             new LegacyProvider().getGuardPolicy(pfDao, null, null);
115         }).hasMessageMatching(POLICY_ID_IS_NULL);
116
117         assertThatThrownBy(() -> {
118             new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist", null);
119         }).hasMessage("no policy found for policy: I Dont Exist:null");
120
121         createPolicyTypes();
122
123         LegacyGuardPolicyInput originalGip =
124                 standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class);
125
126         assertNotNull(originalGip);
127
128         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
129
130         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
131         assertEquals(originalGip.getContent(),
132                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
133
134         Map<String, LegacyGuardPolicyOutput> gotGopm =
135                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
136
137         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
138         assertEquals(originalGip.getContent(),
139                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
140
141         String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON);
142         String actualJsonOutput = standardCoder.encode(gotGopm);
143
144         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
145
146         gotGopm = new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "1");
147
148         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
149         assertEquals(originalGip.getContent(),
150                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
151
152         actualJsonOutput = standardCoder.encode(gotGopm);
153
154         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
155
156         assertThatThrownBy(() -> {
157             new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "2");
158         }).hasMessage("no policy found for policy: guard.frequency.scaleout:2");
159     }
160
161     @Test
162     public void testPolicyCreate() throws Exception {
163         assertThatThrownBy(() -> {
164             new LegacyProvider().createGuardPolicy(null, null);
165         }).hasMessageMatching(DAO_IS_NULL);
166
167         assertThatThrownBy(() -> {
168             new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
169         }).hasMessageMatching(DAO_IS_NULL);
170
171         assertThatThrownBy(() -> {
172             new LegacyProvider().createGuardPolicy(pfDao, null);
173         }).hasMessageMatching(LEGACY_POLICY_IS_NULL);
174
175         createPolicyTypes();
176
177         LegacyGuardPolicyInput originalGip =
178                 standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class);
179
180         assertNotNull(originalGip);
181
182         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
183
184         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
185         assertEquals(originalGip.getContent(),
186                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
187
188         Map<String, LegacyGuardPolicyOutput> gotGopm =
189                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
190
191         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
192         assertEquals(originalGip.getContent(),
193                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
194
195         String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON);
196         String actualJsonOutput = standardCoder.encode(gotGopm);
197
198         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
199     }
200
201     @Test
202     public void testPolicyCreateBad() throws Exception {
203         assertThatThrownBy(() -> {
204             new LegacyProvider().createGuardPolicy(null, null);
205         }).hasMessageMatching(DAO_IS_NULL);
206
207         assertThatThrownBy(() -> {
208             new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
209         }).hasMessageMatching(DAO_IS_NULL);
210
211         assertThatThrownBy(() -> {
212             new LegacyProvider().createGuardPolicy(pfDao, null);
213         }).hasMessageMatching(LEGACY_POLICY_IS_NULL);
214
215         createPolicyTypes();
216
217         LegacyGuardPolicyInput originalGip =
218                 standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class);
219
220         assertNotNull(originalGip);
221
222         originalGip.setPolicyId("i.do.not.exist");
223
224         assertThatThrownBy(() -> {
225             new LegacyProvider().createGuardPolicy(pfDao, originalGip);
226         }).hasMessage("policy type for guard policy \"i.do.not.exist\" unknown");
227     }
228
229     @Test
230     public void testPolicyUpdate() throws Exception {
231         assertThatThrownBy(() -> {
232             new LegacyProvider().updateGuardPolicy(null, null);
233         }).hasMessageMatching(DAO_IS_NULL);
234
235         assertThatThrownBy(() -> {
236             new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
237         }).hasMessageMatching(DAO_IS_NULL);
238
239         assertThatThrownBy(() -> {
240             new LegacyProvider().updateGuardPolicy(pfDao, null);
241         }).hasMessageMatching(LEGACY_POLICY_IS_NULL);
242
243         assertThatThrownBy(() -> {
244             new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
245         }).hasMessage("policy type for guard policy \"null\" unknown");
246
247         createPolicyTypes();
248
249         LegacyGuardPolicyInput originalGip =
250                 standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class);
251
252         assertNotNull(originalGip);
253
254         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
255         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
256         assertEquals(originalGip.getContent(),
257                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
258
259         Map<String, LegacyGuardPolicyOutput> gotGopm =
260                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
261
262         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
263         assertEquals(originalGip.getContent(),
264                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
265
266         originalGip.getContent().setRecipe("Roast Turkey");
267         Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
268         assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
269         assertEquals(originalGip.getContent(),
270                 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
271
272         Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
273                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
274         assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
275         assertEquals(originalGip.getContent(),
276                 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
277         assertEquals("Roast Turkey",
278                 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
279     }
280
281     @Test
282     public void testPoliciesDelete() throws Exception {
283         assertThatThrownBy(() -> {
284             new LegacyProvider().deleteGuardPolicy(null, null, null);
285         }).hasMessageMatching(DAO_IS_NULL);
286
287         assertThatThrownBy(() -> {
288             new LegacyProvider().deleteGuardPolicy(null, null, "");
289         }).hasMessageMatching(DAO_IS_NULL);
290
291         assertThatThrownBy(() -> {
292             new LegacyProvider().deleteGuardPolicy(null, "", null);
293         }).hasMessageMatching(DAO_IS_NULL);
294
295         assertThatThrownBy(() -> {
296             new LegacyProvider().deleteGuardPolicy(null, "", "");
297         }).hasMessageMatching(DAO_IS_NULL);
298
299         assertThatThrownBy(() -> {
300             new LegacyProvider().deleteGuardPolicy(pfDao, null, null);
301         }).hasMessageMatching(POLICY_ID_IS_NULL);
302
303         assertThatThrownBy(() -> {
304             new LegacyProvider().deleteGuardPolicy(pfDao, null, "");
305         }).hasMessageMatching(POLICY_ID_IS_NULL);
306
307         assertThatThrownBy(() -> {
308             new LegacyProvider().deleteGuardPolicy(pfDao, "", null);
309         }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
310
311         assertThatThrownBy(() -> {
312             new LegacyProvider().deleteGuardPolicy(pfDao, "IDontExist", "0");
313         }).hasMessage("no policy found for policy: IDontExist:0");
314
315         createPolicyTypes();
316
317         LegacyGuardPolicyInput originalGip =
318                 standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class);
319
320         assertNotNull(originalGip);
321
322         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
323         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
324         assertEquals(originalGip.getContent(),
325                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
326
327         Map<String, LegacyGuardPolicyOutput> gotGopm =
328                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
329
330         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
331         assertEquals(originalGip.getContent(),
332                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
333
334         String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON);
335         String actualJsonOutput = standardCoder.encode(gotGopm);
336
337         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
338
339         assertThatThrownBy(() -> {
340             new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), null);
341         }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
342
343         Map<String, LegacyGuardPolicyOutput> deletedGopm =
344                 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), "1");
345         assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
346         assertEquals(originalGip.getContent(),
347                 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
348
349         assertThatThrownBy(() -> {
350             new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
351         }).hasMessage("no policy found for policy: guard.frequency.scaleout:null");
352
353         LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
354         otherGip.setPolicyId("guard.blacklist.b0");
355         otherGip.setPolicyVersion("1");
356         otherGip.setContent(new LegacyGuardPolicyContent());
357
358         Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
359         assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
360         assertEquals(otherGip.getContent(),
361                 createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next());
362
363         assertThatThrownBy(() -> {
364             new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null);
365         }).hasMessage("no policy found for policy: guard.frequency.scaleout:null");
366     }
367
368     private void createPolicyTypes() throws CoderException, PfModelException {
369         Object yamlObject = new Yaml().load(
370                 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
371         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
372
373         ToscaServiceTemplate toscaServiceTemplatePolicyType =
374                 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
375
376         assertNotNull(toscaServiceTemplatePolicyType);
377         new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
378
379         yamlObject = new Yaml()
380                 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.Blacklist.yaml"));
381         yamlAsJsonString = new StandardCoder().encode(yamlObject);
382
383         toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
384
385         assertNotNull(toscaServiceTemplatePolicyType);
386         new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
387     }
388 }