Search in sources :

Example 11 with Result

use of com.alibaba.csp.sentinel.dashboard.domain.Result 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 12 with Result

use of com.alibaba.csp.sentinel.dashboard.domain.Result in project XHuiCloud by sindaZeng.

the class AuthorizationInterceptor method responseNoPrivilegeMsg.

private void responseNoPrivilegeMsg(HttpServletResponse response, String message) throws IOException {
    Result result = Result.ofFail(-1, message);
    response.addHeader("Content-Type", "application/json;charset=UTF-8");
    response.getOutputStream().write(JSON.toJSONBytes(result));
}
Also used : Result(com.alibaba.csp.sentinel.dashboard.domain.Result)

Example 13 with Result

use of com.alibaba.csp.sentinel.dashboard.domain.Result in project Sentinel by alibaba.

the class GatewayApiControllerTest method testUpdateApi.

@Test
public void testUpdateApi() throws Exception {
    String path = "/gateway/api/save.json";
    // Add one entity to memory repository for update
    ApiDefinitionEntity addEntity = new ApiDefinitionEntity();
    addEntity.setApp(TEST_APP);
    addEntity.setIp(TEST_IP);
    addEntity.setPort(TEST_PORT);
    addEntity.setApiName("bbb");
    Date date = new Date();
    // To make the gmtModified different when do update
    date = DateUtils.addSeconds(date, -1);
    addEntity.setGmtCreate(date);
    addEntity.setGmtModified(date);
    Set<ApiPredicateItemEntity> addRedicateItemEntities = new HashSet<>();
    addEntity.setPredicateItems(addRedicateItemEntities);
    ApiPredicateItemEntity addPredicateItemEntity = new ApiPredicateItemEntity();
    addPredicateItemEntity.setMatchStrategy(URL_MATCH_STRATEGY_EXACT);
    addPredicateItemEntity.setPattern("/order");
    addEntity = repository.save(addEntity);
    UpdateApiReqVo reqVo = new UpdateApiReqVo();
    reqVo.setId(addEntity.getId());
    reqVo.setApp(TEST_APP);
    List<ApiPredicateItemVo> itemVos = new ArrayList<>();
    ApiPredicateItemVo itemVo = new ApiPredicateItemVo();
    itemVo.setMatchStrategy(URL_MATCH_STRATEGY_PREFIX);
    itemVo.setPattern("/my_order");
    itemVos.add(itemVo);
    reqVo.setPredicateItems(itemVos);
    given(sentinelApiClient.modifyApis(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 modifyApis method has been called
    verify(sentinelApiClient).modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());
    Result<ApiDefinitionEntity> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<ApiDefinitionEntity>>() {
    });
    assertTrue(result.isSuccess());
    ApiDefinitionEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals("bbb", entity.getApiName());
    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());
    Set<ApiPredicateItemEntity> predicateItemEntities = entity.getPredicateItems();
    assertEquals(1, predicateItemEntities.size());
    ApiPredicateItemEntity predicateItemEntity = predicateItemEntities.iterator().next();
    assertEquals(URL_MATCH_STRATEGY_PREFIX, predicateItemEntity.getMatchStrategy().intValue());
    assertEquals("/my_order", predicateItemEntity.getPattern());
    // Verify the entity which is update in memory repository
    List<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(1, entitiesInMem.size());
    assertEquals(entity, entitiesInMem.get(0));
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ArrayList(java.util.ArrayList) ApiDefinitionEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity) 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) UpdateApiReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo) ApiPredicateItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo) ApiPredicateItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) NoAuthConfigurationTest(com.alibaba.csp.sentinel.dashboard.config.NoAuthConfigurationTest) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 14 with Result

use of com.alibaba.csp.sentinel.dashboard.domain.Result in project Sentinel by alibaba.

the class GatewayApiControllerTest method testDeleteApi.

