@Test
public void testAddFlowRule() throws Exception {

    String path = "/gateway/flow/new.json";

    AddFlowRuleReqVo reqVo = new AddFlowRuleReqVo();
    reqVo.setApp(TEST_APP);
    reqVo.setIp(TEST_IP);
    reqVo.setPort(TEST_PORT);

    reqVo.setResourceMode(RESOURCE_MODE_ROUTE_ID);
    reqVo.setResource("httpbin_route");

    reqVo.setGrade(FLOW_GRADE_QPS);
    reqVo.setCount(5D);
    reqVo.setInterval(30L);
    reqVo.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_SECOND);
    reqVo.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT);
    reqVo.setBurst(0);
    reqVo.setMaxQueueingTimeoutMs(0);

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

    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
    requestBuilder.content(JSON.toJSONString(reqVo)).contentType(MediaType.APPLICATION_JSON);

    // 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());

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

    // Verify the result
    GatewayFlowRuleEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals(TEST_APP, entity.getApp());
    assertEquals(TEST_IP, entity.getIp());
    assertEquals(TEST_PORT, entity.getPort());
    assertEquals(RESOURCE_MODE_ROUTE_ID, entity.getResourceMode().intValue());
    assertEquals("httpbin_route", entity.getResource());
    assertNotNull(entity.getId());
    assertNotNull(entity.getGmtCreate());
    assertNotNull(entity.getGmtModified());

    // Verify the entity which is add in memory repository
    List<GatewayFlowRuleEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(1, entitiesInMem.size());
    assertEquals(entity, entitiesInMem.get(0));
}