Merge "Supports new aai changes."
[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.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
32 import java.util.Set;
33 import java.util.TreeSet;
34 import java.util.UUID;
35
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.DaoParameters;
42 import org.onap.policy.models.dao.PfDao;
43 import org.onap.policy.models.dao.PfDaoFactory;
44 import org.onap.policy.models.dao.impl.DefaultPfDao;
45
46 /**
47  * JUnit test class.
48  */
49 public class EntityTest {
50     private PfDao pfDao;
51
52     @Test
53     public void testEntityTestSanity() throws PfModelException {
54         final DaoParameters daoParameters = new DaoParameters();
55
56         Properties jdbcProperties = new Properties();
57         // @formatter:off
58         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER,   "org.h2.Driver");
59         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL,      "jdbc:h2:mem:testdb");
60         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER,     "sa");
61         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "");
62         // @formatter:on
63
64         daoParameters.setJdbcProperties(jdbcProperties);
65
66         pfDao = new PfDaoFactory().createPfDao(daoParameters);
67
68         try {
69             pfDao.init(null);
70             fail("Test should throw an exception here");
71         } catch (final Exception e) {
72             assertEquals("Policy Framework persistence unit parameter not set", e.getMessage());
73         }
74
75         try {
76             pfDao.init(daoParameters);
77             fail("Test should throw an exception here");
78         } catch (final Exception e) {
79             assertEquals("Policy Framework persistence unit parameter not set", e.getMessage());
80         }
81
82         daoParameters.setPluginClass("somewhere.over.the.rainbow");
83         daoParameters.setPersistenceUnit("Dorothy");
84         try {
85             pfDao.init(daoParameters);
86             fail("Test should throw an exception here");
87         } catch (final Exception e) {
88             assertEquals("Creation of Policy Framework persistence unit \"Dorothy\" failed", e.getMessage());
89         }
90         try {
91             pfDao.create(new PfConceptKey());
92             fail("Test should throw an exception here");
93         } catch (final Exception e) {
94             assertEquals("Policy Framework DAO has not been initialized", e.getMessage());
95         }
96         pfDao.close();
97     }
98
99     @Test
100     public void testEntityTestAllOpsJpa() throws PfModelException {
101
102         final DaoParameters daoParameters = new DaoParameters();
103         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
104         daoParameters.setPersistenceUnit("DaoTest");
105
106         Properties jdbcProperties = new Properties();
107         jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
108         jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:testdb");
109         jdbcProperties.setProperty("javax.persistence.jdbc.user", "sa");
110         jdbcProperties.setProperty("javax.persistence.jdbc.password", "");
111
112         daoParameters.setJdbcProperties(jdbcProperties);
113
114         pfDao = new PfDaoFactory().createPfDao(daoParameters);
115         pfDao.init(daoParameters);
116
117         testAllOps();
118
119         testVersionOps();
120
121         testgetFilteredOps();
122
123         pfDao.close();
124     }
125
126     @Test
127     public void testEntityTestBadVals() throws PfModelException {
128         final DaoParameters daoParameters = new DaoParameters();
129         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
130         daoParameters.setPersistenceUnit("DaoTest");
131
132         pfDao = new PfDaoFactory().createPfDao(daoParameters);
133         pfDao.init(daoParameters);
134
135         final PfConceptKey nullKey = null;
136         final PfReferenceKey nullRefKey = null;
137         final List<PfConceptKey> nullKeyList = null;
138         final List<PfConceptKey> emptyKeyList = new ArrayList<>();
139         final List<PfReferenceKey> nullRKeyList = null;
140         final List<PfReferenceKey> emptyRKeyList = new ArrayList<>();
141
142         pfDao.create(nullKey);
143         pfDao.createCollection(nullKeyList);
144         pfDao.createCollection(emptyKeyList);
145
146         pfDao.delete(nullKey);
147         pfDao.deleteCollection(nullKeyList);
148         pfDao.deleteCollection(emptyKeyList);
149         pfDao.delete(PfConceptKey.class, nullKey);
150         pfDao.delete(PfReferenceKey.class, nullRefKey);
151         pfDao.deleteByConceptKey(PfConceptKey.class, nullKeyList);
152         pfDao.deleteByConceptKey(PfConceptKey.class, emptyKeyList);
153         pfDao.deleteByReferenceKey(PfReferenceKey.class, nullRKeyList);
154         pfDao.deleteByReferenceKey(PfReferenceKey.class, emptyRKeyList);
155
156         pfDao.get(null, nullKey);
157         pfDao.get(null, nullRefKey);
158         pfDao.getAll(null);
159         pfDao.getAll(null, nullKey);
160         pfDao.getConcept(null, nullKey);
161         pfDao.getConcept(PfConceptKey.class, nullKey);
162         pfDao.getConcept(null, nullRefKey);
163         pfDao.getConcept(PfReferenceKey.class, nullRefKey);
164         pfDao.size(null);
165
166         pfDao.close();
167     }
168
169     private void testAllOps() {
170         final PfConceptKey aKey0 = new PfConceptKey("A-KEY0", "0.0.1");
171         final PfConceptKey aKey1 = new PfConceptKey("A-KEY1", "0.0.1");
172         final PfConceptKey aKey2 = new PfConceptKey("A-KEY2", "0.0.1");
173         final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
174                 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
175         final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
176                 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
177         final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
178                 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
179
180         pfDao.create(keyInfo0);
181
182         final DummyConceptEntity keyInfoBack0 = pfDao.get(DummyConceptEntity.class, aKey0);
183         assertTrue(keyInfo0.equals(keyInfoBack0));
184
185         final DummyConceptEntity keyInfoBackNull = pfDao.get(DummyConceptEntity.class, PfConceptKey.getNullKey());
186         assertNull(keyInfoBackNull);
187
188         final DummyConceptEntity keyInfoBack1 = pfDao.getConcept(DummyConceptEntity.class, aKey0);
189         assertTrue(keyInfoBack0.equals(keyInfoBack1));
190
191         final DummyConceptEntity keyInfoBack2 =
192                 pfDao.getConcept(DummyConceptEntity.class, new PfConceptKey("A-KEY3", "0.0.1"));
193         assertNull(keyInfoBack2);
194
195         final Set<DummyConceptEntity> keyInfoSetIn = new TreeSet<DummyConceptEntity>();
196         keyInfoSetIn.add(keyInfo1);
197         keyInfoSetIn.add(keyInfo2);
198
199         pfDao.createCollection(keyInfoSetIn);
200
201         Set<DummyConceptEntity> keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
202
203         keyInfoSetIn.add(keyInfo0);
204         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
205
206         pfDao.delete(keyInfo1);
207         keyInfoSetIn.remove(keyInfo1);
208         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
209         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
210
211         pfDao.deleteCollection(keyInfoSetIn);
212         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
213         assertEquals(0, keyInfoSetOut.size());
214
215         keyInfoSetIn.add(keyInfo0);
216         keyInfoSetIn.add(keyInfo1);
217         keyInfoSetIn.add(keyInfo0);
218         pfDao.createCollection(keyInfoSetIn);
219         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
220         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
221
222         pfDao.delete(DummyConceptEntity.class, aKey0);
223         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
224         assertEquals(2, keyInfoSetOut.size());
225         assertEquals(2, pfDao.size(DummyConceptEntity.class));
226
227         final Set<PfConceptKey> keySetIn = new TreeSet<PfConceptKey>();
228         keySetIn.add(aKey1);
229         keySetIn.add(aKey2);
230
231         final int deletedCount = pfDao.deleteByConceptKey(DummyConceptEntity.class, keySetIn);
232         assertEquals(2, deletedCount);
233
234         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
235         assertEquals(0, keyInfoSetOut.size());
236
237         keyInfoSetIn.add(keyInfo0);
238         keyInfoSetIn.add(keyInfo1);
239         keyInfoSetIn.add(keyInfo0);
240         pfDao.createCollection(keyInfoSetIn);
241         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
242         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
243
244         pfDao.deleteAll(DummyConceptEntity.class);
245         assertEquals(0, pfDao.size(DummyConceptEntity.class));
246
247         final PfConceptKey owner0Key = new PfConceptKey("Owner0", "0.0.1");
248         final PfConceptKey owner1Key = new PfConceptKey("Owner1", "0.0.1");
249         final PfConceptKey owner2Key = new PfConceptKey("Owner2", "0.0.1");
250         final PfConceptKey owner3Key = new PfConceptKey("Owner3", "0.0.1");
251         final PfConceptKey owner4Key = new PfConceptKey("Owner4", "0.0.1");
252         final PfConceptKey owner5Key = new PfConceptKey("Owner5", "0.0.1");
253
254         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity0"), 100.0));
255         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity1"), 101.0));
256         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity2"), 102.0));
257         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity3"), 103.0));
258         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity4"), 104.0));
259         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity5"), 105.0));
260         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity6"), 106.0));
261         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity7"), 107.0));
262         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity8"), 108.0));
263         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity9"), 109.0));
264         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner3Key, "EntityA"), 110.0));
265         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner4Key, "EntityB"), 111.0));
266         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityC"), 112.0));
267         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityD"), 113.0));
268         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityE"), 114.0));
269         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 115.0));
270
271         TreeSet<DummyReferenceEntity> testEntitySetOut =
272                 new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class));
273         assertEquals(16, testEntitySetOut.size());
274
275         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner0Key));
276         assertEquals(5, testEntitySetOut.size());
277
278         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner1Key));
279         assertEquals(3, testEntitySetOut.size());
280
281         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner2Key));
282         assertEquals(2, testEntitySetOut.size());
283
284         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner3Key));
285         assertEquals(1, testEntitySetOut.size());
286
287         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner4Key));
288         assertEquals(1, testEntitySetOut.size());
289
290         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner5Key));
291         assertEquals(4, testEntitySetOut.size());
292
293         assertNotNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
294         assertNotNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
295         assertNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
296         assertNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
297         pfDao.delete(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0"));
298
299         final Set<PfReferenceKey> rKeySetIn = new TreeSet<PfReferenceKey>();
300         rKeySetIn.add(new PfReferenceKey(owner4Key, "EntityB"));
301         rKeySetIn.add(new PfReferenceKey(owner5Key, "EntityD"));
302
303         final int deletedRCount = pfDao.deleteByReferenceKey(DummyReferenceEntity.class, rKeySetIn);
304         assertEquals(2, deletedRCount);
305
306         pfDao.update(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 120.0));
307     }
308
309     private void testVersionOps() {
310         final PfConceptKey aKey0 = new PfConceptKey("AAA0", "0.0.1");
311         final PfConceptKey aKey1 = new PfConceptKey("AAA0", "0.0.2");
312         final PfConceptKey aKey2 = new PfConceptKey("AAA0", "0.0.3");
313         final PfConceptKey bKey0 = new PfConceptKey("BBB0", "0.0.1");
314         final PfConceptKey bKey1 = new PfConceptKey("BBB0", "0.0.2");
315         final PfConceptKey bKey2 = new PfConceptKey("BBB0", "0.0.3");
316         final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
317                 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
318         final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
319                 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
320         final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
321                 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
322         final DummyConceptEntity keyInfo3 = new DummyConceptEntity(bKey0,
323                 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
324         final DummyConceptEntity keyInfo4 = new DummyConceptEntity(bKey1,
325                 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
326         final DummyConceptEntity keyInfo5 = new DummyConceptEntity(bKey2,
327                 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
328
329         pfDao.create(keyInfo0);
330         pfDao.create(keyInfo1);
331         pfDao.create(keyInfo2);
332         pfDao.create(keyInfo3);
333         pfDao.create(keyInfo4);
334         pfDao.create(keyInfo5);
335
336         assertEquals(3, pfDao.getAllVersions(DummyConceptEntity.class, "AAA0").size());
337         assertEquals(0, pfDao.getAllVersions(null, "AAA0").size());
338         assertEquals(0, pfDao.getAllVersions(DummyConceptEntity.class, null).size());
339     }
340
341     private void testgetFilteredOps() {
342         final PfConceptKey aKey0 = new PfConceptKey("AAA0", "0.0.1");
343         final PfConceptKey aKey1 = new PfConceptKey("AAA0", "0.0.2");
344         final PfConceptKey aKey2 = new PfConceptKey("AAA0", "0.0.3");
345         final PfConceptKey bKey0 = new PfConceptKey("BBB0", "0.0.1");
346         final PfConceptKey bKey1 = new PfConceptKey("BBB0", "0.0.2");
347         final PfConceptKey bKey2 = new PfConceptKey("BBB0", "0.0.3");
348         final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
349                 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
350         final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
351                 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
352         final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
353                 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
354         final DummyConceptEntity keyInfo3 = new DummyConceptEntity(bKey0,
355                 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
356         final DummyConceptEntity keyInfo4 = new DummyConceptEntity(bKey1,
357                 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
358         final DummyConceptEntity keyInfo5 = new DummyConceptEntity(bKey2,
359                 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
360
361         pfDao.create(keyInfo0);
362         pfDao.create(keyInfo1);
363         pfDao.create(keyInfo2);
364         pfDao.create(keyInfo3);
365         pfDao.create(keyInfo4);
366         pfDao.create(keyInfo5);
367
368         assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, null).size());
369         assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "AAA0", null).size());
370         assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", null).size());
371         assertEquals(1, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", "0.0.3").size());
372         assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, "0.0.3").size());
373     }
374 }