Search in sources :

Example 1 with UpdateFlowRuleReqVo

use of com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.UpdateFlowRuleReqVo in project spring-boot-student by wyh-spring-ecosystem-student.

the class GatewayFlowRuleControllerTest method testUpdateFlowRule.

@Test
public void testUpdateFlowRule() throws Exception {
    String path = "/gateway/flow/save.json";
    // Add one entity into memory repository for update
    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();
    // To make the gmtModified different when do update
    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);
    UpdateFlowRuleReqVo reqVo = new UpdateFlowRuleReqVo();
    reqVo.setId(addEntity.getId());
    reqVo.setApp(TEST_APP);
    reqVo.setGrade(FLOW_GRADE_QPS);
    reqVo.setCount(6D);
    reqVo.setInterval(2L);
    reqVo.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE);
    reqVo.setControlBehavior(CONTROL_BEHAVIOR_RATE_LIMITER);
    reqVo.setMaxQueueingTimeoutMs(500);
    GatewayParamFlowItemVo itemVo = new GatewayParamFlowItemVo();
    reqVo.setParamItem(itemVo);
    itemVo.setParseStrategy(PARAM_PARSE_STRATEGY_URL_PARAM);
    itemVo.setFieldName("pa");
    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());
    GatewayFlowRuleEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals(RESOURCE_MODE_ROUTE_ID, entity.getResourceMode().intValue());
    assertEquals("httpbin_route", entity.getResource());
    assertEquals(6D, entity.getCount().doubleValue(), 0);
    assertEquals(2L, entity.getInterval().longValue());
    assertEquals(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE, entity.getIntervalUnit().intValue());
    assertEquals(CONTROL_BEHAVIOR_RATE_LIMITER, entity.getControlBehavior().intValue());
    assertEquals(0, entity.getBurst().intValue());
    assertEquals(500, entity.getMaxQueueingTimeoutMs().intValue());
    assertEquals(date, entity.getGmtCreate());
    // To make sure gmtModified has been set and it's different from gmtCreate
    assertNotNull(entity.getGmtModified());
    assertNotEquals(entity.getGmtCreate(), entity.getGmtModified());
    // Verify the entity which is update in memory repository
    GatewayParamFlowItemEntity itemEntity = entity.getParamItem();
    assertEquals(PARAM_PARSE_STRATEGY_URL_PARAM, itemEntity.getParseStrategy().intValue());
    assertEquals("pa", itemEntity.getFieldName());
}
Also used : UpdateFlowRuleReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.UpdateFlowRuleReqVo) GatewayFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) GatewayParamFlowItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.GatewayParamFlowItemVo) GatewayParamFlowItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity) MvcResult(org.springframework.test.web.servlet.MvcResult) Date(java.util.Date) Result(com.alibaba.csp.sentinel.dashboard.domain.Result) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 2 with UpdateFlowRuleReqVo

use of com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.UpdateFlowRuleReqVo in project Sentinel by alibaba.

the class GatewayFlowRuleControllerTest method testUpdateFlowRule.

@Test
public void testUpdateFlowRule() throws Exception {
    String path = "/gateway/flow/save.json";
    // Add one entity into memory repository for update
    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();
    // To make the gmtModified different when do update
    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);
    UpdateFlowRuleReqVo reqVo = new UpdateFlowRuleReqVo();
    reqVo.setId(addEntity.getId());
    reqVo.setApp(TEST_APP);
    reqVo.setGrade(FLOW_GRADE_QPS);
    reqVo.setCount(6D);
    reqVo.setInterval(2L);
    reqVo.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE);
    reqVo.setControlBehavior(CONTROL_BEHAVIOR_RATE_LIMITER);
    reqVo.setMaxQueueingTimeoutMs(500);
    GatewayParamFlowItemVo itemVo = new GatewayParamFlowItemVo();
    reqVo.setParamItem(itemVo);
    itemVo.setParseStrategy(PARAM_PARSE_STRATEGY_URL_PARAM);
    itemVo.setFieldName("pa");
    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());
    GatewayFlowRuleEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals(RESOURCE_MODE_ROUTE_ID, entity.getResourceMode().intValue());
    assertEquals("httpbin_route", entity.getResource());
    assertEquals(6D, entity.getCount().doubleValue(), 0);
    assertEquals(2L, entity.getInterval().longValue());
    assertEquals(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE, entity.getIntervalUnit().intValue());
    assertEquals(CONTROL_BEHAVIOR_RATE_LIMITER, entity.getControlBehavior().intValue());
    assertEquals(0, entity.getBurst().intValue());
    assertEquals(500, entity.getMaxQueueingTimeoutMs().intValue());
    assertEquals(date, entity.getGmtCreate());
    // To make sure gmtModified has been set and it's different from gmtCreate
    assertNotNull(entity.getGmtModified());
    assertNotEquals(entity.getGmtCreate(), entity.getGmtModified());
    // Verify the entity which is update in memory repository
    GatewayParamFlowItemEntity itemEntity = entity.getParamItem();
    assertEquals(PARAM_PARSE_STRATEGY_URL_PARAM, itemEntity.getParseStrategy().intValue());
    assertEquals("pa", itemEntity.getFieldName());
}
Also used : UpdateFlowRuleReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.UpdateFlowRuleReqVo) GatewayFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) GatewayParamFlowItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.GatewayParamFlowItemVo) GatewayParamFlowItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity) MvcResult(org.springframework.test.web.servlet.MvcResult) Date(java.util.Date) Result(com.alibaba.csp.sentinel.dashboard.domain.Result) MvcResult(org.springframework.test.web.servlet.MvcResult) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest) NoAuthConfigurationTest(com.alibaba.csp.sentinel.dashboard.config.NoAuthConfigurationTest) Test(org.junit.Test)

Aggregations

GatewayFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity)2 GatewayParamFlowItemEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity)2 Result (com.alibaba.csp.sentinel.dashboard.domain.Result)2 GatewayParamFlowItemVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.GatewayParamFlowItemVo)2 UpdateFlowRuleReqVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.UpdateFlowRuleReqVo)2 Date (java.util.Date)2 Test (org.junit.Test)2 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)2 MvcResult (org.springframework.test.web.servlet.MvcResult)2 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)2 NoAuthConfigurationTest (com.alibaba.csp.sentinel.dashboard.config.NoAuthConfigurationTest)1