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