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