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;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
37 import org.onap.policy.apex.model.basicmodel.dao.DaoParameters;
38 import org.onap.policy.apex.plugins.persistence.jpa.eclipselink.entities.ArtifactKeyTestEntity;
39 import org.onap.policy.apex.plugins.persistence.jpa.eclipselink.entities.ReferenceKeyTestEntity;
42 * Junit tests for class EclipselinkApexDao.
44 * @author Dinh Danh Le (dinh.danh.le@ericsson.com)
48 public class EclipselinkApexDaoTest {
50 private static final List<AxArtifactKey> TEST_ARTIKEYS = Arrays.asList(new AxArtifactKey[]
51 { new AxArtifactKey("ABC", "0.0.1"), new AxArtifactKey("DEF", "0.1.1"), new AxArtifactKey("XYZ", "1.1.1") });
53 private final DaoParameters daoParameters = new DaoParameters();
55 private EclipselinkApexDao eclipselinkApexDao = null;
60 * @throws ApexException Exceptions from test setup
63 public void setup() throws ApexException {
64 daoParameters.setPluginClass(EclipselinkApexDao.class.getName());
65 daoParameters.setPersistenceUnit("DAOTest");
66 eclipselinkApexDao = new EclipselinkApexDao();
67 eclipselinkApexDao.init(daoParameters);
71 public void teardown() {
72 eclipselinkApexDao.close();
76 public void test_NullArguments() {
77 final AxArtifactKey nullArtifactKey = null;
78 final AxArtifactKey nullRefernceKey = null;
79 final List<Object> emptyList = Collections.emptyList();
81 assertNull(eclipselinkApexDao.getArtifact(null, nullArtifactKey));
82 assertNull(eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class, nullArtifactKey));
84 assertNull(eclipselinkApexDao.getArtifact(null, nullRefernceKey));
85 assertNull(eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class, nullRefernceKey));
87 assertNotNull(eclipselinkApexDao.getAll(null));
88 assertTrue(eclipselinkApexDao.getAll(null).equals(emptyList));
89 assertNotNull(eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class));
93 public void test_createObject() throws ApexException {
94 // create 3 more entities from testArtiKeys
95 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
96 eclipselinkApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), Math.random()));
98 assertEquals(3, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
102 public void test_getAll() {
103 // create a list of three entities from testArtiKeys
104 final double[] genDoubleVals = new double[TEST_ARTIKEYS.size()];
106 for (int i = 0; i < TEST_ARTIKEYS.size(); i++) {
107 final AxArtifactKey akey = TEST_ARTIKEYS.get(i);
108 genDoubleVals[i] = Math.random();
109 eclipselinkApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), genDoubleVals[i]));
112 final List<ReferenceKeyTestEntity> ret = eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class);
113 assertEquals(TEST_ARTIKEYS.size(), ret.size());
115 for (int i = 0; i < TEST_ARTIKEYS.size(); i++) {
116 final ReferenceKeyTestEntity e = ret.get(i);
117 assertEquals(TEST_ARTIKEYS.get(i), e.getKey().getParentArtifactKey());
118 assertEquals(genDoubleVals[i], e.getDoubleValue(), 0.0);
123 public void test_getArtifactByReferenceKey() {
124 final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
125 final AxReferenceKey referenceKey = new AxReferenceKey(artifactKey, "Entity1");
127 // assert null if Entity Class is null
128 assertNull(eclipselinkApexDao.getArtifact(null, referenceKey));
130 // create PersistenceContext with an entity
131 eclipselinkApexDao.create(new ReferenceKeyTestEntity(referenceKey, 1.0));
132 // assert null when trying to find an entity with an unknown key
133 final AxArtifactKey anotherArtifactKey = new AxArtifactKey("YYY", "0.0.2");
134 final AxReferenceKey anotherReferenceKey = new AxReferenceKey(anotherArtifactKey);
136 assertNull(eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class, anotherReferenceKey));
138 // assert return only one entity when finding an entity with correct key
139 final ReferenceKeyTestEntity retEntity = eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class,
141 assertEquals(referenceKey, retEntity.getKey());
145 public void test_getArtifactByArtifactKey() {
146 final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
147 // assert null if either Entity Class is null
148 assertNull(eclipselinkApexDao.getArtifact(null, artifactKey));
150 eclipselinkApexDao.create(new ArtifactKeyTestEntity(artifactKey, 1.0));
152 // assert null when trying to find an entity with an unknown key
153 final AxArtifactKey otherArtifactKey = new AxArtifactKey("YYY", "0.0.2");
154 assertNull(eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class, otherArtifactKey));
156 // assert return only one entity when finding an entity with correct key
157 final ArtifactKeyTestEntity retEntity = eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class,
159 assertNotNull(retEntity);
160 assertEquals(artifactKey, retEntity.getKey());
164 public void test_deleteByArtifactKey() {
165 // initialize a list of (3) entities corresponding to the list of testArtiKeys
166 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
167 eclipselinkApexDao.create(new ArtifactKeyTestEntity(akey, Math.random()));
169 // create one more entity
170 final ArtifactKeyTestEntity entity = new ArtifactKeyTestEntity(new AxArtifactKey("XYZ", "100.0.0"), 100.0);
171 eclipselinkApexDao.create(entity);
173 assertEquals(3, eclipselinkApexDao.deleteByArtifactKey(ArtifactKeyTestEntity.class, TEST_ARTIKEYS));
175 // after deleteByArtifactKey()--> getAll().size() == 1
176 final List<ArtifactKeyTestEntity> remainingEntities = eclipselinkApexDao.getAll(ArtifactKeyTestEntity.class);
177 assertEquals(1, remainingEntities.size());
178 // more details about the remainingEntities
179 assertEquals(100.0, remainingEntities.get(0).getDoubleValue(), 0.0);
183 public void test_deleteByReferenceKey() {
184 // prepare 2 AxArtifactKeys
185 final AxArtifactKey owner0Key = new AxArtifactKey("Owner0", "0.0.1");
186 final AxArtifactKey owner1Key = new AxArtifactKey("Owner1", "0.0.1");
188 // prepare a list of (3) AxReferenceKeys corresponding to owner0Key
189 final List<AxReferenceKey> refKey0s = Arrays.asList(new AxReferenceKey[] {
190 new AxReferenceKey(owner0Key, "Entity01"),
191 new AxReferenceKey(owner0Key, "Entity02"),
192 new AxReferenceKey(owner0Key, "Entity03")
195 // prepare 2 more AxReferenceKeys corresponding to owner1Key
196 final AxReferenceKey refKey11 = new AxReferenceKey(owner1Key, "Entity11");
197 final AxReferenceKey refKey12 = new AxReferenceKey(owner1Key, "Entity12");
199 // create a list of 5 entities
200 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(0), 101.0));
201 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(1), 102.0));
202 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(2), 103.0));
203 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey11, 104.0));
204 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey12, 105.0));
206 // assert 3 entities are deleted by this deletion
207 assertEquals(3, eclipselinkApexDao.deleteByReferenceKey(ReferenceKeyTestEntity.class, refKey0s));
208 // after deletion, make sure getAll().size() == 2
209 assertEquals(2, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
213 public void test_deleteAll() {
214 // initialize a list of (3) entities and add to the PersistenceContext
215 for (final AxArtifactKey akey : TEST_ARTIKEYS) {
216 eclipselinkApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), Math.random()));
218 // before deleteAll()--> getAll().size() == 3
219 assertEquals(3, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
220 eclipselinkApexDao.deleteAll(ReferenceKeyTestEntity.class);
221 // after deleteAll()--> getAll().size() == 0
222 assertEquals(0, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
226 public void test_getAllByArtifactKey() {
228 final AxArtifactKey artiKey0 = new AxArtifactKey("XYZA", "0.1.2");
229 final AxArtifactKey artiKey1 = new AxArtifactKey("ONAP", "0.0.1");
231 final AxReferenceKey refKey0 = new AxReferenceKey(artiKey0, "Entity0");
232 final AxReferenceKey refKey1 = new AxReferenceKey(artiKey1, "Entity1");
234 // test with null class with known key --> return an empty list
235 assertNotNull(eclipselinkApexDao.getAll(null, artiKey1));
236 assertTrue(eclipselinkApexDao.getAll(null, artiKey1).equals(Collections.emptyList()));
238 // test with (not_null) ArtifactKeyTestEntity class
239 assertEquals(0, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0).size());
241 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0, 100.0));
242 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0, 200.0));
243 eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey1, 100.0));
245 final List<ReferenceKeyTestEntity> ret = eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0);
246 assertEquals(1, ret.size());
247 final ReferenceKeyTestEntity retEntity = ret.get(0);
248 assertEquals(200.0, retEntity.getDoubleValue(), 0);