Skip to content

Commit c27abcf

Browse files
committed
test fix
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent b4dd9dc commit c27abcf

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/manager/ExtensionsManagerImplTest.java

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static org.mockito.Mockito.doThrow;
3434
import static org.mockito.Mockito.eq;
3535
import static org.mockito.Mockito.mock;
36+
import static org.mockito.Mockito.mockStatic;
3637
import static org.mockito.Mockito.never;
3738
import static org.mockito.Mockito.verify;
3839
import static org.mockito.Mockito.when;
@@ -48,9 +49,13 @@
4849
import java.util.Map;
4950
import java.util.UUID;
5051

52+
import org.apache.cloudstack.acl.Role;
53+
import org.apache.cloudstack.acl.RoleService;
54+
import org.apache.cloudstack.acl.RoleType;
5155
import org.apache.cloudstack.api.ApiConstants;
5256
import org.apache.cloudstack.api.response.ExtensionCustomActionResponse;
5357
import org.apache.cloudstack.api.response.ExtensionResponse;
58+
import org.apache.cloudstack.context.CallContext;
5459
import org.apache.cloudstack.extension.CustomActionResultResponse;
5560
import org.apache.cloudstack.extension.Extension;
5661
import org.apache.cloudstack.extension.ExtensionCustomAction;
@@ -113,6 +118,7 @@
113118
import com.cloud.org.Cluster;
114119
import com.cloud.serializer.GsonHelper;
115120
import com.cloud.storage.dao.VMTemplateDao;
121+
import com.cloud.user.Account;
116122
import com.cloud.utils.Pair;
117123
import com.cloud.utils.db.EntityManager;
118124
import com.cloud.utils.db.SearchBuilder;
@@ -166,6 +172,8 @@ public class ExtensionsManagerImplTest {
166172
private AlertManager alertManager;
167173
@Mock
168174
private VMTemplateDao templateDao;
175+
@Mock
176+
private RoleService roleService;
169177

170178
@Before
171179
public void setUp() {
@@ -685,7 +693,7 @@ public void prepareExtensionPathAcrossServersReturnsTrueWhenAllServersSucceed()
685693

686694
when(managementServerHostDao.listBy(any())).thenReturn(Arrays.asList(msHost1, msHost2));
687695

688-
try (MockedStatic<ManagementServerNode> managementServerNodeMockedStatic = Mockito.mockStatic(ManagementServerNode.class)) {
696+
try (MockedStatic<ManagementServerNode> managementServerNodeMockedStatic = mockStatic(ManagementServerNode.class)) {
689697
managementServerNodeMockedStatic.when(ManagementServerNode::getManagementServerId).thenReturn(101L);
690698
doReturn(new Pair<>(true, "ok")).when(extensionsManager).prepareExtensionPathOnCurrentServer(anyString(), anyBoolean(), anyString());
691699
doReturn(true).when(extensionsManager).prepareExtensionPathOnMSPeer(eq(ext), eq(msHost2));
@@ -720,7 +728,7 @@ public void prepareExtensionPathAcrossServersReturnsFalseWhenAnyServerFails() {
720728

721729
when(managementServerHostDao.listBy(any())).thenReturn(Arrays.asList(msHost1, msHost2));
722730

723-
try (MockedStatic<ManagementServerNode> managementServerNodeMockedStatic = Mockito.mockStatic(ManagementServerNode.class)) {
731+
try (MockedStatic<ManagementServerNode> managementServerNodeMockedStatic = mockStatic(ManagementServerNode.class)) {
724732
managementServerNodeMockedStatic.when(ManagementServerNode::getManagementServerId).thenReturn(101L);
725733
doReturn(new Pair<>(true, "ok")).when(extensionsManager).prepareExtensionPathOnCurrentServer(anyString(), anyBoolean(), anyString());
726734
doReturn(false).when(extensionsManager).prepareExtensionPathOnMSPeer(eq(ext), eq(msHost2));
@@ -748,7 +756,7 @@ public void prepareExtensionPathAcrossServersDoesNotUpdateIfStateUnchanged() {
748756

749757
when(managementServerHostDao.listBy(any())).thenReturn(Collections.singletonList(msHost));
750758

751-
try (MockedStatic<ManagementServerNode> managementServerNodeMockedStatic = Mockito.mockStatic(ManagementServerNode.class)) {
759+
try (MockedStatic<ManagementServerNode> managementServerNodeMockedStatic = mockStatic(ManagementServerNode.class)) {
752760
managementServerNodeMockedStatic.when(ManagementServerNode::getManagementServerId).thenReturn(101L);
753761
doReturn(new Pair<>(true, "ok")).when(extensionsManager).prepareExtensionPathOnCurrentServer(anyString(), anyBoolean(), anyString());
754762

@@ -1272,6 +1280,17 @@ public void deleteCustomAction_RemoveFails() {
12721280
verify(extensionCustomActionDao).remove(actionId);
12731281
}
12741282

1283+
private void mockCallerRole(RoleType roleType) {
1284+
CallContext callContextMock = Mockito.mock(CallContext.class);
1285+
when(CallContext.current()).thenReturn(callContextMock);
1286+
Account accountMock = mock(Account.class);
1287+
when(accountMock.getRoleId()).thenReturn(1L);
1288+
Role role = mock(Role.class);
1289+
when(role.getRoleType()).thenReturn(roleType);
1290+
when(roleService.findRole(1L)).thenReturn(role);
1291+
when(callContextMock.getCallingAccount()).thenReturn(accountMock);
1292+
}
1293+
12751294
@Test
12761295
public void testListCustomActions_ReturnsResponses() {
12771296
ListCustomActionCmd cmd = mock(ListCustomActionCmd.class);
@@ -1299,11 +1318,15 @@ public void testListCustomActions_ReturnsResponses() {
12991318
doReturn(resp1).when(extensionsManager).createCustomActionResponse(eq(action1));
13001319
doReturn(resp2).when(extensionsManager).createCustomActionResponse(eq(action2));
13011320

1302-
List<ExtensionCustomActionResponse> result = extensionsManager.listCustomActions(cmd);
13031321

1304-
assertEquals(2, result.size());
1305-
assertTrue(result.contains(resp1));
1306-
assertTrue(result.contains(resp2));
1322+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
1323+
mockCallerRole(RoleType.Admin);
1324+
List<ExtensionCustomActionResponse> result = extensionsManager.listCustomActions(cmd);
1325+
1326+
assertEquals(2, result.size());
1327+
assertTrue(result.contains(resp1));
1328+
assertTrue(result.contains(resp2));
1329+
}
13071330
}
13081331

13091332
@Test
@@ -1479,6 +1502,8 @@ public void runCustomAction_SuccessfulExecution_ReturnsExpectedResult() throws E
14791502
when(extensionCustomActionDao.findById(1L)).thenReturn(actionVO);
14801503
when(actionVO.isEnabled()).thenReturn(true);
14811504
when(actionVO.getResourceType()).thenReturn(ExtensionCustomAction.ResourceType.VirtualMachine);
1505+
when(actionVO.getAllowedRoleTypes()).thenReturn(
1506+
RoleType.toCombinedMask(List.of(RoleType.Admin, RoleType.DomainAdmin, RoleType.User)));
14821507

14831508
ExtensionVO extensionVO = mock(ExtensionVO.class);
14841509
when(extensionDao.findById(anyLong())).thenReturn(extensionVO);
@@ -1497,9 +1522,12 @@ public void runCustomAction_SuccessfulExecution_ReturnsExpectedResult() throws E
14971522

14981523
when(agentMgr.send(anyLong(), any(Command.class))).thenReturn(answer);
14991524

1500-
CustomActionResultResponse result = extensionsManager.runCustomAction(cmd);
1525+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
1526+
mockCallerRole(RoleType.User);
1527+
CustomActionResultResponse result = extensionsManager.runCustomAction(cmd);
15011528

1502-
assertTrue(result.getSuccess());
1529+
assertTrue(result.getSuccess());
1530+
}
15031531
}
15041532

15051533
@Test(expected = InvalidParameterValueException.class)
@@ -1508,7 +1536,27 @@ public void runCustomAction_ActionNotFound_ThrowsException() {
15081536
when(cmd.getCustomActionId()).thenReturn(99L);
15091537
when(extensionCustomActionDao.findById(99L)).thenReturn(null);
15101538

1511-
extensionsManager.runCustomAction(cmd);
1539+
1540+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
1541+
mockCallerRole(RoleType.Admin);
1542+
extensionsManager.runCustomAction(cmd);
1543+
}
1544+
}
1545+
1546+
@Test(expected = CloudRuntimeException.class)
1547+
public void runCustomAction_ActionNotAllowedForRole_ThrowsException() {
1548+
RunCustomActionCmd cmd = mock(RunCustomActionCmd.class);
1549+
when(cmd.getCustomActionId()).thenReturn(2L);
1550+
1551+
ExtensionCustomActionVO actionVO = mock(ExtensionCustomActionVO.class);
1552+
when(extensionCustomActionDao.findById(2L)).thenReturn(actionVO);
1553+
when(actionVO.getAllowedRoleTypes()).thenReturn(
1554+
RoleType.toCombinedMask(List.of(RoleType.Admin, RoleType.DomainAdmin)));
1555+
1556+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
1557+
mockCallerRole(RoleType.User);
1558+
extensionsManager.runCustomAction(cmd);
1559+
}
15121560
}
15131561

15141562
@Test(expected = CloudRuntimeException.class)
@@ -1520,7 +1568,10 @@ public void runCustomAction_ActionDisabled_ThrowsException() {
15201568
when(extensionCustomActionDao.findById(2L)).thenReturn(actionVO);
15211569
when(actionVO.isEnabled()).thenReturn(false);
15221570

1523-
extensionsManager.runCustomAction(cmd);
1571+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
1572+
mockCallerRole(RoleType.Admin);
1573+
extensionsManager.runCustomAction(cmd);
1574+
}
15241575
}
15251576

15261577
@Test(expected = InvalidParameterValueException.class)
@@ -1537,7 +1588,11 @@ public void runCustomAction_InvalidResourceType_ThrowsException() {
15371588
when(extensionVO.getState()).thenReturn(Extension.State.Enabled);
15381589
when(extensionDao.findById(1L)).thenReturn(extensionVO);
15391590

1540-
extensionsManager.runCustomAction(cmd);
1591+
1592+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
1593+
mockCallerRole(RoleType.Admin);
1594+
extensionsManager.runCustomAction(cmd);
1595+
}
15411596
}
15421597

15431598
@Test
@@ -1566,9 +1621,12 @@ public void runCustomAction_ExecutionThrowsException() throws Exception {
15661621

15671622
when(agentMgr.send(anyLong(), any(Command.class))).thenThrow(OperationTimedoutException.class);
15681623

1569-
CustomActionResultResponse result = extensionsManager.runCustomAction(cmd);
1624+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
1625+
mockCallerRole(RoleType.Admin);
1626+
CustomActionResultResponse result = extensionsManager.runCustomAction(cmd);
15701627

1571-
assertFalse(result.getSuccess());
1628+
assertFalse(result.getSuccess());
1629+
}
15721630
}
15731631

15741632
@Test

0 commit comments

Comments
 (0)