e96c8ea49f9cc1d6a1d74ec02aee8cc6014ca85a
[policy/apex-pdp.git] /
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.hibernate;
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.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;
41
42 /**
43  * Junit test for class HibernateApexDao.
44  * 
45  * @author Dinh Danh Le (dinh.danh.le@ericsson.com)
46  *
47  */
48
49 public class HibernateApexDaoTest {
50     private static final List<AxArtifactKey> TEST_ARTIKEYS = Arrays.asList(new AxArtifactKey[] {
51         new AxArtifactKey("ABC", "0.0.1"),
52         new AxArtifactKey("DEF", "0.1.1"), new AxArtifactKey("XYZ", "1.1.1")
53     });
54
55     private final DaoParameters daoParameters = new DaoParameters();
56
57     private HibernateApexDao hibernateApexDao = null;
58
59     /**
60      * Set up tests.
61      * 
62      * @throws ApexException on test setup errors
63      */
64     @Before
65     public void setupDao() throws ApexException {
66         daoParameters.setPluginClass(HibernateApexDao.class.getCanonicalName());
67         daoParameters.setPersistenceUnit("DAOTest");
68         hibernateApexDao = new HibernateApexDao();
69         hibernateApexDao.init(daoParameters);
70     }
71
72     /**
73      * Cleardown tests.
74      */
75     @After
76     public void teardownDao() {
77         hibernateApexDao.close();
78     }
79
80     @Test
81     public void test_NullArguments() {
82         final AxArtifactKey nullArtifactKey = null;
83         final AxArtifactKey nullRefernceKey = null;
84         final List<Object> emptyList = Collections.emptyList();
85
86         assertNull(hibernateApexDao.getArtifact(null, nullArtifactKey));
87         assertNull(hibernateApexDao.getArtifact(ArtifactKeyTestEntity.class, nullArtifactKey));
88
89         assertNull(hibernateApexDao.getArtifact(null, nullRefernceKey));
90         assertNull(hibernateApexDao.getArtifact(ReferenceKeyTestEntity.class, nullRefernceKey));
91
92         assertNotNull(hibernateApexDao.getAll(null));
93         assertTrue(hibernateApexDao.getAll(null).equals(emptyList));
94         assertNotNull(hibernateApexDao.getAll(ReferenceKeyTestEntity.class));
95     }
96
97     @Test
98     public void test_createObject() throws ApexException {
99         // create 3 more entities from testArtiKeys
100         for (final AxArtifactKey akey : TEST_ARTIKEYS) {
101             hibernateApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), Math.random() + 100.0));
102         }
103         assertEquals(3, hibernateApexDao.getAll(ReferenceKeyTestEntity.class).size());
104     }
105
106     @Test
107     public void test_getAll() {
108         // create a list of three entities from testArtiKeys
109         final double[] genDoubleVals = new double[TEST_ARTIKEYS.size()];
110
111         for (int i = 0; i < TEST_ARTIKEYS.size(); i++) {
112             final AxArtifactKey akey = TEST_ARTIKEYS.get(i);
113             genDoubleVals[i] = Math.random();
114             hibernateApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), genDoubleVals[i]));
115         }
116
117         final List<ReferenceKeyTestEntity> ret = hibernateApexDao.getAll(ReferenceKeyTestEntity.class);
118         assertEquals(TEST_ARTIKEYS.size(), ret.size());
119
120         for (int i = 0; i < TEST_ARTIKEYS.size(); i++) {
121             final ReferenceKeyTestEntity e = ret.get(i);
122             assertEquals(TEST_ARTIKEYS.get(i), e.getKey().getParentArtifactKey());
123             assertEquals(genDoubleVals[i], e.getDoubleValue(), 0.0);
124         }
125     }
126
127     @Test
128     public void test_getArtifactByReferenceKey() {
129         final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
130         final AxReferenceKey referenceKey = new AxReferenceKey(artifactKey, "Entity1");
131
132         // assert null if Entity Class is null
133         assertNull(hibernateApexDao.getArtifact(null, referenceKey));
134
135         // create PersistenceContext with an entity
136         hibernateApexDao.create(new ReferenceKeyTestEntity(referenceKey, 1.0));
137         // assert null when trying to find an entity with an unknown key
138         final AxArtifactKey anotherArtifactKey = new AxArtifactKey("YYY", "0.0.2");
139         final AxReferenceKey anotherReferenceKey = new AxReferenceKey(anotherArtifactKey);
140
141         assertNull(hibernateApexDao.getArtifact(ReferenceKeyTestEntity.class, anotherReferenceKey));
142
143         // assert return only one entity when finding an entity with correct key
144         final ReferenceKeyTestEntity retEntity = hibernateApexDao.getArtifact(ReferenceKeyTestEntity.class,
145                         referenceKey);
146         assertEquals(referenceKey, retEntity.getKey());
147     }
148
149     @Test
150     public void test_getArtifactByArtifactKey() {
151         final AxArtifactKey artifactKey = new AxArtifactKey("XXX", "0.0.1");
152         // assert null if either Entity Class is null
153         assertNull(hibernateApexDao.getArtifact(null, artifactKey));
154         // create an entity
155         hibernateApexDao.create(new ArtifactKeyTestEntity(artifactKey, 1.0));
156
157         // assert null when trying to find an entity with an unknown key
158         final AxArtifactKey otherArtifactKey = new AxArtifactKey("YYY", "0.0.2");
159         assertNull(hibernateApexDao.getArtifact(ArtifactKeyTestEntity.class, otherArtifactKey));
160
161         // assert return only one entity when finding an entity with correct key
162         final ArtifactKeyTestEntity retEntity = hibernateApexDao.getArtifact(ArtifactKeyTestEntity.class, artifactKey);
163         assertNotNull(retEntity);
164         assertEquals(artifactKey, retEntity.getKey());
165     }
166
167     @Test
168     public void test_deleteByArtifactKey() {
169         // initialize a list of (3) entities corresponding to the list of testArtiKeys
170         for (final AxArtifactKey akey : TEST_ARTIKEYS) {
171             hibernateApexDao.create(new ArtifactKeyTestEntity(akey, Math.random()));
172         }
173         // create one more entity
174         final ArtifactKeyTestEntity entity = new ArtifactKeyTestEntity(new AxArtifactKey("XYZ", "100.0.0"), 100.0);
175         hibernateApexDao.create(entity);
176
177         assertEquals(3, hibernateApexDao.deleteByArtifactKey(ArtifactKeyTestEntity.class, TEST_ARTIKEYS));
178
179         // after deleteByArtifactKey()--> getAll().size() == 1
180         final List<ArtifactKeyTestEntity> remainingEntities = hibernateApexDao.getAll(ArtifactKeyTestEntity.class);
181         assertEquals(1, remainingEntities.size());
182         // more details about the remainingEntities
183         assertEquals(100.0, remainingEntities.get(0).getDoubleValue(), 0.0);
184     }
185
186     @Test
187     public void test_deleteByReferenceKey() {
188         // prepare 2 AxArtifactKeys
189         final AxArtifactKey owner0Key = new AxArtifactKey("Owner0", "0.0.1");
190         final AxArtifactKey owner1Key = new AxArtifactKey("Owner1", "0.0.1");
191
192         // prepare a list of (3) AxReferenceKeys corresponding to owner0Key
193         final List<AxReferenceKey> refKey0s = Arrays.asList(new AxReferenceKey[] {
194             new AxReferenceKey(owner0Key, "Entity01"), new AxReferenceKey(owner0Key, "Entity02"),
195             new AxReferenceKey(owner0Key, "Entity03")
196         });
197         // prepare 2 more AxReferenceKeys corresponding to owner1Key
198         final AxReferenceKey refKey11 = new AxReferenceKey(owner1Key, "Entity11");
199         final AxReferenceKey refKey12 = new AxReferenceKey(owner1Key, "Entity12");
200
201         // create a list of 5 entities
202         hibernateApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(0), 101.0));
203         hibernateApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(1), 102.0));
204         hibernateApexDao.create(new ReferenceKeyTestEntity(refKey0s.get(2), 103.0));
205         hibernateApexDao.create(new ReferenceKeyTestEntity(refKey11, 104.0));
206         hibernateApexDao.create(new ReferenceKeyTestEntity(refKey12, 105.0));
207
208         // assert 3 entities are deleted by this deletion
209         assertEquals(3, hibernateApexDao.deleteByReferenceKey(ReferenceKeyTestEntity.class, refKey0s));
210         // after deletion, make sure getAll().size() == 2
211         assertEquals(2, hibernateApexDao.getAll(ReferenceKeyTestEntity.class).size());
212     }
213
214     @Test
215     public void test_deleteAll() {
216         // initialize a list of (3) entities and add to the PersistenceContext
217         for (final AxArtifactKey akey : TEST_ARTIKEYS) {
218             hibernateApexDao.create(new ReferenceKeyTestEntity(new AxReferenceKey(akey), Math.random()));
219         }
220         // before deleteAll()--> getAll().size() == 3
221         assertEquals(3, hibernateApexDao.getAll(ReferenceKeyTestEntity.class).size());
222         hibernateApexDao.deleteAll(ReferenceKeyTestEntity.class);
223         // after deleteAll()--> getAll().size() == 0
224         assertEquals(0, hibernateApexDao.getAll(ReferenceKeyTestEntity.class).size());
225     }
226
227     @Test
228     public void test_getAllByArtifactKey() {
229
230         final AxArtifactKey artiKey0 = new AxArtifactKey("XYZA", "0.1.2");
231         final AxArtifactKey artiKey1 = new AxArtifactKey("ONAP", "0.0.1");
232
233         final AxReferenceKey refKey0 = new AxReferenceKey(artiKey0, "Entity0");
234         final AxReferenceKey refKey1 = new AxReferenceKey(artiKey1, "Entity1");
235
236         // test with null class with known key --> return an empty list
237         assertNotNull(hibernateApexDao.getAll(null, artiKey1));
238         assertTrue(hibernateApexDao.getAll(null, artiKey1).equals(Collections.emptyList()));
239
240         // test with (not_null) ArtifactKeyTestEntity class
241         assertEquals(0, hibernateApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0).size());
242         // create 2 entities
243         hibernateApexDao.create(new ReferenceKeyTestEntity(refKey0, 100.0));
244         hibernateApexDao.create(new ReferenceKeyTestEntity(refKey0, 200.0));
245         hibernateApexDao.create(new ReferenceKeyTestEntity(refKey1, 100.0));
246
247         final List<ReferenceKeyTestEntity> ret = hibernateApexDao.getAll(ReferenceKeyTestEntity.class, artiKey0);
248         assertEquals(1, ret.size());
249         final ReferenceKeyTestEntity retEntity = ret.get(0);
250         assertEquals(200.0, retEntity.getDoubleValue(), 0);
251     }
252
253 }