|
|
|
@ -38,12 +38,16 @@ import org.thingsboard.server.common.data.id.CustomerId; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceId; |
|
|
|
import org.thingsboard.server.common.data.id.EdgeId; |
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
|
import org.thingsboard.server.common.data.id.RpcId; |
|
|
|
import org.thingsboard.server.common.data.ota.OtaPackageType; |
|
|
|
import org.thingsboard.server.common.data.rpc.Rpc; |
|
|
|
import org.thingsboard.server.common.data.rpc.RpcStatus; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
|
|
|
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; |
|
|
|
import org.thingsboard.server.common.msg.session.FeatureType; |
|
|
|
import org.thingsboard.server.dao.edge.EdgeService; |
|
|
|
import org.thingsboard.server.dao.rpc.RpcService; |
|
|
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
|
|
|
import org.thingsboard.server.gen.edge.v1.AttributesRequestMsg; |
|
|
|
import org.thingsboard.server.gen.edge.v1.DeviceCredentialsRequestMsg; |
|
|
|
@ -93,6 +97,9 @@ public class DeviceEdgeTest extends AbstractEdgeTest { |
|
|
|
@Autowired |
|
|
|
protected EdgeService edgeService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
protected RpcService rpcService; |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testDevices() throws Exception { |
|
|
|
// create device and assign to edge; update device
|
|
|
|
@ -654,6 +661,118 @@ public class DeviceEdgeTest extends AbstractEdgeTest { |
|
|
|
Assert.assertEquals(2, latestDeviceRpcCallMsg.getRetries()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testRpcV2StatusUpdateFromEdge() throws Exception { |
|
|
|
Device device = findDeviceByName("Edge Device 1"); |
|
|
|
Rpc rpc = savePersistentRpc(device, RpcStatus.QUEUED); |
|
|
|
|
|
|
|
sendRpcStatusUplink(device, rpc.getId(), RpcStatus.DELIVERED, null); |
|
|
|
Assert.assertEquals(RpcStatus.DELIVERED, getPersistentRpc(rpc.getId()).getStatus()); |
|
|
|
|
|
|
|
sendRpcStatusUplink(device, rpc.getId(), RpcStatus.SUCCESSFUL, "{\"result\":\"ok\"}"); |
|
|
|
Rpc successful = getPersistentRpc(rpc.getId()); |
|
|
|
Assert.assertEquals(RpcStatus.SUCCESSFUL, successful.getStatus()); |
|
|
|
Assert.assertEquals("ok", successful.getResponse().get("result").asText()); |
|
|
|
|
|
|
|
// stale intermediate status must not regress a final RPC
|
|
|
|
sendRpcStatusUplink(device, rpc.getId(), RpcStatus.DELIVERED, null); |
|
|
|
Assert.assertEquals(RpcStatus.SUCCESSFUL, getPersistentRpc(rpc.getId()).getStatus()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testRpcV2StatusRetryAfterTimeoutFromEdge() throws Exception { |
|
|
|
Device device = findDeviceByName("Edge Device 1"); |
|
|
|
Rpc rpc = savePersistentRpc(device, RpcStatus.SENT); |
|
|
|
|
|
|
|
// TIMEOUT with retries left is followed by a re-delivery
|
|
|
|
sendRpcStatusUplink(device, rpc.getId(), RpcStatus.TIMEOUT, null); |
|
|
|
Assert.assertEquals(RpcStatus.TIMEOUT, getPersistentRpc(rpc.getId()).getStatus()); |
|
|
|
|
|
|
|
sendRpcStatusUplink(device, rpc.getId(), RpcStatus.SENT, null); |
|
|
|
Assert.assertEquals(RpcStatus.SENT, getPersistentRpc(rpc.getId()).getStatus()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testRpcV2NonJsonResponseFromEdge() throws Exception { |
|
|
|
Device device = findDeviceByName("Edge Device 1"); |
|
|
|
Rpc rpc = savePersistentRpc(device, RpcStatus.DELIVERED); |
|
|
|
|
|
|
|
sendRpcStatusUplink(device, rpc.getId(), RpcStatus.SUCCESSFUL, "not a json"); |
|
|
|
Rpc successful = getPersistentRpc(rpc.getId()); |
|
|
|
Assert.assertEquals(RpcStatus.SUCCESSFUL, successful.getStatus()); |
|
|
|
Assert.assertEquals("not a json", successful.getResponse().get("error").asText()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testRpcV2StatusForUnknownRpcFromEdge() throws Exception { |
|
|
|
Device device = findDeviceByName("Edge Device 1"); |
|
|
|
RpcId unknownRpcId = new RpcId(Uuids.timeBased()); |
|
|
|
|
|
|
|
sendRpcStatusUplink(device, unknownRpcId, RpcStatus.DELIVERED, null); |
|
|
|
doGet("/api/rpc/persistent/" + unknownRpcId.getId()).andExpect(status().isNotFound()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testRpcV2DeleteFromEdge() throws Exception { |
|
|
|
Device device = findDeviceByName("Edge Device 1"); |
|
|
|
Rpc rpc = savePersistentRpc(device, RpcStatus.DELIVERED); |
|
|
|
|
|
|
|
edgeImitator.expectMessageAmount(1); |
|
|
|
sendRpcStatusUplink(device, rpc.getId(), RpcStatus.DELETED, null); |
|
|
|
doGet("/api/rpc/persistent/" + rpc.getUuidId()).andExpect(status().isNotFound()); |
|
|
|
// delete originated from the edge must not be echoed back
|
|
|
|
Assert.assertFalse(edgeImitator.waitForMessages(1)); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testRpcV2DeleteFromCloud() throws Exception { |
|
|
|
Device device = findDeviceByName("Edge Device 1"); |
|
|
|
Rpc rpc = savePersistentRpc(device, RpcStatus.SUCCESSFUL); |
|
|
|
|
|
|
|
edgeImitator.expectMessageAmount(1); |
|
|
|
doDelete("/api/rpc/persistent/" + rpc.getUuidId()).andExpect(status().isOk()); |
|
|
|
Assert.assertTrue(edgeImitator.waitForMessages()); |
|
|
|
|
|
|
|
AbstractMessage latestMessage = edgeImitator.getLatestMessage(); |
|
|
|
Assert.assertTrue(latestMessage instanceof DeviceRpcCallMsg); |
|
|
|
DeviceRpcCallMsg deviceRpcCallMsg = (DeviceRpcCallMsg) latestMessage; |
|
|
|
Assert.assertEquals(RpcStatus.DELETED.name(), deviceRpcCallMsg.getRpcStatus()); |
|
|
|
Assert.assertEquals(rpc.getUuidId(), new UUID(deviceRpcCallMsg.getRequestUuidMSB(), deviceRpcCallMsg.getRequestUuidLSB())); |
|
|
|
Assert.assertFalse(deviceRpcCallMsg.hasRequestMsg()); |
|
|
|
Assert.assertFalse(deviceRpcCallMsg.hasResponseMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
private Rpc savePersistentRpc(Device device, RpcStatus status) { |
|
|
|
Rpc rpc = new Rpc(new RpcId(Uuids.timeBased())); |
|
|
|
rpc.setTenantId(tenantId); |
|
|
|
rpc.setDeviceId(device.getId()); |
|
|
|
rpc.setExpirationTime(System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(1)); |
|
|
|
rpc.setRequest(JacksonUtil.newObjectNode().put("method", "test_method")); |
|
|
|
rpc.setStatus(status); |
|
|
|
return rpcService.save(rpc); |
|
|
|
} |
|
|
|
|
|
|
|
private Rpc getPersistentRpc(RpcId rpcId) throws Exception { |
|
|
|
return doGet("/api/rpc/persistent/" + rpcId.getId(), Rpc.class); |
|
|
|
} |
|
|
|
|
|
|
|
private void sendRpcStatusUplink(Device device, RpcId rpcId, RpcStatus rpcStatus, String response) throws Exception { |
|
|
|
DeviceRpcCallMsg.Builder builder = DeviceRpcCallMsg.newBuilder() |
|
|
|
.setDeviceIdMSB(device.getUuidId().getMostSignificantBits()) |
|
|
|
.setDeviceIdLSB(device.getUuidId().getLeastSignificantBits()) |
|
|
|
.setRequestUuidMSB(rpcId.getId().getMostSignificantBits()) |
|
|
|
.setRequestUuidLSB(rpcId.getId().getLeastSignificantBits()) |
|
|
|
.setRpcStatus(rpcStatus.name()); |
|
|
|
if (response != null) { |
|
|
|
builder.setResponseMsg(RpcResponseMsg.newBuilder().setResponse(response).build()); |
|
|
|
} |
|
|
|
UplinkMsg uplinkMsg = UplinkMsg.newBuilder().addDeviceRpcCallMsg(builder.build()).build(); |
|
|
|
|
|
|
|
edgeImitator.expectResponsesAmount(1); |
|
|
|
edgeImitator.sendUplinkMsg(uplinkMsg); |
|
|
|
Assert.assertTrue(edgeImitator.waitForResponses()); |
|
|
|
} |
|
|
|
|
|
|
|
private void sendAttributesRequestAndVerify(Device device, String scope, String attributesDataStr, String expectedKey, |
|
|
|
String expectedValue) throws Exception { |
|
|
|
JsonNode attributesData = JacksonUtil.toJsonNode(attributesDataStr); |
|
|
|
|