AccountController { @PreAuthorize("hasRole('ROLE_ADMIN')") @RequestMapping(value = "/user", method = RequestMethod.POST) @ResponseBody public ResponseEntity<InputStreamResource> createUser(@RequestBody OadrUserCreateDto dto, HttpServletResponse response) { OadrUser findOneByUsername = oadrUserService.findByUsername(dto.getUsername()); if (findOneByUsername != null) { LOGGER.warn("User: " + dto.getUsername() + " already exists"); return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE_406) .contentType(OCTET_STREAM_MEDIA_TYPE).body(null); } OadrUser prepare = oadrUserService.prepare(dto); ResponseEntity<InputStreamResource> body = null; try { Optional<File> generateCertificateIfRequired = oadrUserService.generateCertificateIfRequired(dto, prepare); if (generateCertificateIfRequired.isPresent()) { InputStreamResource resource = new InputStreamResource( new FileInputStream(generateCertificateIfRequired.get())); body = ResponseEntity.status(HttpStatus.CREATED_201) .header("Content-Disposition", "attachment; filename=\"archive.tar\"") .header("x-Username", prepare.getUsername()) .contentLength(generateCertificateIfRequired.get().length()) .contentType(OCTET_STREAM_MEDIA_TYPE).body(resource); } else { body = ResponseEntity.status(HttpStatus.CREATED_201).header("x-username", prepare.getUsername()) .body(null); } } catch (GenerateX509VenException e) { LOGGER.error("", e); response.setStatus(HttpStatus.NOT_ACCEPTABLE_406); } catch (FileNotFoundException e) { LOGGER.error("", e); response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); } oadrUserService.save(prepare); LOGGER.info("Create User: " + prepare.getUsername()); return body; }  @RequestMapping(value = "/", method = RequestMethod.GET) @ResponseBody @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_DEVICE_MANAGER') or hasRole('ROLE_DRPROGRAM')") AbstractUserWithRoleDto registeredUser(Principal principal); @PreAuthorize("hasRole('ROLE_ADMIN')") @RequestMapping(value = "/user", method = RequestMethod.GET) @ResponseBody List<OadrUserDto> listUser(); @PreAuthorize("hasRole('ROLE_ADMIN')") @RequestMapping(value = "/user", method = RequestMethod.POST) @ResponseBody ResponseEntity<InputStreamResource> createUser(@RequestBody OadrUserCreateDto dto,
			HttpServletResponse response); @PreAuthorize("hasRole('ROLE_ADMIN')") @RequestMapping(value = "/user/{username}", method = RequestMethod.DELETE) @ResponseBody void deleteUser(@PathVariable("username") String username, HttpServletResponse response); @PreAuthorize("hasRole('ROLE_ADMIN')") @RequestMapping(value = "/app", method = RequestMethod.GET) @ResponseBody List<OadrAppDto> listApp(); @PreAuthorize("hasRole('ROLE_ADMIN')") @RequestMapping(value = "/app", method = RequestMethod.POST) @ResponseBody ResponseEntity<InputStreamResource> createApp(@RequestBody OadrAppCreateDto dto,
			HttpServletResponse response); @PreAuthorize("hasRole('ROLE_ADMIN')") @RequestMapping(value = "/app/{username}", method = RequestMethod.DELETE) @ResponseBody void deleteApp(@PathVariable("username") String username, HttpServletResponse response);  }
@Test public void createUserTest() throws Exception { OadrUserCreateDto dto = new OadrUserCreateDto(); dto.setCommonName("admin"); dto.setUsername("admin"); dto.setAuthenticationType("login"); dto.setPassword("pass"); byte[] content = mapper.writeValueAsBytes(dto); this.mockMvc .perform(MockMvcRequestBuilders.post(ACCOUNT_USER_URL).content(content) .header("Content-Type", "application/json").with(user)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.FORBIDDEN_403)); this.mockMvc .perform(MockMvcRequestBuilders.post(ACCOUNT_USER_URL).content(content) .header("Content-Type", "application/json").with(admin)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.NOT_ACCEPTABLE_406)); dto = new OadrUserCreateDto(); dto.setCommonName("myuser"); dto.setUsername("myuser"); dto.setAuthenticationType("login"); dto.setPassword("pass"); content = mapper.writeValueAsBytes(dto); MvcResult andReturn = this.mockMvc .perform(MockMvcRequestBuilders.post(ACCOUNT_USER_URL).content(content) .header("Content-Type", "application/json").with(admin)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.CREATED_201)).andReturn(); assertEquals(0, andReturn.getResponse().getContentLength()); OadrUser findByUsername = oadrUserService.findByUsername("myuser"); assertNotNull(findByUsername); oadrUserService.delete(findByUsername); dto = new OadrUserCreateDto(); dto.setCommonName("myuser"); dto.setUsername("myuser"); dto.setAuthenticationType("x509"); content = mapper.writeValueAsBytes(dto); andReturn = this.mockMvc .perform(MockMvcRequestBuilders.post(ACCOUNT_USER_URL).content(content) .header("Content-Type", "application/json").with(admin)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.CREATED_201)).andReturn(); assertEquals(0, andReturn.getResponse().getContentLength()); findByUsername = oadrUserService.findByUsername("myuser"); assertNotNull(findByUsername); oadrUserService.delete(findByUsername); dto = new OadrUserCreateDto(); dto.setCommonName("myuser"); dto.setAuthenticationType("x509"); dto.setNeedCertificateGeneration("rsa"); content = mapper.writeValueAsBytes(dto); andReturn = this.mockMvc .perform(MockMvcRequestBuilders.post(ACCOUNT_USER_URL).content(content) .header("Content-Type", "application/json").with(admin)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.CREATED_201)).andReturn(); assertNotEquals(0, andReturn.getResponse().getContentLength()); assertNotNull(andReturn.getResponse().getHeader("x-username")); findByUsername = oadrUserService.findByUsername(andReturn.getResponse().getHeader("x-username")); assertNotNull(findByUsername); oadrUserService.delete(findByUsername); dto = new OadrUserCreateDto(); dto.setCommonName("myuser"); dto.setAuthenticationType("x509"); dto.setNeedCertificateGeneration("ecc"); content = mapper.writeValueAsBytes(dto); andReturn = this.mockMvc .perform(MockMvcRequestBuilders.post(ACCOUNT_USER_URL).content(content) .header("Content-Type", "application/json").with(admin)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.CREATED_201)).andReturn(); assertNotEquals(0, andReturn.getResponse().getContentLength()); assertNotNull(andReturn.getResponse().getHeader("x-username")); findByUsername = oadrUserService.findByUsername(andReturn.getResponse().getHeader("x-username")); assertNotNull(findByUsername); oadrUserService.delete(findByUsername); }