Commit 3b058694634ed127a533ff520c2939cada131c2d

Authored by Andrew Shvayka
Committed by GitHub
2 parents 7a8cc68f d9604b8b

Merge pull request #22 from thingsboard/RPC-TESTS

MQTT RPC One-Way and Negative test cases added
@@ -19,12 +19,18 @@ import lombok.extern.slf4j.Slf4j; @@ -19,12 +19,18 @@ import lombok.extern.slf4j.Slf4j;
19 import org.eclipse.paho.client.mqttv3.*; 19 import org.eclipse.paho.client.mqttv3.*;
20 import org.junit.Assert; 20 import org.junit.Assert;
21 import org.junit.Before; 21 import org.junit.Before;
  22 +import org.junit.Ignore;
22 import org.junit.Test; 23 import org.junit.Test;
  24 +import org.springframework.http.HttpStatus;
  25 +import org.springframework.http.ResponseEntity;
  26 +import org.springframework.web.client.HttpClientErrorException;
23 import org.thingsboard.client.tools.RestClient; 27 import org.thingsboard.client.tools.RestClient;
24 import org.thingsboard.server.common.data.Device; 28 import org.thingsboard.server.common.data.Device;
25 import org.thingsboard.server.common.data.security.DeviceCredentials; 29 import org.thingsboard.server.common.data.security.DeviceCredentials;
26 import org.thingsboard.server.mqtt.AbstractFeatureIntegrationTest; 30 import org.thingsboard.server.mqtt.AbstractFeatureIntegrationTest;
27 31
  32 +import java.util.UUID;
  33 +