@Test
public void testDeleteApi() throws Exception {
    String path = "/gateway/api/delete.json";
    // Add one entity into memory repository for delete
    ApiDefinitionEntity addEntity = new ApiDefinitionEntity();
    addEntity.setApp(TEST_APP);
    addEntity.setIp(TEST_IP);
    addEntity.setPort(TEST_PORT);
    addEntity.setApiName("ccc");
    Date date = new Date();
    addEntity.setGmtCreate(date);
    addEntity.setGmtModified(date);
    Set<ApiPredicateItemEntity> addRedicateItemEntities = new HashSet<>();
    addEntity.setPredicateItems(addRedicateItemEntities);
    ApiPredicateItemEntity addPredicateItemEntity = new ApiPredicateItemEntity();
    addPredicateItemEntity.setMatchStrategy(URL_MATCH_STRATEGY_EXACT);
    addPredicateItemEntity.setPattern("/user/add");
    addEntity = repository.save(addEntity);
    given(sentinelApiClient.modifyApis(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 modifyApis method has been called
    verify(sentinelApiClient).modifyApis(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<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(0, entitiesInMem.size());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ApiDefinitionEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity) 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) ApiPredicateItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) NoAuthConfigurationTest(com.alibaba.csp.sentinel.dashboard.config.NoAuthConfigurationTest) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 15 with Result

use of com.alibaba.csp.sentinel.dashboard.domain.Result in project Sentinel by alibaba.

the class GatewayApiControllerTest method testAddApi.

@Test
public void testAddApi() throws Exception {
    String path = "/gateway/api/new.json";
    AddApiReqVo reqVo = new AddApiReqVo();
    reqVo.setApp(TEST_APP);
    reqVo.setIp(TEST_IP);
    reqVo.setPort(TEST_PORT);
    reqVo.setApiName("customized_api");
    List<ApiPredicateItemVo> itemVos = new ArrayList<>();
    ApiPredicateItemVo itemVo = new ApiPredicateItemVo();
    itemVo.setMatchStrategy(URL_MATCH_STRATEGY_EXACT);
    itemVo.setPattern("/product");
    itemVos.add(itemVo);
    reqVo.setPredicateItems(itemVos);
    given(sentinelApiClient.modifyApis(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 modifyApis method has been called
    verify(sentinelApiClient).modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());
    Result<ApiDefinitionEntity> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<ApiDefinitionEntity>>() {
    });
    assertTrue(result.isSuccess());
    // Verify the result
    ApiDefinitionEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals(TEST_APP, entity.getApp());
    assertEquals(TEST_IP, entity.getIp());
    assertEquals(TEST_PORT, entity.getPort());
    assertEquals("customized_api", entity.getApiName());
    assertNotNull(entity.getId());
    assertNotNull(entity.getGmtCreate());
    assertNotNull(entity.getGmtModified());
    Set<ApiPredicateItemEntity> predicateItemEntities = entity.getPredicateItems();
    assertEquals(1, predicateItemEntities.size());
    ApiPredicateItemEntity predicateItemEntity = predicateItemEntities.iterator().next();
    assertEquals(URL_MATCH_STRATEGY_EXACT, predicateItemEntity.getMatchStrategy().intValue());
    assertEquals("/product", predicateItemEntity.getPattern());
    // Verify the entity which is add in memory repository
    List<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(1, entitiesInMem.size());
    assertEquals(entity, entitiesInMem.get(0));
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ArrayList(java.util.ArrayList) ApiDefinitionEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity) MvcResult(org.springframework.test.web.servlet.MvcResult) Result(com.alibaba.csp.sentinel.dashboard.domain.Result) MvcResult(org.springframework.test.web.servlet.MvcResult) ApiPredicateItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo) ApiPredicateItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity) AddApiReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo) NoAuthConfigurationTest(com.alibaba.csp.sentinel.dashboard.config.NoAuthConfigurationTest) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Aggregations

Result (com.alibaba.csp.sentinel.dashboard.domain.Result)26 Test (org.junit.Test)16 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)16 MvcResult (org.springframework.test.web.servlet.MvcResult)16 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)16 ApiDefinitionEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity)13 ApiPredicateItemEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity)13 ApiPredicateItemVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo)9 NoAuthConfigurationTest (com.alibaba.csp.sentinel.dashboard.config.NoAuthConfigurationTest)8 GatewayFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity)8 AddApiReqVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo)7 UpdateApiReqVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo)7 Date (java.util.Date)7 GatewayParamFlowItemEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity)6 SentinelGatewayConstants (com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants)5 AuthAction (com.alibaba.csp.sentinel.dashboard.auth.AuthAction)5 AuthService (com.alibaba.csp.sentinel.dashboard.auth.AuthService)5 SentinelApiClient (com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient)5 MachineInfo (com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo)5 InMemApiDefinitionStore (com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemApiDefinitionStore)5