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