|
27 | 27 | import org.junit.Test; |
28 | 28 | import org.junit.runner.RunWith; |
29 | 29 | import org.mockito.Mock; |
| 30 | +import org.mockito.MockedStatic; |
30 | 31 | import org.mockito.Mockito; |
31 | 32 | import org.mockito.junit.MockitoJUnitRunner; |
32 | 33 |
|
33 | 34 | import com.cloud.user.Account; |
| 35 | +import com.cloud.user.AccountService; |
34 | 36 | import com.cloud.user.User; |
| 37 | +import com.cloud.utils.component.ComponentContext; |
35 | 38 | import com.cloud.utils.db.EntityManager; |
36 | 39 |
|
37 | 40 | @RunWith(MockitoJUnitRunner.class) |
38 | 41 | public class CallContextTest { |
39 | 42 |
|
40 | 43 | @Mock |
41 | 44 | EntityManager entityMgr; |
| 45 | + @Mock |
| 46 | + User user; |
| 47 | + @Mock |
| 48 | + Account account; |
42 | 49 |
|
43 | 50 | @Before |
44 | 51 | public void setUp() { |
45 | 52 | CallContext.init(entityMgr); |
46 | | - CallContext.register(Mockito.mock(User.class), Mockito.mock(Account.class)); |
| 53 | + CallContext.register(user, account); |
47 | 54 | } |
48 | 55 |
|
49 | 56 | @After |
@@ -80,4 +87,50 @@ public void testGetContextParameter() { |
80 | 87 | Assert.assertEquals("current context map should have exactly three entries", 3, currentContext.getContextParameters().size()); |
81 | 88 | } |
82 | 89 |
|
| 90 | + |
| 91 | + @Test |
| 92 | + public void isCallingAccountRootAdminReturnsTrueWhenAccountIsRootAdminAccountServiceNotAvailable() { |
| 93 | + Mockito.when(account.getType()).thenReturn(Account.Type.ADMIN); |
| 94 | + |
| 95 | + CallContext context = CallContext.current(); |
| 96 | + Assert.assertTrue(context.isCallingAccountRootAdmin()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void isCallingAccountRootAdminReturnsFalseWhenAccountIsNotRootAdminAccountServiceNotAvailable() { |
| 101 | + Mockito.when(account.getType()).thenReturn(Account.Type.NORMAL); |
| 102 | + |
| 103 | + CallContext context = CallContext.current(); |
| 104 | + Assert.assertFalse(context.isCallingAccountRootAdmin()); |
| 105 | + Assert.assertFalse(context.isCallingAccountRootAdmin()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void isCallingAccountRootAdminTrueWhenAccountServiceAvailable() { |
| 110 | + try (MockedStatic<ComponentContext> componentContextMockedStatic = Mockito.mockStatic(ComponentContext.class)) { |
| 111 | + AccountService accountService = Mockito.mock(AccountService.class); |
| 112 | + Mockito.when(accountService.isRootAdmin(account)).thenReturn(true); |
| 113 | + componentContextMockedStatic.when(() -> ComponentContext.getDelegateComponentOfType(AccountService.class)).thenReturn(accountService); |
| 114 | + CallContext context = CallContext.current(); |
| 115 | + Assert.assertTrue(context.isCallingAccountRootAdmin()); |
| 116 | + // Verify isRootAdmin was called only once |
| 117 | + Assert.assertTrue(context.isCallingAccountRootAdmin()); |
| 118 | + componentContextMockedStatic.verify(() -> ComponentContext.getDelegateComponentOfType(AccountService.class)); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void isCallingAccountRootAdminFalseWhenAccountServiceAvailable() { |
| 124 | + try (MockedStatic<ComponentContext> componentContextMockedStatic = Mockito.mockStatic(ComponentContext.class)) { |
| 125 | + AccountService accountService = Mockito.mock(AccountService.class); |
| 126 | + Mockito.when(accountService.isRootAdmin(account)).thenReturn(false); |
| 127 | + componentContextMockedStatic.when(() -> ComponentContext.getDelegateComponentOfType(AccountService.class)).thenReturn(accountService); |
| 128 | + CallContext context = CallContext.current(); |
| 129 | + Assert.assertFalse(context.isCallingAccountRootAdmin()); |
| 130 | + // Verify isRootAdmin was called only once |
| 131 | + Assert.assertFalse(context.isCallingAccountRootAdmin()); |
| 132 | + componentContextMockedStatic.verify(() -> ComponentContext.getDelegateComponentOfType(AccountService.class)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
83 | 136 | } |
0 commit comments