|
121 | 121 | import com.cloud.storage.dao.VMTemplateDao;
|
122 | 122 | import com.cloud.user.Account;
|
123 | 123 | import com.cloud.utils.Pair;
|
| 124 | +import com.cloud.utils.UuidUtils; |
124 | 125 | import com.cloud.utils.db.EntityManager;
|
125 | 126 | import com.cloud.utils.db.SearchBuilder;
|
126 | 127 | import com.cloud.utils.db.SearchCriteria;
|
@@ -525,11 +526,15 @@ public void getExternalAccessDetailsReturnsMapWithHostAndExtension() {
|
525 | 526 | when(hostDetailsDao.findDetails(hostId)).thenReturn(null);
|
526 | 527 | when(extensionResourceMapDetailsDao.listDetailsKeyPairs(2L, true)).thenReturn(Collections.emptyMap());
|
527 | 528 | when(extensionDetailsDao.listDetailsKeyPairs(3L, true)).thenReturn(map);
|
528 |
| - Map<String, Map<String, String>> result = extensionsManager.getExternalAccessDetails(map, hostId, resourceMap); |
529 |
| - assertTrue(result.containsKey(ApiConstants.ACTION)); |
530 |
| - assertFalse(result.containsKey(ApiConstants.HOST)); |
531 |
| - assertFalse(result.containsKey(ApiConstants.RESOURCE_MAP)); |
532 |
| - assertTrue(result.containsKey(ApiConstants.EXTENSION)); |
| 529 | + try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) { |
| 530 | + mockCallerRole(RoleType.Admin); |
| 531 | + Map<String, Map<String, String>> result = extensionsManager.getExternalAccessDetails(map, hostId, resourceMap); |
| 532 | + assertTrue(result.containsKey(ApiConstants.ACTION)); |
| 533 | + assertFalse(result.containsKey(ApiConstants.HOST)); |
| 534 | + assertFalse(result.containsKey(ApiConstants.RESOURCE_MAP)); |
| 535 | + assertTrue(result.containsKey(ApiConstants.EXTENSION)); |
| 536 | + assertTrue(result.containsKey(ApiConstants.CALLER)); |
| 537 | + } |
533 | 538 | }
|
534 | 539 |
|
535 | 540 | @Test(expected = CloudRuntimeException.class)
|
@@ -1285,9 +1290,14 @@ private void mockCallerRole(RoleType roleType) {
|
1285 | 1290 | CallContext callContextMock = mock(CallContext.class);
|
1286 | 1291 | when(CallContext.current()).thenReturn(callContextMock);
|
1287 | 1292 | Account accountMock = mock(Account.class);
|
| 1293 | + when(accountMock.getAccountName()).thenReturn("testAccount"); |
| 1294 | + when(accountMock.getUuid()).thenReturn(UUID.randomUUID().toString()); |
| 1295 | + when(accountMock.getType()).thenReturn(RoleType.Admin.equals(roleType) ? Account.Type.ADMIN : Account.Type.NORMAL); |
1288 | 1296 | when(accountMock.getRoleId()).thenReturn(1L);
|
1289 | 1297 | Role role = mock(Role.class);
|
1290 | 1298 | when(role.getRoleType()).thenReturn(roleType);
|
| 1299 | + when(role.getUuid()).thenReturn("role-uuid-1"); |
| 1300 | + when(role.getName()).thenReturn(roleType.name() + "Role"); |
1291 | 1301 | when(roleService.findRole(1L)).thenReturn(role);
|
1292 | 1302 | when(callContextMock.getCallingAccount()).thenReturn(accountMock);
|
1293 | 1303 | }
|
@@ -1952,4 +1962,78 @@ public void getInstanceConsole_whenAgentManagerFails() {
|
1952 | 1962 | Answer result = extensionsManager.getInstanceConsole(vm, host);
|
1953 | 1963 | assertNull(result);
|
1954 | 1964 | }
|
| 1965 | + |
| 1966 | + @Test |
| 1967 | + public void getExternalAccessDetailsReturnsExpectedDetails() { |
| 1968 | + Host host = mock(Host.class); |
| 1969 | + when(host.getId()).thenReturn(100L); |
| 1970 | + when(host.getClusterId()).thenReturn(1L); |
| 1971 | + Map<String, String> vmDetails = Map.of("key1", "value1", "key2", "value2"); |
| 1972 | + ExtensionResourceMapVO resourceMapVO = mock(ExtensionResourceMapVO.class); |
| 1973 | + when(extensionResourceMapDao.findByResourceIdAndType(1L, ExtensionResourceMap.ResourceType.Cluster)) |
| 1974 | + .thenReturn(resourceMapVO); |
| 1975 | + doReturn(new HashMap<>()).when(extensionsManager).getExternalAccessDetails(null, 100L, resourceMapVO); |
| 1976 | + Map<String, Map<String, String>> result = extensionsManager.getExternalAccessDetails(host, vmDetails); |
| 1977 | + assertNotNull(result); |
| 1978 | + assertNotNull(result.get(ApiConstants.VIRTUAL_MACHINE)); |
| 1979 | + assertEquals(vmDetails, result.get(ApiConstants.VIRTUAL_MACHINE)); |
| 1980 | + } |
| 1981 | + |
| 1982 | + @Test |
| 1983 | + public void getExternalAccessDetailsReturnsExpectedNullDetails() { |
| 1984 | + Host host = mock(Host.class); |
| 1985 | + when(host.getId()).thenReturn(101L); |
| 1986 | + when(host.getClusterId()).thenReturn(1L); |
| 1987 | + Map<String, String> vmDetails = null; |
| 1988 | + ExtensionResourceMapVO resourceMapVO = mock(ExtensionResourceMapVO.class); |
| 1989 | + when(extensionResourceMapDao.findByResourceIdAndType(1L, ExtensionResourceMap.ResourceType.Cluster)) |
| 1990 | + .thenReturn(resourceMapVO); |
| 1991 | + doReturn(new HashMap<>()).when(extensionsManager).getExternalAccessDetails(null, 101L, resourceMapVO); |
| 1992 | + Map<String, Map<String, String>> result = extensionsManager.getExternalAccessDetails(host, vmDetails); |
| 1993 | + assertNotNull(result); |
| 1994 | + assertNull(result.get(ApiConstants.VIRTUAL_MACHINE)); |
| 1995 | + } |
| 1996 | + |
| 1997 | + @Test |
| 1998 | + public void getCallerDetailsReturnsExpectedDetailsForValidCaller() { |
| 1999 | + try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) { |
| 2000 | + mockCallerRole(RoleType.Admin); |
| 2001 | + Map<String, String> result = extensionsManager.getCallerDetails(); |
| 2002 | + assertNotNull(result); |
| 2003 | + assertTrue(UuidUtils.isUuid(result.get(ApiConstants.ID))); |
| 2004 | + assertEquals("testAccount", result.get(ApiConstants.NAME)); |
| 2005 | + assertEquals("ADMIN", result.get(ApiConstants.TYPE)); |
| 2006 | + assertEquals("role-uuid-1", result.get(ApiConstants.ROLE_ID)); |
| 2007 | + assertEquals("AdminRole", result.get(ApiConstants.ROLE_NAME)); |
| 2008 | + assertEquals("Admin", result.get(ApiConstants.ROLE_TYPE)); |
| 2009 | + } |
| 2010 | + } |
| 2011 | + |
| 2012 | + @Test |
| 2013 | + public void getCallerDetailsReturnsNullWhenCallerIsNull() { |
| 2014 | + CallContext callContext = mock(CallContext.class); |
| 2015 | + when(callContext.getCallingAccount()).thenReturn(null); |
| 2016 | + try (MockedStatic<CallContext> mockedCallContext = mockStatic(CallContext.class)) { |
| 2017 | + mockedCallContext.when(CallContext::current).thenReturn(callContext); |
| 2018 | + Map<String, String> result = extensionsManager.getCallerDetails(); |
| 2019 | + assertNull(result); |
| 2020 | + } |
| 2021 | + } |
| 2022 | + |
| 2023 | + @Test |
| 2024 | + public void getCallerDetailsReturnsDetailsWithoutRoleWhenRoleIsNull() { |
| 2025 | + try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) { |
| 2026 | + mockCallerRole(RoleType.User); |
| 2027 | + when(roleService.findRole(1L)).thenReturn(null); |
| 2028 | + Map<String, String> result = extensionsManager.getCallerDetails(); |
| 2029 | + assertNotNull(result); |
| 2030 | + assertTrue(UuidUtils.isUuid(result.get(ApiConstants.ID))); |
| 2031 | + assertEquals("testAccount", result.get(ApiConstants.NAME)); |
| 2032 | + assertEquals("NORMAL", result.get(ApiConstants.TYPE)); |
| 2033 | + assertNull(result.get(ApiConstants.ROLE_ID)); |
| 2034 | + assertNull(result.get(ApiConstants.ROLE_NAME)); |
| 2035 | + assertNull(result.get(ApiConstants.ROLE_TYPE)); |
| 2036 | + } |
| 2037 | + } |
| 2038 | + |
1955 | 2039 | }
|
0 commit comments