cc1f515665f3ad47c3e17490410bc0973e7a8091
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / legacy / provider / LegacyProvider4LegacyOperationalTest.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.Properties;
28
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.coder.StandardCoder;
33 import org.onap.policy.common.utils.resources.ResourceUtils;
34 import org.onap.policy.models.dao.DaoParameters;
35 import org.onap.policy.models.dao.PfDao;
36 import org.onap.policy.models.dao.PfDaoFactory;
37 import org.onap.policy.models.dao.impl.DefaultPfDao;
38 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
39
40 /**
41  * Test the {@link LegacyProvider} class for legacy operational policies.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class LegacyProvider4LegacyOperationalTest {
46     private PfDao pfDao;
47     private StandardCoder standardCoder;
48
49     /**
50      * Set up the DAO towards the database.
51      *
52      * @throws Exception on database errors
53      */
54     @Before
55     public void setupDao() throws Exception {
56         final DaoParameters daoParameters = new DaoParameters();
57         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
58
59         daoParameters.setPersistenceUnit("ToscaConceptTest");
60
61         Properties jdbcProperties = new Properties();
62         jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
63         jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
64
65         // H2
66         jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
67         jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:testdb");
68
69         // MariaDB
70         //jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
71         //jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
72
73         daoParameters.setJdbcProperties(jdbcProperties );
74
75         pfDao = new PfDaoFactory().createPfDao(daoParameters);
76         pfDao.init(daoParameters);
77     }
78
79     /**
80      * Set up standard coder.
81      */
82     @Before
83     public void setupStandardCoder() {
84         standardCoder = new StandardCoder();
85     }
86
87     @After
88     public void teardown() throws Exception {
89         pfDao.close();
90     }
91
92     @Test
93     public void testPoliciesGet() throws Exception {
94         assertThatThrownBy(() -> {
95             new LegacyProvider().getOperationalPolicy(null, null);
96         }).hasMessage("dao is marked @NonNull but is null");
97
98         assertThatThrownBy(() -> {
99             new LegacyProvider().getOperationalPolicy(null, "");
100         }).hasMessage("dao is marked @NonNull but is null");
101
102         assertThatThrownBy(() -> {
103             new LegacyProvider().getOperationalPolicy(pfDao, null);
104         }).hasMessage("policyId is marked @NonNull but is null");
105
106         assertThatThrownBy(() -> {
107             new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist");
108         }).hasMessage("no policy found for policy ID: I Dont Exist");
109
110         LegacyOperationalPolicy originalLop =
111                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
112                         LegacyOperationalPolicy.class);
113
114         assertNotNull(originalLop);
115
116         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
117
118         assertEquals(originalLop, createdLop);
119
120         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
121
122         assertEquals(gotLop, originalLop);
123
124         String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
125         String actualJsonOutput = standardCoder.encode(gotLop);
126
127         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
128
129         LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
130         LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
131         assertEquals(gotLopV2, createdLopV2);
132     }
133
134     @Test
135     public void testPolicyCreate() throws Exception {
136         assertThatThrownBy(() -> {
137             new LegacyProvider().createOperationalPolicy(null, null);
138         }).hasMessage("dao is marked @NonNull but is null");
139
140         assertThatThrownBy(() -> {
141             new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
142         }).hasMessage("dao is marked @NonNull but is null");
143
144         assertThatThrownBy(() -> {
145             new LegacyProvider().createOperationalPolicy(pfDao, null);
146         }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
147
148         LegacyOperationalPolicy originalLop =
149                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
150                         LegacyOperationalPolicy.class);
151
152         assertNotNull(originalLop);
153
154         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
155
156         assertEquals(originalLop, createdLop);
157
158         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
159
160         assertEquals(gotLop, originalLop);
161
162         String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
163         String actualJsonOutput = standardCoder.encode(gotLop);
164
165         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
166     }
167
168
169     @Test
170     public void testPolicyUpdate() throws Exception {
171         assertThatThrownBy(() -> {
172             new LegacyProvider().updateOperationalPolicy(null, null);
173         }).hasMessage("dao is marked @NonNull but is null");
174
175         assertThatThrownBy(() -> {
176             new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
177         }).hasMessage("dao is marked @NonNull but is null");
178
179         assertThatThrownBy(() -> {
180             new LegacyProvider().updateOperationalPolicy(pfDao, null);
181         }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
182
183         assertThatThrownBy(() -> {
184             new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
185         }).hasMessage("no policy found for policy ID: null");
186
187         LegacyOperationalPolicy originalLop =
188                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
189                         LegacyOperationalPolicy.class);
190
191         assertNotNull(originalLop);
192
193         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
194         assertEquals(originalLop, createdLop);
195
196         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
197         assertEquals(gotLop, originalLop);
198
199         originalLop.setContent("Some New Content");
200         LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
201         assertEquals(originalLop, updatedLop);
202
203         LegacyOperationalPolicy gotUpdatedLop =
204                 new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
205         assertEquals(gotUpdatedLop, originalLop);
206         assertEquals("Some New Content", gotUpdatedLop.getContent());
207     }
208
209
210     @Test
211     public void testPoliciesDelete() throws Exception {
212         assertThatThrownBy(() -> {
213             new LegacyProvider().deleteOperationalPolicy(null, null);
214         }).hasMessage("dao is marked @NonNull but is null");
215
216         assertThatThrownBy(() -> {
217             new LegacyProvider().deleteOperationalPolicy(null, "");
218         }).hasMessage("dao is marked @NonNull but is null");
219
220         assertThatThrownBy(() -> {
221             new LegacyProvider().deleteOperationalPolicy(pfDao, null);
222         }).hasMessage("policyId is marked @NonNull but is null");
223
224
225         assertThatThrownBy(() -> {
226             new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist");
227         }).hasMessage("no policy found for policy ID: I Dont Exist");
228
229         LegacyOperationalPolicy originalLop =
230                 standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
231                         LegacyOperationalPolicy.class);
232
233         assertNotNull(originalLop);
234
235         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
236         assertEquals(originalLop, createdLop);
237
238         LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
239
240         assertEquals(gotLop, originalLop);
241
242         String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
243         String actualJsonOutput = standardCoder.encode(gotLop);
244
245         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
246
247         LegacyOperationalPolicy deletedLop =
248                 new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId());
249         assertEquals(originalLop, deletedLop);
250
251         assertThatThrownBy(() -> {
252             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
253         }).hasMessage("no policy found for policy ID: operational.restart");
254
255         LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
256         otherLop.setPolicyId("another-policy");
257         otherLop.setPolicyVersion("1");
258         otherLop.setContent("content");
259
260         LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
261         assertEquals(otherLop, createdOtherLop);
262
263         assertThatThrownBy(() -> {
264             new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
265         }).hasMessage("no policy found for policy ID: operational.restart");
266
267     }
268 }