add corresponding test
This commit is contained in:
parent
2f5648c400
commit
443bb2280f
@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.cassandra.cql3.Json;
|
import org.apache.cassandra.cql3.Json;
|
||||||
import org.apache.commons.lang3.RandomStringUtils;
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
@ -69,6 +70,7 @@ public abstract class AbstractContainerTest {
|
|||||||
protected static String TB_TOKEN;
|
protected static String TB_TOKEN;
|
||||||
protected static RestClient restClient;
|
protected static RestClient restClient;
|
||||||
protected ObjectMapper mapper = new ObjectMapper();
|
protected ObjectMapper mapper = new ObjectMapper();
|
||||||
|
protected JsonParser jsonParser = new JsonParser();
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void before() throws Exception {
|
public static void before() throws Exception {
|
||||||
|
|||||||
@ -20,6 +20,7 @@ import com.google.common.collect.Sets;
|
|||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
import com.google.common.util.concurrent.ListeningExecutorService;
|
import com.google.common.util.concurrent.ListeningExecutorService;
|
||||||
import com.google.common.util.concurrent.MoreExecutors;
|
import com.google.common.util.concurrent.MoreExecutors;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
@ -151,6 +152,76 @@ public class MqttGatewayClientTest extends AbstractContainerTest {
|
|||||||
Assert.assertTrue(verify(actualLatestTelemetry, "attr4", Long.toString(73)));
|
Assert.assertTrue(verify(actualLatestTelemetry, "attr4", Long.toString(73)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void responseDataOnAttributesRequestCheck() throws Exception {
|
||||||
|
Optional<DeviceCredentials> createdDeviceCredentials = restClient.getDeviceCredentialsByDeviceId(createdDevice.getId());
|
||||||
|
Assert.assertTrue(createdDeviceCredentials.isPresent());
|
||||||
|
WsClient wsClient = subscribeToWebSocket(createdDevice.getId(), "CLIENT_SCOPE", CmdsType.ATTR_SUB_CMDS);
|
||||||
|
JsonObject sharedAttributes = new JsonObject();
|
||||||
|
sharedAttributes.addProperty("attr1", "value1");
|
||||||
|
sharedAttributes.addProperty("attr2", true);
|
||||||
|
sharedAttributes.addProperty("attr3", 42.0);
|
||||||
|
sharedAttributes.addProperty("attr4", 73);
|
||||||
|
|
||||||
|
mqttClient.on("v1/gateway/attributes/response", listener, MqttQoS.AT_LEAST_ONCE).get();
|
||||||
|
ResponseEntity sharedAttributesResponse = restClient.getRestTemplate()
|
||||||
|
.postForEntity(HTTPS_URL + "/api/plugins/telemetry/DEVICE/{deviceId}/SHARED_SCOPE",
|
||||||
|
mapper.readTree(sharedAttributes.toString()), ResponseEntity.class,
|
||||||
|
createdDevice.getId());
|
||||||
|
Assert.assertTrue(sharedAttributesResponse.getStatusCode().is2xxSuccessful());
|
||||||
|
var event = listener.getEvents().poll(10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
JsonObject requestData = new JsonObject();
|
||||||
|
requestData.addProperty("id", 1);
|
||||||
|
requestData.addProperty("device", createdDevice.getName());
|
||||||
|
requestData.addProperty("client", false);
|
||||||
|
requestData.addProperty("key", "attr1");
|
||||||
|
|
||||||
|
mqttClient.on("v1/gateway/attributes/response", listener, MqttQoS.AT_LEAST_ONCE).get();
|
||||||
|
mqttClient.publish("v1/gateway/attributes/request", Unpooled.wrappedBuffer(requestData.toString().getBytes())).get();
|
||||||
|
event = listener.getEvents().poll(10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
JsonObject responseData = jsonParser.parse(Objects.requireNonNull(event).getMessage()).getAsJsonObject();
|
||||||
|
Assert.assertTrue(responseData.has("value"));
|
||||||
|
Assert.assertEquals(sharedAttributes.get("attr1").getAsString(), responseData.get("value").getAsString());
|
||||||
|
|
||||||
|
requestData = new JsonObject();
|
||||||
|
requestData.addProperty("id", 1);
|
||||||
|
requestData.addProperty("device", createdDevice.getName());
|
||||||
|
requestData.addProperty("client", false);
|
||||||
|
JsonArray keys = new JsonArray();
|
||||||
|
keys.add("attr1");
|
||||||
|
keys.add("attr2");
|
||||||
|
requestData.add("keys", keys);
|
||||||
|
|
||||||
|
mqttClient.on("v1/gateway/attributes/response", listener, MqttQoS.AT_LEAST_ONCE).get();
|
||||||
|
mqttClient.publish("v1/gateway/attributes/request", Unpooled.wrappedBuffer(requestData.toString().getBytes())).get();
|
||||||
|
event = listener.getEvents().poll(10, TimeUnit.SECONDS);
|
||||||
|
responseData = jsonParser.parse(Objects.requireNonNull(event).getMessage()).getAsJsonObject();
|
||||||
|
|
||||||
|
Assert.assertTrue(responseData.has("values"));
|
||||||
|
Assert.assertEquals(sharedAttributes.get("attr1").getAsString(), responseData.get("values").getAsJsonObject().get("attr1").getAsString());
|
||||||
|
Assert.assertEquals(sharedAttributes.get("attr2").getAsString(), responseData.get("values").getAsJsonObject().get("attr2").getAsString());
|
||||||
|
|
||||||
|
requestData = new JsonObject();
|
||||||
|
requestData.addProperty("id", 1);
|
||||||
|
requestData.addProperty("device", createdDevice.getName());
|
||||||
|
requestData.addProperty("client", false);
|
||||||
|
keys = new JsonArray();
|
||||||
|
keys.add("attr1");
|
||||||
|
keys.add("undefined");
|
||||||
|
requestData.add("keys", keys);
|
||||||
|
|
||||||
|
mqttClient.on("v1/gateway/attributes/response", listener, MqttQoS.AT_LEAST_ONCE).get();
|
||||||
|
mqttClient.publish("v1/gateway/attributes/request", Unpooled.wrappedBuffer(requestData.toString().getBytes())).get();
|
||||||
|
event = listener.getEvents().poll(10, TimeUnit.SECONDS);
|
||||||
|
responseData = jsonParser.parse(Objects.requireNonNull(event).getMessage()).getAsJsonObject();
|
||||||
|
|
||||||
|
Assert.assertTrue(responseData.has("values"));
|
||||||
|
Assert.assertEquals(sharedAttributes.get("attr1").getAsString(), responseData.get("values").getAsJsonObject().get("attr1").getAsString());
|
||||||
|
Assert.assertEquals(1, responseData.get("values").getAsJsonObject().entrySet().size());
|
||||||
|
}
|
||||||
|
|
||||||
@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…
x
Reference in New Issue
Block a user