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.context.test.entities.ArtifactKeyTestEntity;
36 import org.onap.policy.apex.context.test.entities.ReferenceKeyTestEntity;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
40 import org.onap.policy.apex.model.basicmodel.dao.DaoParameters;
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;
59 public void setup() throws ApexException {
60 DaoParameters.setPluginClass(EclipselinkApexDao.class.getCanonicalName());
61 DaoParameters.setPersistenceUnit("DAOTest");
62 eclipselinkApexDao = new EclipselinkApexDao();
63 eclipselinkApexDao.init(DaoParameters);
67 public void teardown() {
68 eclipselinkApexDao.close();
72 public void test_NullArguments() {
73 final AxArtifactKey nullArtifactKey = null;
74 final AxArtifactKey nullRefernceKey = null;
75 final List<Object> emptyList = Collections.emptyList();
77 assertNull(eclipselinkApexDao.getArtifact(null, nullArtifactKey));
78 assertNull(eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class, nullArtifactKey));
80 assertNull(eclipselinkApexDao.getArtifact(null, nullRefernceKey));
81 assertNull(eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class, nullRefernceKey));
83 assertNotNull(eclipselinkApexDao.getAll(null));
84 assertTrue(eclipselinkApexDao.getAll(null).equals(emptyList));
85 assertNotNull(eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class));
89 public void test_createObject() throws ApexException {
90 // create 3 more entities from testArtiKeys
91 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
92 eclipselinkApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), Math.random()));
94 assertEquals(3, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
98 public void test_getAll() {
99 // create a list of three entities from testArtiKeys
100 final double[] genDoubleVals = new double[TEST_ARTIKEYS.size()];
102 for (int i = 0; i < TEST_ARTIKEYS.size(); i++) {
103 final AxArtifactKey akey = TEST_ARTIKEYS.get(i);
104 genDoubleVals[i] = Math.random();
105 eclipselinkApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), genDoubleVals[i]));
108 final List<ReferenceKeyTestEntity> ret = eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class);
109 assertEquals(TEST_ARTIKEYS.size(), ret.size());
111 for (int i = 0; i < TEST_ARTIKEYS.size(); i++) {
112 final ReferenceKeyTestEntity e = ret.get(i);
113 assertEquals(TEST_ARTIKEYS.get(i), e.getKey().getParentArtifactKey());
114 assertEquals(genDoubleVals[i], e.getDoubleValue(), 0.0);
120 public void test_getArtifactByReferenceKey() {
121 final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
122 final AxReferenceKey referenceKey = new AxReferenceKey(artifactKey, "Entity1");
124 // assert null if Entity Class is null
125 assertNull(eclipselinkApexDao.getArtifact(null, referenceKey));
127 // create PersistenceContext with an entity
128 eclipselinkApexDao.create(new ReferenceKeyTestEntity(referenceKey, 1.0));
129 // assert null when trying to find an entity with an unknown key
130 final AxArtifactKey anotherArtifactKey = new AxArtifactKey("YYY", "0.0.2");
131 final AxReferenceKey anotherReferenceKey = new AxReferenceKey(anotherArtifactKey);
133 assertNull(eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class, anotherReferenceKey));
135 // assert return only one entity when finding an entity with correct key
136 final ReferenceKeyTestEntity retEntity =
137 eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class, referenceKey);
138 assertEquals(referenceKey, retEntity.getKey());
143 public void test_getArtifactByArtifactKey() {
144 final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
145 // assert null if either Entity Class is null
146 assertNull(eclipselinkApexDao.getArtifact(null, artifactKey));
148 eclipselinkApexDao.create(new ArtifactKeyTestEntity(artifactKey, 1.0));
150 // assert null when trying to find an entity with an unknown key
151 final AxArtifactKey otherArtifactKey = new AxArtifactKey("YYY", "0.0.2");
152 assertNull(eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class, otherArtifactKey));
154 // assert return only one entity when finding an entity with correct key
155 final ArtifactKeyTestEntity retEntity = eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class, artifactKey);
156 assertNotNull(retEntity);
157 assertEquals(artifactKey, retEntity.getKey());
162 public void test_deleteByArtifactKey() {
163 // initialize a list of (3) entities corresponding to the list of testArtiKeys
164 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
165 eclipselinkApexDao.create(new ArtifactKeyTestEntity(akey, Math.random()));
167 // create one more entity
168 final ArtifactKeyTestEntity entity = new ArtifactKeyTestEntity(new AxArtifactKey("XYZ", "100.0.0"), 100.0);
169 eclipselinkApexDao.create(entity);
171 assertEquals(3, eclipselinkApexDao.deleteByArtifactKey(ArtifactKeyTestEntity.class, TEST_ARTIKEYS));
173 // after deleteByArtifactKey()--> getAll().size() == 1
174 final List<ArtifactKeyTestEntity> remainingEntities = eclipselinkApexDao.getAll(ArtifactKeyTestEntity.class);
175 assertEquals(1, remainingEntities.size());
176 // more details about the remainingEntities
177 assertEquals(100.0, remainingEntities.get(0).getDoubleValue(), 0.0);
182 public void test_deleteByReferenceKey() {
183 // prepare 2 AxArtifactKeys
184 final AxArtifactKey owner0Key = new AxArtifactKey("Owner0", "0.0.1");
185 final AxArtifactKey owner1Key = new AxArtifactKey("Owner1", "0.0.1");
187 // prepare a list of (3) AxReferenceKeys corresponding to owner0Key
188 final List<AxReferenceKey> refKey0s =
189 Arrays.asList(new AxReferenceKey[] {new AxReferenceKey(owner0Key, "Entity01"),
190 new AxReferenceKey(owner0Key, "Entity02"), new AxReferenceKey(owner0Key, "Entity03")});
191 // prepare 2 more AxReferenceKeys corresponding to owner1Key
192 final AxReferenceKey refKey11 = new AxReferenceKey(owner1Key, "Entity11");
193 final AxReferenceKey refKey12 = new AxReferenceKey(owner1Key, "Entity12");
195 // create a list of 5 entities
196 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(0), 101.0));
197 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(1), 102.0));
198 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(2), 103.0));
199 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey11, 104.0));
200 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey12, 105.0));
202 // assert 3 entities are deleted by this deletion
203 assertEquals(3, eclipselinkApexDao.deleteByReferenceKey(ReferenceKeyTestEntity.class, refKey0s));
204 // after deletion, make sure getAll().size() == 2
205 assertEquals(2, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
209 public void test_deleteAll() {
210 // initialize a list of (3) entities and add to the PersistenceContext
211 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
212 eclipselinkApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), Math.random()));
214 // before deleteAll()--> getAll().size() == 3
215 assertEquals(3, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
216 eclipselinkApexDao.deleteAll(ReferenceKeyTestEntity.class);
217 // after deleteAll()--> getAll().size() == 0
218 assertEquals(0, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
223 public void test_getAllByArtifactKey() {
225 final AxArtifactKey artiKey0 = new AxArtifactKey("XYZA", "0.1.2");
226 final AxArtifactKey artiKey1 = new AxArtifactKey("ONAP", "0.0.1");
228 final AxReferenceKey refKey0 = new AxReferenceKey(artiKey0, "Entity0");
229 final AxReferenceKey refKey1 = new AxReferenceKey(artiKey1, "Entity1");
232 // test with null class with known key --> return an empty list
233 assertNotNull(eclipselinkApexDao.getAll(null, artiKey1));
234 assertTrue(eclipselinkApexDao.getAll(null, artiKey1).equals(Collections.emptyList()));
236 // test with (not_null) ArtifactKeyTestEntity class
237 assertEquals(0, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0).size());
239 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0, 100.0));
240 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0, 200.0));
241 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey1, 100.0));
243 final List<ReferenceKeyTestEntity> ret = eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0);
244 assertEquals(1, ret.size());
245 final ReferenceKeyTestEntity retEntity = ret.get(0);
246 assertEquals(200.0, retEntity.getDoubleValue(), 0);