@Test public void saveTest() throws Exception { Principal principal = Mockito.mock(Principal.class); when(principal.getName()).thenReturn("Olivia.Johnson@gmail.com"); String json = "{\n" + "\"title\": \"title\",\n" + " \"tags\": [\"news\"],\n" + " \"text\": \"content content content\", \n" + "\"source\": \"\",\n" + " \"image\": null\n" + "}"; MockMultipartFile jsonFile = new MockMultipartFile("addEcoNewsDtoRequest", "", "application/json", json.getBytes()); this.mockMvc.perform(multipart(ecoNewsLink) .file(jsonFile) .principal(principal) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()); ObjectMapper mapper = new ObjectMapper(); AddEcoNewsDtoRequest addEcoNewsDtoRequest = mapper.readValue(json, AddEcoNewsDtoRequest.class); verify(ecoNewsService) .save(eq(addEcoNewsDtoRequest), isNull(), eq("Olivia.Johnson@gmail.com")); }
@ApiOperation(value = "Add new eco news.") @ApiResponses(value = { @ApiResponse(code = 201, message = HttpStatuses.CREATED, response = AddEcoNewsDtoResponse.class), @ApiResponse(code = 303, message = HttpStatuses.SEE_OTHER), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @PostMapping(consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}) public ResponseEntity<AddEcoNewsDtoResponse> save( @ApiParam(value = "Add Eco News Request", required = true) @RequestPart @ValidEcoNewsDtoRequest AddEcoNewsDtoRequest addEcoNewsDtoRequest, @ApiParam(value = "Image of eco news") @ImageValidation @RequestPart(required = false) MultipartFile image, @ApiIgnore Principal principal) { return ResponseEntity.status(HttpStatus.CREATED).body( ecoNewsService.save(addEcoNewsDtoRequest, image, principal.getName())); }
EcoNewsController { @ApiOperation(value = "Add new eco news.") @ApiResponses(value = { @ApiResponse(code = 201, message = HttpStatuses.CREATED, response = AddEcoNewsDtoResponse.class), @ApiResponse(code = 303, message = HttpStatuses.SEE_OTHER), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @PostMapping(consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}) public ResponseEntity<AddEcoNewsDtoResponse> save( @ApiParam(value = "Add Eco News Request", required = true) @RequestPart @ValidEcoNewsDtoRequest AddEcoNewsDtoRequest addEcoNewsDtoRequest, @ApiParam(value = "Image of eco news") @ImageValidation @RequestPart(required = false) MultipartFile image, @ApiIgnore Principal principal) { return ResponseEntity.status(HttpStatus.CREATED).body( ecoNewsService.save(addEcoNewsDtoRequest, image, principal.getName())); } }
EcoNewsController { @ApiOperation(value = "Add new eco news.") @ApiResponses(value = { @ApiResponse(code = 201, message = HttpStatuses.CREATED, response = AddEcoNewsDtoResponse.class), @ApiResponse(code = 303, message = HttpStatuses.SEE_OTHER), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @PostMapping(consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}) public ResponseEntity<AddEcoNewsDtoResponse> save( @ApiParam(value = "Add Eco News Request", required = true) @RequestPart @ValidEcoNewsDtoRequest AddEcoNewsDtoRequest addEcoNewsDtoRequest, @ApiParam(value = "Image of eco news") @ImageValidation @RequestPart(required = false) MultipartFile image, @ApiIgnore Principal principal) { return ResponseEntity.status(HttpStatus.CREATED).body( ecoNewsService.save(addEcoNewsDtoRequest, image, principal.getName())); }  }
EcoNewsController { @ApiOperation(value = "Add new eco news.") @ApiResponses(value = { @ApiResponse(code = 201, message = HttpStatuses.CREATED, response = AddEcoNewsDtoResponse.class), @ApiResponse(code = 303, message = HttpStatuses.SEE_OTHER), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @PostMapping(consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}) public ResponseEntity<AddEcoNewsDtoResponse> save( @ApiParam(value = "Add Eco News Request", required = true) @RequestPart @ValidEcoNewsDtoRequest AddEcoNewsDtoRequest addEcoNewsDtoRequest, @ApiParam(value = "Image of eco news") @ImageValidation @RequestPart(required = false) MultipartFile image, @ApiIgnore Principal principal) { return ResponseEntity.status(HttpStatus.CREATED).body( ecoNewsService.save(addEcoNewsDtoRequest, image, principal.getName())); }  @ApiOperation(value = "Get three last eco news.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @GetMapping("/newest") ResponseEntity<List<EcoNewsDto>> getThreeLastEcoNews(); @ApiOperation(value = "Add new eco news.") @ApiResponses(value = { @ApiResponse(code = 201, message = HttpStatuses.CREATED, response = AddEcoNewsDtoResponse.class), @ApiResponse(code = 303, message = HttpStatuses.SEE_OTHER), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @PostMapping(consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}) ResponseEntity<AddEcoNewsDtoResponse> save(
            @ApiParam(value = "Add Eco News Request", required = true)
            @RequestPart @ValidEcoNewsDtoRequest AddEcoNewsDtoRequest addEcoNewsDtoRequest,
            @ApiParam(value = "Image of eco news")
            @ImageValidation
            @RequestPart(required = false) MultipartFile image,
            @ApiIgnore Principal principal); @ApiOperation(value = "Get eco news by id.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @GetMapping("/{id}") ResponseEntity<EcoNewsDto> getEcoNewsById(@PathVariable Long id); @ApiOperation(value = "Find all eco news by page.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @GetMapping("") @ApiPageable ResponseEntity<PageableDto<EcoNewsDto>> findAll(@ApiIgnore Pageable page); @ApiOperation(value = "Delete eco news.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 303, message = HttpStatuses.SEE_OTHER), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN) }) @DeleteMapping("/{econewsId}") ResponseEntity<Object> delete(@PathVariable Long econewsId); @ApiOperation(value = "Get eco news by tags") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @GetMapping("/tags") @ApiPageable ResponseEntity<PageableDto<EcoNewsDto>> getEcoNews(
            @ApiIgnore Pageable page,
            @ApiParam(value = "Tags to filter (if do not input tags get all)")
            @RequestParam(required = false) List<String> tags
    ); @ApiOperation(value = "Get three recommended eco news.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @GetMapping("/recommended") ResponseEntity<List<EcoNewsDto>> getThreeRecommendedEcoNews(
            @RequestParam(required = true) Long openedEcoNewsId

    ); @ApiOperation(value = "Find all eco news tags") @GetMapping("/tags/all") ResponseEntity<List<String>> findAllEcoNewsTags(); }
EcoNewsController { @ApiOperation(value = "Add new eco news.") @ApiResponses(value = { @ApiResponse(code = 201, message = HttpStatuses.CREATED, response = AddEcoNewsDtoResponse.class), @ApiResponse(code = 303, message = HttpStatuses.SEE_OTHER), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @PostMapping(consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}) public ResponseEntity<AddEcoNewsDtoResponse> save( @ApiParam(value = "Add Eco News Request", required = true) @RequestPart @ValidEcoNewsDtoRequest AddEcoNewsDtoRequest addEcoNewsDtoRequest, @ApiParam(value = "Image of eco news") @ImageValidation @RequestPart(required = false) MultipartFile image, @ApiIgnore Principal principal) { return ResponseEntity.status(HttpStatus.CREATED).body( ecoNewsService.save(addEcoNewsDtoRequest, image, principal.getName())); }  @ApiOperation(value = "Get three last eco news.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @GetMapping("/newest") ResponseEntity<List<EcoNewsDto>> getThreeLastEcoNews(); @ApiOperation(value = "Add new eco news.") @ApiResponses(value = { @ApiResponse(code = 201, message = HttpStatuses.CREATED, response = AddEcoNewsDtoResponse.class), @ApiResponse(code = 303, message = HttpStatuses.SEE_OTHER), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @PostMapping(consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}) ResponseEntity<AddEcoNewsDtoResponse> save(
            @ApiParam(value = "Add Eco News Request", required = true)
            @RequestPart @ValidEcoNewsDtoRequest AddEcoNewsDtoRequest addEcoNewsDtoRequest,
            @ApiParam(value = "Image of eco news")
            @ImageValidation
            @RequestPart(required = false) MultipartFile image,
            @ApiIgnore Principal principal); @ApiOperation(value = "Get eco news by id.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @GetMapping("/{id}") ResponseEntity<EcoNewsDto> getEcoNewsById(@PathVariable Long id); @ApiOperation(value = "Find all eco news by page.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @GetMapping("") @ApiPageable ResponseEntity<PageableDto<EcoNewsDto>> findAll(@ApiIgnore Pageable page); @ApiOperation(value = "Delete eco news.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 303, message = HttpStatuses.SEE_OTHER), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN) }) @DeleteMapping("/{econewsId}") ResponseEntity<Object> delete(@PathVariable Long econewsId); @ApiOperation(value = "Get eco news by tags") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @GetMapping("/tags") @ApiPageable ResponseEntity<PageableDto<EcoNewsDto>> getEcoNews(
            @ApiIgnore Pageable page,
            @ApiParam(value = "Tags to filter (if do not input tags get all)")
            @RequestParam(required = false) List<String> tags
    ); @ApiOperation(value = "Get three recommended eco news.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), }) @GetMapping("/recommended") ResponseEntity<List<EcoNewsDto>> getThreeRecommendedEcoNews(
            @RequestParam(required = true) Long openedEcoNewsId

    ); @ApiOperation(value = "Find all eco news tags") @GetMapping("/tags/all") ResponseEntity<List<String>> findAllEcoNewsTags();  }