Skip to content

Commit 2a1be7c

Browse files
committed
Added UT for BucketApiServiceImpl
1 parent 5783cd2 commit 2a1be7c

File tree

2 files changed

+186
-1
lines changed

2 files changed

+186
-1
lines changed

server/src/test/java/org/apache/cloudstack/backup/BackupManagerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ public void testCreateScheduledBackup() throws ResourceAllocationException {
436436
when(offering.getProvider()).thenReturn("test");
437437

438438
Account account = Mockito.mock(Account.class);
439-
when(account.getId()).thenReturn(accountId);
440439
when(accountManager.getAccount(accountId)).thenReturn(account);
441440

442441
BackupScheduleVO schedule = mock(BackupScheduleVO.class);
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. 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,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.storage.object;
18+
19+
import org.apache.cloudstack.api.command.user.bucket.CreateBucketCmd;
20+
import org.apache.cloudstack.api.command.user.bucket.UpdateBucketCmd;
21+
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
22+
import org.apache.cloudstack.storage.datastore.db.ObjectStoreDao;
23+
import org.apache.cloudstack.storage.datastore.db.ObjectStoreVO;
24+
import org.junit.Assert;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.mockito.InjectMocks;
28+
import org.mockito.Mock;
29+
import org.mockito.Mockito;
30+
import org.mockito.Spy;
31+
import org.mockito.junit.MockitoJUnitRunner;
32+
import org.springframework.test.util.ReflectionTestUtils;
33+
34+
import com.cloud.agent.api.to.BucketTO;
35+
import com.cloud.configuration.Resource;
36+
import com.cloud.exception.ResourceAllocationException;
37+
import com.cloud.resourcelimit.ResourceLimitManagerImpl;
38+
import com.cloud.storage.BucketVO;
39+
import com.cloud.storage.DataStoreRole;
40+
import com.cloud.storage.dao.BucketDao;
41+
import com.cloud.user.Account;
42+
import com.cloud.user.AccountManager;
43+
import com.cloud.user.ResourceLimitService;
44+
45+
@RunWith(MockitoJUnitRunner.class)
46+
public class BucketApiServiceImplTest {
47+
@Spy
48+
@InjectMocks
49+
BucketApiServiceImpl bucketApiService;
50+
51+
@Mock
52+
AccountManager accountManager;
53+
54+
@Mock
55+
ObjectStoreDao objectStoreDao;
56+
57+
@Mock
58+
DataStoreManager dataStoreMgr;
59+
60+
@Mock
61+
private ResourceLimitManagerImpl resourceLimitManager;
62+
63+
@Mock
64+
private BucketDao bucketDao;
65+
66+
@Test
67+
public void testAllocBucket() throws ResourceAllocationException {
68+
String bucketName = "bucket1";
69+
Long accountId = 1L;
70+
Long poolId = 2L;
71+
Long objectStoreId = 3L;
72+
73+
CreateBucketCmd cmd = Mockito.mock(CreateBucketCmd.class);
74+
Mockito.when(cmd.getBucketName()).thenReturn(bucketName);
75+
Mockito.when(cmd.getEntityOwnerId()).thenReturn(accountId);
76+
Mockito.when(cmd.getObjectStoragePoolId()).thenReturn(poolId);
77+
Mockito.when(cmd.getQuota()).thenReturn(1);
78+
79+
Account account = Mockito.mock(Account.class);
80+
Mockito.when(accountManager.getActiveAccountById(accountId)).thenReturn(account);
81+
82+
ObjectStoreVO objectStoreVO = Mockito.mock(ObjectStoreVO.class);
83+
Mockito.when(objectStoreVO.getId()).thenReturn(objectStoreId);
84+
Mockito.when(objectStoreDao.findById(poolId)).thenReturn(objectStoreVO);
85+
ObjectStoreEntity objectStore = Mockito.mock(ObjectStoreEntity.class);
86+
Mockito.when(objectStore.getId()).thenReturn(objectStoreId);
87+
Mockito.when(dataStoreMgr.getDataStore(objectStoreId, DataStoreRole.Object)).thenReturn(objectStore);
88+
Mockito.when(objectStore.createUser(accountId)).thenReturn(true);
89+
90+
bucketApiService.allocBucket(cmd);
91+
92+
Mockito.verify(resourceLimitManager, Mockito.times(1)).checkResourceLimit(account, Resource.ResourceType.bucket);
93+
Mockito.verify(resourceLimitManager, Mockito.times(1)).checkResourceLimit(account, Resource.ResourceType.object_storage, 1 * Resource.ResourceType.bytesToGiB);
94+
}
95+
96+
@Test
97+
public void testCreateBucket() {
98+
Long objectStoreId = 1L;
99+
Long poolId = 2L;
100+
Long bucketId = 3L;
101+
Long accountId = 4L;
102+
String bucketName = "bucket1";
103+
104+
CreateBucketCmd cmd = Mockito.mock(CreateBucketCmd.class);
105+
Mockito.when(cmd.getObjectStoragePoolId()).thenReturn(poolId);
106+
Mockito.when(cmd.getEntityId()).thenReturn(bucketId);
107+
Mockito.when(cmd.getQuota()).thenReturn(1);
108+
109+
BucketVO bucket = new BucketVO(bucketName);
110+
Mockito.when(bucketDao.findById(bucketId)).thenReturn(bucket);
111+
ReflectionTestUtils.setField(bucket, "accountId", accountId);
112+
113+
ObjectStoreVO objectStoreVO = Mockito.mock(ObjectStoreVO.class);
114+
Mockito.when(objectStoreVO.getId()).thenReturn(objectStoreId);
115+
Mockito.when(objectStoreDao.findById(poolId)).thenReturn(objectStoreVO);
116+
ObjectStoreEntity objectStore = Mockito.mock(ObjectStoreEntity.class);
117+
Mockito.when(dataStoreMgr.getDataStore(objectStoreId, DataStoreRole.Object)).thenReturn(objectStore);
118+
Mockito.when(objectStore.createBucket(bucket, false)).thenReturn(bucket);
119+
120+
bucketApiService.createBucket(cmd);
121+
122+
Mockito.verify(resourceLimitManager, Mockito.times(1)).incrementResourceCount(accountId, Resource.ResourceType.bucket);
123+
Mockito.verify(resourceLimitManager, Mockito.times(1)).incrementResourceCount(accountId, Resource.ResourceType.object_storage, 1 * Resource.ResourceType.bytesToGiB);
124+
Assert.assertEquals(bucket.getState(), Bucket.State.Created);
125+
}
126+
127+
@Test
128+
public void testDeleteBucket() {
129+
Long bucketId = 1L;
130+
Long accountId = 2L;
131+
Long objectStoreId = 3L;
132+
String bucketName = "bucket1";
133+
134+
BucketVO bucket = new BucketVO(bucketName);
135+
Mockito.when(bucketDao.findById(bucketId)).thenReturn(bucket);
136+
ReflectionTestUtils.setField(bucket, "objectStoreId", objectStoreId);
137+
ReflectionTestUtils.setField(bucket, "quota", 1);
138+
ReflectionTestUtils.setField(bucket, "accountId", accountId);
139+
140+
ObjectStoreVO objectStoreVO = Mockito.mock(ObjectStoreVO.class);
141+
Mockito.when(objectStoreVO.getId()).thenReturn(objectStoreId);
142+
Mockito.when(objectStoreDao.findById(objectStoreId)).thenReturn(objectStoreVO);
143+
ObjectStoreEntity objectStore = Mockito.mock(ObjectStoreEntity.class);
144+
Mockito.when(dataStoreMgr.getDataStore(objectStoreId, DataStoreRole.Object)).thenReturn(objectStore);
145+
Mockito.when(objectStore.deleteBucket(Mockito.any(BucketTO.class))).thenReturn(true);
146+
147+
bucketApiService.deleteBucket(bucketId, null);
148+
149+
Mockito.verify(resourceLimitManager, Mockito.times(1)).decrementResourceCount(accountId, Resource.ResourceType.bucket);
150+
Mockito.verify(resourceLimitManager, Mockito.times(1)).decrementResourceCount(accountId, Resource.ResourceType.object_storage, 1 * Resource.ResourceType.bytesToGiB);
151+
}
152+
153+
@Test
154+
public void testUpdateBucket() throws ResourceAllocationException {
155+
Long bucketId = 1L;
156+
Long objectStoreId = 2L;
157+
Long accountId = 3L;
158+
Integer bucketQuota = 2;
159+
Integer cmdQuota = 1;
160+
String bucketName = "bucket1";
161+
162+
UpdateBucketCmd cmd = Mockito.mock(UpdateBucketCmd.class);
163+
Mockito.when(cmd.getId()).thenReturn(bucketId);
164+
Mockito.when(cmd.getQuota()).thenReturn(cmdQuota);
165+
166+
BucketVO bucket = new BucketVO(bucketName);
167+
ReflectionTestUtils.setField(bucket, "quota", bucketQuota);
168+
ReflectionTestUtils.setField(bucket, "accountId", accountId);
169+
ReflectionTestUtils.setField(bucket, "objectStoreId", objectStoreId);
170+
Mockito.when(bucketDao.findById(bucketId)).thenReturn(bucket);
171+
172+
Account account = Mockito.mock(Account.class);
173+
Mockito.when(accountManager.getActiveAccountById(accountId)).thenReturn(account);
174+
175+
ObjectStoreVO objectStoreVO = Mockito.mock(ObjectStoreVO.class);
176+
Mockito.when(objectStoreVO.getId()).thenReturn(objectStoreId);
177+
Mockito.when(objectStoreDao.findById(objectStoreId)).thenReturn(objectStoreVO);
178+
ObjectStoreEntity objectStore = Mockito.mock(ObjectStoreEntity.class);
179+
Mockito.when(dataStoreMgr.getDataStore(objectStoreId, DataStoreRole.Object)).thenReturn(objectStore);
180+
Mockito.when(objectStore.deleteBucket(Mockito.any(BucketTO.class))).thenReturn(true);
181+
182+
bucketApiService.updateBucket(cmd, null);
183+
184+
Mockito.verify(resourceLimitManager, Mockito.times(1)).decrementResourceCount(accountId, Resource.ResourceType.object_storage, (bucketQuota - cmdQuota) * Resource.ResourceType.bytesToGiB);
185+
}
186+
}

0 commit comments

Comments
 (0)