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