de3a39ebd604aa1df1f64078ca65a88b5ffed4b8
[policy/apex-pdp.git] / plugins / plugins-persistence / plugins-persistence-jpa / plugins-persistence-jpa-eclipselink / src / test / java / org / onap / policy / apex / plugins / persistence / jpa / eclipselink / EclipselinkApexDaoTest.java
1 /*-
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
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.apex.plugins.persistence.jpa.eclipselink;
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
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
31
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;
41
42 /**
43  * Junit tests for class EclipselinkApexDao.
44  *
45  * @author Dinh Danh Le (dinh.danh.le@ericsson.com)
46  *
47  */
48
49 public class EclipselinkApexDaoTest {
50
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") });
53
54     private final DaoParameters daoParameters = new DaoParameters();
55
56     private EclipselinkApexDao eclipselinkApexDao = null;
57
58     /**
59      * Setup the tests.
60      *
61      * @throws ApexException Exceptions from test setup
62      */
63     @Before
64     public void setup() throws ApexException {
65         daoParameters.setPluginClass(EclipselinkApexDao.class.getName());
66         daoParameters.setPersistenceUnit("DAOTest");
67         eclipselinkApexDao = new EclipselinkApexDao();
68         eclipselinkApexDao.init(daoParameters);
69     }
70
71     @After
72     public void teardown() {
73         eclipselinkApexDao.close();
74     }
75
76     @Test
77     public void test_NullArguments() {
78         final AxArtifactKey nullArtifactKey = null;
79         final AxArtifactKey nullRefernceKey = null;
80         final List<Object> emptyList = Collections.emptyList();
81
82         assertNull(eclipselinkApexDao.getArtifact(null, nullArtifactKey));
83         assertNull(eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class, nullArtifactKey));
84
85         assertNull(eclipselinkApexDao.getArtifact(null, nullRefernceKey));
86         assertNull(eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class, nullRefernceKey));
87
88         assertNotNull(eclipselinkApexDao.getAll(null));
89         assertTrue(eclipselinkApexDao.getAll(null).equals(emptyList));
90         assertNotNull(eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class));
91     }
92
93     @Test
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()));
98         }
99         assertEquals(3, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class).size());
100     }
101
102     @Test
103     public void test_getAll() {
104         // create a list of three entities from testArtiKeys
105         final double[] genDoubleVals = new double[TEST_ARTIKEYS.size()];
106
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]));
111         }
112
113         final List<ReferenceKeyTestEntity> ret = eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class);
114         assertEquals(TEST_ARTIKEYS.size(), ret.size());
115
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);
120         }
121     }
122
123     @Test
124     public void test_getArtifactByReferenceKey() {
125         final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
126         final AxReferenceKey referenceKey = new AxReferenceKey(artifactKey, "Entity1");
127
128         // assert null if Entity Class is null
129         assertNull(eclipselinkApexDao.getArtifact(null, referenceKey));
130
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);
136
137         assertNull(eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class, anotherReferenceKey));
138
139         // assert return only one entity when finding an entity with correct key
140         final ReferenceKeyTestEntity retEntity = eclipselinkApexDao.getArtifact(ReferenceKeyTestEntity.class,
141                         referenceKey);
142         assertEquals(referenceKey, retEntity.getKey());
143     }
144
145     @Test
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));
150         // create an entity
151         eclipselinkApexDao.create(new ArtifactKeyTestEntity(artifactKey, 1.0));
152
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));
156
157         // assert return only one entity when finding an entity with correct key
158         final ArtifactKeyTestEntity retEntity = eclipselinkApexDao.getArtifact(ArtifactKeyTestEntity.class,
159                         artifactKey);
160         assertNotNull(retEntity);
161         assertEquals(artifactKey, retEntity.getKey());
162     }
163
164     @Test
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()));
169         }
170         // create one more entity
171         final ArtifactKeyTestEntity entity = new ArtifactKeyTestEntity(new AxArtifactKey("XYZ", "100.0.0"), 100.0);
172         eclipselinkApexDao.create(entity);
173
174         assertEquals(3, eclipselinkApexDao.deleteByArtifactKey(ArtifactKeyTestEntity.class, TEST_ARTIKEYS));
175
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);
181     }
182
183     @Test
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");
188
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")
194         });
195
196         // prepare 2 more AxReferenceKeys corresponding to owner1Key
197         final AxReferenceKey refKey11 = new AxReferenceKey(owner1Key, "Entity11");
198         final AxReferenceKey refKey12 = new AxReferenceKey(owner1Key, "Entity12");
199
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));
206
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());
211     }
212
213     @Test
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()));
218         }
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());
224     }
225
226     @Test
227     public void test_getAllByArtifactKey() {
228
229         final AxArtifactKey artiKey0 = new AxArtifactKey("XYZA", "0.1.2");
230         final AxArtifactKey artiKey1 = new AxArtifactKey("ONAP", "0.0.1");
231
232         final AxReferenceKey refKey0 = new AxReferenceKey(artiKey0, "Entity0");
233         final AxReferenceKey refKey1 = new AxReferenceKey(artiKey1, "Entity1");
234
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()));
238
239         // test with (not_null) ArtifactKeyTestEntity class
240         assertEquals(0, eclipselinkApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0).size());
241         // create 2 entities
242         eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0, 100.0));
243         eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey0, 200.0));
244         eclipselinkApexDao.create(new ReferenceKeyTestEntity(refKey1, 100.0));
245
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);
250     }
251
252 }