15 changed files with 137 additions and 171 deletions
@ -1,65 +0,0 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.common.adaptor; |
|||
|
|||
import com.fasterxml.jackson.core.type.TypeReference; |
|||
import org.apache.commons.lang3.math.NumberUtils; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.thingsboard.server.common.data.kv.DataType; |
|||
import org.thingsboard.server.common.data.kv.KvEntry; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
|
|||
import java.util.Collection; |
|||
import java.util.Collections; |
|||
import java.util.Set; |
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* Carries the keys whose numeric-looking string values must stay strings, from an integration uplink |
|||
* or an attributes updated notification, to the rule nodes that parse the message data again with {@link JsonConverter}. |
|||
*/ |
|||
public final class TypeCastDisabledKeys { |
|||
|
|||
public static final String METADATA_KEY = "typeCastDisabledKeys"; |
|||
|
|||
private static final TypeReference<Set<String>> SET_TYPE = new TypeReference<>() {}; |
|||
|
|||
private TypeCastDisabledKeys() { |
|||
} |
|||
|
|||
// A JSON array, since keys may contain commas.
|
|||
public static void put(TbMsgMetaData metaData, Collection<String> keys) { |
|||
if (keys != null && !keys.isEmpty()) { |
|||
metaData.putValue(METADATA_KEY, JacksonUtil.toString(keys)); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Marks the string entries that {@link JsonConverter} would cast to a number, so that a message |
|||
* built from typed entries, such as an attributes updated notification, reads back the same types. |
|||
*/ |
|||
public static void putNumericStrings(TbMsgMetaData metaData, Collection<? extends KvEntry> entries) { |
|||
if (entries == null) { |
|||
return; |
|||
} |
|||
put(metaData, entries.stream() |
|||
.filter(entry -> entry.getDataType() == DataType.STRING && NumberUtils.isParsable(entry.getValueAsString())) |
|||
.map(KvEntry::getKey) |
|||
.collect(Collectors.toList())); |
|||
} |
|||
|
|||
static Set<String> get(TbMsgMetaData metaData) { |
|||
String value = metaData.getValue(METADATA_KEY); |
|||
if (StringUtils.isEmpty(value)) { |
|||
return Collections.emptySet(); |
|||
} |
|||
try { |
|||
Set<String> keys = JacksonUtil.fromString(value, SET_TYPE); |
|||
return keys != null ? keys : Collections.emptySet(); |
|||
} catch (IllegalArgumentException e) { |
|||
return Collections.emptySet(); |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -1,67 +0,0 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.common.adaptor; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; |
|||
import org.thingsboard.server.common.data.kv.DoubleDataEntry; |
|||
import org.thingsboard.server.common.data.kv.LongDataEntry; |
|||
import org.thingsboard.server.common.data.kv.StringDataEntry; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
|
|||
import java.util.List; |
|||
import java.util.Set; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
|
|||
public class TypeCastDisabledKeysTest { |
|||
|
|||
@Test |
|||
public void testPutAndGetRoundTripKeysWithCommas() { |
|||
TbMsgMetaData metaData = new TbMsgMetaData(); |
|||
TypeCastDisabledKeys.put(metaData, List.of("a,b", "c\"d")); |
|||
assertThat(TypeCastDisabledKeys.get(metaData)).containsExactlyInAnyOrder("a,b", "c\"d"); |
|||
} |
|||
|
|||
@Test |
|||
public void testPutSkipsEmptySet() { |
|||
TbMsgMetaData metaData = new TbMsgMetaData(); |
|||
TypeCastDisabledKeys.put(metaData, Set.of()); |
|||
TypeCastDisabledKeys.put(metaData, null); |
|||
assertThat(metaData.getValue(TypeCastDisabledKeys.METADATA_KEY)).isNull(); |
|||
assertThat(TypeCastDisabledKeys.get(metaData)).isEmpty(); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetReturnsEmptySetForInvalidValue() { |
|||
TbMsgMetaData metaData = new TbMsgMetaData(); |
|||
metaData.putValue(TypeCastDisabledKeys.METADATA_KEY, "joinEui,devAddr"); |
|||
assertThat(TypeCastDisabledKeys.get(metaData)).isEmpty(); |
|||
metaData.putValue(TypeCastDisabledKeys.METADATA_KEY, "{\"a\":1}"); |
|||
assertThat(TypeCastDisabledKeys.get(metaData)).isEmpty(); |
|||
assertThat(TypeCastDisabledKeys.get(new TbMsgMetaData())).isEqualTo(Set.of()); |
|||
} |
|||
|
|||
@Test |
|||
public void testPutNumericStringsMarksOnlyStringsThatWouldBeCast() { |
|||
TbMsgMetaData metaData = new TbMsgMetaData(); |
|||
TypeCastDisabledKeys.putNumericStrings(metaData, List.of( |
|||
new BaseAttributeKvEntry(new StringDataEntry("joinEui", "0000000000000000"), 0L), |
|||
new BaseAttributeKvEntry(new StringDataEntry("firmwareVersion", "1.10"), 0L), |
|||
new BaseAttributeKvEntry(new StringDataEntry("devEui", "70B3D57ED0012345"), 0L), |
|||
new BaseAttributeKvEntry(new LongDataEntry("fCnt", 42L), 0L), |
|||
new BaseAttributeKvEntry(new DoubleDataEntry("rssi", -97.5), 0L))); |
|||
assertThat(TypeCastDisabledKeys.get(metaData)).containsExactlyInAnyOrder("joinEui", "firmwareVersion"); |
|||
} |
|||
|
|||
@Test |
|||
public void testPutNumericStringsSkipsEntriesWithoutNumericStrings() { |
|||
TbMsgMetaData metaData = new TbMsgMetaData(); |
|||
TypeCastDisabledKeys.putNumericStrings(metaData, List.of( |
|||
new BaseAttributeKvEntry(new StringDataEntry("devEui", "70B3D57ED0012345"), 0L), |
|||
new BaseAttributeKvEntry(new LongDataEntry("fCnt", 42L), 0L))); |
|||
TypeCastDisabledKeys.putNumericStrings(metaData, null); |
|||
assertThat(metaData.getValue(TypeCastDisabledKeys.METADATA_KEY)).isNull(); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue