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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.pdp.persistence.provider;
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;
29 import java.sql.Connection;
30 import java.sql.DriverManager;
31 import java.util.ArrayList;
32 import java.util.List;
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;
50 * Test the {@link SimpleToscaProvider} class.
52 * @author Liam Fallon (liam.fallon@est.tech)
54 public class PdpProviderTest {
55 private Connection connection;
57 private StandardCoder standardCoder;
61 * Set up the DAO towards the database.
63 * @throws Exception on database errors
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");
71 final DaoParameters daoParameters = new DaoParameters();
72 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
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");
78 pfDao = new PfDaoFactory().createPfDao(daoParameters);
79 pfDao.init(daoParameters);
86 public void setupGson() {
87 standardCoder = new StandardCoder();
91 public void teardown() throws Exception {
97 public void testGroupsGet() throws Exception {
98 assertThatThrownBy(() -> {
99 new PdpProvider().getPdpGroups(null, null, null);
100 }).hasMessage("dao is marked @NonNull but is null");
102 assertThatThrownBy(() -> {
103 new PdpProvider().getPdpGroups(null, null, "version");
104 }).hasMessage("dao is marked @NonNull but is null");
106 assertThatThrownBy(() -> {
107 new PdpProvider().getPdpGroups(null, "name", "version");
108 }).hasMessage("dao is marked @NonNull but is null");
110 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
111 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
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+", ""));
118 PdpGroups gotPdpGroups0 = new PdpGroups();
119 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
121 String gotJson = standardCoder.encode(gotPdpGroups0);
123 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
127 public void testGroupsCreate() throws Exception {
128 assertThatThrownBy(() -> {
129 new PdpProvider().createPdpGroups(null, null);
130 }).hasMessage("dao is marked @NonNull but is null");
132 assertThatThrownBy(() -> {
133 new PdpProvider().createPdpGroups(null, new ArrayList<>());
134 }).hasMessage("dao is marked @NonNull but is null");
136 assertThatThrownBy(() -> {
137 new PdpProvider().createPdpGroups(pfDao, null);
138 }).hasMessage("pdpGroups is marked @NonNull but is null");
140 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
141 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
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+", ""));
148 PdpGroups gotPdpGroups0 = new PdpGroups();
149 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
151 String gotJson = standardCoder.encode(gotPdpGroups0);
152 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
156 public void testGroupsCreateNoPdp() throws Exception {
157 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsNoPDPs.json");
159 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
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+", ""));
169 PdpGroups gotPdpGroups0 = new PdpGroups();
170 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "TestPdpGroup", "1.2.3"));
172 String gotJson = standardCoder.encode(gotPdpGroups0);
173 assertEquals(originalTweakedJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
177 public void testGroupsUpdate() throws Exception {
178 assertThatThrownBy(() -> {
179 new PdpProvider().updatePdpGroups(null, null);
180 }).hasMessage("dao is marked @NonNull but is null");
182 assertThatThrownBy(() -> {
183 new PdpProvider().updatePdpGroups(null, new ArrayList<>());
184 }).hasMessage("dao is marked @NonNull but is null");
186 assertThatThrownBy(() -> {
187 new PdpProvider().updatePdpGroups(pfDao, null);
188 }).hasMessage("pdpGroups is marked @NonNull but is null");
190 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
191 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
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+", ""));
198 PdpGroups gotPdpGroups0 = new PdpGroups();
199 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
201 String gotJson = standardCoder.encode(gotPdpGroups0);
202 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
204 String updateJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0Update.json");
205 PdpGroups updatePdpGroups0 = standardCoder.decode(updateJson, PdpGroups.class);
207 PdpGroups updatedPdpGroups0 = new PdpGroups();
208 updatedPdpGroups0.setGroups(new PdpProvider().updatePdpGroups(pfDao, updatePdpGroups0.getGroups()));
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));
216 public void testPoliciesDelete() throws Exception {
217 assertThatThrownBy(() -> {
218 new PdpProvider().deletePdpGroup(null, null, null);
219 }).hasMessage("dao is marked @NonNull but is null");
221 assertThatThrownBy(() -> {
222 new PdpProvider().deletePdpGroup(null, null, "version");
223 }).hasMessage("dao is marked @NonNull but is null");
225 assertThatThrownBy(() -> {
226 new PdpProvider().deletePdpGroup(null, "name", "version");
227 }).hasMessage("dao is marked @NonNull but is null");
229 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
230 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
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+", ""));
237 PdpGroups gotPdpGroups0 = new PdpGroups();
238 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
240 String gotJson = standardCoder.encode(gotPdpGroups0);
241 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
243 PdpGroup deletedPdpGroup = new PdpProvider().deletePdpGroup(pfDao, "PdpGroup0", "1.2.3");
245 assertEquals(createdPdpGroups0.getGroups().get(0), deletedPdpGroup);
247 assertEquals(0, new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3").size());
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");