34 changed files with 1559 additions and 107 deletions
@ -0,0 +1,152 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.service.integration; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import com.fasterxml.jackson.databind.node.ObjectNode; |
|||
import org.junit.After; |
|||
import org.junit.Test; |
|||
import org.springframework.test.context.TestPropertySource; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.converter.Converter; |
|||
import org.thingsboard.server.common.data.converter.ConverterType; |
|||
import org.thingsboard.server.common.data.debug.DebugSettings; |
|||
import org.thingsboard.server.common.data.integration.IntegrationType; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.awaitility.Awaitility.await; |
|||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|||
|
|||
/** |
|||
* An uplink through a typed (version 2) LoRaWAN converter and the default root rule chain: |
|||
* identifiers the network server sends as strings are saved as strings, decoder output is still cast. |
|||
*/ |
|||
@TestPropertySource(properties = { |
|||
"js.evaluator=local", |
|||
"service.integrations.supported=ALL" |
|||
}) |
|||
@DaoSqlTest |
|||
public class TypedConverterUplinkTypesTest extends AbstractIntegrationTest { |
|||
|
|||
private static final String EUI = "1000000000000001"; |
|||
private static final String DEV_ADDR = "01020304"; |
|||
|
|||
private static final String DECODER = """ |
|||
var data = JSON.parse(String.fromCharCode.apply(String, payload)); |
|||
return {attributes: {}, telemetry: {temperature: data.temperature}}; |
|||
"""; |
|||
|
|||
@After |
|||
public void afterTest() throws Exception { |
|||
if (integration != null) { |
|||
disableIntegration(); |
|||
removeIntegration(integration); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
protected JsonNode createIntegrationClientConfiguration() { |
|||
return JacksonUtil.newObjectNode(); |
|||
} |
|||
|
|||
@Test |
|||
public void givenChirpStackUplink_whenSaved_thenStringIdentifiersStayStrings() throws Exception { |
|||
loginTenantAdmin(); |
|||
createTypedConverter(); |
|||
createIntegration("Typed ChirpStack integration", IntegrationType.CHIRPSTACK); |
|||
enableIntegration(); |
|||
waitUntilIntegrationStarted(tenantId, integration.getId()); |
|||
|
|||
doPost("/api/v1/integrations/chirpstack/{routingKey}", chirpStackUplink(), integration.getRoutingKey()).andExpect(status().isOk()); |
|||
|
|||
String deviceName = "Device " + EUI; |
|||
await().atMost(TIMEOUT, TimeUnit.SECONDS).until(() -> |
|||
doGet("/api/tenant/devices?deviceName={name}", deviceName).andReturn().getResponse().getStatus() == 200); |
|||
Device device = doGet("/api/tenant/devices?deviceName={name}", Device.class, deviceName); |
|||
|
|||
String attributesUrl = "/api/plugins/telemetry/DEVICE/" + device.getUuidId() + "/values/attributes/CLIENT_SCOPE?keys=eui,devAddr"; |
|||
Map<String, JsonNode> attributes = new HashMap<>(); |
|||
await().atMost(TIMEOUT, TimeUnit.SECONDS).until(() -> { |
|||
attributes.clear(); |
|||
doGetAsync(attributesUrl, JsonNode.class).forEach(kv -> attributes.put(kv.get("key").asText(), kv.get("value"))); |
|||
return attributes.size() == 2; |
|||
}); |
|||
assertThat(attributes.get("eui").isTextual()).as("eui is a string: %s", attributes.get("eui")).isTrue(); |
|||
assertThat(attributes.get("eui").asText()).isEqualTo(EUI); |
|||
assertThat(attributes.get("devAddr").isTextual()).as("devAddr is a string: %s", attributes.get("devAddr")).isTrue(); |
|||
assertThat(attributes.get("devAddr").asText()).isEqualTo(DEV_ADDR); |
|||
|
|||
String telemetryUrl = "/api/plugins/telemetry/DEVICE/" + device.getUuidId() + "/values/timeseries?keys=fCnt,temperature&useStrictDataTypes=true"; |
|||
Map<String, JsonNode> telemetry = new HashMap<>(); |
|||
await().atMost(TIMEOUT, TimeUnit.SECONDS).until(() -> { |
|||
telemetry.clear(); |
|||
JsonNode latest = doGetAsync(telemetryUrl, JsonNode.class); |
|||
for (String key : List.of("fCnt", "temperature")) { |
|||
if (latest.has(key) && !latest.get(key).isEmpty()) { |
|||
telemetry.put(key, latest.get(key).get(0).get("value")); |
|||
} |
|||
} |
|||
return telemetry.size() == 2; |
|||
}); |
|||
assertThat(telemetry.get("fCnt").isIntegralNumber()).as("fCnt is a number: %s", telemetry.get("fCnt")).isTrue(); |
|||
assertThat(telemetry.get("fCnt").asLong()).isEqualTo(10L); |
|||
// The decoder returns "22.5" as a string; the usual type cast still applies to it.
|
|||
assertThat(telemetry.get("temperature").isNumber()).as("temperature is a number: %s", telemetry.get("temperature")).isTrue(); |
|||
assertThat(telemetry.get("temperature").asDouble()).isEqualTo(22.5); |
|||
} |
|||
|
|||
private void createTypedConverter() { |
|||
ObjectNode configuration = JacksonUtil.newObjectNode(); |
|||
configuration.put("type", "DEVICE"); |
|||
configuration.put("name", "Device $eui"); |
|||
configuration.put("profile", "default"); |
|||
configuration.set("attributes", JacksonUtil.toJsonNode("[\"eui\", \"devAddr\"]")); |
|||
configuration.set("telemetry", JacksonUtil.toJsonNode("[\"fCnt\"]")); |
|||
configuration.put("scriptLang", "JS"); |
|||
configuration.put("decoder", DECODER); |
|||
|
|||
Converter converter = new Converter(); |
|||
converter.setTenantId(tenantId); |
|||
converter.setName("Typed ChirpStack converter"); |
|||
converter.setType(ConverterType.UPLINK); |
|||
converter.setIntegrationType(IntegrationType.CHIRPSTACK); |
|||
converter.setConverterVersion(2); |
|||
converter.setConfiguration(configuration); |
|||
converter.setDebugSettings(DebugSettings.all()); |
|||
uplinkConverter = doPost("/api/converter", converter, Converter.class); |
|||
} |
|||
|
|||
private static JsonNode chirpStackUplink() { |
|||
return JacksonUtil.toJsonNode(""" |
|||
{ |
|||
"deduplicationId": "3ac7e3c4-4401-4b8d-9386-a5c902f9202d", |
|||
"time": "2026-11-17T09:34:15.775023242+00:00", |
|||
"deviceInfo": { |
|||
"tenantId": "52f14cd4-c6f1-4fbd-8f87-4025e1d49242", |
|||
"tenantName": "ChirpStack", |
|||
"applicationId": "17c82e96-be03-4f38-aef3-f83d48582d97", |
|||
"applicationName": "Test application", |
|||
"deviceProfileId": "14855bf7-d10d-4aee-b618-ebfcb64dc7ad", |
|||
"deviceProfileName": "default", |
|||
"deviceName": "Chirpstack", |
|||
"devEui": "%s" |
|||
}, |
|||
"devAddr": "%s", |
|||
"dr": 1, |
|||
"fCnt": 10, |
|||
"fPort": 80, |
|||
"object": {"temperature": "22.5"}, |
|||
"rxInfo": [{"uplinkId": 1, "rssi": -22, "snr": 10.5}], |
|||
"txInfo": {"frequency": 867100000, "modulation": {"lora": {"bandwidth": 125000, "spreadingFactor": 11, "codeRate": "CR_4_5"}}} |
|||
} |
|||
""".formatted(EUI, DEV_ADDR)); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,100 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.integration.api; |
|||
|
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.ArgumentCaptor; |
|||
import org.thingsboard.integration.api.data.UplinkData; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.integration.Integration; |
|||
import org.thingsboard.server.gen.integration.AssetUplinkDataProto; |
|||
import org.thingsboard.server.gen.integration.DeviceUplinkDataProto; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.PostAttributeMsg; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.PostTelemetryMsg; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.TsKvListProto; |
|||
|
|||
import java.util.Optional; |
|||
import java.util.Set; |
|||
import java.util.UUID; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.Mockito.mock; |
|||
import static org.mockito.Mockito.verify; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
public class AbstractIntegrationUplinkProtoTest { |
|||
|
|||
private static final PostTelemetryMsg TELEMETRY = PostTelemetryMsg.newBuilder().addTsKvList(TsKvListProto.newBuilder().setTs(1L)).build(); |
|||
private static final PostAttributeMsg ATTRIBUTES = PostAttributeMsg.newBuilder().build(); |
|||
|
|||
private IntegrationContext context; |
|||
private AbstractIntegration<String> integration; |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
context = mock(IntegrationContext.class); |
|||
when(context.getRateLimitService()).thenReturn(Optional.empty()); |
|||
Integration configuration = new Integration(); |
|||
configuration.setTenantId(TenantId.fromUUID(UUID.randomUUID())); |
|||
integration = new AbstractIntegration<>() { |
|||
@Override |
|||
public void process(String msg) { |
|||
} |
|||
|
|||
@Override |
|||
public void destroy() { |
|||
} |
|||
}; |
|||
integration.setConfiguration(configuration); |
|||
} |
|||
|
|||
@Test |
|||
public void testDeviceUplinkProtoCarriesTypeCastDisabledKeys() throws Exception { |
|||
integration.processUplinkData(context, UplinkData.builder() |
|||
.deviceName("d").deviceType("default") |
|||
.telemetry(TELEMETRY).attributesUpdate(ATTRIBUTES) |
|||
.telemetryTypeCastDisabledKeys(Set.of("devAddr")) |
|||
.attributesTypeCastDisabledKeys(Set.of("joinEui", "hardwareVersion")) |
|||
.build()); |
|||
|
|||
ArgumentCaptor<DeviceUplinkDataProto> captor = ArgumentCaptor.forClass(DeviceUplinkDataProto.class); |
|||
verify(context).processUplinkData(captor.capture(), any()); |
|||
DeviceUplinkDataProto proto = captor.getValue(); |
|||
assertThat(proto.getTelemetryTypeCastDisabledKeysList()).containsExactly("devAddr"); |
|||
assertThat(proto.getAttributesTypeCastDisabledKeysList()).containsExactlyInAnyOrder("joinEui", "hardwareVersion"); |
|||
assertThat(DeviceUplinkDataProto.parseFrom(proto.toByteArray())).isEqualTo(proto); |
|||
} |
|||
|
|||
@Test |
|||
public void testAssetUplinkProtoCarriesTypeCastDisabledKeys() throws Exception { |
|||
integration.processUplinkData(context, UplinkData.builder() |
|||
.isAsset(true).assetName("a").assetType("default") |
|||
.telemetry(TELEMETRY).attributesUpdate(ATTRIBUTES) |
|||
.telemetryTypeCastDisabledKeys(Set.of("devAddr")) |
|||
.attributesTypeCastDisabledKeys(Set.of("joinEui")) |
|||
.build()); |
|||
|
|||
ArgumentCaptor<AssetUplinkDataProto> captor = ArgumentCaptor.forClass(AssetUplinkDataProto.class); |
|||
verify(context).processUplinkData(captor.capture(), any()); |
|||
AssetUplinkDataProto proto = captor.getValue(); |
|||
assertThat(proto.getTelemetryTypeCastDisabledKeysList()).containsExactly("devAddr"); |
|||
assertThat(proto.getAttributesTypeCastDisabledKeysList()).containsExactly("joinEui"); |
|||
assertThat(AssetUplinkDataProto.parseFrom(proto.toByteArray())).isEqualTo(proto); |
|||
} |
|||
|
|||
@Test |
|||
public void testUplinkWithoutTypeCastDisabledKeysLeavesProtoFieldsEmpty() { |
|||
integration.processUplinkData(context, UplinkData.builder() |
|||
.deviceName("d").deviceType("default") |
|||
.telemetry(TELEMETRY).attributesUpdate(ATTRIBUTES) |
|||
.build()); |
|||
|
|||
ArgumentCaptor<DeviceUplinkDataProto> captor = ArgumentCaptor.forClass(DeviceUplinkDataProto.class); |
|||
verify(context).processUplinkData(captor.capture(), any()); |
|||
assertThat(captor.getValue().getTelemetryTypeCastDisabledKeysList()).isEmpty(); |
|||
assertThat(captor.getValue().getAttributesTypeCastDisabledKeysList()).isEmpty(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,219 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.integration.service.api; |
|||
|
|||
import com.google.common.util.concurrent.Futures; |
|||
import org.junit.jupiter.api.AfterEach; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.ArgumentCaptor; |
|||
import org.springframework.test.util.ReflectionTestUtils; |
|||
import org.thingsboard.integration.api.IntegrationCallback; |
|||
import org.thingsboard.server.common.data.AssetCacheInfo; |
|||
import org.thingsboard.server.common.data.AssetProfileCacheInfo; |
|||
import org.thingsboard.server.common.data.DeviceCacheInfo; |
|||
import org.thingsboard.server.common.data.DeviceProfileCacheInfo; |
|||
import org.thingsboard.server.common.data.id.AssetProfileId; |
|||
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|||
import org.thingsboard.server.common.data.id.IntegrationId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.integration.Integration; |
|||
import org.thingsboard.server.common.data.msg.TbMsgType; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; |
|||
import org.thingsboard.server.common.stats.MessagesStats; |
|||
import org.thingsboard.server.common.stats.StatsFactory; |
|||
import org.thingsboard.server.common.stats.TbApiUsageReportClient; |
|||
import org.thingsboard.server.gen.integration.AssetUplinkDataProto; |
|||
import org.thingsboard.server.gen.integration.DeviceUplinkDataProto; |
|||
import org.thingsboard.server.gen.integration.IntegrationApiRequestMsg; |
|||
import org.thingsboard.server.gen.integration.IntegrationApiResponseMsg; |
|||
import org.thingsboard.server.gen.integration.IntegrationInfoProto; |
|||
import org.thingsboard.server.gen.integration.ToCoreIntegrationMsg; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueType; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.PostAttributeMsg; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.PostTelemetryMsg; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.TsKvListProto; |
|||
import org.thingsboard.server.queue.TbQueueCallback; |
|||
import org.thingsboard.server.queue.TbQueueProducer; |
|||
import org.thingsboard.server.queue.TbQueueRequestTemplate; |
|||
import org.thingsboard.server.queue.common.TbProtoQueueMsg; |
|||
import org.thingsboard.server.queue.common.TbRuleEngineProducerService; |
|||
import org.thingsboard.server.queue.discovery.PartitionService; |
|||
import org.thingsboard.server.queue.provider.TbQueueProducerProvider; |
|||
import org.thingsboard.server.service.cache.IntegrationExecutorAssetCache; |
|||
import org.thingsboard.server.service.cache.IntegrationExecutorAssetProfileCache; |
|||
import org.thingsboard.server.service.cache.IntegrationExecutorDeviceCache; |
|||
import org.thingsboard.server.service.cache.IntegrationExecutorDeviceProfileCache; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
import java.util.UUID; |
|||
import java.util.function.Function; |
|||
import java.util.stream.Collectors; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.ArgumentMatchers.eq; |
|||
import static org.mockito.Mockito.doAnswer; |
|||
import static org.mockito.Mockito.mock; |
|||
import static org.mockito.Mockito.timeout; |
|||
import static org.mockito.Mockito.verify; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
public class DefaultIntegrationApiServiceTest { |
|||
|
|||
private static final long TIMEOUT_MS = 5000; |
|||
|
|||
private final TenantId tenantId = TenantId.fromUUID(UUID.randomUUID()); |
|||
private final Integration integration = new Integration(new IntegrationId(UUID.randomUUID())); |
|||
|
|||
private TbRuleEngineProducerService ruleEngineProducerService; |
|||
private IntegrationExecutorDeviceProfileCache deviceProfileCache; |
|||
private IntegrationExecutorAssetProfileCache assetProfileCache; |
|||
private IntegrationExecutorDeviceCache deviceCache; |
|||
private IntegrationExecutorAssetCache assetCache; |
|||
private DefaultIntegrationApiService service; |
|||
|
|||
@BeforeEach |
|||
@SuppressWarnings("unchecked") |
|||
public void setUp() { |
|||
integration.setTenantId(tenantId); |
|||
|
|||
StatsFactory statsFactory = mock(StatsFactory.class); |
|||
when(statsFactory.createMessagesStats(any())).thenReturn(mock(MessagesStats.class)); |
|||
PartitionService partitionService = mock(PartitionService.class); |
|||
when(partitionService.resolve(any(), any(TenantId.class), any())).thenReturn(new TopicPartitionInfo("tb_core", tenantId, 0, true)); |
|||
TbQueueProducerProvider producerProvider = mock(TbQueueProducerProvider.class); |
|||
TbQueueProducer<TbProtoQueueMsg<ToCoreIntegrationMsg>> coreProducer = mock(TbQueueProducer.class); |
|||
when(producerProvider.getTbCoreIntegrationMsgProducer()).thenReturn(coreProducer); |
|||
when(producerProvider.getRuleEngineMsgProducer()).thenReturn(mock(TbQueueProducer.class)); |
|||
|
|||
ruleEngineProducerService = mock(TbRuleEngineProducerService.class); |
|||
doAnswer(invocation -> { |
|||
TbQueueCallback callback = invocation.getArgument(3); |
|||
callback.onSuccess(null); |
|||
return null; |
|||
}).when(ruleEngineProducerService).sendToRuleEngine(any(), any(), any(), any()); |
|||
|
|||
deviceProfileCache = mock(IntegrationExecutorDeviceProfileCache.class); |
|||
assetProfileCache = mock(IntegrationExecutorAssetProfileCache.class); |
|||
deviceCache = mock(IntegrationExecutorDeviceCache.class); |
|||
assetCache = mock(IntegrationExecutorAssetCache.class); |
|||
|
|||
service = new DefaultIntegrationApiService(statsFactory, partitionService, producerProvider, ruleEngineProducerService, |
|||
mock(TbApiUsageReportClient.class), deviceProfileCache, assetProfileCache, deviceCache, assetCache, |
|||
(TbQueueRequestTemplate<TbProtoQueueMsg<IntegrationApiRequestMsg>, TbProtoQueueMsg<IntegrationApiResponseMsg>>) mock(TbQueueRequestTemplate.class)); |
|||
ReflectionTestUtils.setField(service, "maxCallbackThreads", 1); |
|||
service.init(); |
|||
} |
|||
|
|||
@AfterEach |
|||
public void tearDown() { |
|||
service.destroy(); |
|||
} |
|||
|
|||
@Test |
|||
public void deviceUplinkPassesTelemetryAndAttributeTypeCastDisabledKeysToTheirOwnMessages() { |
|||
DeviceProfileId deviceProfileId = new DeviceProfileId(UUID.randomUUID()); |
|||
when(deviceCache.getEntity(any(), any())).thenReturn(Futures.immediateFuture( |
|||
new DeviceCacheInfo(UUID.randomUUID(), tenantId, null, "Device A", "default", deviceProfileId))); |
|||
when(deviceProfileCache.getProfile(eq(deviceProfileId.getId()), any())).thenReturn(Futures.immediateFuture( |
|||
new DeviceProfileCacheInfo(deviceProfileId.getId(), tenantId, "default", null, "Main"))); |
|||
|
|||
DeviceUplinkDataProto data = DeviceUplinkDataProto.newBuilder() |
|||
.setDeviceName("Device A") |
|||
.setDeviceType("default") |
|||
.setPostTelemetryMsg(telemetry()) |
|||
.setPostAttributesMsg(attributes()) |
|||
.addAllTelemetryTypeCastDisabledKeys(List.of("devAddr")) |
|||
.addAllAttributesTypeCastDisabledKeys(List.of("joinEui", "firmwareVersion")) |
|||
.build(); |
|||
IntegrationCallback<Void> callback = mock(IntegrationCallback.class); |
|||
|
|||
service.sendUplinkData(integration, IntegrationInfoProto.getDefaultInstance(), data, callback); |
|||
|
|||
Map<TbMsgType, TbMsg> msgs = captureRuleEngineMsgs(); |
|||
assertThat(msgs.get(TbMsgType.POST_TELEMETRY_REQUEST).getTypeCastDisabledKeys()).isEqualTo(Set.of("devAddr")); |
|||
assertThat(msgs.get(TbMsgType.POST_ATTRIBUTES_REQUEST).getTypeCastDisabledKeys()).isEqualTo(Set.of("joinEui", "firmwareVersion")); |
|||
verify(callback, timeout(TIMEOUT_MS)).onSuccess(null); |
|||
} |
|||
|
|||
@Test |
|||
public void assetUplinkPassesTelemetryAndAttributeTypeCastDisabledKeysToTheirOwnMessages() { |
|||
AssetProfileId assetProfileId = new AssetProfileId(UUID.randomUUID()); |
|||
when(assetCache.getEntity(any(), any())).thenReturn(Futures.immediateFuture( |
|||
new AssetCacheInfo(UUID.randomUUID(), tenantId, null, "Asset A", "default", assetProfileId))); |
|||
when(assetProfileCache.getProfile(eq(assetProfileId.getId()), any())).thenReturn(Futures.immediateFuture( |
|||
new AssetProfileCacheInfo(assetProfileId.getId(), tenantId, "default", null, "Main"))); |
|||
|
|||
AssetUplinkDataProto data = AssetUplinkDataProto.newBuilder() |
|||
.setAssetName("Asset A") |
|||
.setAssetType("default") |
|||
.setPostTelemetryMsg(telemetry()) |
|||
.setPostAttributesMsg(attributes()) |
|||
.addAllTelemetryTypeCastDisabledKeys(List.of("devAddr")) |
|||
.addAllAttributesTypeCastDisabledKeys(List.of("joinEui", "firmwareVersion")) |
|||
.build(); |
|||
IntegrationCallback<Void> callback = mock(IntegrationCallback.class); |
|||
|
|||
service.sendUplinkData(integration, IntegrationInfoProto.getDefaultInstance(), data, callback); |
|||
|
|||
Map<TbMsgType, TbMsg> msgs = captureRuleEngineMsgs(); |
|||
assertThat(msgs.get(TbMsgType.POST_TELEMETRY_REQUEST).getTypeCastDisabledKeys()).isEqualTo(Set.of("devAddr")); |
|||
assertThat(msgs.get(TbMsgType.POST_ATTRIBUTES_REQUEST).getTypeCastDisabledKeys()).isEqualTo(Set.of("joinEui", "firmwareVersion")); |
|||
verify(callback, timeout(TIMEOUT_MS)).onSuccess(null); |
|||
} |
|||
|
|||
@Test |
|||
public void uplinkWithoutTypeCastDisabledKeysAddsNoMetadata() { |
|||
DeviceProfileId deviceProfileId = new DeviceProfileId(UUID.randomUUID()); |
|||
when(deviceCache.getEntity(any(), any())).thenReturn(Futures.immediateFuture( |
|||
new DeviceCacheInfo(UUID.randomUUID(), tenantId, null, "Device B", "default", deviceProfileId))); |
|||
when(deviceProfileCache.getProfile(eq(deviceProfileId.getId()), any())).thenReturn(Futures.immediateFuture( |
|||
new DeviceProfileCacheInfo(deviceProfileId.getId(), tenantId, "default", null, "Main"))); |
|||
|
|||
DeviceUplinkDataProto data = DeviceUplinkDataProto.newBuilder() |
|||
.setDeviceName("Device B") |
|||
.setDeviceType("default") |
|||
.setPostTelemetryMsg(telemetry()) |
|||
.setPostAttributesMsg(attributes()) |
|||
.build(); |
|||
|
|||
service.sendUplinkData(integration, IntegrationInfoProto.getDefaultInstance(), data, mock(IntegrationCallback.class)); |
|||
|
|||
Map<TbMsgType, TbMsg> msgs = captureRuleEngineMsgs(); |
|||
assertThat(msgs.get(TbMsgType.POST_TELEMETRY_REQUEST).getMetaData().getValue(TbMsgMetaData.TYPE_CAST_DISABLED_KEYS)).isNull(); |
|||
assertThat(msgs.get(TbMsgType.POST_ATTRIBUTES_REQUEST).getMetaData().getValue(TbMsgMetaData.TYPE_CAST_DISABLED_KEYS)).isNull(); |
|||
} |
|||
|
|||
private Map<TbMsgType, TbMsg> captureRuleEngineMsgs() { |
|||
ArgumentCaptor<TbMsg> captor = ArgumentCaptor.forClass(TbMsg.class); |
|||
verify(ruleEngineProducerService, timeout(TIMEOUT_MS).times(2)).sendToRuleEngine(any(), eq(tenantId), captor.capture(), any()); |
|||
return captor.getAllValues().stream().collect(Collectors.toMap(TbMsg::getInternalType, Function.identity())); |
|||
} |
|||
|
|||
private static PostTelemetryMsg telemetry() { |
|||
return PostTelemetryMsg.newBuilder() |
|||
.addTsKvList(TsKvListProto.newBuilder() |
|||
.setTs(1000L) |
|||
.addKv(stringKv("devAddr", "01020304"))) |
|||
.build(); |
|||
} |
|||
|
|||
private static PostAttributeMsg attributes() { |
|||
return PostAttributeMsg.newBuilder() |
|||
.addKv(stringKv("joinEui", "0000000000000000")) |
|||
.addKv(stringKv("firmwareVersion", "1.0")) |
|||
.build(); |
|||
} |
|||
|
|||
private static KeyValueProto stringKv(String key, String value) { |
|||
return KeyValueProto.newBuilder().setKey(key).setType(KeyValueType.STRING_V).setStringV(value).build(); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue