2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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.eclipselink;
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;
39 import org.onap.policy.apex.plugins.persistence.jpa.eclipselink.entities.ArtifactKeyTestEntity;
40 import org.onap.policy.apex.plugins.persistence.jpa.eclipselink.entities.ReferenceKeyTestEntity;
43 * Junit tests for class EclipselinkApexDao.
45 * @author Dinh Danh Le (dinh.danh.le@ericsson.com)
49 public class EclipselinkApexDaoTest {
51 private static final List<AxArtifactKey> TEST_ARTIKEYS = Arrays.asList(new AxArtifactKey[]
52 { new AxArtifactKey("ABC", "0.0.1"), new AxArtifactKey("DEF", "0.1.1"), new AxArtifactKey("XYZ", "1.1.1") });
54 private final DaoParameters daoParameters = new DaoParameters();
56 private EclipselinkApexDao eclipselinkApexDao = null;
61 * @throws ApexException Exceptions from test setup
64 public void setup() throws ApexException {
65 daoParameters.setPluginClass(EclipselinkApexDao.class.getName());
66 daoParameters.setPersistenceUnit("DAOTest");
67 eclipselinkApexDao = new EclipselinkApexDao();
68 eclipselinkApexDao.init(daoParameters);
72 public void teardown() {
73 eclipselinkApexDao.close();
77 public void test_NullArguments() {
78 final AxArtifactKey nullArtifactKey = null;
79 final AxArtifactKey nullRefernceKey = null;
80 final List<Object> emptyList = Collections.emptyList();
82 assertNull(eclipselinkApexDao.getArtifact(null, nullArtifactKey));
83 assertNull(eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class, nullArtifactKey));
85 assertNull(eclipselinkApexDao.getArtifact(null, nullRefernceKey));
86 assertNull(eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class, nullRefernceKey));
88 assertNotNull(eclipselinkApexDao.getAll(null));
89 assertTrue(eclipselinkApexDao.getAll(null).equals(emptyList));
90 assertNotNull(eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class));
94 public void test_createObject() throws ApexException {
95 // create 3 more entities from testArtiKeys
96 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
97 eclipselinkApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), Math.random()));
99 assertEquals(3, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
103 public void test_getAll() {
104 // create a list of three entities from testArtiKeys
105 final double[] genDoubleVals = new double[TEST_ARTIKEYS.size()];
107 for (int i = 0; i < TEST_ARTIKEYS.size(); i++) {
108 final AxArtifactKey akey = TEST_ARTIKEYS.get(i);
109 genDoubleVals[i] = Math.random();
110 eclipselinkApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), genDoubleVals[i]));
113 final List<ReferenceKeyTestEntity> ret = eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class);
114 assertEquals(TEST_ARTIKEYS.size(), ret.size());
116 for (int i = 0; i < TEST_ARTIKEYS.size(); i++) {
117 final ReferenceKeyTestEntity e = ret.get(i);
118 assertEquals(TEST_ARTIKEYS.get(i), e.getKey().getParentArtifactKey());
119 assertEquals(genDoubleVals[i], e.getDoubleValue(), 0.0);
124 public void test_getArtifactByReferenceKey() {
125 final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
126 final AxReferenceKey referenceKey = new AxReferenceKey(artifactKey, "Entity1");
128 // assert null if Entity Class is null
129 assertNull(eclipselinkApexDao.getArtifact(null, referenceKey));
131 // create PersistenceContext with an entity
132 eclipselinkApexDao.create(new ReferenceKeyTestEntity(referenceKey, 1.0));
133 // assert null when trying to find an entity with an unknown key
134 final AxArtifactKey anotherArtifactKey = new AxArtifactKey("YYY", "0.0.2");
135 final AxReferenceKey anotherReferenceKey = new AxReferenceKey(anotherArtifactKey);
137 assertNull(eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class, anotherReferenceKey));
139 // assert return only one entity when finding an entity with correct key
140 final ReferenceKeyTestEntity retEntity = eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class,
142 assertEquals(referenceKey, retEntity.getKey());
146 public void test_getArtifactByArtifactKey() {
147 final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
148 // assert null if either Entity Class is null
149 assertNull(eclipselinkApexDao.getArtifact(null, artifactKey));
151 eclipselinkApexDao.create(new ArtifactKeyTestEntity(artifactKey, 1.0));
153 // assert null when trying to find an entity with an unknown key
154 final AxArtifactKey otherArtifactKey = new AxArtifactKey("YYY", "0.0.2");
155 assertNull(eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class, otherArtifactKey));
157 // assert return only one entity when finding an entity with correct key
158 final ArtifactKeyTestEntity retEntity = eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class,
160 assertNotNull(retEntity);
161 assertEquals(artifactKey, retEntity.getKey());
165 public void test_deleteByArtifactKey() {
166 // initialize a list of (3) entities corresponding to the list of testArtiKeys
167 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
168 eclipselinkApexDao.create(new ArtifactKeyTestEntity(akey, Math.random()));
170 // create one more entity
171 final ArtifactKeyTestEntity entity = new ArtifactKeyTestEntity(new AxArtifactKey("XYZ", "100.0.0"), 100.0);
172 eclipselinkApexDao.create(entity);
174 assertEquals(3, eclipselinkApexDao.deleteByArtifactKey(ArtifactKeyTestEntity.class, TEST_ARTIKEYS));
176 // after deleteByArtifactKey()--> getAll().size() == 1
177 final List<ArtifactKeyTestEntity> remainingEntities = eclipselinkApexDao.getAll(ArtifactKeyTestEntity.class);
178 assertEquals(1, remainingEntities.size());
179 // more details about the remainingEntities
180 assertEquals(100.0, remainingEntities.get(0).getDoubleValue(), 0.0);
184 public void test_deleteByReferenceKey() {
185 // prepare 2 AxArtifactKeys
186 final AxArtifactKey owner0Key = new AxArtifactKey("Owner0", "0.0.1");
187 final AxArtifactKey owner1Key = new AxArtifactKey("Owner1", "0.0.1");
189 // prepare a list of (3) AxReferenceKeys corresponding to owner0Key
190 final List<AxReferenceKey> refKey0s = Arrays.asList(new AxReferenceKey[] {
191 new AxReferenceKey(owner0Key, "Entity01"),
192 new AxReferenceKey(owner0Key, "Entity02"),
193 new AxReferenceKey(owner0Key, "Entity03")
196 // prepare 2 more AxReferenceKeys corresponding to owner1Key
197 final AxReferenceKey refKey11 = new AxReferenceKey(owner1Key, "Entity11");
198 final AxReferenceKey refKey12 = new AxReferenceKey(owner1Key, "Entity12");
200 // create a list of 5 entities
201 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(0), 101.0));
202 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(1), 102.0));
203 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(2), 103.0));
204 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey11, 104.0));
205 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey12, 105.0));
207 // assert 3 entities are deleted by this deletion
208 assertEquals(3, eclipselinkApexDao.deleteByReferenceKey(ReferenceKeyTestEntity.class, refKey0s));
209 // after deletion, make sure getAll().size() == 2
210 assertEquals(2, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
214 public void test_deleteAll() {
215 // initialize a list of (3) entities and add to the PersistenceContext
216 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
217 eclipselinkApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), Math.random()));
219 // before deleteAll()--> getAll().size() == 3
220 assertEquals(3, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
221 eclipselinkApexDao.deleteAll(ReferenceKeyTestEntity.class);
222 // after deleteAll()--> getAll().size() == 0
223 assertEquals(0, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
227 public void test_getAllByArtifactKey() {
229 final AxArtifactKey artiKey0 = new AxArtifactKey("XYZA", "0.1.2");
230 final AxArtifactKey artiKey1 = new AxArtifactKey("ONAP", "0.0.1");
232 final AxReferenceKey refKey0 = new AxReferenceKey(artiKey0, "Entity0");
233 final AxReferenceKey refKey1 = new AxReferenceKey(artiKey1, "Entity1");
235 // test with null class with known key --> return an empty list
236 assertNotNull(eclipselinkApexDao.getAll(null, artiKey1));
237 assertTrue(eclipselinkApexDao.getAll(null, artiKey1).equals(Collections.emptyList()));
239 // test with (not_null) ArtifactKeyTestEntity class
240 assertEquals(0, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0).size());
242 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0, 100.0));
243 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0, 200.0));
244 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey1, 100.0));
246 final List<ReferenceKeyTestEntity> ret = eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0);
247 assertEquals(1, ret.size());
248 final ReferenceKeyTestEntity retEntity = ret.get(0);
249 assertEquals(200.0, retEntity.getDoubleValue(), 0);