Add support for legacy guard policies
[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.sql.Connection;
28 import java.sql.DriverManager;
29 import java.util.Map;
30
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.models.dao.DaoParameters;
37 import org.onap.policy.models.dao.PfDao;
38 import org.onap.policy.models.dao.PfDaoFactory;
39 import org.onap.policy.models.dao.impl.DefaultPfDao;
40 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent;
41 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
42 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
43
44 /**
45  * Test the {@link LegacyProvider} class for legacy guard policies.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 public class LegacyProvider4LegacyGuardTest {
50     private Connection connection;
51     private PfDao pfDao;
52     private StandardCoder standardCoder;
53
54
55     /**
56      * Set up the DAO towards the database.
57      *
58      * @throws Exception on database errors
59      */
60     @Before
61     public void setupDao() throws Exception {
62         // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
63         // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
64         connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
65
66         final DaoParameters daoParameters = new DaoParameters();
67         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
68
69         // Use the persistence unit ToscaConceptTest to test towards the h2 database
70         // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
71         daoParameters.setPersistenceUnit("ToscaConceptTest");
72
73         pfDao = new PfDaoFactory().createPfDao(daoParameters);
74         pfDao.init(daoParameters);
75     }
76
77     /**
78      * Set up standard coder.
79      */
80     @Before
81     public void setupStandardCoder() {
82         standardCoder = new StandardCoder();
83     }
84
85     @After
86     public void teardown() throws Exception {
87         pfDao.close();
88         connection.close();
89     }
90
91     @Test
92     public void testPoliciesGet() throws Exception {
93         assertThatThrownBy(() -> {
94             new LegacyProvider().getGuardPolicy(null, null);
95         }).hasMessage("dao is marked @NonNull but is null");
96
97         assertThatThrownBy(() -> {
98             new LegacyProvider().getGuardPolicy(null, "");
99         }).hasMessage("dao is marked @NonNull but is null");
100
101         assertThatThrownBy(() -> {
102             new LegacyProvider().getGuardPolicy(pfDao, null);
103         }).hasMessage("policyId is marked @NonNull but is null");
104
105         assertThatThrownBy(() -> {
106             new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist");
107         }).hasMessage("no policy found for policy ID: I Dont Exist");
108
109         LegacyGuardPolicyInput originalGip = standardCoder.decode(
110                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
111                 LegacyGuardPolicyInput.class);
112
113         assertNotNull(originalGip);
114
115         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
116
117         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
118         assertEquals(originalGip.getContent(),
119                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
120
121         Map<String, LegacyGuardPolicyOutput> gotGopm =
122                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
123
124         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
125         assertEquals(originalGip.getContent(),
126                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
127
128         String expectedJsonOutput =
129                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
130         String actualJsonOutput = standardCoder.encode(gotGopm);
131
132         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
133     }
134
135     @Test
136     public void testPolicyCreate() throws Exception {
137         assertThatThrownBy(() -> {
138             new LegacyProvider().createGuardPolicy(null, null);
139         }).hasMessage("dao is marked @NonNull but is null");
140
141         assertThatThrownBy(() -> {
142             new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
143         }).hasMessage("dao is marked @NonNull but is null");
144
145         assertThatThrownBy(() -> {
146             new LegacyProvider().createGuardPolicy(pfDao, null);
147         }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
148
149         LegacyGuardPolicyInput originalGip = standardCoder.decode(
150                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
151                 LegacyGuardPolicyInput.class);
152
153         assertNotNull(originalGip);
154
155         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
156
157         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
158         assertEquals(originalGip.getContent(),
159                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
160
161         Map<String, LegacyGuardPolicyOutput> gotGopm =
162                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
163
164         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
165         assertEquals(originalGip.getContent(),
166                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
167
168         String expectedJsonOutput =
169                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
170         String actualJsonOutput = standardCoder.encode(gotGopm);
171
172         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
173     }
174
175
176     @Test
177     public void testPolicyUpdate() throws Exception {
178         assertThatThrownBy(() -> {
179             new LegacyProvider().updateGuardPolicy(null, null);
180         }).hasMessage("dao is marked @NonNull but is null");
181
182         assertThatThrownBy(() -> {
183             new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput());
184         }).hasMessage("dao is marked @NonNull but is null");
185
186         assertThatThrownBy(() -> {
187             new LegacyProvider().updateGuardPolicy(pfDao, null);
188         }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
189
190         assertThatThrownBy(() -> {
191             new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
192         }).hasMessage("policy type for guard policy \"null\" unknown");
193
194         LegacyGuardPolicyInput originalGip = standardCoder.decode(
195                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
196                 LegacyGuardPolicyInput.class);
197
198         assertNotNull(originalGip);
199
200         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
201         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
202         assertEquals(originalGip.getContent(),
203                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
204
205         Map<String, LegacyGuardPolicyOutput> gotGopm =
206                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
207
208         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
209         assertEquals(originalGip.getContent(),
210                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
211
212         originalGip.getContent().setRecipe("Roast Turkey");
213         Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip);
214         assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next());
215         assertEquals(originalGip.getContent(),
216                 updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
217
218         Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm =
219                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
220         assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next());
221         assertEquals(originalGip.getContent(),
222                 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
223         assertEquals("Roast Turkey",
224                 gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe());
225     }
226
227
228     @Test
229     public void testPoliciesDelete() throws Exception {
230         assertThatThrownBy(() -> {
231             new LegacyProvider().deleteGuardPolicy(null, null);
232         }).hasMessage("dao is marked @NonNull but is null");
233
234         assertThatThrownBy(() -> {
235             new LegacyProvider().deleteGuardPolicy(null, "");
236         }).hasMessage("dao is marked @NonNull but is null");
237
238         assertThatThrownBy(() -> {
239             new LegacyProvider().deleteGuardPolicy(pfDao, null);
240         }).hasMessage("policyId is marked @NonNull but is null");
241
242
243         assertThatThrownBy(() -> {
244             new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist");
245         }).hasMessage("no policy found for policy ID: I Dont Exist");
246
247         LegacyGuardPolicyInput originalGip = standardCoder.decode(
248                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
249                 LegacyGuardPolicyInput.class);
250
251         assertNotNull(originalGip);
252
253         Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip);
254         assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next());
255         assertEquals(originalGip.getContent(),
256                 createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
257
258         Map<String, LegacyGuardPolicyOutput> gotGopm =
259                 new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
260
261         assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next());
262         assertEquals(originalGip.getContent(),
263                 gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
264
265         String expectedJsonOutput =
266                 ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json");
267         String actualJsonOutput = standardCoder.encode(gotGopm);
268
269         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
270
271         Map<String, LegacyGuardPolicyOutput> deletedGopm =
272                 new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId());
273         assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next());
274         assertEquals(originalGip.getContent(),
275                 deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next());
276
277         assertThatThrownBy(() -> {
278             new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
279         }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
280
281         LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
282         otherGip.setPolicyId("guard.blacklist");
283         otherGip.setPolicyVersion("1");
284         otherGip.setContent(new LegacyGuardPolicyContent());
285
286         Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip);
287         assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next());
288         assertEquals(otherGip.getContent(),
289                 createdOtherGopm.get(otherGip.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 }