@ -22,6 +22,7 @@ import org.thingsboard.server.common.data.id.DeviceProfileId;
import org.thingsboard.server.common.data.id.EntityId ;
import org.thingsboard.server.common.data.id.IntegrationId ;
import org.thingsboard.server.common.data.id.IotHubInstalledItemId ;
import org.thingsboard.server.common.data.id.RuleChainId ;
import org.thingsboard.server.common.data.id.TenantId ;
import org.thingsboard.server.common.data.integration.Integration ;
import org.thingsboard.server.common.data.iot_hub.DeviceInstalledItemDescriptor ;
@ -39,10 +40,12 @@ import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.dao.entity.EntityService ;
import org.thingsboard.server.dao.integration.IntegrationService ;
import org.thingsboard.server.dao.iot_hub.IotHubInstalledItemService ;
import org.thingsboard.server.dao.rule.RuleChainService ;
import org.thingsboard.server.service.entitiy.dashboard.TbDashboardService ;
import org.thingsboard.server.service.entitiy.converter.TbConverterService ;
import org.thingsboard.server.service.entitiy.device.TbDeviceService ;
import org.thingsboard.server.service.entitiy.device.profile.TbDeviceProfileService ;
import org.thingsboard.server.service.rule.TbRuleChainService ;
import org.thingsboard.server.service.security.model.SecurityUser ;
import java.util.List ;
@ -95,6 +98,10 @@ class DefaultIotHubServiceTest {
private ConverterService converterService ;
@Mock
private TbConverterService tbConverterService ;
@Mock
private RuleChainService ruleChainService ;
@Mock
private TbRuleChainService tbRuleChainService ;
@Spy
@InjectMocks
@ -474,6 +481,7 @@ class DefaultIotHubServiceTest {
when ( iotHubInstalledItemService . findByTenantId ( eq ( tenantId ) , eq ( List . of ( "DEVICE" ) ) , isNull ( ) , any ( ) ) )
. thenReturn ( new PageData < > ( List . of ( deleted ) , 1 , 1 , false ) ) ;
when ( deviceProfileService . findDeviceProfileById ( tenantId , profile . getId ( ) ) ) . thenReturn ( profile ) ;
noDevices ( profile . getId ( ) ) ;
when ( dashboardService . findDashboardById ( tenantId , dashboard . getId ( ) ) ) . thenReturn ( dashboard ) ;
when ( integrationService . findIntegrationById ( tenantId , integration . getId ( ) ) ) . thenReturn ( integration ) ;
when ( converterService . findConverterById ( tenantId , converter . getId ( ) ) ) . thenReturn ( converter ) ;
@ -736,6 +744,81 @@ class DefaultIotHubServiceTest {
return integration ;
}
@Test
void findKeptEntities_devicePackage_namesTheProfileDevicesUseAndItsRuleChain_apartFromTheShared ( ) {
DeviceProfile profile = new DeviceProfile ( new DeviceProfileId ( UUID . randomUUID ( ) ) ) ;
RuleChainId ruleChainId = new RuleChainId ( UUID . randomUUID ( ) ) ;
profile . setDefaultRuleChainId ( ruleChainId ) ;
Device packageDevice = new Device ( new DeviceId ( UUID . randomUUID ( ) ) ) ;
packageDevice . setDeviceProfileId ( profile . getId ( ) ) ;
IotHubInstalledItem checked = devicePackage ( ruleChainId , profile . getId ( ) , packageDevice . getId ( ) ) ;
when ( iotHubInstalledItemService . findById ( tenantId , checked . getId ( ) ) ) . thenReturn ( checked ) ;
when ( iotHubInstalledItemService . findByTenantId ( eq ( tenantId ) , eq ( List . of ( "DEVICE" ) ) , isNull ( ) , any ( ) ) )
. thenReturn ( new PageData < > ( List . of ( checked ) , 1 , 1 , false ) ) ;
// 3 devices on the profile: the package's own, which the delete removes first, and 2 the integration created
when ( deviceService . findDeviceIdsByTenantIdAndDeviceProfileId ( eq ( tenantId ) , eq ( profile . getId ( ) ) , any ( ) ) )
. thenReturn ( new PageData < > ( List . of ( ) , 3 , 3 , true ) ) ;
when ( deviceService . findDeviceById ( tenantId , packageDevice . getId ( ) ) ) . thenReturn ( packageDevice ) ;
when ( deviceProfileService . findDeviceProfileById ( tenantId , profile . getId ( ) ) ) . thenReturn ( profile ) ;
when ( entityService . fetchEntityName ( tenantId , profile . getId ( ) ) ) . thenReturn ( Optional . of ( "smartico-l2-lr" ) ) ;
when ( entityService . fetchEntityName ( tenantId , ruleChainId ) ) . thenReturn ( Optional . of ( "Smartico L2-LR" ) ) ;
InstalledItemKeptEntities kept = service . findKeptEntities ( tenantId , checked . getId ( ) ) ;
// An update reads entities as shared with other packages: the profile is not among them
assertThat ( kept . getEntities ( ) ) . isEmpty ( ) ;
assertThat ( kept . getKeptForDevices ( ) ) . containsExactly (
new InstalledItemKeptEntities . KeptEntity ( profile . getId ( ) , "smartico-l2-lr" ) ,
new InstalledItemKeptEntities . KeptEntity ( ruleChainId , "Smartico L2-LR" ) ) ;
assertThat ( kept . getDevicesCount ( ) ) . isEqualTo ( 2 ) ;
}
@Test
void findKeptEntities_devicePackage_aKeptDeviceKeepsItsProfile ( ) {
DeviceProfileId profileId = new DeviceProfileId ( UUID . randomUUID ( ) ) ;
DeviceId sharedDeviceId = new DeviceId ( UUID . randomUUID ( ) ) ;
IotHubInstalledItem checked = devicePackage ( profileId , sharedDeviceId ) ;
IotHubInstalledItem other = devicePackage ( sharedDeviceId ) ;
when ( iotHubInstalledItemService . findById ( tenantId , checked . getId ( ) ) ) . thenReturn ( checked ) ;
when ( iotHubInstalledItemService . findByTenantId ( eq ( tenantId ) , eq ( List . of ( "DEVICE" ) ) , isNull ( ) , any ( ) ) )
. thenReturn ( new PageData < > ( List . of ( checked , other ) , 1 , 2 , false ) ) ;
// The only device on the profile is the one another package uses: the delete keeps it, so it keeps the profile
when ( deviceService . findDeviceIdsByTenantIdAndDeviceProfileId ( eq ( tenantId ) , eq ( profileId ) , any ( ) ) )
. thenReturn ( new PageData < > ( List . of ( sharedDeviceId ) , 1 , 1 , false ) ) ;
InstalledItemKeptEntities kept = service . findKeptEntities ( tenantId , checked . getId ( ) ) ;
assertThat ( kept . getKeptForDevices ( ) ) . extracting ( InstalledItemKeptEntities . KeptEntity : : getEntityId ) . containsExactly ( profileId ) ;
assertThat ( kept . getDevicesCount ( ) ) . isEqualTo ( 1 ) ;
}
@Test
void deleteInstalledItem_devicePackage_keepsTheProfileDevicesUseAndItsRuleChain ( ) throws Exception {
mockTenant ( ) ;
DeviceProfile profile = new DeviceProfile ( new DeviceProfileId ( UUID . randomUUID ( ) ) ) ;
RuleChainId ruleChainId = new RuleChainId ( UUID . randomUUID ( ) ) ;
profile . setDefaultRuleChainId ( ruleChainId ) ;
IotHubInstalledItem deleted = devicePackage ( ruleChainId , profile . getId ( ) ) ;
when ( iotHubInstalledItemService . findById ( tenantId , deleted . getId ( ) ) ) . thenReturn ( deleted ) ;
when ( iotHubInstalledItemService . findByTenantId ( eq ( tenantId ) , eq ( List . of ( "DEVICE" ) ) , isNull ( ) , any ( ) ) )
. thenReturn ( new PageData < > ( List . of ( deleted ) , 1 , 1 , false ) ) ;
when ( deviceService . findDeviceIdsByTenantIdAndDeviceProfileId ( eq ( tenantId ) , eq ( profile . getId ( ) ) , any ( ) ) )
. thenReturn ( new PageData < > ( List . of ( new DeviceId ( UUID . randomUUID ( ) ) ) , 1 , 1 , false ) ) ;
when ( deviceProfileService . findDeviceProfileById ( tenantId , profile . getId ( ) ) ) . thenReturn ( profile ) ;
service . deleteInstalledItem ( user , deleted . getId ( ) ) ;
verify ( tbDeviceProfileService , never ( ) ) . delete ( any ( ) , any ( ) ) ;
verify ( ruleChainService , never ( ) ) . findRuleChainById ( any ( ) , any ( ) ) ;
verify ( tbRuleChainService , never ( ) ) . delete ( any ( ) , any ( ) ) ;
verify ( iotHubInstalledItemService ) . deleteById ( tenantId , deleted . getId ( ) ) ;
}
private void noDevices ( DeviceProfileId profileId ) {
when ( deviceService . findDeviceIdsByTenantIdAndDeviceProfileId ( eq ( tenantId ) , eq ( profileId ) , any ( ) ) )
. thenReturn ( new PageData < > ( List . of ( ) , 0 , 0 , false ) ) ;
}
private IotHubInstalledItem devicePackage ( EntityId . . . createdEntityIds ) {
DeviceInstalledItemDescriptor descriptor = new DeviceInstalledItemDescriptor ( ) ;
descriptor . setCreatedEntityIds ( List . of ( createdEntityIds ) ) ;