Merge "Add bug fixes and tests for filters"
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / persistence / provider / PdpProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pdp.persistence.provider;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertTrue;
28
29 import java.sql.Connection;
30 import java.sql.DriverManager;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.dao.DaoParameters;
40 import org.onap.policy.models.dao.PfDao;
41 import org.onap.policy.models.dao.PfDaoFactory;
42 import org.onap.policy.models.dao.impl.DefaultPfDao;
43 import org.onap.policy.models.pdp.concepts.Pdp;
44 import org.onap.policy.models.pdp.concepts.PdpGroup;
45 import org.onap.policy.models.pdp.concepts.PdpGroups;
46 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
47 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
48
49 /**
50  * Test the {@link SimpleToscaProvider} class.
51  *
52  * @author Liam Fallon (liam.fallon@est.tech)
53  */
54 public class PdpProviderTest {
55     private Connection connection;
56     private PfDao pfDao;
57     private StandardCoder standardCoder;
58
59
60     /**
61      * Set up the DAO towards the database.
62      *
63      * @throws Exception on database errors
64      */
65     @Before
66     public void setupDao() throws Exception {
67         // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
68         // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
69         connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
70
71         final DaoParameters daoParameters = new DaoParameters();
72         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
73
74         // Use the persistence unit ToscaConceptTest to test towards the h2 database
75         // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
76         daoParameters.setPersistenceUnit("ToscaConceptTest");
77
78         pfDao = new PfDaoFactory().createPfDao(daoParameters);
79         pfDao.init(daoParameters);
80     }
81
82     /**
83      * Set up GSON.
84      */
85     @Before
86     public void setupGson() {
87         standardCoder = new StandardCoder();
88     }
89
90     @After
91     public void teardown() throws Exception {
92         pfDao.close();
93         connection.close();
94     }
95
96     @Test
97     public void testGroupsGet() throws Exception {
98         assertThatThrownBy(() -> {
99             new PdpProvider().getPdpGroups(null, null, null);
100         }).hasMessage("dao is marked @NonNull but is null");
101
102         assertThatThrownBy(() -> {
103             new PdpProvider().getPdpGroups(null, null, "version");
104         }).hasMessage("dao is marked @NonNull but is null");
105
106         assertThatThrownBy(() -> {
107             new PdpProvider().getPdpGroups(null, "name", "version");
108         }).hasMessage("dao is marked @NonNull but is null");
109
110         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
111         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
112
113         PdpGroups createdPdpGroups0 = new PdpGroups();
114         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
115         String createdJson = standardCoder.encode(createdPdpGroups0);
116         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
117
118         PdpGroups gotPdpGroups0 = new PdpGroups();
119         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
120
121         String gotJson = standardCoder.encode(gotPdpGroups0);
122
123         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
124     }
125
126     @Test
127     public void testGroupsCreate() throws Exception {
128         assertThatThrownBy(() -> {
129             new PdpProvider().createPdpGroups(null, null);
130         }).hasMessage("dao is marked @NonNull but is null");
131
132         assertThatThrownBy(() -> {
133             new PdpProvider().createPdpGroups(null, new ArrayList<>());
134         }).hasMessage("dao is marked @NonNull but is null");
135
136         assertThatThrownBy(() -> {
137             new PdpProvider().createPdpGroups(pfDao, null);
138         }).hasMessage("pdpGroups is marked @NonNull but is null");
139
140         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
141         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
142
143         PdpGroups createdPdpGroups0 = new PdpGroups();
144         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
145         String createdJson = standardCoder.encode(createdPdpGroups0);
146         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
147
148         PdpGroups gotPdpGroups0 = new PdpGroups();
149         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
150
151         String gotJson = standardCoder.encode(gotPdpGroups0);
152         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
153     }
154
155     @Test
156     public void testGroupsCreateNoPdp() throws Exception {
157         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsNoPDPs.json");
158
159         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
160
161         PdpGroups createdPdpGroups0 = new PdpGroups();
162         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
163         assertNotEquals(pdpGroups0, createdPdpGroups0);
164         pdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).setPdpInstances(new ArrayList<>());
165         String originalTweakedJson = standardCoder.encode(pdpGroups0);
166         String createdJson = standardCoder.encode(createdPdpGroups0);
167         assertEquals(originalTweakedJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
168
169         PdpGroups gotPdpGroups0 = new PdpGroups();
170         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "TestPdpGroup", "1.2.3"));
171
172         String gotJson = standardCoder.encode(gotPdpGroups0);
173         assertEquals(originalTweakedJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
174     }
175
176     @Test
177     public void testGroupsUpdate() throws Exception {
178         assertThatThrownBy(() -> {
179             new PdpProvider().updatePdpGroups(null, null);
180         }).hasMessage("dao is marked @NonNull but is null");
181
182         assertThatThrownBy(() -> {
183             new PdpProvider().updatePdpGroups(null, new ArrayList<>());
184         }).hasMessage("dao is marked @NonNull but is null");
185
186         assertThatThrownBy(() -> {
187             new PdpProvider().updatePdpGroups(pfDao, null);
188         }).hasMessage("pdpGroups is marked @NonNull but is null");
189
190         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
191         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
192
193         PdpGroups createdPdpGroups0 = new PdpGroups();
194         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
195         String createdJson = standardCoder.encode(createdPdpGroups0);
196         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
197
198         PdpGroups gotPdpGroups0 = new PdpGroups();
199         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
200
201         String gotJson = standardCoder.encode(gotPdpGroups0);
202         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
203
204         String updateJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0Update.json");
205         PdpGroups updatePdpGroups0 = standardCoder.decode(updateJson, PdpGroups.class);
206
207         PdpGroups updatedPdpGroups0 = new PdpGroups();
208         updatedPdpGroups0.setGroups(new PdpProvider().updatePdpGroups(pfDao, updatePdpGroups0.getGroups()));
209
210         List<Pdp> beforePdpInstances = updatePdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances();
211         List<Pdp> afterPdpInstances = updatedPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances();
212         assertTrue(beforePdpInstances.containsAll(afterPdpInstances));
213     }
214
215     @Test
216     public void testPoliciesDelete() throws Exception {
217         assertThatThrownBy(() -> {
218             new PdpProvider().deletePdpGroup(null, null, null);
219         }).hasMessage("dao is marked @NonNull but is null");
220
221         assertThatThrownBy(() -> {
222             new PdpProvider().deletePdpGroup(null, null, "version");
223         }).hasMessage("dao is marked @NonNull but is null");
224
225         assertThatThrownBy(() -> {
226             new PdpProvider().deletePdpGroup(null, "name", "version");
227         }).hasMessage("dao is marked @NonNull but is null");
228
229         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
230         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
231
232         PdpGroups createdPdpGroups0 = new PdpGroups();
233         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
234         String createdJson = standardCoder.encode(createdPdpGroups0);
235         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
236
237         PdpGroups gotPdpGroups0 = new PdpGroups();
238         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
239
240         String gotJson = standardCoder.encode(gotPdpGroups0);
241         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
242
243         PdpGroup deletedPdpGroup = new PdpProvider().deletePdpGroup(pfDao, "PdpGroup0", "1.2.3");
244
245         assertEquals(createdPdpGroups0.getGroups().get(0), deletedPdpGroup);
246
247         assertEquals(0, new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3").size());
248
249         assertThatThrownBy(() -> {
250             new PdpProvider().deletePdpGroup(pfDao, "PdpGroup0", "1.2.3");
251         }).hasMessage("delete of PDP group \"PdpGroup0:1.2.3\" failed, PDP group does not exist");
252     }
253 }