Search in sources :

Example 1 with AddFlowRuleReqVo

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

the class GatewayFlowRuleControllerTest method testAddFlowRule.

@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));
}
Also used : GatewayFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) AddFlowRuleReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.AddFlowRuleReqVo) MvcResult(org.springframework.test.web.servlet.MvcResult) 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 AddFlowRuleReqVo

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

the class GatewayFlowRuleControllerTest method testAddFlowRule.

@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));
}
Also used : GatewayFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) AddFlowRuleReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.AddFlowRuleReqVo) MvcResult(org.springframework.test.web.servlet.MvcResult) 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 Result (com.alibaba.csp.sentinel.dashboard.domain.Result)2 AddFlowRuleReqVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.AddFlowRuleReqVo)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