Merge "Add filter obejcts for concepts"
[policy/models.git] / models-dao / src / test / java / org / onap / policy / models / dao / EntityTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.dao;
22
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;
28
29 import java.sql.Connection;
30 import java.sql.DriverManager;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Set;
34 import java.util.TreeSet;
35 import java.util.UUID;
36
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;
47
48 /**
49  * JUnit test class.
50  */
51 public class EntityTest {
52     private Connection connection;
53     private PfDao pfDao;
54
55     @Before
56     public void setup() throws Exception {
57         connection = DriverManager.getConnection("jdbc:h2:mem:test");
58     }
59
60     @After
61     public void teardown() throws Exception {
62         connection.close();
63     }
64
65     @Test
66     public void testEntityTestSanity() throws PfModelException {
67         final DaoParameters daoParameters = new DaoParameters();
68
69         pfDao = new PfDaoFactory().createPfDao(daoParameters);
70
71         try {
72             pfDao.init(null);
73             fail("Test should throw an exception here");
74         } catch (final Exception e) {
75             assertEquals("Policy Framework persistence unit parameter not set", e.getMessage());
76         }
77
78         try {
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());
83         }
84
85         daoParameters.setPluginClass("somewhere.over.the.rainbow");
86         daoParameters.setPersistenceUnit("Dorothy");
87         try {
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());
92         }
93         try {
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());
98         }
99         pfDao.close();
100     }
101
102     @Test
103     public void testEntityTestAllOpsJpa() throws PfModelException {
104         final DaoParameters daoParameters = new DaoParameters();
105         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
106         daoParameters.setPersistenceUnit("DaoTest");
107
108         pfDao = new PfDaoFactory().createPfDao(daoParameters);
109         pfDao.init(daoParameters);
110
111         testAllOps();
112
113         testVersionOps();
114
115         testgetFilteredOps();
116
117         pfDao.close();
118     }
119
120     @Test
121     public void testEntityTestBadVals() throws PfModelException {
122         final DaoParameters daoParameters = new DaoParameters();
123         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
124         daoParameters.setPersistenceUnit("DaoTest");
125
126         pfDao = new PfDaoFactory().createPfDao(daoParameters);
127         pfDao.init(daoParameters);
128
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<>();
135
136         pfDao.create(nullKey);
137         pfDao.createCollection(nullKeyList);
138         pfDao.createCollection(emptyKeyList);
139
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);
149
150         pfDao.get(null, nullKey);
151         pfDao.get(null, nullRefKey);
152         pfDao.getAll(null);
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);
158         pfDao.size(null);
159
160         pfDao.close();
161     }
162
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");
173
174         pfDao.create(keyInfo0);
175
176         final DummyConceptEntity keyInfoBack0 = pfDao.get(DummyConceptEntity.class, aKey0);
177         assertTrue(keyInfo0.equals(keyInfoBack0));
178
179         final DummyConceptEntity keyInfoBackNull = pfDao.get(DummyConceptEntity.class, PfConceptKey.getNullKey());
180         assertNull(keyInfoBackNull);
181
182         final DummyConceptEntity keyInfoBack1 = pfDao.getConcept(DummyConceptEntity.class, aKey0);
183         assertTrue(keyInfoBack0.equals(keyInfoBack1));
184
185         final DummyConceptEntity keyInfoBack2 =
186                 pfDao.getConcept(DummyConceptEntity.class, new PfConceptKey("A-KEY3", "0.0.1"));
187         assertNull(keyInfoBack2);
188
189         final Set<DummyConceptEntity> keyInfoSetIn = new TreeSet<DummyConceptEntity>();
190         keyInfoSetIn.add(keyInfo1);
191         keyInfoSetIn.add(keyInfo2);
192
193         pfDao.createCollection(keyInfoSetIn);
194
195         Set<DummyConceptEntity> keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
196
197         keyInfoSetIn.add(keyInfo0);
198         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
199
200         pfDao.delete(keyInfo1);
201         keyInfoSetIn.remove(keyInfo1);
202         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
203         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
204
205         pfDao.deleteCollection(keyInfoSetIn);
206         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
207         assertEquals(0, keyInfoSetOut.size());
208
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));
215
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));
220
221         final Set<PfConceptKey> keySetIn = new TreeSet<PfConceptKey>();
222         keySetIn.add(aKey1);
223         keySetIn.add(aKey2);
224
225         final int deletedCount = pfDao.deleteByConceptKey(DummyConceptEntity.class, keySetIn);
226         assertEquals(2, deletedCount);
227
228         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
229         assertEquals(0, keyInfoSetOut.size());
230
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));
237
238         pfDao.deleteAll(DummyConceptEntity.class);
239         assertEquals(0, pfDao.size(DummyConceptEntity.class));
240
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");
247
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));
264
265         TreeSet<DummyReferenceEntity> testEntitySetOut =
266                 new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class));
267         assertEquals(16, testEntitySetOut.size());
268
269         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner0Key));
270         assertEquals(5, testEntitySetOut.size());
271
272         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner1Key));
273         assertEquals(3, testEntitySetOut.size());
274
275         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner2Key));
276         assertEquals(2, testEntitySetOut.size());
277
278         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner3Key));
279         assertEquals(1, testEntitySetOut.size());
280
281         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner4Key));
282         assertEquals(1, testEntitySetOut.size());
283
284         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner5Key));
285         assertEquals(4, testEntitySetOut.size());
286
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"));
292
293         final Set<PfReferenceKey> rKeySetIn = new TreeSet<PfReferenceKey>();
294         rKeySetIn.add(new PfReferenceKey(owner4Key, "EntityB"));
295         rKeySetIn.add(new PfReferenceKey(owner5Key, "EntityD"));
296
297         final int deletedRCount = pfDao.deleteByReferenceKey(DummyReferenceEntity.class, rKeySetIn);
298         assertEquals(2, deletedRCount);
299
300         pfDao.update(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 120.0));
301     }
302
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");
322
323         pfDao.create(keyInfo0);
324         pfDao.create(keyInfo1);
325         pfDao.create(keyInfo2);
326         pfDao.create(keyInfo3);
327         pfDao.create(keyInfo4);
328         pfDao.create(keyInfo5);
329
330         assertEquals(3, pfDao.getAllVersions(DummyConceptEntity.class, "AAA0").size());
331     }
332
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");
352
353         pfDao.create(keyInfo0);
354         pfDao.create(keyInfo1);
355         pfDao.create(keyInfo2);
356         pfDao.create(keyInfo3);
357         pfDao.create(keyInfo4);
358         pfDao.create(keyInfo5);
359
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());
365     }
366 }