Allow policy type prefix on policy guard policy id
[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);
102         }).hasMessage("dao is marked @NonNull but is null");
103
104         assertThatThrownBy(() -> {
105             new LegacyProvider().getGuardPolicy(null, "");
106         }).hasMessage("dao is marked @NonNull but is null");
107
108         assertThatThrownBy(() -> {
109             new LegacyProvider().getGuardPolicy(pfDao, null);
110         }).hasMessage("policyId is marked @NonNull but is null");
111
112         assertThatThrownBy(() -> {
113             new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist");
114         }).hasMessage("no policy found for policy ID: I Dont Exist");
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());
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
144     @Test
145     public void testPolicyCreate() throws Exception {
146         assertThatThrownBy(() -> {
147             new LegacyProvider().createGuardPolicy(null, null);
148         }).hasMessage("dao is marked @NonNull but is null");
149
150         assertThatThrownBy(() -> {
151             new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
152         }).hasMessage("dao is marked @NonNull but is null");
153
154         assertThatThrownBy(() -> {
155             new LegacyProvider().createGuardPolicy(pfDao, null);
156         }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
157
158         createPolicyTypes();
159
160         LegacyGuardPolicyInput originalGip = standardCoder.decode(
161                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
162                 LegacyGuardPolicyInput.class);
163
164         assertNotNull(originalGip);
165
166         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
167
168         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
169         assertEquals(originalGip.getContent(),
170                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
171
172         Map<String, LegacyGuardPolicyOutput> gotGopm =
173                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
174
175         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
176         assertEquals(originalGip.getContent(),
177                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
178
179         String expectedJsonOutput =
180                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
181         String actualJsonOutput = standardCoder.encode(gotGopm);
182
183         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
184     }
185
186     @Test
187     public void testPolicyCreateBad() throws Exception {
188         assertThatThrownBy(() -> {
189             new LegacyProvider().createGuardPolicy(null, null);
190         }).hasMessage("dao is marked @NonNull but is null");
191
192         assertThatThrownBy(() -> {
193             new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
194         }).hasMessage("dao is marked @NonNull but is null");
195
196         assertThatThrownBy(() -> {
197             new LegacyProvider().createGuardPolicy(pfDao, null);
198         }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
199
200         createPolicyTypes();
201
202         LegacyGuardPolicyInput originalGip = standardCoder.decode(
203                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
204                 LegacyGuardPolicyInput.class);
205
206         assertNotNull(originalGip);
207
208         originalGip.setPolicyId("i.do.not.exist");
209
210         assertThatThrownBy(() -> {
211             new LegacyProvider().createGuardPolicy(pfDao, originalGip);
212         }).hasMessage("policy type for guard policy \"i.do.not.exist\" unknown");
213     }
214
215     @Test
216     public void testPolicyUpdate() throws Exception {
217         assertThatThrownBy(() -> {
218             new LegacyProvider().updateGuardPolicy(null, null);
219         }).hasMessage("dao is marked @NonNull but is null");
220
221         assertThatThrownBy(() -> {
222             new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
223         }).hasMessage("dao is marked @NonNull but is null");
224
225         assertThatThrownBy(() -> {
226             new LegacyProvider().updateGuardPolicy(pfDao, null);
227         }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
228
229         assertThatThrownBy(() -> {
230             new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
231         }).hasMessage("policy type for guard policy \"null\" unknown");
232
233         createPolicyTypes();
234
235         LegacyGuardPolicyInput originalGip = standardCoder.decode(
236                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
237                 LegacyGuardPolicyInput.class);
238
239         assertNotNull(originalGip);
240
241         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
242         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
243         assertEquals(originalGip.getContent(),
244                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
245
246         Map<String, LegacyGuardPolicyOutput> gotGopm =
247                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
248
249         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
250         assertEquals(originalGip.getContent(),
251                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
252
253         originalGip.getContent().setRecipe("Roast Turkey");
254         Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
255         assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
256         assertEquals(originalGip.getContent(),
257                 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
258
259         Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
260                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
261         assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
262         assertEquals(originalGip.getContent(),
263                 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
264         assertEquals("Roast Turkey",
265                 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
266     }
267
268
269     @Test
270     public void testPoliciesDelete() throws Exception {
271         assertThatThrownBy(() -> {
272             new LegacyProvider().deleteGuardPolicy(null, null);
273         }).hasMessage("dao is marked @NonNull but is null");
274
275         assertThatThrownBy(() -> {
276             new LegacyProvider().deleteGuardPolicy(null, "");
277         }).hasMessage("dao is marked @NonNull but is null");
278
279         assertThatThrownBy(() -> {
280             new LegacyProvider().deleteGuardPolicy(pfDao, null);
281         }).hasMessage("policyId is marked @NonNull but is null");
282
283
284         assertThatThrownBy(() -> {
285             new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist");
286         }).hasMessage("no policy found for policy ID: I Dont Exist");
287
288         createPolicyTypes();
289
290         LegacyGuardPolicyInput originalGip = standardCoder.decode(
291                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
292                 LegacyGuardPolicyInput.class);
293
294         assertNotNull(originalGip);
295
296         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
297         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
298         assertEquals(originalGip.getContent(),
299                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
300
301         Map<String, LegacyGuardPolicyOutput> gotGopm =
302                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
303
304         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
305         assertEquals(originalGip.getContent(),
306                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
307
308         String expectedJsonOutput =
309                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
310         String actualJsonOutput = standardCoder.encode(gotGopm);
311
312         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
313
314         Map<String, LegacyGuardPolicyOutput> deletedGopm =
315                 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId());
316         assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
317         assertEquals(originalGip.getContent(),
318                 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
319
320         assertThatThrownBy(() -> {
321             new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
322         }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
323
324         LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
325         otherGip.setPolicyId("guard.blacklist.b0");
326         otherGip.setPolicyVersion("1");
327         otherGip.setContent(new LegacyGuardPolicyContent());
328
329         Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
330         assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
331         assertEquals(otherGip.getContent(),
332                 createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next());
333
334         assertThatThrownBy(() -> {
335             new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
336         }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
337     }
338
339     private void createPolicyTypes() throws CoderException, PfModelException {
340         Object yamlObject = new Yaml().load(
341                 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
342         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
343
344         ToscaServiceTemplate toscaServiceTemplatePolicyType =
345                 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
346
347         assertNotNull(toscaServiceTemplatePolicyType);
348         new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
349
350         yamlObject = new Yaml()
351                 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.Blacklist.yaml"));
352         yamlAsJsonString = new StandardCoder().encode(yamlObject);
353
354         toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
355
356         assertNotNull(toscaServiceTemplatePolicyType);
357         new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
358     }
359 }