2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
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.apex.plugins.persistence.jpa.hibernate;
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;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
38 import org.onap.policy.apex.model.basicmodel.dao.DAOParameters;
41 * unit test for class HibernateApexDao
43 * @author Dinh Danh Le (dinh.danh.le@ericsson.com)
47 public class HibernateApexDaoTest {
49 private static final List<AxArtifactKey> TEST_ARTIKEYS = Arrays.asList(new AxArtifactKey[] {
50 new AxArtifactKey("ABC", "0.0.1"), new AxArtifactKey("DEF", "0.1.1"), new AxArtifactKey("XYZ", "1.1.1")});
52 private final DAOParameters daoParameters = new DAOParameters();
54 private HibernateApexDao hibernateApexDao = null;
57 public void setupDAO() throws ApexException {
58 daoParameters.setPluginClass(HibernateApexDao.class.getCanonicalName());
59 daoParameters.setPersistenceUnit("HiDAOtest");
60 hibernateApexDao = new HibernateApexDao();
61 hibernateApexDao.init(daoParameters);
65 public void teardownDAO() {
66 hibernateApexDao.close();
70 public void test_NullArguments() {
71 final AxArtifactKey nullArtifactKey = null;
72 final AxArtifactKey nullRefernceKey = null;
73 final List<Object> emptyList = Collections.emptyList();
75 assertNull(hibernateApexDao.getArtifact(null, nullArtifactKey));
76 assertNull(hibernateApexDao.getArtifact(ArtifactKeyTestEntity.class, nullArtifactKey));
78 assertNull(hibernateApexDao.getArtifact(null, nullRefernceKey));
79 assertNull(hibernateApexDao.getArtifact(ReferenceKeyTestEntity.class, nullRefernceKey));
81 assertNotNull(hibernateApexDao.getAll(null));
82 assertTrue(hibernateApexDao.getAll(null).equals(emptyList));
83 assertNotNull(hibernateApexDao.getAll(ReferenceKeyTestEntity.class));
87 public void test_createObject() throws ApexException {
88 // create 3 more entities from testArtiKeys
89 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
90 hibernateApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), Math.random() + 100.0));
92 assertEquals(3, hibernateApexDao.getAll(ReferenceKeyTestEntity.class).size());
96 public void test_getAll() {
97 // create a list of three entities from testArtiKeys
98 final double[] genDoubleVals = new double[TEST_ARTIKEYS.size()];
100 for (int i = 0; i < TEST_ARTIKEYS.size(); i++) {
101 final AxArtifactKey akey = TEST_ARTIKEYS.get(i);
102 genDoubleVals[i] = Math.random();
103 hibernateApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), genDoubleVals[i]));
106 final List<ReferenceKeyTestEntity> ret = hibernateApexDao.getAll(ReferenceKeyTestEntity.class);
107 assertEquals(TEST_ARTIKEYS.size(), ret.size());
109 for (int i = 0; i < TEST_ARTIKEYS.size(); i++) {
110 final ReferenceKeyTestEntity e = ret.get(i);
111 assertEquals(TEST_ARTIKEYS.get(i), e.getKey().getParentArtifactKey());
112 assertEquals(genDoubleVals[i], e.getDoubleValue(), 0.0);
118 public void test_getArtifactByReferenceKey() {
119 final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
120 final AxReferenceKey referenceKey = new AxReferenceKey(artifactKey, "Entity1");
122 // assert null if Entity Class is null
123 assertNull(hibernateApexDao.getArtifact(null, referenceKey));
125 // create PersistenceContext with an entity
126 hibernateApexDao.create(new ReferenceKeyTestEntity(referenceKey, 1.0));
127 // assert null when trying to find an entity with an unknown key
128 final AxArtifactKey anotherArtifactKey = new AxArtifactKey("YYY", "0.0.2");
129 final AxReferenceKey anotherReferenceKey = new AxReferenceKey(anotherArtifactKey);
131 assertNull(hibernateApexDao.getArtifact(ReferenceKeyTestEntity.class, anotherReferenceKey));
133 // assert return only one entity when finding an entity with correct key
134 final ReferenceKeyTestEntity retEntity =
135 hibernateApexDao.getArtifact(ReferenceKeyTestEntity.class, referenceKey);
136 assertEquals(referenceKey, retEntity.getKey());
141 public void test_getArtifactByArtifactKey() {
142 final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
143 // assert null if either Entity Class is null
144 assertNull(hibernateApexDao.getArtifact(null, artifactKey));
146 hibernateApexDao.create(new ArtifactKeyTestEntity(artifactKey, 1.0));
148 // assert null when trying to find an entity with an unknown key
149 final AxArtifactKey otherArtifactKey = new AxArtifactKey("YYY", "0.0.2");
150 assertNull(hibernateApexDao.getArtifact(ArtifactKeyTestEntity.class, otherArtifactKey));
152 // assert return only one entity when finding an entity with correct key
153 final ArtifactKeyTestEntity retEntity = hibernateApexDao.getArtifact(ArtifactKeyTestEntity.class, artifactKey);
154 assertNotNull(retEntity);
155 assertEquals(artifactKey, retEntity.getKey());
160 public void test_deleteByArtifactKey() {
161 // initialize a list of (3) entities corresponding to the list of testArtiKeys
162 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
163 hibernateApexDao.create(new ArtifactKeyTestEntity(akey, Math.random()));
165 // create one more entity
166 final ArtifactKeyTestEntity entity = new ArtifactKeyTestEntity(new AxArtifactKey("XYZ", "100.0.0"), 100.0);
167 hibernateApexDao.create(entity);
169 assertEquals(3, hibernateApexDao.deleteByArtifactKey(ArtifactKeyTestEntity.class, TEST_ARTIKEYS));
171 // after deleteByArtifactKey()--> getAll().size() == 1
172 final List<ArtifactKeyTestEntity> remainingEntities = hibernateApexDao.getAll(ArtifactKeyTestEntity.class);
173 assertEquals(1, remainingEntities.size());
174 // more details about the remainingEntities
175 assertEquals(100.0, remainingEntities.get(0).getDoubleValue(), 0.0);
180 public void test_deleteByReferenceKey() {
181 // prepare 2 AxArtifactKeys
182 final AxArtifactKey owner0Key = new AxArtifactKey("Owner0", "0.0.1");
183 final AxArtifactKey owner1Key = new AxArtifactKey("Owner1", "0.0.1");
185 // prepare a list of (3) AxReferenceKeys corresponding to owner0Key
186 final List<AxReferenceKey> refKey0s =
187 Arrays.asList(new AxReferenceKey[] {new AxReferenceKey(owner0Key, "Entity01"),
188 new AxReferenceKey(owner0Key, "Entity02"), new AxReferenceKey(owner0Key, "Entity03")});
189 // prepare 2 more AxReferenceKeys corresponding to owner1Key
190 final AxReferenceKey refKey11 = new AxReferenceKey(owner1Key, "Entity11");
191 final AxReferenceKey refKey12 = new AxReferenceKey(owner1Key, "Entity12");
193 // create a list of 5 entities
194 hibernateApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(0), 101.0));
195 hibernateApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(1), 102.0));
196 hibernateApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(2), 103.0));
197 hibernateApexDao.create(new ReferenceKeyTestEntity(refKey11, 104.0));
198 hibernateApexDao.create(new ReferenceKeyTestEntity(refKey12, 105.0));
200 // assert 3 entities are deleted by this deletion
201 assertEquals(3, hibernateApexDao.deleteByReferenceKey(ReferenceKeyTestEntity.class, refKey0s));
202 // after deletion, make sure getAll().size() == 2
203 assertEquals(2, hibernateApexDao.getAll(ReferenceKeyTestEntity.class).size());
207 public void test_deleteAll() {
208 // initialize a list of (3) entities and add to the PersistenceContext
209 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
210 hibernateApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), Math.random()));
212 // before deleteAll()--> getAll().size() == 3
213 assertEquals(3, hibernateApexDao.getAll(ReferenceKeyTestEntity.class).size());
214 hibernateApexDao.deleteAll(ReferenceKeyTestEntity.class);
215 // after deleteAll()--> getAll().size() == 0
216 assertEquals(0, hibernateApexDao.getAll(ReferenceKeyTestEntity.class).size());
221 public void test_getAllByArtifactKey() {
223 final AxArtifactKey artiKey0 = new AxArtifactKey("XYZA", "0.1.2");
224 final AxArtifactKey artiKey1 = new AxArtifactKey("ONAP", "0.0.1");
226 final AxReferenceKey refKey0 = new AxReferenceKey(artiKey0, "Entity0");
227 final AxReferenceKey refKey1 = new AxReferenceKey(artiKey1, "Entity1");
230 // test with null class with known key --> return an empty list
231 assertNotNull(hibernateApexDao.getAll(null, artiKey1));
232 assertTrue(hibernateApexDao.getAll(null, artiKey1).equals(Collections.emptyList()));
234 // test with (not_null) ArtifactKeyTestEntity class
235 assertEquals(0, hibernateApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0).size());
237 hibernateApexDao.create(new ReferenceKeyTestEntity(refKey0, 100.0));
238 hibernateApexDao.create(new ReferenceKeyTestEntity(refKey0, 200.0));
239 hibernateApexDao.create(new ReferenceKeyTestEntity(refKey1, 100.0));
241 final List<ReferenceKeyTestEntity> ret = hibernateApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0);
242 assertEquals(1, ret.size());
243 final ReferenceKeyTestEntity retEntity = ret.get(0);
244 assertEquals(200.0, retEntity.getDoubleValue(), 0);