Merge pull request #5654 from desoliture1/rpc-delete-feature
[3.3.3] Deleted rpc status
This commit is contained in:
commit
21a1ab2f4b
@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.request.async.DeferredResult;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
import org.thingsboard.server.common.data.id.DeviceId;
|
||||
@ -187,10 +188,15 @@ public class RpcV2Controller extends AbstractRpcController {
|
||||
@RequestParam(required = false) String sortOrder) throws ThingsboardException {
|
||||
checkParameter("DeviceId", strDeviceId);
|
||||
try {
|
||||
if (rpcStatus.equals(RpcStatus.DELETED)) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "RpcStatus: DELETED");
|
||||
}
|
||||
|
||||
TenantId tenantId = getCurrentUser().getTenantId();
|
||||
PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
|
||||
DeviceId deviceId = new DeviceId(UUID.fromString(strDeviceId));
|
||||
final DeferredResult<ResponseEntity> response = new DeferredResult<>();
|
||||
|
||||
accessValidator.validate(getCurrentUser(), Operation.RPC_CALL, deviceId, new HttpValidationCallback(response, new FutureCallback<>() {
|
||||
@Override
|
||||
public void onSuccess(@Nullable DeferredResult<ResponseEntity> result) {
|
||||
@ -219,7 +225,7 @@ public class RpcV2Controller extends AbstractRpcController {
|
||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
||||
@RequestMapping(value = "/persistent/{rpcId}", method = RequestMethod.DELETE)
|
||||
@ResponseBody
|
||||
public void deleteResource(
|
||||
public void deleteRpc(
|
||||
@ApiParam(value = RPC_ID_PARAM_DESCRIPTION, required = true)
|
||||
@PathVariable(RPC_ID) String strRpc) throws ThingsboardException {
|
||||
checkParameter("RpcId", strRpc);
|
||||
@ -235,6 +241,7 @@ public class RpcV2Controller extends AbstractRpcController {
|
||||
}
|
||||
|
||||
rpcService.deleteRpc(getTenantId(), rpcId);
|
||||
rpc.setStatus(RpcStatus.DELETED);
|
||||
|
||||
TbMsg msg = TbMsg.newMsg(RPC_DELETED, rpc.getDeviceId(), TbMsgMetaData.EMPTY, JacksonUtil.toString(rpc));
|
||||
tbClusterService.pushMsgToRuleEngine(getTenantId(), rpc.getDeviceId(), msg, null);
|
||||
|
||||
@ -0,0 +1,196 @@
|
||||
/**
|
||||
* Copyright © 2016-2021 The Thingsboard Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.controller;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.server.common.data.*;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
import org.thingsboard.server.common.data.rpc.Rpc;
|
||||
import org.thingsboard.server.common.data.rpc.RpcStatus;
|
||||
import org.thingsboard.server.common.data.security.Authority;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
public abstract class BaseRpcControllerTest extends AbstractControllerTest {
|
||||
|
||||
private Tenant savedTenant;
|
||||
private User tenantAdmin;
|
||||
|
||||
@Before
|
||||
public void beforeTest() throws Exception {
|
||||
loginSysAdmin();
|
||||
|
||||
Tenant tenant = new Tenant();
|
||||
tenant.setTitle("My tenant");
|
||||
savedTenant = doPost("/api/tenant", tenant, Tenant.class);
|
||||
Assert.assertNotNull(savedTenant);
|
||||
|
||||
tenantAdmin = new User();
|
||||
tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
|
||||
tenantAdmin.setTenantId(savedTenant.getId());
|
||||
tenantAdmin.setEmail("tenant2@thingsboard.org");
|
||||
tenantAdmin.setFirstName("Joe");
|
||||
tenantAdmin.setLastName("Downs");
|
||||
|
||||
tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
|
||||
}
|
||||
|
||||
@After
|
||||
public void afterTest() throws Exception {
|
||||
loginSysAdmin();
|
||||
|
||||
doDelete("/api/tenant/" + savedTenant.getId().getId().toString())
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
private Device createDefaultDevice() {
|
||||
Device device = new Device();
|
||||
device.setName("My device");
|
||||
device.setType("default");
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
private ObjectNode createDefaultRpc() {
|
||||
ObjectNode rpc = JacksonUtil.newObjectNode();
|
||||
rpc.put("method", "setGpio");
|
||||
|
||||
ObjectNode params = JacksonUtil.newObjectNode();
|
||||
|
||||
params.put("pin", 7);
|
||||
params.put("value", 1);
|
||||
|
||||
rpc.set("params", params);
|
||||
rpc.put("persistent", true);
|
||||
rpc.put("timeout", 5000);
|
||||
|
||||
return rpc;
|
||||
}
|
||||
|
||||
private Rpc getRpcById(String rpcId) throws Exception {
|
||||
return doGet("/api/rpc/persistent/" + rpcId, Rpc.class);
|
||||
}
|
||||
|
||||
private MvcResult removeRpcById(String rpcId) throws Exception {
|
||||
return doDelete("/api/rpc/persistent/" + rpcId).andReturn();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveRpc() throws Exception {
|
||||
Device device = createDefaultDevice();
|
||||
Device savedDevice = doPost("/api/device", device, Device.class);
|
||||
|
||||
ObjectNode rpc = createDefaultRpc();
|
||||
String result = doPostAsync(
|
||||
"/api/rpc/oneway/" + savedDevice.getId().getId().toString(),
|
||||
JacksonUtil.toString(rpc),
|
||||
String.class,
|
||||
status().isOk()
|
||||
);
|
||||
String rpcId = JacksonUtil.fromString(result, JsonNode.class)
|
||||
.get("rpcId")
|
||||
.asText();
|
||||
Rpc savedRpc = getRpcById(rpcId);
|
||||
|
||||
Assert.assertNotNull(savedRpc);
|
||||
Assert.assertEquals(savedDevice.getId(), savedRpc.getDeviceId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteRpc() throws Exception {
|
||||
Device device = createDefaultDevice();
|
||||
Device savedDevice = doPost("/api/device", device, Device.class);
|
||||
|
||||
ObjectNode rpc = createDefaultRpc();
|
||||
String result = doPostAsync(
|
||||
"/api/rpc/oneway/" + savedDevice.getId().getId().toString(),
|
||||
JacksonUtil.toString(rpc),
|
||||
String.class,
|
||||
status().isOk()
|
||||
);
|
||||
String rpcId = JacksonUtil.fromString(result, JsonNode.class)
|
||||
.get("rpcId")
|
||||
.asText();
|
||||
Rpc savedRpc = getRpcById(rpcId);
|
||||
|
||||
MvcResult mvcResult = removeRpcById(savedRpc.getId().getId().toString());
|
||||
MvcResult res = doGet("/api/rpc/persistent/" + rpcId)
|
||||
.andExpect(status().isNotFound())
|
||||
.andReturn();
|
||||
|
||||
JsonNode deleteResponse = JacksonUtil.fromString(res.getResponse().getContentAsString(), JsonNode.class);
|
||||
Assert.assertEquals(404, deleteResponse.get("status").asInt());
|
||||
|
||||
String url = "/api/rpc/persistent/device/" + savedDevice.getUuidId().toString()
|
||||
+ "?" + "page=0" + "&" +
|
||||
"pageSize=" + Integer.MAX_VALUE + "&" +
|
||||
"rpcStatus=" + RpcStatus.DELETED.name();
|
||||
MvcResult byDeviceResult = doGet(url).andReturn();
|
||||
JsonNode byDeviceResponse = JacksonUtil.fromString(byDeviceResult.getResponse().getContentAsString(), JsonNode.class);
|
||||
|
||||
Assert.assertEquals(500, byDeviceResponse.get("status").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRpcsByDeviceId() throws Exception {
|
||||
Device device = createDefaultDevice();
|
||||
Device savedDevice = doPost("/api/device", device, Device.class);
|
||||
|
||||
ObjectNode rpc = createDefaultRpc();
|
||||
|
||||
String result = doPostAsync(
|
||||
"/api/rpc/oneway/" + savedDevice.getId().getId().toString(),
|
||||
JacksonUtil.toString(rpc),
|
||||
String.class,
|
||||
status().isOk()
|
||||
);
|
||||
String rpcId = JacksonUtil.fromString(result, JsonNode.class)
|
||||
.get("rpcId")
|
||||
.asText();
|
||||
|
||||
String url = "/api/rpc/persistent/device/" + savedDevice.getId().getId()
|
||||
+ "?" + "page=0" + "&" +
|
||||
"pageSize=" + Integer.MAX_VALUE + "&" +
|
||||
"rpcStatus=" + RpcStatus.QUEUED;
|
||||
|
||||
MvcResult byDeviceResult = doGetAsync(url).andReturn();
|
||||
|
||||
List<Rpc> byDeviceRpcs = JacksonUtil.fromString(
|
||||
byDeviceResult
|
||||
.getResponse()
|
||||
.getContentAsString(),
|
||||
new TypeReference<PageData<Rpc>>() {}
|
||||
).getData();
|
||||
|
||||
|
||||
boolean found = byDeviceRpcs.stream().anyMatch(r ->
|
||||
r.getUuidId().toString().equals(rpcId)
|
||||
&& r.getDeviceId().equals(savedDevice.getId())
|
||||
);
|
||||
|
||||
Assert.assertTrue(found);
|
||||
}
|
||||
}
|
||||
@ -18,9 +18,6 @@ package org.thingsboard.server.controller.sql;
|
||||
import org.thingsboard.server.controller.BaseComponentDescriptorControllerTest;
|
||||
import org.thingsboard.server.dao.service.DaoSqlTest;
|
||||
|
||||
/**
|
||||
* Created by Valerii Sosliuk on 6/28/2017.
|
||||
*/
|
||||
@DaoSqlTest
|
||||
public class ComponentDescriptorControllerSqlTest extends BaseComponentDescriptorControllerTest {
|
||||
}
|
||||
|
||||
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright © 2016-2021 The Thingsboard Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.controller.sql;
|
||||
|
||||
import org.thingsboard.server.controller.BaseRpcControllerTest;
|
||||
import org.thingsboard.server.dao.service.DaoSqlTest;
|
||||
|
||||
@DaoSqlTest
|
||||
public class RpcControllerTest extends BaseRpcControllerTest {
|
||||
}
|
||||
@ -16,5 +16,5 @@
|
||||
package org.thingsboard.server.common.data.rpc;
|
||||
|
||||
public enum RpcStatus {
|
||||
QUEUED, SENT, DELIVERED, SUCCESSFUL, TIMEOUT, EXPIRED, FAILED
|
||||
QUEUED, SENT, DELIVERED, SUCCESSFUL, TIMEOUT, EXPIRED, FAILED, DELETED
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user