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.dao;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Properties;
34 import java.util.TreeSet;
35 import java.util.UUID;
36 import org.eclipse.persistence.config.PersistenceUnitProperties;
37 import org.junit.Test;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.base.PfReferenceKey;
41 import org.onap.policy.models.dao.impl.DefaultPfDao;
46 public class EntityTest {
47 private static final String DESCRIPTION2 = "key description 2";
48 private static final String DESCRIPTION1 = "key description 1";
49 private static final String DESCRIPTION0 = "key description 0";
50 private static final String ENTITY0 = "Entity0";
51 private static final String UUID2 = "00000000-0000-0000-0000-000000000002";
52 private static final String UUID1 = "00000000-0000-0000-0000-000000000001";
53 private static final String UUID0 = "00000000-0000-0000-0000-000000000000";
54 private static final String VERSION003 = "0.0.3";
55 private static final String VERSION002 = "0.0.2";
56 private static final String VERSION001 = "0.0.1";
60 public void testEntityTestSanity() throws PfModelException {
61 final DaoParameters daoParameters = new DaoParameters();
63 Properties jdbcProperties = new Properties();
65 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
66 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
67 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "sa");
68 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "");
71 daoParameters.setJdbcProperties(jdbcProperties);
73 pfDao = new PfDaoFactory().createPfDao(daoParameters);
75 assertThatThrownBy(() -> pfDao.init(null)).hasMessage("Policy Framework persistence unit parameter not set");
77 assertThatThrownBy(() -> pfDao.init(daoParameters))
78 .hasMessage("Policy Framework persistence unit parameter not set");
80 daoParameters.setPluginClass("somewhere.over.the.rainbow");
81 daoParameters.setPersistenceUnit("Dorothy");
83 assertThatThrownBy(() -> pfDao.init(daoParameters))
84 .hasMessage("Creation of Policy Framework persistence unit \"Dorothy\" failed");
86 assertThatThrownBy(() -> pfDao.create(new PfConceptKey()))
87 .hasMessage("Policy Framework DAO has not been initialized");
93 public void testEntityTestAllOpsJpa() throws PfModelException {
95 final DaoParameters daoParameters = new DaoParameters();
96 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
97 daoParameters.setPersistenceUnit("DaoTest");
99 Properties jdbcProperties = new Properties();
100 jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
101 jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:testdb");
102 jdbcProperties.setProperty("javax.persistence.jdbc.user", "sa");
103 jdbcProperties.setProperty("javax.persistence.jdbc.password", "");
105 daoParameters.setJdbcProperties(jdbcProperties);
107 pfDao = new PfDaoFactory().createPfDao(daoParameters);
108 pfDao.init(daoParameters);
114 testgetFilteredOps();
120 public void testEntityTestBadVals() throws PfModelException {
121 final DaoParameters daoParameters = new DaoParameters();
122 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
123 daoParameters.setPersistenceUnit("DaoTest");
125 pfDao = new PfDaoFactory().createPfDao(daoParameters);
126 pfDao.init(daoParameters);
128 final PfConceptKey nullKey = null;
129 final PfReferenceKey nullRefKey = null;
130 final List<PfConceptKey> nullKeyList = null;
131 final List<PfConceptKey> emptyKeyList = new ArrayList<>();
132 final List<PfReferenceKey> nullRKeyList = null;
133 final List<PfReferenceKey> emptyRKeyList = new ArrayList<>();
135 pfDao.create(nullKey);
136 pfDao.createCollection(nullKeyList);
137 pfDao.createCollection(emptyKeyList);
139 pfDao.delete(nullKey);
140 pfDao.deleteCollection(nullKeyList);
141 pfDao.deleteCollection(emptyKeyList);
142 pfDao.delete(PfConceptKey.class, nullKey);
143 pfDao.delete(PfReferenceKey.class, nullRefKey);
144 pfDao.deleteByConceptKey(PfConceptKey.class, nullKeyList);
145 pfDao.deleteByConceptKey(PfConceptKey.class, emptyKeyList);
146 pfDao.deleteByReferenceKey(PfReferenceKey.class, nullRKeyList);
147 pfDao.deleteByReferenceKey(PfReferenceKey.class, emptyRKeyList);
149 pfDao.get(null, nullKey);
150 pfDao.get(null, nullRefKey);
152 pfDao.getAll(null, nullKey);
153 pfDao.getConcept(null, nullKey);
154 pfDao.getConcept(PfConceptKey.class, nullKey);
155 pfDao.getConcept(null, nullRefKey);
156 pfDao.getConcept(PfReferenceKey.class, nullRefKey);
162 private void testAllOps() {
163 final PfConceptKey aKey0 = new PfConceptKey("A-KEY0", VERSION001);
164 final PfConceptKey aKey1 = new PfConceptKey("A-KEY1", VERSION001);
165 final PfConceptKey aKey2 = new PfConceptKey("A-KEY2", VERSION001);
166 final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
167 UUID.fromString(UUID0), DESCRIPTION0);
168 final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
169 UUID.fromString(UUID1), DESCRIPTION1);
170 final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
171 UUID.fromString(UUID2), DESCRIPTION2);
173 pfDao.create(keyInfo0);
175 final DummyConceptEntity keyInfoBack0 = pfDao.get(DummyConceptEntity.class, aKey0);
176 assertTrue(keyInfo0.equals(keyInfoBack0));
178 final DummyConceptEntity keyInfoBackNull = pfDao.get(DummyConceptEntity.class, PfConceptKey.getNullKey());
179 assertNull(keyInfoBackNull);
181 final DummyConceptEntity keyInfoBack1 = pfDao.getConcept(DummyConceptEntity.class, aKey0);
182 assertTrue(keyInfoBack0.equals(keyInfoBack1));
184 final DummyConceptEntity keyInfoBack2 =
185 pfDao.getConcept(DummyConceptEntity.class, new PfConceptKey("A-KEY3", VERSION001));
186 assertNull(keyInfoBack2);
188 final Set<DummyConceptEntity> keyInfoSetIn = new TreeSet<>();
189 keyInfoSetIn.add(keyInfo1);
190 keyInfoSetIn.add(keyInfo2);
192 pfDao.createCollection(keyInfoSetIn);
194 Set<DummyConceptEntity> keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
196 keyInfoSetIn.add(keyInfo0);
197 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
199 pfDao.delete(keyInfo1);
200 keyInfoSetIn.remove(keyInfo1);
201 keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
202 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
204 pfDao.deleteCollection(keyInfoSetIn);
205 keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
206 assertEquals(0, keyInfoSetOut.size());
208 keyInfoSetIn.add(keyInfo0);
209 keyInfoSetIn.add(keyInfo1);
210 keyInfoSetIn.add(keyInfo0);
211 pfDao.createCollection(keyInfoSetIn);
212 keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
213 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
215 pfDao.delete(DummyConceptEntity.class, aKey0);
216 keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
217 assertEquals(2, keyInfoSetOut.size());
218 assertEquals(2, pfDao.size(DummyConceptEntity.class));
220 final Set<PfConceptKey> keySetIn = new TreeSet<>();
224 final int deletedCount = pfDao.deleteByConceptKey(DummyConceptEntity.class, keySetIn);
225 assertEquals(2, deletedCount);
227 keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
228 assertEquals(0, keyInfoSetOut.size());
230 keyInfoSetIn.add(keyInfo0);
231 keyInfoSetIn.add(keyInfo1);
232 keyInfoSetIn.add(keyInfo0);
233 pfDao.createCollection(keyInfoSetIn);
234 keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
235 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
237 pfDao.deleteAll(DummyConceptEntity.class);
238 assertEquals(0, pfDao.size(DummyConceptEntity.class));
240 final PfConceptKey owner0Key = new PfConceptKey("Owner0", VERSION001);
241 final PfConceptKey owner1Key = new PfConceptKey("Owner1", VERSION001);
242 final PfConceptKey owner2Key = new PfConceptKey("Owner2", VERSION001);
243 final PfConceptKey owner3Key = new PfConceptKey("Owner3", VERSION001);
244 final PfConceptKey owner4Key = new PfConceptKey("Owner4", VERSION001);
245 final PfConceptKey owner5Key = new PfConceptKey("Owner5", VERSION001);
247 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, ENTITY0), 100.0));
248 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity1"), 101.0));
249 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity2"), 102.0));
250 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity3"), 103.0));
251 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity4"), 104.0));
252 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity5"), 105.0));
253 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity6"), 106.0));
254 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity7"), 107.0));
255 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity8"), 108.0));
256 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity9"), 109.0));
257 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner3Key, "EntityA"), 110.0));
258 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner4Key, "EntityB"), 111.0));
259 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityC"), 112.0));
260 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityD"), 113.0));
261 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityE"), 114.0));
262 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 115.0));
264 TreeSet<DummyReferenceEntity> testEntitySetOut =
265 new TreeSet<>(pfDao.getAll(DummyReferenceEntity.class));
266 assertEquals(16, testEntitySetOut.size());
268 testEntitySetOut = new TreeSet<>(pfDao.getAll(DummyReferenceEntity.class, owner0Key));
269 assertEquals(5, testEntitySetOut.size());
271 testEntitySetOut = new TreeSet<>(pfDao.getAll(DummyReferenceEntity.class, owner1Key));
272 assertEquals(3, testEntitySetOut.size());
274 testEntitySetOut = new TreeSet<>(pfDao.getAll(DummyReferenceEntity.class, owner2Key));
275 assertEquals(2, testEntitySetOut.size());
277 testEntitySetOut = new TreeSet<>(pfDao.getAll(DummyReferenceEntity.class, owner3Key));
278 assertEquals(1, testEntitySetOut.size());
280 testEntitySetOut = new TreeSet<>(pfDao.getAll(DummyReferenceEntity.class, owner4Key));
281 assertEquals(1, testEntitySetOut.size());
283 testEntitySetOut = new TreeSet<>(pfDao.getAll(DummyReferenceEntity.class, owner5Key));
284 assertEquals(4, testEntitySetOut.size());
286 assertNotNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, ENTITY0)));
287 assertNotNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, ENTITY0)));
288 assertNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
289 assertNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
290 pfDao.delete(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, ENTITY0));
292 final Set<PfReferenceKey> rKeySetIn = new TreeSet<>();
293 rKeySetIn.add(new PfReferenceKey(owner4Key, "EntityB"));
294 rKeySetIn.add(new PfReferenceKey(owner5Key, "EntityD"));
296 final int deletedRCount = pfDao.deleteByReferenceKey(DummyReferenceEntity.class, rKeySetIn);
297 assertEquals(2, deletedRCount);
299 pfDao.update(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 120.0));
302 private void testVersionOps() {
303 final PfConceptKey aKey0 = new PfConceptKey("AAA0", VERSION001);
304 final PfConceptKey aKey1 = new PfConceptKey("AAA0", VERSION002);
305 final PfConceptKey aKey2 = new PfConceptKey("AAA0", VERSION003);
306 final PfConceptKey bKey0 = new PfConceptKey("BBB0", VERSION001);
307 final PfConceptKey bKey1 = new PfConceptKey("BBB0", VERSION002);
308 final PfConceptKey bKey2 = new PfConceptKey("BBB0", VERSION003);
309 final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
310 UUID.fromString(UUID0), DESCRIPTION0);
311 final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
312 UUID.fromString(UUID1), DESCRIPTION1);
313 final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
314 UUID.fromString(UUID2), DESCRIPTION2);
315 final DummyConceptEntity keyInfo3 = new DummyConceptEntity(bKey0,
316 UUID.fromString(UUID0), DESCRIPTION0);
317 final DummyConceptEntity keyInfo4 = new DummyConceptEntity(bKey1,
318 UUID.fromString(UUID1), DESCRIPTION1);
319 final DummyConceptEntity keyInfo5 = new DummyConceptEntity(bKey2,
320 UUID.fromString(UUID2), DESCRIPTION2);
322 pfDao.create(keyInfo0);
323 pfDao.create(keyInfo1);
324 pfDao.create(keyInfo2);
325 pfDao.create(keyInfo3);
326 pfDao.create(keyInfo4);
327 pfDao.create(keyInfo5);
329 assertEquals(3, pfDao.getAllVersions(DummyConceptEntity.class, "AAA0").size());
330 assertEquals(0, pfDao.getAllVersions(null, "AAA0").size());
331 assertEquals(0, pfDao.getAllVersions(DummyConceptEntity.class, null).size());
334 private void testgetFilteredOps() {
335 final PfConceptKey aKey0 = new PfConceptKey("AAA0", VERSION001);
336 final PfConceptKey aKey1 = new PfConceptKey("AAA0", VERSION002);
337 final PfConceptKey aKey2 = new PfConceptKey("AAA0", VERSION003);
338 final PfConceptKey bKey0 = new PfConceptKey("BBB0", VERSION001);
339 final PfConceptKey bKey1 = new PfConceptKey("BBB0", VERSION002);
340 final PfConceptKey bKey2 = new PfConceptKey("BBB0", VERSION003);
341 final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
342 UUID.fromString(UUID0), DESCRIPTION0);
343 final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
344 UUID.fromString(UUID1), DESCRIPTION1);
345 final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
346 UUID.fromString(UUID2), DESCRIPTION2);
347 final DummyConceptEntity keyInfo3 = new DummyConceptEntity(bKey0,
348 UUID.fromString(UUID0), DESCRIPTION0);
349 final DummyConceptEntity keyInfo4 = new DummyConceptEntity(bKey1,
350 UUID.fromString(UUID1), DESCRIPTION1);
351 final DummyConceptEntity keyInfo5 = new DummyConceptEntity(bKey2,
352 UUID.fromString(UUID2), DESCRIPTION2);
354 pfDao.create(keyInfo0);
355 pfDao.create(keyInfo1);
356 pfDao.create(keyInfo2);
357 pfDao.create(keyInfo3);
358 pfDao.create(keyInfo4);
359 pfDao.create(keyInfo5);
361 assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, null).size());
362 assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "AAA0", null).size());
363 assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", null).size());
364 assertEquals(1, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", VERSION003).size());
365 assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, VERSION003).size());