Set default and check existance of Policy Type
[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 testPolicyUpdate() throws Exception {
188         assertThatThrownBy(() -> {
189             new LegacyProvider().updateGuardPolicy(null, null);
190         }).hasMessage("dao is marked @NonNull but is null");
191
192         assertThatThrownBy(() -> {
193             new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
194         }).hasMessage("dao is marked @NonNull but is null");
195
196         assertThatThrownBy(() -> {
197             new LegacyProvider().updateGuardPolicy(pfDao, null);
198         }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
199
200         assertThatThrownBy(() -> {
201             new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
202         }).hasMessage("policy type for guard policy \"null\" unknown");
203
204         createPolicyTypes();
205
206         LegacyGuardPolicyInput originalGip = standardCoder.decode(
207                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
208                 LegacyGuardPolicyInput.class);
209
210         assertNotNull(originalGip);
211
212         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
213         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
214         assertEquals(originalGip.getContent(),
215                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
216
217         Map<String, LegacyGuardPolicyOutput> gotGopm =
218                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
219
220         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
221         assertEquals(originalGip.getContent(),
222                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
223
224         originalGip.getContent().setRecipe("Roast Turkey");
225         Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
226         assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
227         assertEquals(originalGip.getContent(),
228                 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
229
230         Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
231                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
232         assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
233         assertEquals(originalGip.getContent(),
234                 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
235         assertEquals("Roast Turkey",
236                 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
237     }
238
239
240     @Test
241     public void testPoliciesDelete() throws Exception {
242         assertThatThrownBy(() -> {
243             new LegacyProvider().deleteGuardPolicy(null, null);
244         }).hasMessage("dao is marked @NonNull but is null");
245
246         assertThatThrownBy(() -> {
247             new LegacyProvider().deleteGuardPolicy(null, "");
248         }).hasMessage("dao is marked @NonNull but is null");
249
250         assertThatThrownBy(() -> {
251             new LegacyProvider().deleteGuardPolicy(pfDao, null);
252         }).hasMessage("policyId is marked @NonNull but is null");
253
254
255         assertThatThrownBy(() -> {
256             new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist");
257         }).hasMessage("no policy found for policy ID: I Dont Exist");
258
259         createPolicyTypes();
260
261         LegacyGuardPolicyInput originalGip = standardCoder.decode(
262                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
263                 LegacyGuardPolicyInput.class);
264
265         assertNotNull(originalGip);
266
267         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
268         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
269         assertEquals(originalGip.getContent(),
270                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
271
272         Map<String, LegacyGuardPolicyOutput> gotGopm =
273                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
274
275         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
276         assertEquals(originalGip.getContent(),
277                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
278
279         String expectedJsonOutput =
280                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
281         String actualJsonOutput = standardCoder.encode(gotGopm);
282
283         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
284
285         Map<String, LegacyGuardPolicyOutput> deletedGopm =
286                 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId());
287         assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
288         assertEquals(originalGip.getContent(),
289                 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
290
291         assertThatThrownBy(() -> {
292             new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
293         }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
294
295         LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
296         otherGip.setPolicyId("guard.blacklist");
297         otherGip.setPolicyVersion("1");
298         otherGip.setContent(new LegacyGuardPolicyContent());
299
300         Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
301         assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
302         assertEquals(otherGip.getContent(),
303                 createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next());
304
305         assertThatThrownBy(() -> {
306             new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
307         }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
308     }
309
310     private void createPolicyTypes() throws CoderException, PfModelException {
311         Object yamlObject = new Yaml().load(
312                 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
313         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
314
315         ToscaServiceTemplate toscaServiceTemplatePolicyType =
316                 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
317
318         assertNotNull(toscaServiceTemplatePolicyType);
319         new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
320
321         yamlObject = new Yaml()
322                 .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.Blacklist.yaml"));
323         yamlAsJsonString = new StandardCoder().encode(yamlObject);
324
325         toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
326
327         assertNotNull(toscaServiceTemplatePolicyType);
328         new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
329     }
330 }