Merge "Clean up models-pap based on updates to models-pdp"
[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         pfDao.close();
116     }
117
118     @Test
119     public void testEntityTestBadVals() throws PfModelException {
120         final DaoParameters daoParameters = new DaoParameters();
121         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
122         daoParameters.setPersistenceUnit("DaoTest");
123
124         pfDao = new PfDaoFactory().createPfDao(daoParameters);
125         pfDao.init(daoParameters);
126
127         final PfConceptKey nullKey = null;
128         final PfReferenceKey nullRefKey = null;
129         final List<PfConceptKey> nullKeyList = null;
130         final List<PfConceptKey> emptyKeyList = new ArrayList<>();
131         final List<PfReferenceKey> nullRKeyList = null;
132         final List<PfReferenceKey> emptyRKeyList = new ArrayList<>();
133
134         pfDao.create(nullKey);
135         pfDao.createCollection(nullKeyList);
136         pfDao.createCollection(emptyKeyList);
137
138         pfDao.delete(nullKey);
139         pfDao.deleteCollection(nullKeyList);
140         pfDao.deleteCollection(emptyKeyList);
141         pfDao.delete(PfConceptKey.class, nullKey);
142         pfDao.delete(PfReferenceKey.class, nullRefKey);
143         pfDao.deleteByConceptKey(PfConceptKey.class, nullKeyList);
144         pfDao.deleteByConceptKey(PfConceptKey.class, emptyKeyList);
145         pfDao.deleteByReferenceKey(PfReferenceKey.class, nullRKeyList);
146         pfDao.deleteByReferenceKey(PfReferenceKey.class, emptyRKeyList);
147
148         pfDao.get(null, nullKey);
149         pfDao.get(null, nullRefKey);
150         pfDao.getAll(null);
151         pfDao.getAll(null, nullKey);
152         pfDao.getConcept(null, nullKey);
153         pfDao.getConcept(PfConceptKey.class, nullKey);
154         pfDao.getConcept(null, nullRefKey);
155         pfDao.getConcept(PfReferenceKey.class, nullRefKey);
156         pfDao.size(null);
157
158         pfDao.close();
159     }
160
161     private void testAllOps() {
162         final PfConceptKey aKey0 = new PfConceptKey("A-KEY0", "0.0.1");
163         final PfConceptKey aKey1 = new PfConceptKey("A-KEY1", "0.0.1");
164         final PfConceptKey aKey2 = new PfConceptKey("A-KEY2", "0.0.1");
165         final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
166                 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
167         final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
168                 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
169         final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
170                 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
171
172         pfDao.create(keyInfo0);
173
174         final DummyConceptEntity keyInfoBack0 = pfDao.get(DummyConceptEntity.class, aKey0);
175         assertTrue(keyInfo0.equals(keyInfoBack0));
176
177         final DummyConceptEntity keyInfoBackNull = pfDao.get(DummyConceptEntity.class, PfConceptKey.getNullKey());
178         assertNull(keyInfoBackNull);
179
180         final DummyConceptEntity keyInfoBack1 = pfDao.getConcept(DummyConceptEntity.class, aKey0);
181         assertTrue(keyInfoBack0.equals(keyInfoBack1));
182
183         final DummyConceptEntity keyInfoBack2 =
184                 pfDao.getConcept(DummyConceptEntity.class, new PfConceptKey("A-KEY3", "0.0.1"));
185         assertNull(keyInfoBack2);
186
187         final Set<DummyConceptEntity> keyInfoSetIn = new TreeSet<DummyConceptEntity>();
188         keyInfoSetIn.add(keyInfo1);
189         keyInfoSetIn.add(keyInfo2);
190
191         pfDao.createCollection(keyInfoSetIn);
192
193         Set<DummyConceptEntity> keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
194
195         keyInfoSetIn.add(keyInfo0);
196         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
197
198         pfDao.delete(keyInfo1);
199         keyInfoSetIn.remove(keyInfo1);
200         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
201         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
202
203         pfDao.deleteCollection(keyInfoSetIn);
204         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
205         assertEquals(0, keyInfoSetOut.size());
206
207         keyInfoSetIn.add(keyInfo0);
208         keyInfoSetIn.add(keyInfo1);
209         keyInfoSetIn.add(keyInfo0);
210         pfDao.createCollection(keyInfoSetIn);
211         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
212         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
213
214         pfDao.delete(DummyConceptEntity.class, aKey0);
215         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
216         assertEquals(2, keyInfoSetOut.size());
217         assertEquals(2, pfDao.size(DummyConceptEntity.class));
218
219         final Set<PfConceptKey> keySetIn = new TreeSet<PfConceptKey>();
220         keySetIn.add(aKey1);
221         keySetIn.add(aKey2);
222
223         final int deletedCount = pfDao.deleteByConceptKey(DummyConceptEntity.class, keySetIn);
224         assertEquals(2, deletedCount);
225
226         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
227         assertEquals(0, keyInfoSetOut.size());
228
229         keyInfoSetIn.add(keyInfo0);
230         keyInfoSetIn.add(keyInfo1);
231         keyInfoSetIn.add(keyInfo0);
232         pfDao.createCollection(keyInfoSetIn);
233         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
234         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
235
236         pfDao.deleteAll(DummyConceptEntity.class);
237         assertEquals(0, pfDao.size(DummyConceptEntity.class));
238
239         final PfConceptKey owner0Key = new PfConceptKey("Owner0", "0.0.1");
240         final PfConceptKey owner1Key = new PfConceptKey("Owner1", "0.0.1");
241         final PfConceptKey owner2Key = new PfConceptKey("Owner2", "0.0.1");
242         final PfConceptKey owner3Key = new PfConceptKey("Owner3", "0.0.1");
243         final PfConceptKey owner4Key = new PfConceptKey("Owner4", "0.0.1");
244         final PfConceptKey owner5Key = new PfConceptKey("Owner5", "0.0.1");
245
246         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity0"), 100.0));
247         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity1"), 101.0));
248         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity2"), 102.0));
249         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity3"), 103.0));
250         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity4"), 104.0));
251         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity5"), 105.0));
252         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity6"), 106.0));
253         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity7"), 107.0));
254         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity8"), 108.0));
255         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity9"), 109.0));
256         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner3Key, "EntityA"), 110.0));
257         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner4Key, "EntityB"), 111.0));
258         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityC"), 112.0));
259         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityD"), 113.0));
260         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityE"), 114.0));
261         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 115.0));
262
263         TreeSet<DummyReferenceEntity> testEntitySetOut =
264                 new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class));
265         assertEquals(16, testEntitySetOut.size());
266
267         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner0Key));
268         assertEquals(5, testEntitySetOut.size());
269
270         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner1Key));
271         assertEquals(3, testEntitySetOut.size());
272
273         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner2Key));
274         assertEquals(2, testEntitySetOut.size());
275
276         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner3Key));
277         assertEquals(1, testEntitySetOut.size());
278
279         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner4Key));
280         assertEquals(1, testEntitySetOut.size());
281
282         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner5Key));
283         assertEquals(4, testEntitySetOut.size());
284
285         assertNotNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
286         assertNotNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
287         assertNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
288         assertNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
289         pfDao.delete(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0"));
290
291         final Set<PfReferenceKey> rKeySetIn = new TreeSet<PfReferenceKey>();
292         rKeySetIn.add(new PfReferenceKey(owner4Key, "EntityB"));
293         rKeySetIn.add(new PfReferenceKey(owner5Key, "EntityD"));
294
295         final int deletedRCount = pfDao.deleteByReferenceKey(DummyReferenceEntity.class, rKeySetIn);
296         assertEquals(2, deletedRCount);
297
298         pfDao.update(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 120.0));
299     }
300
301     private void testVersionOps() {
302         final PfConceptKey aKey0 = new PfConceptKey("AAA0", "0.0.1");
303         final PfConceptKey aKey1 = new PfConceptKey("AAA0", "0.0.2");
304         final PfConceptKey aKey2 = new PfConceptKey("AAA0", "0.0.3");
305         final PfConceptKey bKey0 = new PfConceptKey("BBB0", "0.0.1");
306         final PfConceptKey bKey1 = new PfConceptKey("BBB0", "0.0.2");
307         final PfConceptKey bKey2 = new PfConceptKey("BBB0", "0.0.3");
308         final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
309                 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
310         final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
311                 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
312         final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
313                 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
314         final DummyConceptEntity keyInfo3 = new DummyConceptEntity(bKey0,
315                 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
316         final DummyConceptEntity keyInfo4 = new DummyConceptEntity(bKey1,
317                 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
318         final DummyConceptEntity keyInfo5 = new DummyConceptEntity(bKey2,
319                 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
320
321         pfDao.create(keyInfo0);
322         pfDao.create(keyInfo1);
323         pfDao.create(keyInfo2);
324         pfDao.create(keyInfo3);
325         pfDao.create(keyInfo4);
326         pfDao.create(keyInfo5);
327
328         assertEquals(3, pfDao.getAllVersions(DummyConceptEntity.class, "AAA0").size());
329         DummyConceptEntity latestVersionEntity = pfDao.getLatestVersion(DummyConceptEntity.class, "AAA0");
330         assertEquals(aKey2, latestVersionEntity.getKey());
331         List<DummyConceptEntity> returnedLatestVersions = pfDao.getLatestVersions(DummyConceptEntity.class);
332         assertEquals(2, returnedLatestVersions.size());
333         assertEquals("0.0.3", returnedLatestVersions.get(0).getKey().getVersion());
334         assertEquals("0.0.3", returnedLatestVersions.get(1).getKey().getVersion());
335     }
336 }