|
|
|
@ -16,6 +16,21 @@ import org.mockito.Spy; |
|
|
|
import org.mockito.junit.jupiter.MockitoExtension; |
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.cf.CalculatedField; |
|
|
|
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.AlarmCalculatedFieldConfiguration; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.Argument; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.ArgumentType; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.ArgumentsBasedCalculatedFieldConfiguration; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.CalculatedFieldConfiguration; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.ReferencedEntityKey; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.SimpleCalculatedFieldConfiguration; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.TimeSeriesOutput; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.geofencing.EntityCoordinates; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.geofencing.GeofencingCalculatedFieldConfiguration; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.geofencing.GeofencingReportStrategy; |
|
|
|
import org.thingsboard.server.common.data.cf.configuration.geofencing.ZoneGroupConfiguration; |
|
|
|
import org.thingsboard.server.common.data.id.CalculatedFieldId; |
|
|
|
import org.thingsboard.server.common.data.id.DashboardId; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceId; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|
|
|
@ -24,6 +39,8 @@ import org.thingsboard.server.common.data.id.IntegrationId; |
|
|
|
import org.thingsboard.server.common.data.id.IotHubInstalledItemId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.integration.Integration; |
|
|
|
import org.thingsboard.server.common.data.iot_hub.AlarmRuleInstalledItemDescriptor; |
|
|
|
import org.thingsboard.server.common.data.iot_hub.CalculatedFieldInstalledItemDescriptor; |
|
|
|
import org.thingsboard.server.common.data.iot_hub.DeviceInstalledItemDescriptor; |
|
|
|
import org.thingsboard.server.common.data.iot_hub.IotHubInstalledItem; |
|
|
|
import org.thingsboard.server.common.data.iot_hub.RuleChainInstalledItemDescriptor; |
|
|
|
@ -32,6 +49,7 @@ import org.thingsboard.server.common.data.DeviceProfile; |
|
|
|
import org.thingsboard.server.common.data.converter.Converter; |
|
|
|
import org.thingsboard.server.common.data.id.ConverterId; |
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
import org.thingsboard.server.dao.cf.CalculatedFieldService; |
|
|
|
import org.thingsboard.server.dao.dashboard.DashboardService; |
|
|
|
import org.thingsboard.server.dao.converter.ConverterService; |
|
|
|
import org.thingsboard.server.dao.device.DeviceProfileService; |
|
|
|
@ -39,12 +57,14 @@ 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.service.entitiy.cf.TbCalculatedFieldService; |
|
|
|
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.security.model.SecurityUser; |
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
@ -76,6 +96,10 @@ class DefaultIotHubServiceTest { |
|
|
|
@Mock |
|
|
|
private IotHubInstalledItemService iotHubInstalledItemService; |
|
|
|
@Mock |
|
|
|
private TbCalculatedFieldService tbCalculatedFieldService; |
|
|
|
@Mock |
|
|
|
private CalculatedFieldService calculatedFieldService; |
|
|
|
@Mock |
|
|
|
private DeviceService deviceService; |
|
|
|
@Mock |
|
|
|
private EntityService entityService; |
|
|
|
@ -461,6 +485,151 @@ class DefaultIotHubServiceTest { |
|
|
|
assertThat(result.getErrorMessage()).contains("install failed"); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void doInstallVersion_calculatedField_mapsTenantReferencesToInstallingTenant() throws Exception { |
|
|
|
mockTenant(); |
|
|
|
String versionId = UUID.randomUUID().toString(); |
|
|
|
when(iotHubRestClient.getVersionInfo(versionId)) |
|
|
|
.thenReturn(version(versionId, UUID.randomUUID().toString(), "CALCULATED_FIELD", "CF", "1.0")); |
|
|
|
when(iotHubRestClient.getVersionFileData(versionId)).thenReturn(calculatedFieldFileData()); |
|
|
|
when(tbCalculatedFieldService.save(any(), eq(user))).thenAnswer(invocation -> invocation.getArgument(0)); |
|
|
|
when(iotHubInstalledItemService.save(eq(tenantId), any())).thenAnswer(invocation -> invocation.getArgument(1)); |
|
|
|
|
|
|
|
service.doInstallVersion(user, versionId, null, null); |
|
|
|
|
|
|
|
ArgumentCaptor<CalculatedField> captor = ArgumentCaptor.forClass(CalculatedField.class); |
|
|
|
verify(tbCalculatedFieldService).save(captor.capture(), eq(user)); |
|
|
|
assertThat(((ArgumentsBasedCalculatedFieldConfiguration) captor.getValue().getConfiguration()).getArguments().get("T").getRefEntityId()).isEqualTo(tenantId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void updateItemVersion_calculatedField_mapsTenantReferencesToInstallingTenant() throws Exception { |
|
|
|
mockTenant(); |
|
|
|
String versionId = UUID.randomUUID().toString(); |
|
|
|
CalculatedFieldId calculatedFieldId = new CalculatedFieldId(UUID.randomUUID()); |
|
|
|
CalculatedFieldInstalledItemDescriptor descriptor = new CalculatedFieldInstalledItemDescriptor(); |
|
|
|
descriptor.setCalculatedFieldId(calculatedFieldId); |
|
|
|
IotHubInstalledItem item = installedItem(UUID.randomUUID()); |
|
|
|
item.setItemType("CALCULATED_FIELD"); |
|
|
|
item.setItemVersionId(UUID.randomUUID()); |
|
|
|
item.setDescriptor(descriptor); |
|
|
|
when(iotHubInstalledItemService.findById(tenantId, item.getId())).thenReturn(item); |
|
|
|
when(iotHubRestClient.getVersionInfo(anyString())) |
|
|
|
.thenReturn(version(versionId, UUID.randomUUID().toString(), "CALCULATED_FIELD", "CF", "2.0")); |
|
|
|
when(iotHubRestClient.getVersionFileData(versionId)).thenReturn(calculatedFieldFileData()); |
|
|
|
CalculatedField existing = new CalculatedField(calculatedFieldId); |
|
|
|
existing.setTenantId(tenantId); |
|
|
|
when(calculatedFieldService.findById(tenantId, calculatedFieldId)).thenReturn(existing); |
|
|
|
|
|
|
|
UpdateItemVersionResult result = service.updateItemVersion(user, item.getId(), versionId, true, null); |
|
|
|
|
|
|
|
assertThat(result.isSuccess()).isTrue(); |
|
|
|
ArgumentCaptor<CalculatedField> captor = ArgumentCaptor.forClass(CalculatedField.class); |
|
|
|
verify(tbCalculatedFieldService).save(captor.capture(), eq(user)); |
|
|
|
assertThat(((ArgumentsBasedCalculatedFieldConfiguration) captor.getValue().getConfiguration()).getArguments().get("T").getRefEntityId()).isEqualTo(tenantId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void doInstallVersion_alarmRule_mapsTenantReferencesToInstallingTenant() throws Exception { |
|
|
|
mockTenant(); |
|
|
|
String versionId = UUID.randomUUID().toString(); |
|
|
|
when(iotHubRestClient.getVersionInfo(versionId)) |
|
|
|
.thenReturn(version(versionId, UUID.randomUUID().toString(), "ALARM_RULE", "Alarm", "1.0")); |
|
|
|
when(iotHubRestClient.getVersionFileData(versionId)).thenReturn(alarmRuleFileData()); |
|
|
|
when(tbCalculatedFieldService.save(any(), eq(user))).thenAnswer(invocation -> invocation.getArgument(0)); |
|
|
|
when(iotHubInstalledItemService.save(eq(tenantId), any())).thenAnswer(invocation -> invocation.getArgument(1)); |
|
|
|
|
|
|
|
service.doInstallVersion(user, versionId, null, null); |
|
|
|
|
|
|
|
ArgumentCaptor<CalculatedField> captor = ArgumentCaptor.forClass(CalculatedField.class); |
|
|
|
verify(tbCalculatedFieldService).save(captor.capture(), eq(user)); |
|
|
|
assertThat(((ArgumentsBasedCalculatedFieldConfiguration) captor.getValue().getConfiguration()).getArguments().get("T").getRefEntityId()).isEqualTo(tenantId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void updateItemVersion_alarmRule_mapsTenantReferencesToInstallingTenant() throws Exception { |
|
|
|
mockTenant(); |
|
|
|
String versionId = UUID.randomUUID().toString(); |
|
|
|
CalculatedFieldId calculatedFieldId = new CalculatedFieldId(UUID.randomUUID()); |
|
|
|
AlarmRuleInstalledItemDescriptor descriptor = new AlarmRuleInstalledItemDescriptor(); |
|
|
|
descriptor.setCalculatedFieldId(calculatedFieldId); |
|
|
|
IotHubInstalledItem item = installedItem(UUID.randomUUID()); |
|
|
|
item.setItemType("ALARM_RULE"); |
|
|
|
item.setItemVersionId(UUID.randomUUID()); |
|
|
|
item.setDescriptor(descriptor); |
|
|
|
when(iotHubInstalledItemService.findById(tenantId, item.getId())).thenReturn(item); |
|
|
|
when(iotHubRestClient.getVersionInfo(anyString())) |
|
|
|
.thenReturn(version(versionId, UUID.randomUUID().toString(), "ALARM_RULE", "Alarm", "2.0")); |
|
|
|
when(iotHubRestClient.getVersionFileData(versionId)).thenReturn(alarmRuleFileData()); |
|
|
|
CalculatedField existing = new CalculatedField(calculatedFieldId); |
|
|
|
existing.setTenantId(tenantId); |
|
|
|
when(calculatedFieldService.findById(tenantId, calculatedFieldId)).thenReturn(existing); |
|
|
|
|
|
|
|
UpdateItemVersionResult result = service.updateItemVersion(user, item.getId(), versionId, true, null); |
|
|
|
|
|
|
|
assertThat(result.isSuccess()).isTrue(); |
|
|
|
ArgumentCaptor<CalculatedField> captor = ArgumentCaptor.forClass(CalculatedField.class); |
|
|
|
verify(tbCalculatedFieldService).save(captor.capture(), eq(user)); |
|
|
|
assertThat(((ArgumentsBasedCalculatedFieldConfiguration) captor.getValue().getConfiguration()).getArguments().get("T").getRefEntityId()).isEqualTo(tenantId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void doInstallVersion_geofencingCalculatedField_mapsZoneGroupTenantReferencesToInstallingTenant() throws Exception { |
|
|
|
mockTenant(); |
|
|
|
String versionId = UUID.randomUUID().toString(); |
|
|
|
when(iotHubRestClient.getVersionInfo(versionId)) |
|
|
|
.thenReturn(version(versionId, UUID.randomUUID().toString(), "CALCULATED_FIELD", "Geofencing", "1.0")); |
|
|
|
when(iotHubRestClient.getVersionFileData(versionId)).thenReturn(geofencingFileData()); |
|
|
|
when(tbCalculatedFieldService.save(any(), eq(user))).thenAnswer(invocation -> invocation.getArgument(0)); |
|
|
|
when(iotHubInstalledItemService.save(eq(tenantId), any())).thenAnswer(invocation -> invocation.getArgument(1)); |
|
|
|
|
|
|
|
service.doInstallVersion(user, versionId, null, null); |
|
|
|
|
|
|
|
ArgumentCaptor<CalculatedField> captor = ArgumentCaptor.forClass(CalculatedField.class); |
|
|
|
verify(tbCalculatedFieldService).save(captor.capture(), eq(user)); |
|
|
|
assertThat(((GeofencingCalculatedFieldConfiguration) captor.getValue().getConfiguration()).getZoneGroups().get("zones").getRefEntityId()).isEqualTo(tenantId); |
|
|
|
} |
|
|
|
|
|
|
|
private byte[] calculatedFieldFileData() { |
|
|
|
SimpleCalculatedFieldConfiguration configuration = new SimpleCalculatedFieldConfiguration(); |
|
|
|
configuration.setArguments(Map.of("T", otherTenantArgument())); |
|
|
|
configuration.setExpression("T"); |
|
|
|
return fileData(CalculatedFieldType.SIMPLE, configuration); |
|
|
|
} |
|
|
|
|
|
|
|
private byte[] alarmRuleFileData() { |
|
|
|
AlarmCalculatedFieldConfiguration configuration = new AlarmCalculatedFieldConfiguration(); |
|
|
|
configuration.setArguments(Map.of("T", otherTenantArgument())); |
|
|
|
configuration.setCreateRules(Map.of()); |
|
|
|
return fileData(CalculatedFieldType.ALARM, configuration); |
|
|
|
} |
|
|
|
|
|
|
|
private byte[] geofencingFileData() { |
|
|
|
ZoneGroupConfiguration zoneGroup = new ZoneGroupConfiguration("perimeter", GeofencingReportStrategy.REPORT_PRESENCE_STATUS_ONLY, false); |
|
|
|
zoneGroup.setRefEntityId(TenantId.fromUUID(UUID.randomUUID())); |
|
|
|
GeofencingCalculatedFieldConfiguration configuration = new GeofencingCalculatedFieldConfiguration(); |
|
|
|
configuration.setEntityCoordinates(new EntityCoordinates("latitude", "longitude")); |
|
|
|
configuration.setZoneGroups(Map.of("zones", zoneGroup)); |
|
|
|
configuration.setOutput(new TimeSeriesOutput()); |
|
|
|
return fileData(CalculatedFieldType.GEOFENCING, configuration); |
|
|
|
} |
|
|
|
|
|
|
|
private Argument otherTenantArgument() { |
|
|
|
Argument argument = new Argument(); |
|
|
|
argument.setRefEntityId(TenantId.fromUUID(UUID.randomUUID())); |
|
|
|
argument.setRefEntityKey(new ReferencedEntityKey("temperature", ArgumentType.TS_LATEST, null)); |
|
|
|
return argument; |
|
|
|
} |
|
|
|
|
|
|
|
private byte[] fileData(CalculatedFieldType type, CalculatedFieldConfiguration configuration) { |
|
|
|
CalculatedField calculatedField = new CalculatedField(); |
|
|
|
calculatedField.setEntityId(new DeviceId(UUID.randomUUID())); |
|
|
|
calculatedField.setType(type); |
|
|
|
calculatedField.setName("CF"); |
|
|
|
calculatedField.setConfiguration(configuration); |
|
|
|
return JacksonUtil.toString(calculatedField).getBytes(StandardCharsets.UTF_8); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void deleteInstalledItem_devicePackage_removesEachEntityBeforeWhatItRefersTo() throws Exception { |
|
|
|
mockTenant(); |
|
|
|
|