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);
116 public void testEntityTestBadVals() throws PfModelException {
117 final DaoParameters daoParameters = new DaoParameters();
118 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
119 daoParameters.setPersistenceUnit("DaoTest");
121 pfDao = new PfDaoFactory().createPfDao(daoParameters);
122 pfDao.init(daoParameters);
124 final PfConceptKey nullKey = null;
125 final PfReferenceKey nullRefKey = null;
126 final List<PfConceptKey> nullKeyList = null;
127 final List<PfConceptKey> emptyKeyList = new ArrayList<>();
128 final List<PfReferenceKey> nullRKeyList = null;
129 final List<PfReferenceKey> emptyRKeyList = new ArrayList<>();
131 pfDao.create(nullKey);
132 pfDao.createCollection(nullKeyList);
133 pfDao.createCollection(emptyKeyList);
135 pfDao.delete(nullKey);
136 pfDao.deleteCollection(nullKeyList);
137 pfDao.deleteCollection(emptyKeyList);
138 pfDao.delete(PfConceptKey.class, nullKey);
139 pfDao.delete(PfReferenceKey.class, nullRefKey);
140 pfDao.deleteByConceptKey(PfConceptKey.class, nullKeyList);
141 pfDao.deleteByConceptKey(PfConceptKey.class, emptyKeyList);
142 pfDao.deleteByReferenceKey(PfReferenceKey.class, nullRKeyList);
143 pfDao.deleteByReferenceKey(PfReferenceKey.class, emptyRKeyList);
145 pfDao.get(null, nullKey);
146 pfDao.get(null, nullRefKey);
148 pfDao.getAll(null, nullKey);
149 pfDao.getConcept(null, nullKey);
150 pfDao.getConcept(PfConceptKey.class, nullKey);
151 pfDao.getConcept(null, nullRefKey);
152 pfDao.getConcept(PfReferenceKey.class, nullRefKey);
158 private void testAllOps() {
159 final PfConceptKey aKey0 = new PfConceptKey("A-KEY0", "0.0.1");
160 final PfConceptKey aKey1 = new PfConceptKey("A-KEY1", "0.0.1");
161 final PfConceptKey aKey2 = new PfConceptKey("A-KEY2", "0.0.1");
162 final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
163 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
164 final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
165 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
166 final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
167 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
169 pfDao.create(keyInfo0);
171 final DummyConceptEntity keyInfoBack0 = pfDao.get(DummyConceptEntity.class, aKey0);
172 assertTrue(keyInfo0.equals(keyInfoBack0));
174 final DummyConceptEntity keyInfoBackNull = pfDao.get(DummyConceptEntity.class, PfConceptKey.getNullKey());
175 assertNull(keyInfoBackNull);
177 final DummyConceptEntity keyInfoBack1 = pfDao.getConcept(DummyConceptEntity.class, aKey0);
178 assertTrue(keyInfoBack0.equals(keyInfoBack1));
180 final DummyConceptEntity keyInfoBack2 =
181 pfDao.getConcept(DummyConceptEntity.class, new PfConceptKey("A-KEY3", "0.0.1"));
182 assertNull(keyInfoBack2);
184 final Set<DummyConceptEntity> keyInfoSetIn = new TreeSet<DummyConceptEntity>();
185 keyInfoSetIn.add(keyInfo1);
186 keyInfoSetIn.add(keyInfo2);
188 pfDao.createCollection(keyInfoSetIn);
190 Set<DummyConceptEntity> keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
192 keyInfoSetIn.add(keyInfo0);
193 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
195 pfDao.delete(keyInfo1);
196 keyInfoSetIn.remove(keyInfo1);
197 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
198 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
200 pfDao.deleteCollection(keyInfoSetIn);
201 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
202 assertEquals(0, keyInfoSetOut.size());
204 keyInfoSetIn.add(keyInfo0);
205 keyInfoSetIn.add(keyInfo1);
206 keyInfoSetIn.add(keyInfo0);
207 pfDao.createCollection(keyInfoSetIn);
208 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
209 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
211 pfDao.delete(DummyConceptEntity.class, aKey0);
212 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
213 assertEquals(2, keyInfoSetOut.size());
214 assertEquals(2, pfDao.size(DummyConceptEntity.class));
216 final Set<PfConceptKey> keySetIn = new TreeSet<PfConceptKey>();
220 final int deletedCount = pfDao.deleteByConceptKey(DummyConceptEntity.class, keySetIn);
221 assertEquals(2, deletedCount);
223 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
224 assertEquals(0, keyInfoSetOut.size());
226 keyInfoSetIn.add(keyInfo0);
227 keyInfoSetIn.add(keyInfo1);
228 keyInfoSetIn.add(keyInfo0);
229 pfDao.createCollection(keyInfoSetIn);
230 keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
231 assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
233 pfDao.deleteAll(DummyConceptEntity.class);
234 assertEquals(0, pfDao.size(DummyConceptEntity.class));
236 final PfConceptKey owner0Key = new PfConceptKey("Owner0", "0.0.1");
237 final PfConceptKey owner1Key = new PfConceptKey("Owner1", "0.0.1");
238 final PfConceptKey owner2Key = new PfConceptKey("Owner2", "0.0.1");
239 final PfConceptKey owner3Key = new PfConceptKey("Owner3", "0.0.1");
240 final PfConceptKey owner4Key = new PfConceptKey("Owner4", "0.0.1");
241 final PfConceptKey owner5Key = new PfConceptKey("Owner5", "0.0.1");
243 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity0"), 100.0));
244 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity1"), 101.0));
245 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity2"), 102.0));
246 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity3"), 103.0));
247 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity4"), 104.0));
248 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity5"), 105.0));
249 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity6"), 106.0));
250 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity7"), 107.0));
251 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity8"), 108.0));
252 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity9"), 109.0));
253 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner3Key, "EntityA"), 110.0));
254 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner4Key, "EntityB"), 111.0));
255 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityC"), 112.0));
256 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityD"), 113.0));
257 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityE"), 114.0));
258 pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 115.0));
260 TreeSet<DummyReferenceEntity> testEntitySetOut =
261 new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class));
262 assertEquals(16, testEntitySetOut.size());
264 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner0Key));
265 assertEquals(5, testEntitySetOut.size());
267 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner1Key));
268 assertEquals(3, testEntitySetOut.size());
270 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner2Key));
271 assertEquals(2, testEntitySetOut.size());
273 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner3Key));
274 assertEquals(1, testEntitySetOut.size());
276 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner4Key));
277 assertEquals(1, testEntitySetOut.size());
279 testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner5Key));
280 assertEquals(4, testEntitySetOut.size());
282 assertNotNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
283 assertNotNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
284 assertNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
285 assertNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
286 pfDao.delete(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0"));
288 final Set<PfReferenceKey> rKeySetIn = new TreeSet<PfReferenceKey>();
289 rKeySetIn.add(new PfReferenceKey(owner4Key, "EntityB"));
290 rKeySetIn.add(new PfReferenceKey(owner5Key, "EntityD"));
292 final int deletedRCount = pfDao.deleteByReferenceKey(DummyReferenceEntity.class, rKeySetIn);
293 assertEquals(2, deletedRCount);
295 pfDao.update(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 120.0));