Browse Source

test(attributes): black-box tests for unified gateway/device request

Gateway: sharedKeys empty -> scope-separated {shared} response.
Device: sharedKeys empty -> all shared, no client. Legacy value/values test kept.
pull/15865/head
dshvaika 3 months ago
parent
commit
ce95bdc322
  1. 27
      msa/black-box-tests/src/test/java/org/thingsboard/server/msa/connectivity/MqttClientTest.java
  2. 25
      msa/black-box-tests/src/test/java/org/thingsboard/server/msa/connectivity/MqttGatewayClientTest.java

27
msa/black-box-tests/src/test/java/org/thingsboard/server/msa/connectivity/MqttClientTest.java

@ -234,6 +234,33 @@ public class MqttClientTest extends AbstractContainerTest {
assertThat(attributes.getShared().get("sharedAttr")).isEqualTo(sharedAttributeValue); assertThat(attributes.getShared().get("sharedAttr")).isEqualTo(sharedAttributeValue);
} }
@Test
public void requestAllSharedAttributesFromServerViaEmptyValue() throws Exception {
DeviceCredentials deviceCredentials = testRestClient.getDeviceCredentialsByDeviceId(device.getId());
MqttMessageListener listener = new MqttMessageListener();
MqttClient mqttClient = getMqttClient(deviceCredentials, listener);
// Add a new shared attribute
JsonObject sharedAttributes = new JsonObject();
String sharedAttributeValue = StringUtils.randomAlphanumeric(8);
sharedAttributes.addProperty("sharedAttr", sharedAttributeValue);
testRestClient.postTelemetryAttribute(device.getId(), SHARED_SCOPE, mapper.readTree(sharedAttributes.toString()));
mqttClient.on("v1/devices/me/attributes/response/+", listener, MqttQoS.AT_LEAST_ONCE).get();
TimeUnit.SECONDS.sleep(3 * timeoutMultiplier);
// empty sharedKeys => all shared; clientKeys absent => no client
JsonObject request = new JsonObject();
request.addProperty("sharedKeys", "");
mqttClient.publish("v1/devices/me/attributes/request/" + new Random().nextInt(100), Unpooled.wrappedBuffer(request.toString().getBytes())).get();
MqttEvent event = listener.getEvents().poll(10 * timeoutMultiplier, TimeUnit.SECONDS);
AttributesResponse attributes = mapper.readValue(Objects.requireNonNull(event).getMessage(), AttributesResponse.class);
assertThat(attributes.getShared()).hasSize(1);
assertThat(attributes.getShared().get("sharedAttr")).isEqualTo(sharedAttributeValue);
assertThat(attributes.getClient()).isNullOrEmpty();
}
@Test @Test
public void subscribeToAttributeUpdatesFromServer() throws Exception { public void subscribeToAttributeUpdatesFromServer() throws Exception {
DeviceCredentials deviceCredentials = testRestClient.getDeviceCredentialsByDeviceId(device.getId()); DeviceCredentials deviceCredentials = testRestClient.getDeviceCredentialsByDeviceId(device.getId());

25
msa/black-box-tests/src/test/java/org/thingsboard/server/msa/connectivity/MqttGatewayClientTest.java

@ -235,6 +235,31 @@ public class MqttGatewayClientTest extends AbstractContainerTest {
assertThat(responseData.get("values").getAsJsonObject().entrySet()).hasSize(1); assertThat(responseData.get("values").getAsJsonObject().entrySet()).hasSize(1);
} }
@Test
public void gatewayRequestAllSharedReturnsSeparatedResponse() throws Exception {
JsonObject sharedAttributes = new JsonObject();
sharedAttributes.addProperty("attr1", "value1");
sharedAttributes.addProperty("attr2", true);
testRestClient.postTelemetryAttribute(createdDevice.getId(), SHARED_SCOPE, mapper.readTree(sharedAttributes.toString()));
mqttClient.on("v1/gateway/attributes/response", listener, MqttQoS.AT_LEAST_ONCE).get();
// new format: empty sharedKeys => all shared; scope-separated response
JsonObject requestData = new JsonObject();
requestData.addProperty("id", 1);
requestData.addProperty("device", createdDevice.getName());
requestData.addProperty("sharedKeys", "");
mqttClient.publish("v1/gateway/attributes/request", Unpooled.wrappedBuffer(requestData.toString().getBytes())).get();
MqttEvent event = listener.getEvents().poll(10 * timeoutMultiplier, TimeUnit.SECONDS);
JsonObject responseData = JsonParser.parseString(Objects.requireNonNull(event).getMessage()).getAsJsonObject();
assertThat(responseData.has("shared")).isTrue();
assertThat(responseData.has("value")).isFalse();
assertThat(responseData.has("values")).isFalse();
assertThat(responseData.getAsJsonObject("shared").get("attr1").getAsString()).isEqualTo("value1");
assertThat(responseData.getAsJsonObject("shared").get("attr2").getAsBoolean()).isTrue();
}
@Test @Test
public void requestAttributeValuesFromServer() throws Exception { public void requestAttributeValuesFromServer() throws Exception {
WsClient wsClient = subscribeToWebSocket(createdDevice.getId(), "CLIENT_SCOPE", CmdsType.ATTR_SUB_CMDS); WsClient wsClient = subscribeToWebSocket(createdDevice.getId(), "CLIENT_SCOPE", CmdsType.ATTR_SUB_CMDS);

Loading…
Cancel
Save