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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.dao;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.sql.Connection;
30 import java.sql.DriverManager;
31 import java.util.ArrayList;
32 import java.util.List;
34 import java.util.TreeSet;
35 import java.util.UUID;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfModelException;
42 import org.onap.policy.models.base.PfReferenceKey;
43 import org.onap.policy.models.dao.DaoParameters;
44 import org.onap.policy.models.dao.PfDao;
45 import org.onap.policy.models.dao.PfDaoFactory;
46 import org.onap.policy.models.dao.impl.DefaultPfDao;
51 public class EntityTest {
52 private Connection connection;
56 public void setup() throws Exception {
57 connection = DriverManager.getConnection("jdbc:h2:mem:test");
61 public void teardown() throws Exception {
66 public void testEntityTestSanity() throws PfModelException {
67 final DaoParameters daoParameters = new DaoParameters();
69 pfDao = new PfDaoFactory().createPfDao(daoParameters);
73 fail("Test should throw an exception here");
74 } catch (final Exception e) {
75 assertEquals("Policy Framework persistence unit parameter not set", e.getMessage());
79 pfDao.init(daoParameters);
80 fail("Test should throw an exception here");
81 } catch (final Exception e) {
82 assertEquals("Policy Framework persistence unit parameter not set", e.getMessage());
85 daoParameters.setPluginClass("somewhere.over.the.rainbow");
86 daoParameters.setPersistenceUnit("Dorothy");
88 pfDao.init(daoParameters);
89 fail("Test should throw an exception here");
90 } catch (final Exception e) {
91 assertEquals("Creation of Policy Framework persistence unit \"Dorothy\" failed", e.getMessage());
94 pfDao.create(new PfConceptKey());
95 fail("Test should throw an exception here");
96 } catch (final Exception e) {
97 assertEquals("Policy Framework DAO has not been initialized", e.getMessage());
103 public void testEntityTestAllOpsJpa() throws PfModelException {
104 final DaoParameters daoParameters = new DaoParameters();
105 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
106 daoParameters.setPersistenceUnit("DaoTest");
108 pfDao = new PfDaoFactory().createPfDao(daoParameters);
109 pfDao.init(daoParameters);
115 testgetFilteredOps();
121 public void testEntityTestBadVals() throws PfModelException {
122 final DaoParameters daoParameters = new DaoParameters();
123 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
124 daoParameters.setPersistenceUnit("DaoTest");
126 pfDao = new PfDaoFactory().createPfDao(daoParameters);
127 pfDao.init(daoParameters);
129 final PfConceptKey nullKey = null;
130 final PfReferenceKey nullRefKey = null;
131 final List<PfConceptKey> nullKeyList = null;
132 final List<PfConceptKey> emptyKeyList = new ArrayList<>();
133 final List<PfReferenceKey> nullRKeyList = null;
134 final List<PfReferenceKey> emptyRKeyList = new ArrayList<>();
136 pfDao.create(nullKey);
137 pfDao.createCollection(nullKeyList);
138 pfDao.createCollection(emptyKeyList);
140 pfDao.delete(nullKey);
141 pfDao.deleteCollection(nullKeyList);
142 pfDao.deleteCollection(emptyKeyList);
143 pfDao.delete(PfConceptKey.class, nullKey);
144 pfDao.delete(PfReferenceKey.class, nullRefKey);
145 pfDao.deleteByConceptKey(PfConceptKey.class, nullKeyList);
146 pfDao.deleteByConceptKey(PfConceptKey.class, emptyKeyList);
147 pfDao.deleteByReferenceKey(PfReferenceKey.class, nullRKeyList);
148 pfDao.deleteByReferenceKey(PfReferenceKey.class, emptyRKeyList);
150 pfDao.get(null, nullKey);
151 pfDao.get(null, nullRefKey);
153 pfDao.getAll(null, nullKey);
154 pfDao.getConcept(null, nullKey);
155 pfDao.getConcept(PfConceptKey.class, nullKey);
156 pfDao.getConcept(null, nullRefKey);
157 pfDao.getConcept(PfReferenceKey.class, nullRefKey);
163 private void testAllOps() {
164 final PfConceptKey aKey0 = new PfConceptKey("A-KEY0", "0.0.1");
165 final PfConceptKey aKey1 = new PfConceptKey("A-KEY1", "0.0.1");
166 final PfConceptKey aKey2 = new PfConceptKey("A-KEY2", "0.0.1");
167 final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
168 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
169 final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
170 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
171 final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
172 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
174 pfDao.create(keyInfo0);
176 final DummyConceptEntity keyInfoBack0 = pfDao.get(DummyConceptEntity.class, aKey0);
177 assertTrue(keyInfo0.equals(keyInfoBack0));
179 final DummyConceptEntity keyInfoBackNull = pfDao.get(DummyConceptEntity.class, PfConceptKey.getNullKey());
180 assertNull(keyInfoBackNull);
182 final DummyConceptEntity keyInfoBack1 = pfDao.getConcept(DummyConceptEntity.class, aKey0);
183 assertTrue(keyInfoBack0.equals(keyInfoBack1));
185 final DummyConceptEntity keyInfoBack2 =
186 pfDao.getConcept(DummyConceptEntity.class, new PfConceptKey("A-KEY3", "0.0.1"));
187 assertNull(keyInfoBack2);
189 final Set<DummyConceptEntity> keyInfoSetIn = new TreeSet<DummyConceptEntity>();
190 keyInfoSetIn.add(keyInfo1);
191 keyInfoSetIn.add(keyInfo2);
193 pfDao.createCollection(keyInfoSetIn);
195 Set<DummyConceptEntity> keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
197 keyInfoSetIn.add(keyInfo0);
198 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
200 pfDao.delete(keyInfo1);
201 keyInfoSetIn.remove(keyInfo1);
202 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
203 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
205 pfDao.deleteCollection(keyInfoSetIn);
206 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
207 assertEquals(0, keyInfoSetOut.size());
209 keyInfoSetIn.add(keyInfo0);
210 keyInfoSetIn.add(keyInfo1);
211 keyInfoSetIn.add(keyInfo0);
212 pfDao.createCollection(keyInfoSetIn);
213 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
214 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
216 pfDao.delete(DummyConceptEntity.class, aKey0);
217 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
218 assertEquals(2, keyInfoSetOut.size());
219 assertEquals(2, pfDao.size(DummyConceptEntity.class));
221 final Set<PfConceptKey> keySetIn = new TreeSet<PfConceptKey>();
225 final int deletedCount = pfDao.deleteByConceptKey(DummyConceptEntity.class, keySetIn);
226 assertEquals(2, deletedCount);
228 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
229 assertEquals(0, keyInfoSetOut.size());
231 keyInfoSetIn.add(keyInfo0);
232 keyInfoSetIn.add(keyInfo1);
233 keyInfoSetIn.add(keyInfo0);
234 pfDao.createCollection(keyInfoSetIn);
235 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
236 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
238 pfDao.deleteAll(DummyConceptEntity.class);
239 assertEquals(0, pfDao.size(DummyConceptEntity.class));
241 final PfConceptKey owner0Key = new PfConceptKey("Owner0", "0.0.1");
242 final PfConceptKey owner1Key = new PfConceptKey("Owner1", "0.0.1");
243 final PfConceptKey owner2Key = new PfConceptKey("Owner2", "0.0.1");
244 final PfConceptKey owner3Key = new PfConceptKey("Owner3", "0.0.1");
245 final PfConceptKey owner4Key = new PfConceptKey("Owner4", "0.0.1");
246 final PfConceptKey owner5Key = new PfConceptKey("Owner5", "0.0.1");
248 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity0"), 100.0));
249 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity1"), 101.0));
250 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity2"), 102.0));
251 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity3"), 103.0));
252 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity4"), 104.0));
253 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity5"), 105.0));
254 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity6"), 106.0));
255 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity7"), 107.0));
256 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity8"), 108.0));
257 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity9"), 109.0));
258 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner3Key, "EntityA"), 110.0));
259 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner4Key, "EntityB"), 111.0));
260 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityC"), 112.0));
261 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityD"), 113.0));
262 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityE"), 114.0));
263 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 115.0));
265 TreeSet<DummyReferenceEntity> testEntitySetOut =
266 new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class));
267 assertEquals(16, testEntitySetOut.size());
269 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner0Key));
270 assertEquals(5, testEntitySetOut.size());
272 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner1Key));
273 assertEquals(3, testEntitySetOut.size());
275 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner2Key));
276 assertEquals(2, testEntitySetOut.size());
278 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner3Key));
279 assertEquals(1, testEntitySetOut.size());
281 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner4Key));
282 assertEquals(1, testEntitySetOut.size());
284 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner5Key));
285 assertEquals(4, testEntitySetOut.size());
287 assertNotNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
288 assertNotNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
289 assertNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
290 assertNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
291 pfDao.delete(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0"));
293 final Set<PfReferenceKey> rKeySetIn = new TreeSet<PfReferenceKey>();
294 rKeySetIn.add(new PfReferenceKey(owner4Key, "EntityB"));
295 rKeySetIn.add(new PfReferenceKey(owner5Key, "EntityD"));
297 final int deletedRCount = pfDao.deleteByReferenceKey(DummyReferenceEntity.class, rKeySetIn);
298 assertEquals(2, deletedRCount);
300 pfDao.update(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 120.0));
303 private void testVersionOps() {
304 final PfConceptKey aKey0 = new PfConceptKey("AAA0", "0.0.1");
305 final PfConceptKey aKey1 = new PfConceptKey("AAA0", "0.0.2");
306 final PfConceptKey aKey2 = new PfConceptKey("AAA0", "0.0.3");
307 final PfConceptKey bKey0 = new PfConceptKey("BBB0", "0.0.1");
308 final PfConceptKey bKey1 = new PfConceptKey("BBB0", "0.0.2");
309 final PfConceptKey bKey2 = new PfConceptKey("BBB0", "0.0.3");
310 final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
311 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
312 final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
313 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
314 final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
315 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
316 final DummyConceptEntity keyInfo3 = new DummyConceptEntity(bKey0,
317 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
318 final DummyConceptEntity keyInfo4 = new DummyConceptEntity(bKey1,
319 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
320 final DummyConceptEntity keyInfo5 = new DummyConceptEntity(bKey2,
321 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
323 pfDao.create(keyInfo0);
324 pfDao.create(keyInfo1);
325 pfDao.create(keyInfo2);
326 pfDao.create(keyInfo3);
327 pfDao.create(keyInfo4);
328 pfDao.create(keyInfo5);
330 assertEquals(3, pfDao.getAllVersions(DummyConceptEntity.class, "AAA0").size());
333 private void testgetFilteredOps() {
334 final PfConceptKey aKey0 = new PfConceptKey("AAA0", "0.0.1");
335 final PfConceptKey aKey1 = new PfConceptKey("AAA0", "0.0.2");
336 final PfConceptKey aKey2 = new PfConceptKey("AAA0", "0.0.3");
337 final PfConceptKey bKey0 = new PfConceptKey("BBB0", "0.0.1");
338 final PfConceptKey bKey1 = new PfConceptKey("BBB0", "0.0.2");
339 final PfConceptKey bKey2 = new PfConceptKey("BBB0", "0.0.3");
340 final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
341 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
342 final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
343 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
344 final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
345 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
346 final DummyConceptEntity keyInfo3 = new DummyConceptEntity(bKey0,
347 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
348 final DummyConceptEntity keyInfo4 = new DummyConceptEntity(bKey1,
349 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
350 final DummyConceptEntity keyInfo5 = new DummyConceptEntity(bKey2,
351 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
353 pfDao.create(keyInfo0);
354 pfDao.create(keyInfo1);
355 pfDao.create(keyInfo2);
356 pfDao.create(keyInfo3);
357 pfDao.create(keyInfo4);
358 pfDao.create(keyInfo5);
360 assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, null).size());
361 assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "AAA0", null).size());
362 assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", null).size());
363 assertEquals(1, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", "0.0.3").size());
364 assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, "0.0.3").size());