committed by
GitHub
19 changed files with 797 additions and 139 deletions
@ -0,0 +1,124 @@ |
|||
/** |
|||
* Copyright © 2016-2026 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.adaptor; |
|||
|
|||
import com.google.gson.JsonNull; |
|||
import com.google.gson.JsonObject; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeRequestMsg; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.concurrent.atomic.AtomicBoolean; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
import static org.junit.jupiter.api.Assertions.assertFalse; |
|||
import static org.junit.jupiter.api.Assertions.assertTrue; |
|||
|
|||
public class JsonConverterAttributeRequestTest { |
|||
|
|||
private final List<String> names = new ArrayList<>(); |
|||
private final AtomicBoolean allInvoked = new AtomicBoolean(false); |
|||
|
|||
private void parseClientKeys(JsonObject json) { |
|||
JsonConverter.parseAttributeScope(json, "clientKeys", () -> allInvoked.set(true), names::addAll); |
|||
} |
|||
|
|||
@Test |
|||
public void fieldAbsent_doesNothing() { |
|||
parseClientKeys(new JsonObject()); |
|||
assertTrue(names.isEmpty()); |
|||
assertFalse(allInvoked.get()); |
|||
} |
|||
|
|||
@Test |
|||
public void fieldNull_doesNothing() { |
|||
JsonObject json = new JsonObject(); |
|||
json.add("clientKeys", JsonNull.INSTANCE); |
|||
parseClientKeys(json); |
|||
assertTrue(names.isEmpty()); |
|||
assertFalse(allInvoked.get()); |
|||
} |
|||
|
|||
@Test |
|||
public void emptyString_setsAll() { |
|||
JsonObject json = new JsonObject(); |
|||
json.addProperty("clientKeys", ""); |
|||
parseClientKeys(json); |
|||
assertTrue(allInvoked.get()); |
|||
assertTrue(names.isEmpty()); |
|||
} |
|||
|
|||
@Test |
|||
public void blankString_setsAll() { |
|||
JsonObject json = new JsonObject(); |
|||
json.addProperty("clientKeys", " "); |
|||
parseClientKeys(json); |
|||
assertTrue(allInvoked.get()); |
|||
assertTrue(names.isEmpty()); |
|||
} |
|||
|
|||
@Test |
|||
public void commaSeparatedString_setsNames() { |
|||
JsonObject json = new JsonObject(); |
|||
json.addProperty("clientKeys", "a,b,c"); |
|||
parseClientKeys(json); |
|||
assertFalse(allInvoked.get()); |
|||
assertEquals(List.of("a", "b", "c"), names); |
|||
} |
|||
|
|||
@Test |
|||
public void applyClientScope_all_winsOverNames() { |
|||
GetAttributeRequestMsg.Builder b = GetAttributeRequestMsg.newBuilder(); |
|||
JsonConverter.applyClientScope(b, true, List.of("ignored")); |
|||
assertThat(b.getAllClientAttributes()).isTrue(); |
|||
assertThat(b.getClientAttributeNamesList()).isEmpty(); |
|||
} |
|||
|
|||
@Test |
|||
public void applyClientScope_names_addsNames() { |
|||
GetAttributeRequestMsg.Builder b = GetAttributeRequestMsg.newBuilder(); |
|||
JsonConverter.applyClientScope(b, false, List.of("a", "b")); |
|||
assertThat(b.getAllClientAttributes()).isFalse(); |
|||
assertThat(b.getClientAttributeNamesList()).containsExactly("a", "b"); |
|||
} |
|||
|
|||
@Test |
|||
public void applyClientScope_nullOrEmpty_setsNothing() { |
|||
GetAttributeRequestMsg.Builder b = GetAttributeRequestMsg.newBuilder(); |
|||
JsonConverter.applyClientScope(b, false, null); |
|||
JsonConverter.applyClientScope(b, false, List.of()); |
|||
assertThat(b.getAllClientAttributes()).isFalse(); |
|||
assertThat(b.getClientAttributeNamesList()).isEmpty(); |
|||
} |
|||
|
|||
@Test |
|||
public void applySharedScope_all_winsOverNames() { |
|||
GetAttributeRequestMsg.Builder b = GetAttributeRequestMsg.newBuilder(); |
|||
JsonConverter.applySharedScope(b, true, null); |
|||
assertThat(b.getAllSharedAttributes()).isTrue(); |
|||
assertThat(b.getSharedAttributeNamesList()).isEmpty(); |
|||
} |
|||
|
|||
@Test |
|||
public void applySharedScope_names_addsNames() { |
|||
GetAttributeRequestMsg.Builder b = GetAttributeRequestMsg.newBuilder(); |
|||
JsonConverter.applySharedScope(b, false, List.of("s1")); |
|||
assertThat(b.getAllSharedAttributes()).isFalse(); |
|||
assertThat(b.getSharedAttributeNamesList()).containsExactly("s1"); |
|||
} |
|||
} |
|||
@ -0,0 +1,74 @@ |
|||
/** |
|||
* Copyright © 2016-2026 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.adaptor; |
|||
|
|||
import com.google.gson.JsonObject; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueType; |
|||
import org.thingsboard.server.gen.transport.TransportProtos.TsKvProto; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
|
|||
public class JsonConverterGatewayResponseTest { |
|||
|
|||
private TsKvProto kv(String key, long v) { |
|||
return TsKvProto.newBuilder().setTs(1L).setKv( |
|||
KeyValueProto.newBuilder().setKey(key).setType(KeyValueType.LONG_V).setLongV(v).build()).build(); |
|||
} |
|||
|
|||
@Test |
|||
public void separateScopes_keepsScopeLabels_keyNames_andOverlap() { |
|||
GetAttributeResponseMsg msg = GetAttributeResponseMsg.newBuilder() |
|||
.setRequestId(1) |
|||
.setSeparateScopesResponse(true) |
|||
.addClientAttributeList(kv("test", 27)) |
|||
.addSharedAttributeList(kv("test", 99)) |
|||
.build(); |
|||
|
|||
JsonObject out = JsonConverter.getJsonObjectForGateway("DeviceA", msg); |
|||
|
|||
assertThat(out.get("id").getAsInt()).isEqualTo(1); |
|||
assertThat(out.get("device").getAsString()).isEqualTo("DeviceA"); |
|||
assertThat(out.getAsJsonObject("client").get("test").getAsLong()).isEqualTo(27); |
|||
assertThat(out.getAsJsonObject("shared").get("test").getAsLong()).isEqualTo(99); |
|||
assertThat(out.has("value")).isFalse(); |
|||
assertThat(out.has("values")).isFalse(); |
|||
} |
|||
|
|||
@Test |
|||
public void separateScopes_omitsEmptyScope() { |
|||
GetAttributeResponseMsg msg = GetAttributeResponseMsg.newBuilder() |
|||
.setRequestId(2).setSeparateScopesResponse(true) |
|||
.addSharedAttributeList(kv("s", 1)).build(); |
|||
JsonObject out = JsonConverter.getJsonObjectForGateway("DeviceA", msg); |
|||
assertThat(out.has("client")).isFalse(); |
|||
assertThat(out.getAsJsonObject("shared").get("s").getAsLong()).isEqualTo(1); |
|||
} |
|||
|
|||
@Test |
|||
@SuppressWarnings("deprecation") |
|||
public void legacy_singleValue_unchanged() { |
|||
GetAttributeResponseMsg msg = GetAttributeResponseMsg.newBuilder() |
|||
.setRequestId(3).setSeparateScopesResponse(false) |
|||
.setIsMultipleAttributesRequest(false) |
|||
.addSharedAttributeList(kv("s", 5)).build(); |
|||
JsonObject out = JsonConverter.getJsonObjectForGateway("DeviceA", msg); |
|||
assertThat(out.get("value").getAsLong()).isEqualTo(5); |
|||
assertThat(out.has("shared")).isFalse(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue