forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathQueryManagerImplTest.java
More file actions
397 lines (342 loc) · 17.8 KB
/
QueryManagerImplTest.java
File metadata and controls
397 lines (342 loc) · 17.8 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.api.query;
import com.cloud.api.query.dao.TemplateJoinDao;
import com.cloud.api.query.vo.EventJoinVO;
import com.cloud.api.query.vo.TemplateJoinVO;
import com.cloud.event.dao.EventJoinDao;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.network.Network;
import com.cloud.network.VNF;
import com.cloud.network.dao.NetworkVO;
import com.cloud.server.ResourceTag;
import com.cloud.storage.BucketVO;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.dao.BucketDao;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.user.Account;
import com.cloud.user.AccountManager;
import com.cloud.user.AccountVO;
import com.cloud.user.User;
import com.cloud.user.UserVO;
import com.cloud.utils.Pair;
import com.cloud.utils.db.EntityManager;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.vm.VirtualMachine;
import org.apache.cloudstack.acl.SecurityChecker;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.api.command.admin.storage.ListObjectStoragePoolsCmd;
import org.apache.cloudstack.api.command.user.bucket.ListBucketsCmd;
import org.apache.cloudstack.api.command.user.event.ListEventsCmd;
import org.apache.cloudstack.api.command.user.resource.ListDetailOptionsCmd;
import org.apache.cloudstack.api.response.DetailOptionsResponse;
import org.apache.cloudstack.api.response.EventResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.ObjectStoreResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.storage.datastore.db.ObjectStoreDao;
import org.apache.cloudstack.storage.datastore.db.ObjectStoreVO;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class QueryManagerImplTest {
public static final long USER_ID = 1;
public static final long ACCOUNT_ID = 1;
@Spy
@InjectMocks
private QueryManagerImpl queryManagerImplSpy = new QueryManagerImpl();
@Mock
EntityManager entityManager;
@Mock
AccountManager accountManager;
@Mock
EventJoinDao eventJoinDao;
@Mock
Account accountMock;
@Mock
TemplateJoinDao templateJoinDaoMock;
@Mock
SearchCriteria searchCriteriaMock;
@Mock
ObjectStoreDao objectStoreDao;
@Mock
BucketDao bucketDao;
@Mock
VMTemplateDao templateDao;
private AccountVO account;
private UserVO user;
@Before
public void setUp() throws Exception {
setupCommonMocks();
}
@InjectMocks
private QueryManagerImpl queryManager = new QueryManagerImpl();
private void setupCommonMocks() {
account = new AccountVO("testaccount", 1L, "networkdomain", Account.Type.NORMAL, "uuid");
account.setId(ACCOUNT_ID);
user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone",
UUID.randomUUID().toString(), User.Source.UNKNOWN);
CallContext.register(user, account);
Mockito.when(accountManager.isRootAdmin(account.getId())).thenReturn(false);
final SearchBuilder<EventJoinVO> searchBuilder = Mockito.mock(SearchBuilder.class);
final SearchCriteria<EventJoinVO> searchCriteria = Mockito.mock(SearchCriteria.class);
final EventJoinVO eventJoinVO = Mockito.mock(EventJoinVO.class);
when(searchBuilder.entity()).thenReturn(eventJoinVO);
when(searchBuilder.create()).thenReturn(searchCriteria);
Mockito.when(eventJoinDao.createSearchBuilder()).thenReturn(searchBuilder);
}
private ListEventsCmd setupMockListEventsCmd() {
ListEventsCmd cmd = Mockito.mock(ListEventsCmd.class);
Mockito.when(cmd.getEntryTime()).thenReturn(null);
Mockito.when(cmd.listAll()).thenReturn(true);
return cmd;
}
@Test
public void searchForEventsSuccess() {
ListEventsCmd cmd = setupMockListEventsCmd();
String uuid = UUID.randomUUID().toString();
Mockito.when(cmd.getResourceId()).thenReturn(uuid);
Mockito.when(cmd.getResourceType()).thenReturn(ApiCommandResourceType.Network.toString());
List<EventJoinVO> events = new ArrayList<>();
events.add(Mockito.mock(EventJoinVO.class));
events.add(Mockito.mock(EventJoinVO.class));
events.add(Mockito.mock(EventJoinVO.class));
Pair<List<EventJoinVO>, Integer> pair = new Pair<>(events, events.size());
NetworkVO network = Mockito.mock(NetworkVO.class);
Mockito.when(network.getId()).thenReturn(1L);
Mockito.when(network.getAccountId()).thenReturn(account.getId());
Mockito.when(entityManager.findByUuidIncludingRemoved(Network.class, uuid)).thenReturn(network);
Mockito.doNothing().when(accountManager).checkAccess(account, SecurityChecker.AccessType.ListEntry, true, network);
Mockito.when(eventJoinDao.searchAndCount(Mockito.any(), Mockito.any(Filter.class))).thenReturn(pair);
List<EventResponse> respList = new ArrayList<EventResponse>();
for (EventJoinVO vt : events) {
respList.add(eventJoinDao.newEventResponse(vt));
}
try (MockedStatic<ViewResponseHelper> ignored = Mockito.mockStatic(ViewResponseHelper.class)) {
Mockito.when(ViewResponseHelper.createEventResponse(Mockito.any())).thenReturn(respList);
ListResponse<EventResponse> result = queryManager.searchForEvents(cmd);
Assert.assertEquals((int) result.getCount(), events.size());
}
}
@Test(expected = InvalidParameterValueException.class)
public void searchForEventsFailResourceTypeNull() {
ListEventsCmd cmd = setupMockListEventsCmd();
String uuid = UUID.randomUUID().toString();
Mockito.when(cmd.getResourceId()).thenReturn(uuid);
Mockito.when(cmd.getResourceType()).thenReturn(null);
queryManager.searchForEvents(cmd);
}
@Test(expected = InvalidParameterValueException.class)
public void searchForEventsFailResourceTypeInvalid() {
ListEventsCmd cmd = setupMockListEventsCmd();
Mockito.when(cmd.getResourceType()).thenReturn("Some");
queryManager.searchForEvents(cmd);
}
@Test(expected = InvalidParameterValueException.class)
public void searchForEventsFailResourceIdInvalid() {
ListEventsCmd cmd = setupMockListEventsCmd();
Mockito.when(cmd.getResourceId()).thenReturn("random");
Mockito.when(cmd.getResourceType()).thenReturn(ApiCommandResourceType.VirtualMachine.toString());
queryManager.searchForEvents(cmd);
}
@Test(expected = InvalidParameterValueException.class)
public void searchForEventsFailResourceNotFound() {
ListEventsCmd cmd = setupMockListEventsCmd();
String uuid = UUID.randomUUID().toString();
Mockito.when(cmd.getResourceId()).thenReturn(uuid);
Mockito.when(cmd.getResourceType()).thenReturn(ApiCommandResourceType.VirtualMachine.toString());
Mockito.when(entityManager.findByUuidIncludingRemoved(VirtualMachine.class, uuid)).thenReturn(null);
queryManager.searchForEvents(cmd);
}
@Test(expected = PermissionDeniedException.class)
public void searchForEventsFailPermissionDenied() {
ListEventsCmd cmd = setupMockListEventsCmd();
String uuid = UUID.randomUUID().toString();
Mockito.when(cmd.getResourceId()).thenReturn(uuid);
Mockito.when(cmd.getResourceType()).thenReturn(ApiCommandResourceType.Network.toString());
NetworkVO network = Mockito.mock(NetworkVO.class);
Mockito.when(network.getId()).thenReturn(1L);
Mockito.when(network.getAccountId()).thenReturn(2L);
Mockito.when(entityManager.findByUuidIncludingRemoved(Network.class, uuid)).thenReturn(network);
Mockito.doThrow(new PermissionDeniedException("Denied")).when(accountManager).checkAccess(account, SecurityChecker.AccessType.ListEntry, false, network);
queryManager.searchForEvents(cmd);
}
@Test
public void listVnfDetailOptionsCmd() {
ListDetailOptionsCmd cmd = Mockito.mock(ListDetailOptionsCmd.class);
when(cmd.getResourceType()).thenReturn(ResourceTag.ResourceObjectType.VnfTemplate);
DetailOptionsResponse response = queryManager.listDetailOptions(cmd);
Map<String, List<String>> options = response.getDetails();
int expectedLength = VNF.AccessDetail.values().length + VNF.VnfDetail.values().length;
Assert.assertEquals(expectedLength, options.size());
Set<String> keys = options.keySet();
for (VNF.AccessDetail detail : VNF.AccessDetail.values()) {
Assert.assertTrue(keys.contains(detail.name().toLowerCase()));
}
for (VNF.VnfDetail detail : VNF.VnfDetail.values()) {
Assert.assertTrue(keys.contains(detail.name().toLowerCase()));
}
List<String> expectedAccessMethods = Arrays.stream(VNF.AccessMethod.values()).map(method -> method.toString()).sorted().collect(Collectors.toList());
Assert.assertEquals(expectedAccessMethods, options.get(VNF.AccessDetail.ACCESS_METHODS.name().toLowerCase()));
}
@Test
public void applyPublicTemplateRestrictionsTestDoesNotApplyRestrictionsWhenCallerIsRootAdmin() {
Mockito.when(accountMock.getType()).thenReturn(Account.Type.ADMIN);
queryManagerImplSpy.applyPublicTemplateSharingRestrictions(searchCriteriaMock, accountMock);
Mockito.verify(searchCriteriaMock, Mockito.never()).addAnd(Mockito.anyString(), Mockito.any(), Mockito.any());
}
@Test
public void applyPublicTemplateRestrictionsTestAppliesRestrictionsWhenCallerIsNotRootAdmin() {
long callerDomainId = 1L;
long sharableDomainId = 2L;
long unsharableDomainId = 3L;
Mockito.when(accountMock.getType()).thenReturn(Account.Type.NORMAL);
Mockito.when(accountMock.getDomainId()).thenReturn(callerDomainId);
TemplateJoinVO templateMock1 = Mockito.mock(TemplateJoinVO.class);
Mockito.when(templateMock1.getDomainId()).thenReturn(callerDomainId);
Mockito.lenient().doReturn(false).when(queryManagerImplSpy).checkIfDomainSharesTemplates(callerDomainId);
TemplateJoinVO templateMock2 = Mockito.mock(TemplateJoinVO.class);
Mockito.when(templateMock2.getDomainId()).thenReturn(sharableDomainId);
Mockito.doReturn(true).when(queryManagerImplSpy).checkIfDomainSharesTemplates(sharableDomainId);
TemplateJoinVO templateMock3 = Mockito.mock(TemplateJoinVO.class);
Mockito.when(templateMock3.getDomainId()).thenReturn(unsharableDomainId);
Mockito.doReturn(false).when(queryManagerImplSpy).checkIfDomainSharesTemplates(unsharableDomainId);
List<TemplateJoinVO> publicTemplates = List.of(templateMock1, templateMock2, templateMock3);
Mockito.when(templateJoinDaoMock.listPublicTemplates()).thenReturn(publicTemplates);
queryManagerImplSpy.applyPublicTemplateSharingRestrictions(searchCriteriaMock, accountMock);
Mockito.verify(searchCriteriaMock).addAnd("domainId", SearchCriteria.Op.NOTIN, unsharableDomainId);
}
@Test
public void addDomainIdToSetIfDomainDoesNotShareTemplatesTestDoesNotAddWhenCallerBelongsToDomain() {
long domainId = 1L;
Set<Long> set = new HashSet<>();
Mockito.when(accountMock.getDomainId()).thenReturn(domainId);
queryManagerImplSpy.addDomainIdToSetIfDomainDoesNotShareTemplates(domainId, accountMock, set);
Assert.assertEquals(0, set.size());
}
@Test
public void addDomainIdToSetIfDomainDoesNotShareTemplatesTestAddsWhenDomainDoesNotShareTemplates() {
long domainId = 1L;
Set<Long> set = new HashSet<>();
Mockito.when(accountMock.getDomainId()).thenReturn(2L);
Mockito.doReturn(false).when(queryManagerImplSpy).checkIfDomainSharesTemplates(domainId);
queryManagerImplSpy.addDomainIdToSetIfDomainDoesNotShareTemplates(domainId, accountMock, set);
Assert.assertTrue(set.contains(domainId));
}
@Test
public void testSearchForObjectStores() {
ListObjectStoragePoolsCmd cmd = new ListObjectStoragePoolsCmd();
List<ObjectStoreVO> objectStores = new ArrayList<>();
ObjectStoreVO os1 = new ObjectStoreVO();
os1.setName("MinIOStore");
ObjectStoreVO os2 = new ObjectStoreVO();
os1.setName("Simulator");
objectStores.add(os1);
objectStores.add(os2);
SearchBuilder<ObjectStoreVO> sb = Mockito.mock(SearchBuilder.class);
ObjectStoreVO objectStoreVO = Mockito.mock(ObjectStoreVO.class);
when(sb.entity()).thenReturn(objectStoreVO);
when(objectStoreDao.createSearchBuilder()).thenReturn(sb);
when(objectStoreDao.searchAndCount(any(), any())).thenReturn(new Pair<>(objectStores, 2));
ListResponse<ObjectStoreResponse> result = queryManagerImplSpy.searchForObjectStores(cmd);
assertEquals(2, result.getCount().intValue());
}
@Test
public void testSearchForBuckets() {
ListBucketsCmd listBucketsCmd = new ListBucketsCmd();
List<BucketVO> buckets = new ArrayList<>();
BucketVO b1 = new BucketVO();
b1.setName("test-bucket-1");
BucketVO b2 = new BucketVO();
b2.setName("test-bucket-1");
buckets.add(b1);
buckets.add(b2);
SearchBuilder<BucketVO> sb = Mockito.mock(SearchBuilder.class);
BucketVO bucketVO = Mockito.mock(BucketVO.class);
when(sb.entity()).thenReturn(bucketVO);
when(bucketDao.createSearchBuilder()).thenReturn(sb);
when(bucketDao.searchAndCount(any(), any())).thenReturn(new Pair<>(buckets, 2));
queryManagerImplSpy.searchForBuckets(listBucketsCmd);
}
@Test
public void testGetHostTagsFromTemplateForServiceOfferingsListingNoTemplateId() {
Assert.assertTrue(CollectionUtils.isEmpty(queryManager.getHostTagsFromTemplateForServiceOfferingsListing(Mockito.mock(AccountVO.class), null)));
}
@Test(expected = InvalidParameterValueException.class)
public void testGetHostTagsFromTemplateForServiceOfferingsListingException() {
queryManager.getHostTagsFromTemplateForServiceOfferingsListing(Mockito.mock(AccountVO.class), 1L);
}
@Test(expected = PermissionDeniedException.class)
public void testGetHostTagsForServiceOfferingsListingNoAccess() {
long templateId = 1L;
Account account = Mockito.mock(Account.class);
Mockito.when(account.getType()).thenReturn(Account.Type.NORMAL);
VMTemplateVO template = Mockito.mock(VMTemplateVO.class);
Mockito.when(templateDao.findByIdIncludingRemoved(templateId)).thenReturn(template);
Mockito.lenient().doThrow(PermissionDeniedException.class).when(accountManager).checkAccess(account, null, false, template);
queryManager.getHostTagsFromTemplateForServiceOfferingsListing(account, templateId);
}
@Test
public void testGetHostTagsFromTemplateForServiceOfferingsListingAdmin() {
long templateId = 1L;
Account account = Mockito.mock(Account.class);
Mockito.when(account.getType()).thenReturn(Account.Type.ADMIN);
VMTemplateVO template = Mockito.mock(VMTemplateVO.class);
Mockito.when(template.getTemplateTag()).thenReturn("tag");
Mockito.when(templateDao.findByIdIncludingRemoved(templateId)).thenReturn(template);
Mockito.lenient().doThrow(PermissionDeniedException.class).when(accountManager).checkAccess(account, null, false, template);
List<String> result = queryManager.getHostTagsFromTemplateForServiceOfferingsListing(account, templateId);
Assert.assertTrue(CollectionUtils.isNotEmpty(result));
}
@Test
public void testGetHostTagsForServiceOfferingsListingSuccess() {
long templateId = 1L;
Account account = Mockito.mock(Account.class);
Mockito.when(account.getType()).thenReturn(Account.Type.NORMAL);
VMTemplateVO template = Mockito.mock(VMTemplateVO.class);
Mockito.when(templateDao.findByIdIncludingRemoved(templateId)).thenReturn(template);
Mockito.lenient().doNothing().when(accountManager).checkAccess(account, null, false, template);
List<String> result = queryManager.getHostTagsFromTemplateForServiceOfferingsListing(account, templateId);
Assert.assertTrue(CollectionUtils.isEmpty(result));
Mockito.when(template.getTemplateTag()).thenReturn("tag");
result = queryManager.getHostTagsFromTemplateForServiceOfferingsListing(account, templateId);
Assert.assertTrue(CollectionUtils.isNotEmpty(result));
}
}