BotRestartController { @PreAuthorize("hasRole('ADMIN')") @PostMapping(value = RESTART_RESOURCE_PATH) public ResponseEntity<String> restart(@ApiIgnore Principal principal) { LOG.info( () -> "POST " + RESTART_RESOURCE_PATH + " - restart() - caller: " + principal.getName()); final Object status = botRestartService.restart(); LOG.info(() -> "Response: " + status.toString()); return new ResponseEntity<>(status.toString(), null, HttpStatus.OK); } @Autowired  BotRestartController(BotRestartService botRestartService); @PreAuthorize("hasRole('ADMIN')") @PostMapping(value = RESTART_RESOURCE_PATH) ResponseEntity<String> restart(@ApiIgnore Principal principal);  }
@Test public void testBotRestartWithAdminTokenAuthorized() throws Exception { given(botRestartService.restart()).willReturn(BOT_STATUS); mockMvc .perform( post(RESTART_ENDPOINT_URI) .header( "Authorization", "Bearer " + getJwt(VALID_ADMIN_NAME, VALID_ADMIN_PASSWORD))) .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$").value(BOT_STATUS)); verify(botRestartService, times(1)).restart(); }
@Test public void testBotRestartWithUserTokenForbidden() throws Exception { given(botRestartService.restart()).willReturn(BOT_STATUS); mockMvc .perform( post(RESTART_ENDPOINT_URI) .header( "Authorization", "Bearer " + getJwt(VALID_USER_NAME, VALID_USER_PASSWORD))) .andDo(print()) .andExpect(status().isForbidden()); verify(botRestartService, times(0)).restart(); }