@@ -4622,6 +4622,106 @@ public void checkHostsDedicationAlertsIncludeResolvedAccountAndDomainNames() {
46224622 assertTrue (messages .stream ().anyMatch (m -> m .contains ("domain-a" ) && m .contains ("domain-b" )));
46234623 }
46244624
4625+ @ Test
4626+ public void checkHostsDedicationAlertNotesDestinationNotDedicatedToSpecificAccount () {
4627+ long srcHostId = 10L ;
4628+ long destHostId = 20L ;
4629+ long vmId = 1L ;
4630+ long serviceOfferingId = 2L ;
4631+
4632+ VMInstanceVO vm = Mockito .mock (VMInstanceVO .class );
4633+ when (vm .getId ()).thenReturn (vmId );
4634+ when (vm .getDataCenterId ()).thenReturn (1L );
4635+ when (vm .getPodIdToDeployIn ()).thenReturn (2L );
4636+ when (vm .getServiceOfferingId ()).thenReturn (serviceOfferingId );
4637+
4638+ HostVO srcHost = Mockito .mock (HostVO .class );
4639+ when (srcHost .getId ()).thenReturn (srcHostId );
4640+ HostVO destHost = Mockito .mock (HostVO .class );
4641+ when (destHost .getId ()).thenReturn (destHostId );
4642+ when (hostDao .findById (srcHostId )).thenReturn (srcHost );
4643+ when (hostDao .findById (destHostId )).thenReturn (destHost );
4644+
4645+ // src host is dedicated to an account; dest host is dedicated to a whole domain (no account), so
4646+ // destAccountId resolves to null even though destHost is explicitly dedicated.
4647+ DedicatedResourceVO srcDedication = Mockito .mock (DedicatedResourceVO .class );
4648+ when (srcDedication .getAccountId ()).thenReturn (100L );
4649+ when (srcDedication .getDomainId ()).thenReturn ((Long ) null );
4650+ DedicatedResourceVO destDedication = Mockito .mock (DedicatedResourceVO .class );
4651+ when (destDedication .getAccountId ()).thenReturn ((Long ) null );
4652+ when (destDedication .getDomainId ()).thenReturn (400L );
4653+ when (dedicatedResourceDao .findByHostId (srcHostId )).thenReturn (srcDedication );
4654+ when (dedicatedResourceDao .findByHostId (destHostId )).thenReturn (destDedication );
4655+
4656+ AccountVO srcAccount = Mockito .mock (AccountVO .class );
4657+ when (srcAccount .toString ()).thenReturn ("Account {accountName=account-a}" );
4658+ when (accountDao .findById (100L )).thenReturn (srcAccount );
4659+
4660+ ServiceOfferingVO serviceOffering = Mockito .mock (ServiceOfferingVO .class );
4661+ when (serviceOffering .getDeploymentPlanner ()).thenReturn (null );
4662+ when (_serviceOfferingDao .findById (vmId , serviceOfferingId )).thenReturn (serviceOffering );
4663+
4664+ when (plannerHostReservationDao .listAllDedicatedHosts ()).thenReturn (new ArrayList <>());
4665+
4666+ userVmManagerImpl .checkHostsDedication (vm , srcHostId , destHostId );
4667+
4668+ ArgumentCaptor <String > bodyCaptor = ArgumentCaptor .forClass (String .class );
4669+ Mockito .verify (alertManager , Mockito .times (1 )).sendAlert (Mockito .eq (AlertManager .AlertType .ALERT_TYPE_USERVM ),
4670+ Mockito .eq (1L ), Mockito .eq (2L ), Mockito .anyString (), bodyCaptor .capture ());
4671+ assertTrue (bodyCaptor .getValue ().contains ("account-a" ));
4672+ assertTrue (bodyCaptor .getValue ().contains ("not dedicated to a specific account" ));
4673+ }
4674+
4675+ @ Test
4676+ public void checkHostsDedicationAlertNotesDestinationNotDedicatedToSpecificDomain () {
4677+ long srcHostId = 10L ;
4678+ long destHostId = 20L ;
4679+ long vmId = 1L ;
4680+ long serviceOfferingId = 2L ;
4681+
4682+ VMInstanceVO vm = Mockito .mock (VMInstanceVO .class );
4683+ when (vm .getId ()).thenReturn (vmId );
4684+ when (vm .getDataCenterId ()).thenReturn (1L );
4685+ when (vm .getPodIdToDeployIn ()).thenReturn (2L );
4686+ when (vm .getServiceOfferingId ()).thenReturn (serviceOfferingId );
4687+
4688+ HostVO srcHost = Mockito .mock (HostVO .class );
4689+ when (srcHost .getId ()).thenReturn (srcHostId );
4690+ HostVO destHost = Mockito .mock (HostVO .class );
4691+ when (destHost .getId ()).thenReturn (destHostId );
4692+ when (hostDao .findById (srcHostId )).thenReturn (srcHost );
4693+ when (hostDao .findById (destHostId )).thenReturn (destHost );
4694+
4695+ // src host is dedicated to a whole domain (no account); dest host is dedicated to an account, so
4696+ // destDomainId resolves to null even though destHost is explicitly dedicated.
4697+ DedicatedResourceVO srcDedication = Mockito .mock (DedicatedResourceVO .class );
4698+ when (srcDedication .getAccountId ()).thenReturn ((Long ) null );
4699+ when (srcDedication .getDomainId ()).thenReturn (200L );
4700+ DedicatedResourceVO destDedication = Mockito .mock (DedicatedResourceVO .class );
4701+ when (destDedication .getAccountId ()).thenReturn (300L );
4702+ when (destDedication .getDomainId ()).thenReturn ((Long ) null );
4703+ when (dedicatedResourceDao .findByHostId (srcHostId )).thenReturn (srcDedication );
4704+ when (dedicatedResourceDao .findByHostId (destHostId )).thenReturn (destDedication );
4705+
4706+ DomainVO srcDomain = Mockito .mock (DomainVO .class );
4707+ when (srcDomain .toString ()).thenReturn ("Domain {name=domain-a}" );
4708+ when (domainDaoMock .findById (200L )).thenReturn (srcDomain );
4709+
4710+ ServiceOfferingVO serviceOffering = Mockito .mock (ServiceOfferingVO .class );
4711+ when (serviceOffering .getDeploymentPlanner ()).thenReturn (null );
4712+ when (_serviceOfferingDao .findById (vmId , serviceOfferingId )).thenReturn (serviceOffering );
4713+
4714+ when (plannerHostReservationDao .listAllDedicatedHosts ()).thenReturn (new ArrayList <>());
4715+
4716+ userVmManagerImpl .checkHostsDedication (vm , srcHostId , destHostId );
4717+
4718+ ArgumentCaptor <String > bodyCaptor = ArgumentCaptor .forClass (String .class );
4719+ Mockito .verify (alertManager , Mockito .times (1 )).sendAlert (Mockito .eq (AlertManager .AlertType .ALERT_TYPE_USERVM ),
4720+ Mockito .eq (1L ), Mockito .eq (2L ), Mockito .anyString (), bodyCaptor .capture ());
4721+ assertTrue (bodyCaptor .getValue ().contains ("domain-a" ));
4722+ assertTrue (bodyCaptor .getValue ().contains ("not dedicated to a specific domain" ));
4723+ }
4724+
46254725 private UserVmVO mockStoppedVmForFailedCreation (Long vmId ) {
46264726 UserVmVO vm = Mockito .mock (UserVmVO .class );
46274727 when (vm .getState ()).thenReturn (VirtualMachine .State .Stopped );
0 commit comments