Search in sources :

Example 16 with GatewayParamFlowItemEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity in project spring-boot-student by wyh-spring-ecosystem-student.

the class GatewayFlowRuleControllerTest method testQueryFlowRules.

@Test
public void testQueryFlowRules() throws Exception {
    String path = "/gateway/flow/list.json";
    List<GatewayFlowRuleEntity> entities = new ArrayList<>();
    // Mock two entities
    GatewayFlowRuleEntity entity = new GatewayFlowRuleEntity();
    entity.setId(1L);
    entity.setApp(TEST_APP);
    entity.setIp(TEST_IP);
    entity.setPort(TEST_PORT);
    entity.setResource("httpbin_route");
    entity.setResourceMode(RESOURCE_MODE_ROUTE_ID);
    entity.setGrade(FLOW_GRADE_QPS);
    entity.setCount(5D);
    entity.setInterval(30L);
    entity.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_SECOND);
    entity.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT);
    entity.setBurst(0);
    entity.setMaxQueueingTimeoutMs(0);
    GatewayParamFlowItemEntity itemEntity = new GatewayParamFlowItemEntity();
    entity.setParamItem(itemEntity);
    itemEntity.setParseStrategy(PARAM_PARSE_STRATEGY_CLIENT_IP);
    entities.add(entity);
    GatewayFlowRuleEntity entity2 = new GatewayFlowRuleEntity();
    entity2.setId(2L);
    entity2.setApp(TEST_APP);
    entity2.setIp(TEST_IP);
    entity2.setPort(TEST_PORT);
    entity2.setResource("some_customized_api");
    entity2.setResourceMode(RESOURCE_MODE_CUSTOM_API_NAME);
    entity2.setCount(30D);
    entity2.setInterval(2L);
    entity2.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE);
    entity2.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT);
    entity2.setBurst(0);
    entity2.setMaxQueueingTimeoutMs(0);
    GatewayParamFlowItemEntity itemEntity2 = new GatewayParamFlowItemEntity();
    entity2.setParamItem(itemEntity2);
    itemEntity2.setParseStrategy(PARAM_PARSE_STRATEGY_CLIENT_IP);
    entities.add(entity2);
    CompletableFuture<List<GatewayFlowRuleEntity>> completableFuture = mock(CompletableFuture.class);
    given(completableFuture.get()).willReturn(entities);
    given(sentinelApiClient.fetchGatewayFlowRules(TEST_APP, TEST_IP, TEST_PORT)).willReturn(completableFuture);
    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(path);
    requestBuilder.param("app", TEST_APP);
    requestBuilder.param("ip", TEST_IP);
    requestBuilder.param("port", String.valueOf(TEST_PORT));
    // Do controller logic
    MvcResult mvcResult = mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();
    // Verify the fetchGatewayFlowRules method has been called
    verify(sentinelApiClient).fetchGatewayFlowRules(TEST_APP, TEST_IP, TEST_PORT);
    // Verify if two same entities are got
    Result<List<GatewayFlowRuleEntity>> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<List<GatewayFlowRuleEntity>>>() {
    });
    assertTrue(result.isSuccess());
    List<GatewayFlowRuleEntity> data = result.getData();
    assertEquals(2, data.size());
    assertEquals(entities, data);
    // Verify the entities are add into memory repository
    List<GatewayFlowRuleEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(2, entitiesInMem.size());
    assertEquals(entities, entitiesInMem);
}
Also used : GatewayFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ArrayList(java.util.ArrayList) GatewayParamFlowItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity) ArrayList(java.util.ArrayList) List(java.util.List) 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)

Aggregations

GatewayFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity)16 GatewayParamFlowItemEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity)16 Date (java.util.Date)14 GatewayParamFlowItemVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.GatewayParamFlowItemVo)12 AuthAction (com.alibaba.csp.sentinel.dashboard.auth.AuthAction)10 Result (com.alibaba.csp.sentinel.dashboard.domain.Result)6 Test (org.junit.Test)6 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)6 MvcResult (org.springframework.test.web.servlet.MvcResult)6 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)6 NoAuthConfigurationTest (com.alibaba.csp.sentinel.dashboard.config.NoAuthConfigurationTest)3 UpdateFlowRuleReqVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.UpdateFlowRuleReqVo)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2