/
MfaOptionDAOTest.java
334 lines (278 loc) · 16.9 KB
/
MfaOptionDAOTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.integration.misc;
import org.eclipse.kapua.KapuaEntityExistsException;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.jpa.EntityManager;
import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.eclipse.kapua.service.authentication.credential.mfa.MfaOption;
import org.eclipse.kapua.service.authentication.credential.mfa.MfaOptionCreator;
import org.eclipse.kapua.service.authentication.credential.mfa.MfaOptionListResult;
import org.eclipse.kapua.service.authentication.credential.mfa.shiro.MfaOptionDAO;
import org.eclipse.kapua.service.authentication.credential.mfa.shiro.MfaOptionImpl;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;
import javax.persistence.EntityExistsException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.EntityType;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Category(JUnitTests.class)
public class MfaOptionDAOTest extends Assert {
EntityManager entityManager;
MfaOptionCreator mfaOptionCreator;
MfaOptionImpl mfaOption, entityToFindOrDelete;
Date createdOn;
KapuaId createdBy, scopeId, mfaOptionId;
KapuaQuery kapuaQuery;
@Before
public void initialize() {
entityManager = Mockito.mock(EntityManager.class);
mfaOptionCreator = Mockito.mock(MfaOptionCreator.class);
mfaOption = Mockito.mock(MfaOptionImpl.class);
entityToFindOrDelete = Mockito.mock(MfaOptionImpl.class);
createdOn = new Date();
createdBy = KapuaId.ONE;
scopeId = KapuaId.ONE;
mfaOptionId = KapuaId.ONE;
kapuaQuery = Mockito.mock(KapuaQuery.class);
}
@Test
public void createTest() throws KapuaException {
Mockito.when(mfaOptionCreator.getMfaSecretKey()).thenReturn("mfa secret key");
Mockito.when(mfaOptionCreator.getScopeId()).thenReturn(KapuaId.ONE);
Mockito.when(mfaOptionCreator.getUserId()).thenReturn(new KapuaEid(BigInteger.ONE));
assertThat("Instance of MfaOption object expected.", MfaOptionDAO.create(entityManager, mfaOptionCreator), IsInstanceOf.instanceOf(MfaOption.class));
assertEquals("Expected and actual values should be the same.", "kOhsl1vEPvSuZcifFlMCvg", MfaOptionDAO.create(entityManager, mfaOptionCreator).getMfaSecretKey());
assertEquals("Expected and actual values should be the same.", KapuaId.ONE, MfaOptionDAO.create(entityManager, mfaOptionCreator).getScopeId());
assertEquals("Expected and actual values should be the same.", KapuaId.ONE, MfaOptionDAO.create(entityManager, mfaOptionCreator).getUserId());
}
@Test(expected = KapuaEntityExistsException.class)
public void createEntityExistsExceptionTest() throws KapuaException {
Mockito.when(mfaOptionCreator.getMfaSecretKey()).thenReturn("mfa secret key");
Mockito.when(mfaOptionCreator.getScopeId()).thenReturn(KapuaId.ONE);
Mockito.when(mfaOptionCreator.getUserId()).thenReturn(new KapuaEid(BigInteger.ONE));
Mockito.doThrow(new EntityExistsException()).when(entityManager).flush();
MfaOptionDAO.create(entityManager, mfaOptionCreator);
}
@Test(expected = PersistenceException.class)
public void createPersistenceExceptionTest() throws KapuaException {
Mockito.when(mfaOptionCreator.getMfaSecretKey()).thenReturn("mfa secret key");
Mockito.when(mfaOptionCreator.getScopeId()).thenReturn(KapuaId.ONE);
Mockito.when(mfaOptionCreator.getUserId()).thenReturn(new KapuaEid(BigInteger.ONE));
Mockito.doThrow(new PersistenceException()).when(entityManager).flush();
MfaOptionDAO.create(entityManager, mfaOptionCreator);
}
@Test(expected = PersistenceException.class)
public void createPersistenceExceptionWithCauseTest() throws KapuaException {
Mockito.when(mfaOptionCreator.getMfaSecretKey()).thenReturn("mfa secret key");
Mockito.when(mfaOptionCreator.getScopeId()).thenReturn(KapuaId.ONE);
Mockito.when(mfaOptionCreator.getUserId()).thenReturn(new KapuaEid(BigInteger.ONE));
Mockito.doThrow(new PersistenceException(new Exception(new Exception()))).when(entityManager).flush();
MfaOptionDAO.create(entityManager, mfaOptionCreator);
}
@Test(expected = PersistenceException.class)
public void createPersistenceExceptionWithSQLCauseTest() throws KapuaException {
Mockito.when(mfaOptionCreator.getMfaSecretKey()).thenReturn("mfa secret key");
Mockito.when(mfaOptionCreator.getScopeId()).thenReturn(KapuaId.ONE);
Mockito.when(mfaOptionCreator.getUserId()).thenReturn(new KapuaEid(BigInteger.ONE));
Mockito.doThrow(new PersistenceException(new SQLException("reason", "23505", new Exception()))).when(entityManager).flush();
MfaOptionDAO.create(entityManager, mfaOptionCreator);
}
@Test(expected = NullPointerException.class)
public void createNullEntityManagerTest() throws KapuaException {
Mockito.when(mfaOptionCreator.getMfaSecretKey()).thenReturn("mfa secret key");
Mockito.when(mfaOptionCreator.getScopeId()).thenReturn(KapuaId.ONE);
Mockito.when(mfaOptionCreator.getUserId()).thenReturn(new KapuaEid());
MfaOptionDAO.create(null, mfaOptionCreator);
}
@Test(expected = NullPointerException.class)
public void createNullMfaOptionCreatorTest() throws KapuaException {
MfaOptionDAO.create(entityManager, (MfaOptionCreator) null);
}
@Test
public void updateTest() throws KapuaException {
Mockito.when(mfaOption.getId()).thenReturn(KapuaId.ONE);
Mockito.when(entityManager.find(MfaOptionImpl.class, KapuaId.ONE)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getCreatedOn()).thenReturn(createdOn);
Mockito.when(entityToFindOrDelete.getCreatedBy()).thenReturn(createdBy);
assertThat("Instance of MfaOption object expected.", MfaOptionDAO.update(entityManager, mfaOption), IsInstanceOf.instanceOf(MfaOption.class));
assertEquals("Expected and actual values should be the same.", createdBy, MfaOptionDAO.update(entityManager, mfaOption).getCreatedBy());
assertEquals("Expected and actual values should be the same.", createdOn, MfaOptionDAO.update(entityManager, mfaOption).getCreatedOn());
}
@Test(expected = NullPointerException.class)
public void updateNullEntityManagerTest() throws KapuaException {
MfaOptionDAO.update(null, mfaOption);
}
@Test(expected = NullPointerException.class)
public void updateNullMfaOptionTest() throws KapuaException {
Mockito.when(entityManager.find(MfaOptionImpl.class, KapuaId.ONE)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getCreatedOn()).thenReturn(createdOn);
Mockito.when(entityToFindOrDelete.getCreatedBy()).thenReturn(createdBy);
MfaOptionDAO.update(entityManager, null);
}
@Test
public void findSameScopeIdsTest() {
Mockito.when(entityManager.find(MfaOptionImpl.class, mfaOptionId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);
assertThat("Instance of MfaOption object expected.", MfaOptionDAO.find(entityManager, scopeId, mfaOptionId), IsInstanceOf.instanceOf(MfaOption.class));
assertEquals("Expected and actual values should be the same.", KapuaId.ONE, MfaOptionDAO.find(entityManager, scopeId, mfaOptionId).getScopeId());
}
@Test
public void findDifferentScopeIdsTest() {
Mockito.when(entityManager.find(MfaOptionImpl.class, mfaOptionId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ANY);
assertNull("Null expected.", MfaOptionDAO.find(entityManager, scopeId, mfaOptionId));
}
@Test
public void findNullEntityToFindTest() {
Mockito.when(entityManager.find(MfaOptionImpl.class, mfaOptionId)).thenReturn(null);
assertNull("Null expected.", MfaOptionDAO.find(entityManager, scopeId, mfaOptionId));
}
@Test
public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(MfaOptionImpl.class, mfaOptionId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);
assertThat("Instance of MfaOption object expected.", MfaOptionDAO.find(entityManager, scopeId, mfaOptionId), IsInstanceOf.instanceOf(MfaOption.class));
}
@Test(expected = NullPointerException.class)
public void findNullEntityManagerTest() {
MfaOptionDAO.find(null, scopeId, mfaOptionId);
}
@Test
public void findNullScopeIdTest() {
Mockito.when(entityManager.find(MfaOptionImpl.class, mfaOptionId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);
assertThat("Instance of MfaOption object expected.", MfaOptionDAO.find(entityManager, null, mfaOptionId), IsInstanceOf.instanceOf(MfaOption.class));
assertEquals("Expected and actual values should be the same.", KapuaId.ONE, MfaOptionDAO.find(entityManager, null, mfaOptionId).getScopeId());
}
@Test
public void findNullMfaOptionIdTest() {
Mockito.when(entityManager.find(MfaOptionImpl.class, null)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);
assertThat("Instance of MfaOption object expected.", MfaOptionDAO.find(entityManager, scopeId, null), IsInstanceOf.instanceOf(MfaOption.class));
assertEquals("Expected and actual values should be the same.", KapuaId.ONE, MfaOptionDAO.find(entityManager, scopeId, null).getScopeId());
}
@Test
public void queryTest() throws KapuaException {
CriteriaBuilder criteriaBuilder = Mockito.mock(CriteriaBuilder.class);
CriteriaQuery<MfaOptionImpl> criteriaQuery1 = Mockito.mock(CriteriaQuery.class);
CriteriaQuery<MfaOptionImpl> criteriaQuery2 = Mockito.mock(CriteriaQuery.class);
CriteriaQuery<MfaOptionImpl> criteriaQuery3 = Mockito.mock(CriteriaQuery.class);
Root<MfaOptionImpl> entityRoot = Mockito.mock(Root.class);
EntityType<MfaOptionImpl> entityType = Mockito.mock(EntityType.class);
List<String> list = new ArrayList<>();
TypedQuery<MfaOptionImpl> query = Mockito.mock(TypedQuery.class);
Mockito.when(entityManager.getCriteriaBuilder()).thenReturn(criteriaBuilder);
Mockito.when(criteriaBuilder.createQuery(MfaOptionImpl.class)).thenReturn(criteriaQuery1);
Mockito.when(criteriaQuery1.from(MfaOptionImpl.class)).thenReturn(entityRoot);
Mockito.when(entityRoot.getModel()).thenReturn(entityType);
Mockito.when(criteriaQuery1.select(entityRoot)).thenReturn(criteriaQuery2);
Mockito.when(criteriaQuery2.distinct(true)).thenReturn(criteriaQuery3);
Mockito.when(kapuaQuery.getFetchAttributes()).thenReturn(list);
Mockito.when(entityManager.createQuery(criteriaQuery1)).thenReturn(query);
assertThat("Instance of MfaOptionListResult object expected.", MfaOptionDAO.query(entityManager, kapuaQuery), IsInstanceOf.instanceOf(MfaOptionListResult.class));
}
@Test(expected = NullPointerException.class)
public void queryNullEntityManagerTest() throws KapuaException {
MfaOptionDAO.query(null, kapuaQuery);
}
@Test(expected = NullPointerException.class)
public void queryNullKapuaQueryTest() throws KapuaException {
CriteriaBuilder criteriaBuilder = Mockito.mock(CriteriaBuilder.class);
CriteriaQuery<MfaOptionImpl> criteriaQuery1 = Mockito.mock(CriteriaQuery.class);
CriteriaQuery<MfaOptionImpl> criteriaQuery2 = Mockito.mock(CriteriaQuery.class);
CriteriaQuery<MfaOptionImpl> criteriaQuery3 = Mockito.mock(CriteriaQuery.class);
Root<MfaOptionImpl> entityRoot = Mockito.mock(Root.class);
EntityType<MfaOptionImpl> entityType = Mockito.mock(EntityType.class);
TypedQuery<MfaOptionImpl> query = Mockito.mock(TypedQuery.class);
Mockito.when(entityManager.getCriteriaBuilder()).thenReturn(criteriaBuilder);
Mockito.when(criteriaBuilder.createQuery(MfaOptionImpl.class)).thenReturn(criteriaQuery1);
Mockito.when(criteriaQuery1.from(MfaOptionImpl.class)).thenReturn(entityRoot);
Mockito.when(entityRoot.getModel()).thenReturn(entityType);
Mockito.when(criteriaQuery1.select(entityRoot)).thenReturn(criteriaQuery2);
Mockito.when(criteriaQuery2.distinct(true)).thenReturn(criteriaQuery3);
Mockito.when(entityManager.createQuery(criteriaQuery1)).thenReturn(query);
MfaOptionDAO.query(entityManager, null);
}
@Test
public void countTest() throws KapuaException {
CriteriaBuilder criteriaBuilder = Mockito.mock(CriteriaBuilder.class);
CriteriaQuery<Long> criteriaQuery1 = Mockito.mock(CriteriaQuery.class);
CriteriaQuery<Long> criteriaQuery2 = Mockito.mock(CriteriaQuery.class);
Root<MfaOptionImpl> entityRoot = Mockito.mock(Root.class);
TypedQuery<Long> query = Mockito.mock(TypedQuery.class);
Selection<Long> selection = Mockito.mock(Selection.class);
Expression<Long> expression = Mockito.mock(Expression.class);
long[] longNumberList = {0L, 10L, 100L, 1000000L, 9223372036854775807L, -100L, -9223372036854775808L};
Mockito.when(entityManager.getCriteriaBuilder()).thenReturn(criteriaBuilder);
Mockito.when(criteriaBuilder.createQuery(Long.class)).thenReturn(criteriaQuery1);
Mockito.when(criteriaQuery1.from(MfaOptionImpl.class)).thenReturn(entityRoot);
Mockito.when(criteriaBuilder.countDistinct(entityRoot)).thenReturn(expression);
Mockito.when(criteriaQuery1.select(selection)).thenReturn(criteriaQuery2);
Mockito.when(entityManager.createQuery(criteriaQuery1)).thenReturn(query);
for (long number : longNumberList) {
Mockito.doReturn(number).when(query).getSingleResult();
assertThat("Long object expected.", MfaOptionDAO.count(entityManager, kapuaQuery), IsInstanceOf.instanceOf(Long.class));
assertEquals("Expected and actual values should be the same.", number, MfaOptionDAO.count(entityManager, kapuaQuery));
}
}
@Test(expected = NullPointerException.class)
public void countNullEntityManagerTest() throws KapuaException {
MfaOptionDAO.count(null, kapuaQuery);
}
@Test(expected = NullPointerException.class)
public void countNullKapuaQueryTest() throws KapuaException {
MfaOptionDAO.count(entityManager, null);
}
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(MfaOptionImpl.class, mfaOptionId)).thenReturn(entityToFindOrDelete);
assertThat("Instance of MfaOption object expected.", MfaOptionDAO.delete(entityManager, scopeId, mfaOptionId), IsInstanceOf.instanceOf(MfaOption.class));
}
@Test(expected = NullPointerException.class)
public void deleteNullEntityManagerTest() throws KapuaEntityNotFoundException {
MfaOptionDAO.delete(null, scopeId, mfaOptionId);
}
@Test
public void deleteNullScopeIdTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(MfaOptionImpl.class, mfaOptionId)).thenReturn(entityToFindOrDelete);
assertThat("Instance of MfaOption object expected.", MfaOptionDAO.delete(entityManager, null, mfaOptionId), IsInstanceOf.instanceOf(MfaOption.class));
}
@Test(expected = NullPointerException.class)
public void deleteNullMfaOptionIdTest() throws KapuaEntityNotFoundException {
MfaOptionDAO.delete(entityManager, scopeId, null);
}
@Test(expected = KapuaEntityNotFoundException.class)
public void deleteNullEntityToDeleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(MfaOptionImpl.class, mfaOptionId)).thenReturn(null);
MfaOptionDAO.delete(entityManager, scopeId, mfaOptionId);
}
}