Add DAO module for Models
[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.io.File;
30 import java.sql.Connection;
31 import java.sql.DriverManager;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Set;
35 import java.util.TreeSet;
36 import java.util.UUID;
37
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfModelException;
43 import org.onap.policy.models.base.PfReferenceKey;
44 import org.onap.policy.models.dao.DaoParameters;
45 import org.onap.policy.models.dao.PfDao;
46 import org.onap.policy.models.dao.PfDaoFactory;
47 import org.onap.policy.models.dao.impl.DefaultPfDao;
48
49 /**
50  * JUnit test class.
51  */
52 public class EntityTest {
53     private Connection connection;
54     private PfDao pfDao;
55
56     @Before
57     public void setup() throws Exception {
58         connection = DriverManager.getConnection("jdbc:h2:mem:test");
59     }
60
61     @After
62     public void teardown() throws Exception {
63         connection.close();
64         new File("derby.log").delete();
65     }
66
67     @Test
68     public void testEntityTestSanity() throws PfModelException {
69         final DaoParameters daoParameters = new DaoParameters();
70
71         pfDao = new PfDaoFactory().createPfDao(daoParameters);
72
73         try {
74             pfDao.init(null);
75             fail("Test should throw an exception here");
76         } catch (final Exception e) {
77             assertEquals("Policy Framework persistence unit parameter not set", e.getMessage());
78         }
79
80         try {
81             pfDao.init(daoParameters);
82             fail("Test should throw an exception here");
83         } catch (final Exception e) {
84             assertEquals("Policy Framework persistence unit parameter not set", e.getMessage());
85         }
86
87         daoParameters.setPluginClass("somewhere.over.the.rainbow");
88         daoParameters.setPersistenceUnit("Dorothy");
89         try {
90             pfDao.init(daoParameters);
91             fail("Test should throw an exception here");
92         } catch (final Exception e) {
93             assertEquals("Creation of Policy Framework persistence unit \"Dorothy\" failed", e.getMessage());
94         }
95         try {
96             pfDao.create(new PfConceptKey());
97             fail("Test should throw an exception here");
98         } catch (final Exception e) {
99             assertEquals("Policy Framework DAO has not been initialized", e.getMessage());
100         }
101         pfDao.close();
102     }
103
104     @Test
105     public void testEntityTestAllOpsJpa() throws PfModelException {
106         final DaoParameters daoParameters = new DaoParameters();
107         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
108         daoParameters.setPersistenceUnit("DaoTest");
109
110         pfDao = new PfDaoFactory().createPfDao(daoParameters);
111         pfDao.init(daoParameters);
112
113         testAllOps();
114         pfDao.close();
115     }
116
117     @Test
118     public void testEntityTestBadVals() throws PfModelException {
119         final DaoParameters daoParameters = new DaoParameters();
120         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
121         daoParameters.setPersistenceUnit("DaoTest");
122
123         pfDao = new PfDaoFactory().createPfDao(daoParameters);
124         pfDao.init(daoParameters);
125
126         final PfConceptKey nullKey = null;
127         final PfReferenceKey nullRefKey = null;
128         final List<PfConceptKey> nullKeyList = null;
129         final List<PfConceptKey> emptyKeyList = new ArrayList<>();
130         final List<PfReferenceKey> nullRKeyList = null;
131         final List<PfReferenceKey> emptyRKeyList = new ArrayList<>();
132
133         pfDao.create(nullKey);
134         pfDao.createCollection(nullKeyList);
135         pfDao.createCollection(emptyKeyList);
136
137         pfDao.delete(nullKey);
138         pfDao.deleteCollection(nullKeyList);
139         pfDao.deleteCollection(emptyKeyList);
140         pfDao.delete(PfConceptKey.class, nullKey);
141         pfDao.delete(PfReferenceKey.class, nullRefKey);
142         pfDao.deleteByConceptKey(PfConceptKey.class, nullKeyList);
143         pfDao.deleteByConceptKey(PfConceptKey.class, emptyKeyList);
144         pfDao.deleteByReferenceKey(PfReferenceKey.class, nullRKeyList);
145         pfDao.deleteByReferenceKey(PfReferenceKey.class, emptyRKeyList);
146
147         pfDao.get(null, nullKey);
148         pfDao.get(null, nullRefKey);
149         pfDao.getAll(null);
150         pfDao.getAll(null, nullKey);
151         pfDao.getConcept(null, nullKey);
152         pfDao.getConcept(PfConceptKey.class, nullKey);
153         pfDao.getConcept(null, nullRefKey);
154         pfDao.getConcept(PfReferenceKey.class, nullRefKey);
155         pfDao.size(null);
156
157         pfDao.close();
158     }
159
160     private void testAllOps() {
161         final PfConceptKey aKey0 = new PfConceptKey("A-KEY0", "0.0.1");
162         final PfConceptKey aKey1 = new PfConceptKey("A-KEY1", "0.0.1");
163         final PfConceptKey aKey2 = new PfConceptKey("A-KEY2", "0.0.1");
164         final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
165                 UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
166         final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
167                 UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
168         final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
169                 UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");
170
171         pfDao.create(keyInfo0);
172
173         final DummyConceptEntity keyInfoBack0 = pfDao.get(DummyConceptEntity.class, aKey0);
174         assertTrue(keyInfo0.equals(keyInfoBack0));
175
176         final DummyConceptEntity keyInfoBackNull = pfDao.get(DummyConceptEntity.class, PfConceptKey.getNullKey());
177         assertNull(keyInfoBackNull);
178
179         final DummyConceptEntity keyInfoBack1 = pfDao.getConcept(DummyConceptEntity.class, aKey0);
180         assertTrue(keyInfoBack0.equals(keyInfoBack1));
181
182         final DummyConceptEntity keyInfoBack2 =
183                 pfDao.getConcept(DummyConceptEntity.class, new PfConceptKey("A-KEY3", "0.0.1"));
184         assertNull(keyInfoBack2);
185
186         final Set<DummyConceptEntity> keyInfoSetIn = new TreeSet<DummyConceptEntity>();
187         keyInfoSetIn.add(keyInfo1);
188         keyInfoSetIn.add(keyInfo2);
189
190         pfDao.createCollection(keyInfoSetIn);
191
192         Set<DummyConceptEntity> keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
193
194         keyInfoSetIn.add(keyInfo0);
195         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
196
197         pfDao.delete(keyInfo1);
198         keyInfoSetIn.remove(keyInfo1);
199         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
200         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
201
202         pfDao.deleteCollection(keyInfoSetIn);
203         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
204         assertEquals(0, keyInfoSetOut.size());
205
206         keyInfoSetIn.add(keyInfo0);
207         keyInfoSetIn.add(keyInfo1);
208         keyInfoSetIn.add(keyInfo0);
209         pfDao.createCollection(keyInfoSetIn);
210         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
211         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
212
213         pfDao.delete(DummyConceptEntity.class, aKey0);
214         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
215         assertEquals(2, keyInfoSetOut.size());
216         assertEquals(2, pfDao.size(DummyConceptEntity.class));
217
218         final Set<PfConceptKey> keySetIn = new TreeSet<PfConceptKey>();
219         keySetIn.add(aKey1);
220         keySetIn.add(aKey2);
221
222         final int deletedCount = pfDao.deleteByConceptKey(DummyConceptEntity.class, keySetIn);
223         assertEquals(2, deletedCount);
224
225         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
226         assertEquals(0, keyInfoSetOut.size());
227
228         keyInfoSetIn.add(keyInfo0);
229         keyInfoSetIn.add(keyInfo1);
230         keyInfoSetIn.add(keyInfo0);
231         pfDao.createCollection(keyInfoSetIn);
232         keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
233         assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
234
235         pfDao.deleteAll(DummyConceptEntity.class);
236         assertEquals(0, pfDao.size(DummyConceptEntity.class));
237
238         final PfConceptKey owner0Key = new PfConceptKey("Owner0", "0.0.1");
239         final PfConceptKey owner1Key = new PfConceptKey("Owner1", "0.0.1");
240         final PfConceptKey owner2Key = new PfConceptKey("Owner2", "0.0.1");
241         final PfConceptKey owner3Key = new PfConceptKey("Owner3", "0.0.1");
242         final PfConceptKey owner4Key = new PfConceptKey("Owner4", "0.0.1");
243         final PfConceptKey owner5Key = new PfConceptKey("Owner5", "0.0.1");
244
245         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity0"), 100.0));
246         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity1"), 101.0));
247         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity2"), 102.0));
248         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity3"), 103.0));
249         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity4"), 104.0));
250         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity5"), 105.0));
251         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity6"), 106.0));
252         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity7"), 107.0));
253         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity8"), 108.0));
254         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity9"), 109.0));
255         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner3Key, "EntityA"), 110.0));
256         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner4Key, "EntityB"), 111.0));
257         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityC"), 112.0));
258         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityD"), 113.0));
259         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityE"), 114.0));
260         pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 115.0));
261
262         TreeSet<DummyReferenceEntity> testEntitySetOut =
263                 new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class));
264         assertEquals(16, testEntitySetOut.size());
265
266         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner0Key));
267         assertEquals(5, testEntitySetOut.size());
268
269         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner1Key));
270         assertEquals(3, testEntitySetOut.size());
271
272         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner2Key));
273         assertEquals(2, testEntitySetOut.size());
274
275         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner3Key));
276         assertEquals(1, testEntitySetOut.size());
277
278         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner4Key));
279         assertEquals(1, testEntitySetOut.size());
280
281         testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner5Key));
282         assertEquals(4, testEntitySetOut.size());
283
284         assertNotNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
285         assertNotNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
286         assertNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
287         assertNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
288         pfDao.delete(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0"));
289
290         final Set<PfReferenceKey> rKeySetIn = new TreeSet<PfReferenceKey>();
291         rKeySetIn.add(new PfReferenceKey(owner4Key, "EntityB"));
292         rKeySetIn.add(new PfReferenceKey(owner5Key, "EntityD"));
293
294         final int deletedRCount = pfDao.deleteByReferenceKey(DummyReferenceEntity.class, rKeySetIn);
295         assertEquals(2, deletedRCount);
296
297         pfDao.update(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 120.0));
298     }
299 }