use of com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo 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));
}
use of com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo in project spring-boot-student by wyh-spring-ecosystem-student.
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));
}
Aggregations