@PostMapping("/delete.json")
    @AuthAction(AuthService.PrivilegeType.DELETE_RULE)
    public Result<Long> deleteFlowRule(Long id) {

        if (id == null) {
            return Result.ofFail(-1, "id can't be null");
        }

        GatewayFlowRuleEntity oldEntity = repository.findById(id);
        if (oldEntity == null) {
            return Result.ofSuccess(null);
        }

        try {
            repository.delete(id);
        } catch (Throwable throwable) {
            logger.error("delete gateway flow rule error:", throwable);
            return Result.ofThrowable(-1, throwable);
        }

        if (!publishRules(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) {
            logger.warn("publish gateway flow rules fail after delete");
        }

        return Result.ofSuccess(id);
    }
@Test
    public void testDeleteFlowRule() throws Exception {
        String path = "/gateway/flow/delete.json";

        // Add one entity into memory repository for delete
        GatewayFlowRuleEntity addEntity = new GatewayFlowRuleEntity();
        addEntity.setId(1L);
        addEntity.setApp(TEST_APP);
        addEntity.setIp(TEST_IP);
        addEntity.setPort(TEST_PORT);
        addEntity.setResource("httpbin_route");
        addEntity.setResourceMode(RESOURCE_MODE_ROUTE_ID);
        addEntity.setGrade(FLOW_GRADE_QPS);
        addEntity.setCount(5D);
        addEntity.setInterval(30L);
        addEntity.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_SECOND);
        addEntity.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT);
        addEntity.setBurst(0);
        addEntity.setMaxQueueingTimeoutMs(0);
        Date date = new Date();
        date = DateUtils.addSeconds(date, -1);
        addEntity.setGmtCreate(date);
        addEntity.setGmtModified(date);

        GatewayParamFlowItemEntity addItemEntity = new GatewayParamFlowItemEntity();
        addEntity.setParamItem(addItemEntity);
        addItemEntity.setParseStrategy(PARAM_PARSE_STRATEGY_CLIENT_IP);

        repository.save(addEntity);

        given(sentinelApiClient.modifyGatewayFlowRules(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any())).willReturn(true);

        MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
        requestBuilder.param("id", String.valueOf(addEntity.getId()));

        // Do controller logic
        MvcResult mvcResult = mockMvc.perform(requestBuilder)
                .andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();

        // Verify the modifyGatewayFlowRules method has been called
        verify(sentinelApiClient).modifyGatewayFlowRules(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());

        // Verify the result
        Result<Long> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<Long>>() {});
        assertTrue(result.isSuccess());

        assertEquals(addEntity.getId(), result.getData());

        // Now no entities in memory
        List<GatewayFlowRuleEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
        assertEquals(0, entitiesInMem.size());
    }