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