@Test public void testDeleteApi() throws Exception { String path = "/gateway/api/delete.json"; 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())); MvcResult mvcResult = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn(); verify(sentinelApiClient).modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any()); Result<Long> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<Long>>() {}); assertTrue(result.isSuccess()); assertEquals(addEntity.getId(), result.getData()); List<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP); assertEquals(0, entitiesInMem.size()); }
@PostMapping("/delete.json") public Result<Long> deleteApi(HttpServletRequest request, Long id) { AuthService.AuthUser authUser = authService.getAuthUser(request); if (id == null) { return Result.ofFail(-1, "id can't be null"); } ApiDefinitionEntity oldEntity = repository.findById(id); if (oldEntity == null) { return Result.ofSuccess(null); } authUser.authTarget(oldEntity.getApp(), AuthService.PrivilegeType.DELETE_RULE); try { repository.delete(id); } catch (Throwable throwable) { logger.error("delete gateway api error:", throwable); return Result.ofThrowable(-1, throwable); } if (!publishApis(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) { logger.warn("publish gateway apis fail after delete"); } return Result.ofSuccess(id); }
GatewayApiController { @PostMapping("/delete.json") public Result<Long> deleteApi(HttpServletRequest request, Long id) { AuthService.AuthUser authUser = authService.getAuthUser(request); if (id == null) { return Result.ofFail(-1, "id can't be null"); } ApiDefinitionEntity oldEntity = repository.findById(id); if (oldEntity == null) { return Result.ofSuccess(null); } authUser.authTarget(oldEntity.getApp(), AuthService.PrivilegeType.DELETE_RULE); try { repository.delete(id); } catch (Throwable throwable) { logger.error("delete gateway api error:", throwable); return Result.ofThrowable(-1, throwable); } if (!publishApis(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) { logger.warn("publish gateway apis fail after delete"); } return Result.ofSuccess(id); } }
GatewayApiController { @PostMapping("/delete.json") public Result<Long> deleteApi(HttpServletRequest request, Long id) { AuthService.AuthUser authUser = authService.getAuthUser(request); if (id == null) { return Result.ofFail(-1, "id can't be null"); } ApiDefinitionEntity oldEntity = repository.findById(id); if (oldEntity == null) { return Result.ofSuccess(null); } authUser.authTarget(oldEntity.getApp(), AuthService.PrivilegeType.DELETE_RULE); try { repository.delete(id); } catch (Throwable throwable) { logger.error("delete gateway api error:", throwable); return Result.ofThrowable(-1, throwable); } if (!publishApis(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) { logger.warn("publish gateway apis fail after delete"); } return Result.ofSuccess(id); }  }
GatewayApiController { @PostMapping("/delete.json") public Result<Long> deleteApi(HttpServletRequest request, Long id) { AuthService.AuthUser authUser = authService.getAuthUser(request); if (id == null) { return Result.ofFail(-1, "id can't be null"); } ApiDefinitionEntity oldEntity = repository.findById(id); if (oldEntity == null) { return Result.ofSuccess(null); } authUser.authTarget(oldEntity.getApp(), AuthService.PrivilegeType.DELETE_RULE); try { repository.delete(id); } catch (Throwable throwable) { logger.error("delete gateway api error:", throwable); return Result.ofThrowable(-1, throwable); } if (!publishApis(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) { logger.warn("publish gateway apis fail after delete"); } return Result.ofSuccess(id); }  @GetMapping("/list.json") Result<List<ApiDefinitionEntity>> queryApis(HttpServletRequest request, String app, String ip, Integer port); @PostMapping("/new.json") Result<ApiDefinitionEntity> addApi(HttpServletRequest request, @RequestBody AddApiReqVo reqVo); @PostMapping("/save.json") Result<ApiDefinitionEntity> updateApi(HttpServletRequest request, @RequestBody UpdateApiReqVo reqVo); @PostMapping("/delete.json") Result<Long> deleteApi(HttpServletRequest request, Long id); }
GatewayApiController { @PostMapping("/delete.json") public Result<Long> deleteApi(HttpServletRequest request, Long id) { AuthService.AuthUser authUser = authService.getAuthUser(request); if (id == null) { return Result.ofFail(-1, "id can't be null"); } ApiDefinitionEntity oldEntity = repository.findById(id); if (oldEntity == null) { return Result.ofSuccess(null); } authUser.authTarget(oldEntity.getApp(), AuthService.PrivilegeType.DELETE_RULE); try { repository.delete(id); } catch (Throwable throwable) { logger.error("delete gateway api error:", throwable); return Result.ofThrowable(-1, throwable); } if (!publishApis(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) { logger.warn("publish gateway apis fail after delete"); } return Result.ofSuccess(id); }  @GetMapping("/list.json") Result<List<ApiDefinitionEntity>> queryApis(HttpServletRequest request, String app, String ip, Integer port); @PostMapping("/new.json") Result<ApiDefinitionEntity> addApi(HttpServletRequest request, @RequestBody AddApiReqVo reqVo); @PostMapping("/save.json") Result<ApiDefinitionEntity> updateApi(HttpServletRequest request, @RequestBody UpdateApiReqVo reqVo); @PostMapping("/delete.json") Result<Long> deleteApi(HttpServletRequest request, Long id);  }