Commit 7a8cc68fcc97f98836db096f45209ca270977a1d

Authored by Andrew Shvayka
Committed by GitHub
2 parents 23531e80 69a51ea3

Merge pull request #21 from thingsboard/RPC-TESTS

Server-side Two-way MQTT RPC Test
  1 +/**
  2 + * Copyright © 2016 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.mqtt.rpc;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.eclipse.paho.client.mqttv3.*;
  20 +import org.junit.Assert;
  21 +import org.junit.Before;
  22 +import org.junit.Test;
  23 +import org.thingsboard.client.tools.RestClient;
  24 +import org.thingsboard.server.common.data.Device;
  25 +import org.thingsboard.server.common.data.security.DeviceCredentials;
  26 +import org.thingsboard.server.mqtt.AbstractFeatureIntegrationTest;
  27 +
  28 +import static org.junit.Assert.assertEquals;
  29 +import static org.junit.Assert.assertNotNull;
  30 +
  31 +/**
  32 + * @author Valerii Sosliuk
  33 + */
  34 +@Slf4j
  35 +public class MqttServerSideRpcIntegrationTest extends AbstractFeatureIntegrationTest {
  36 +
  37 + private static final String MQTT_URL = "tcp://localhost:1883";
  38 + private static final String BASE_URL = "http://localhost:8080";
  39 +
  40 + private static final String USERNAME = "tenant@thingsboard.org";
  41 + private static final String PASSWORD = "tenant";
  42 +
  43 + private Device savedDevice;
  44 +
  45 + private String accessToken;
  46 + private RestClient restClient;
  47 +
  48 + @Before
  49 + public void beforeTest() throws Exception {
  50 + restClient = new RestClient(BASE_URL);
  51 + restClient.login(USERNAME, PASSWORD);
  52 +
  53 + Device device = new Device();
  54 + device.setName("Test Server-Side RPC Device");
  55 + savedDevice = restClient.getRestTemplate().postForEntity(BASE_URL + "/api/device", device, Device.class).getBody();
  56 + DeviceCredentials deviceCredentials =
  57 + restClient.getRestTemplate().getForEntity(BASE_URL + "/api/device/" + savedDevice.getId().getId().toString() + "/credentials", DeviceCredentials.class).getBody();
  58 + assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
  59 + accessToken = deviceCredentials.getCredentialsId();
  60 + assertNotNull(accessToken);
  61 + }
  62 +
  63 + @Test
  64 + public void testServerMqttTwoWayRpc() throws Exception {
  65 + String clientId = MqttAsyncClient.generateClientId();
  66 + MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, clientId);
  67 +
  68 + MqttConnectOptions options = new MqttConnectOptions();
  69 + options.setUserName(accessToken);
  70 + client.connect(options);
  71 + Thread.sleep(3000);
  72 + client.subscribe("v1/devices/me/rpc/request/+",1);
  73 + client.setCallback(new TestMqttCallback(client));
  74 +
  75 + String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
  76 + String deviceId = savedDevice.getId().getId().toString();
  77 + String result = restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/twoway/" + deviceId, setGpioRequest, String.class).getBody();
  78 + log.info("Result: " + result);
  79 + Assert.assertEquals("{\"value1\":\"A\",\"value2\":\"B\"}", result);
  80 + }
  81 +
  82 + private static class TestMqttCallback implements MqttCallback {
  83 +
  84 + private final MqttAsyncClient client;
  85 +
  86 + TestMqttCallback(MqttAsyncClient client) {
  87 + this.client = client;
  88 + }
  89 +
  90 + @Override
  91 + public void connectionLost(Throwable throwable) {
  92 + }
  93 +
  94 + @Override
  95 + public void messageArrived(String requestTopic, MqttMessage mqttMessage) throws Exception {
  96 + log.info("Message Arrived: " + mqttMessage.getPayload().toString());
  97 + MqttMessage message = new MqttMessage();
  98 + String responseTopic = requestTopic.replace("request", "response");
  99 + message.setPayload("{\"value1\":\"A\", \"value2\":\"B\"}".getBytes());
  100 + client.publish(responseTopic, message);
  101 + }
  102 +
  103 + @Override
  104 + public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
  105 +
  106 + }
  107 + }
  108 +}
... ...