28 import static org.junit.Assert.assertEquals; 34 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull; 35 import static org.junit.Assert.assertNotNull;
30 36
@@ -40,28 +46,88 @@ public class MqttServerSideRpcIntegrationTest extends AbstractFeatureIntegration @@ -40,28 +46,88 @@ public class MqttServerSideRpcIntegrationTest extends AbstractFeatureIntegration
40 private static final String USERNAME = "tenant@thingsboard.org"; 46 private static final String USERNAME = "tenant@thingsboard.org";
41 private static final String PASSWORD = "tenant"; 47 private static final String PASSWORD = "tenant";
42 48
43 - private Device savedDevice;  
44 -  
45 - private String accessToken;  
46 private RestClient restClient; 49 private RestClient restClient;
47 50
48 @Before 51 @Before
49 public void beforeTest() throws Exception { 52 public void beforeTest() throws Exception {
50 restClient = new RestClient(BASE_URL); 53 restClient = new RestClient(BASE_URL);
51 restClient.login(USERNAME, PASSWORD); 54 restClient.login(USERNAME, PASSWORD);
  55 + }
52 56
  57 + @Test
  58 + public void testServerMqttOneWayRpc() throws Exception {
53 Device device = new Device(); 59 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(); 60 + device.setName("Test One-Way Server-Side RPC");
  61 + Device savedDevice = getSavedDevice(device);
  62 + DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice);
58 assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId()); 63 assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
59 - accessToken = deviceCredentials.getCredentialsId(); 64 + String accessToken = deviceCredentials.getCredentialsId();
60 assertNotNull(accessToken); 65 assertNotNull(accessToken);
  66 +
  67 + String clientId = MqttAsyncClient.generateClientId();
  68 + MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, clientId);
  69 +
  70 + MqttConnectOptions options = new MqttConnectOptions();
  71 + options.setUserName(accessToken);
  72 + client.connect(options);
  73 + Thread.sleep(3000);
  74 + client.subscribe("v1/devices/me/rpc/request/+", 1);
  75 + client.setCallback(new TestMqttCallback(client));
  76 +
  77 + String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
  78 + String deviceId = savedDevice.getId().getId().toString();
  79 + ResponseEntity result = restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/oneway/" + deviceId, setGpioRequest, String.class);
  80 + Assert.assertEquals(HttpStatus.OK, result.getStatusCode());
  81 + Assert.assertNull(result.getBody());
  82 + }
  83 +
  84 + @Test
  85 + public void testServerMqttOneWayRpcDeviceOffline() throws Exception {
  86 + Device device = new Device();
  87 + device.setName("Test One-Way Server-Side RPC Device Offline");
  88 + Device savedDevice = getSavedDevice(device);
  89 + DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice);
  90 + assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
  91 + String accessToken = deviceCredentials.getCredentialsId();
  92 + assertNotNull(accessToken);
  93 +
  94 + String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
  95 + String deviceId = savedDevice.getId().getId().toString();
  96 + try {
  97 + restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/oneway/" + deviceId, setGpioRequest, String.class);
  98 + Assert.fail("HttpClientErrorException expected, but not encountered");
  99 + } catch (HttpClientErrorException e) {
  100 + log.error(e.getMessage(), e);
  101 + Assert.assertEquals(HttpStatus.REQUEST_TIMEOUT, e.getStatusCode());
  102 + Assert.assertEquals("408 null", e.getMessage());
  103 + }
  104 + }
  105 +
  106 + @Test
  107 + public void testServerMqttOneWayRpcDeviceDoesNotExist() throws Exception {
  108 + String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
  109 + String nonExistentDeviceId = UUID.randomUUID().toString();
  110 + try {
  111 + restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/oneway/" + nonExistentDeviceId, setGpioRequest, String.class);
  112 + Assert.fail("HttpClientErrorException expected, but not encountered");
  113 + } catch (HttpClientErrorException e) {
  114 + log.error(e.getMessage(), e);
  115 + Assert.assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
  116 + Assert.assertEquals("400 null", e.getMessage());
  117 + }
61 } 118 }
62 119
63 @Test 120 @Test
64 public void testServerMqttTwoWayRpc() throws Exception { 121 public void testServerMqttTwoWayRpc() throws Exception {
  122 +
  123 + Device device = new Device();
  124 + device.setName("Test Two-Way Server-Side RPC");
  125 + Device savedDevice = getSavedDevice(device);
  126 + DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice);
  127 + assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
  128 + String accessToken = deviceCredentials.getCredentialsId();
  129 + assertNotNull(accessToken);
  130 +
65 String clientId = MqttAsyncClient.generateClientId(); 131 String clientId = MqttAsyncClient.generateClientId();
66 MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, clientId); 132 MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, clientId);
67 133
@@ -69,16 +135,63 @@ public class MqttServerSideRpcIntegrationTest extends AbstractFeatureIntegration @@ -69,16 +135,63 @@ public class MqttServerSideRpcIntegrationTest extends AbstractFeatureIntegration
69 options.setUserName(accessToken); 135 options.setUserName(accessToken);
70 client.connect(options); 136 client.connect(options);
71 Thread.sleep(3000); 137 Thread.sleep(3000);
72 - client.subscribe("v1/devices/me/rpc/request/+",1); 138 + client.subscribe("v1/devices/me/rpc/request/+", 1);
73 client.setCallback(new TestMqttCallback(client)); 139 client.setCallback(new TestMqttCallback(client));
74 140
75 String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}"; 141 String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
76 String deviceId = savedDevice.getId().getId().toString(); 142 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); 143 + String result = getStringResult(setGpioRequest, "twoway", deviceId);
79 Assert.assertEquals("{\"value1\":\"A\",\"value2\":\"B\"}", result); 144 Assert.assertEquals("{\"value1\":\"A\",\"value2\":\"B\"}", result);
80 } 145 }
81 146
  147 + @Test
  148 + public void testServerMqttTwoWayRpcDeviceOffline() throws Exception {
  149 + Device device = new Device();
  150 + device.setName("Test Two-Way Server-Side RPC Device Offline");
  151 + Device savedDevice = getSavedDevice(device);
  152 + DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice);
  153 + assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
  154 + String accessToken = deviceCredentials.getCredentialsId();
  155 + assertNotNull(accessToken);
  156 +
  157 + String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
  158 + String deviceId = savedDevice.getId().getId().toString();
  159 + try {
  160 + restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/twoway/" + deviceId, setGpioRequest, String.class);
  161 + Assert.fail("HttpClientErrorException expected, but not encountered");
  162 + } catch (HttpClientErrorException e) {
  163 + log.error(e.getMessage(), e);
  164 + Assert.assertEquals(HttpStatus.REQUEST_TIMEOUT, e.getStatusCode());
  165 + Assert.assertEquals("408 null", e.getMessage());
  166 + }
  167 + }
  168 +
  169 + @Test
  170 + public void testServerMqttTwoWayRpcDeviceDoesNotExist() throws Exception {
  171 + String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
  172 + String nonExistentDeviceId = UUID.randomUUID().toString();
  173 + try {
  174 + restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/oneway/" + nonExistentDeviceId, setGpioRequest, String.class);
  175 + Assert.fail("HttpClientErrorException expected, but not encountered");
  176 + } catch (HttpClientErrorException e) {
  177 + log.error(e.getMessage(), e);
  178 + Assert.assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
  179 + Assert.assertEquals("400 null", e.getMessage());
  180 + }
  181 + }
  182 +
  183 + private Device getSavedDevice(Device device) {
  184 + return restClient.getRestTemplate().postForEntity(BASE_URL + "/api/device", device, Device.class).getBody();
  185 + }
  186 +
  187 + private DeviceCredentials getDeviceCredentials(Device savedDevice) {
  188 + return restClient.getRestTemplate().getForEntity(BASE_URL + "/api/device/" + savedDevice.getId().getId().toString() + "/credentials", DeviceCredentials.class).getBody();
  189 + }
  190 +
  191 + private String getStringResult(String requestData, String callType, String deviceId) {
  192 + return restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/" + callType + "/" + deviceId, requestData, String.class).getBody();
  193 + }
  194 +
82 private static class TestMqttCallback implements MqttCallback { 195 private static class TestMqttCallback implements MqttCallback {
83 196
84 private final MqttAsyncClient client; 197 private final MqttAsyncClient client;