WebfluxTest and MockBean - java

I've got problem with mock. It's null. Could you help me?
UsersService
public interface UsersService {
Mono<GetUserDto> findById(String id);
Mono<GetUserDto> findByUsername(String username);
Mono<CreateUserResponseDto> create(Mono<CreateUserDto> createUserDtoMono);
}
RoutingHandlers
package com.app.routing;
import com.app.dto.CreateUserDto;
import com.app.service.UsersService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
#Component
#RequiredArgsConstructor
public class RoutingHandlers {
private final UsersService usersService;
public Mono<ServerResponse> register(ServerRequest serverRequest) {
Mono<CreateUserDto> createUserDtoMono = serverRequest.bodyToMono(CreateUserDto.class);
var create = usersService.create(createUserDtoMono);
return toServerResponse(create, HttpStatus.CREATED);
}
public Mono<ServerResponse> findByUsername(ServerRequest serverRequest) {
String username = serverRequest.pathVariable("username");
return toServerResponse(usersService.findByUsername(username), HttpStatus.OK);
}
public Mono<ServerResponse> findById(ServerRequest serverRequest) {
String id = serverRequest.pathVariable("id");
return toServerResponse(usersService.findById(id), HttpStatus.OK);
}
private static <T> Mono<ServerResponse> toServerResponse(Mono<T> mono, HttpStatus status) {
return mono
.flatMap(item -> ServerResponse
.status(status)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(item)))
.doOnError(err -> {
System.out.println("---------------------------- ERROR -----------------------------");
System.out.println(err);
System.out.println("----------------------------------------------------------------");
})
.onErrorResume(e -> ServerResponse
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(e.getMessage())));
}
}
Routing
package com.app.routing;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
#Configuration
public class Routing {
#Bean
public RouterFunction<ServerResponse> routerFunction(RoutingHandlers routingHandlers) {
return nest(
path("/users"),
route(POST("").and(accept(MediaType.APPLICATION_JSON)), routingHandlers::register)
.andRoute(GET("/username/{username}").and(accept(MediaType.APPLICATION_JSON)), routingHandlers::findByUsername)
.andRoute(GET("/id/{id}").and(accept(MediaType.APPLICATION_JSON)), routingHandlers::findById)
);
}
}
RoutingTest
package com.app;
import com.app.dto.CreateUserDto;
import com.app.dto.CreateUserResponseDto;
import com.app.routing.Routing;
import com.app.routing.RoutingHandlers;
import com.app.service.UsersService;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
#ExtendWith(SpringExtension.class)
#ContextConfiguration(classes = {RoutingHandlers.class, Routing.class})
#WebFluxTest
class RoutingTest {
#Autowired
private ApplicationContext applicationContext;
#MockBean
private UsersService usersService;
private WebTestClient webTestClient;
#BeforeEach
public void setup() {
webTestClient = WebTestClient.bindToApplicationContext(applicationContext).build();
}
#Test
public void test1() {
var userBody = CreateUserDto
.builder()
.username("u")
.password("1234")
.passwordConfirmation("1234")
.build();
var body = Mono.just(userBody);
var userResponse = CreateUserResponseDto
.builder()
.id("1")
.username("u")
.build();
var response = Mono.just(userResponse);
Mockito
.when(usersService.create(body))
.thenReturn(response);
webTestClient
.post()
.uri("/users")
.accept(MediaType.APPLICATION_JSON)
.body(body, CreateUserDto.class)
.exchange()
.expectStatus().isCreated()
.expectBody(CreateUserResponseDto.class)
.value(createUserResponseDto -> {
Assertions.assertThat(createUserResponseDto)
.hasFieldOrPropertyWithValue("username", "u")
.hasFieldOrPropertyWithValue("password", "1234");
});
}
}
DTOs
#Data
#AllArgsConstructor
#NoArgsConstructor
#Builder
public class CreateUserDto {
private String username;
private String password;
private String passwordConfirmation;
}
#Data
#AllArgsConstructor
#NoArgsConstructor
#Builder
public class CreateUserResponseDto {
private String id;
private String username;
}
#Data
#AllArgsConstructor
#NoArgsConstructor
#Builder
public class GetUserDto {
private String id;
private String username;
private String password;
}
After I ran test I got error:
Cannot invoke "reactor.core.publisher.Mono.flatMap(java.util.function.Function)" because "mono" is null.
In RoutingHandlers in register method instance called create is null:
public Mono<ServerResponse> register(ServerRequest serverRequest) {
Mono<CreateUserDto> createUserDtoMono = serverRequest.bodyToMono(CreateUserDto.class);
var create = usersService.create(createUserDtoMono);
return toServerResponse(create, HttpStatus.CREATED);
}

Related

generate dynamically a service in Java

How to retrieve data from ligneCommandeDto and pass it to lignebpdto to generate dynamically a lignebpService from ligneCommandeService if affercter() is true.
the affercter() method for checking if the isAffected attribute is true, if that I call the genererbp() methode , this methode create a ligneBp so I need to fill il with data from ligneCommandeDTO.
package org.backend.gcmd.service;
import org.backend.gcmd.dto.LigneBpDTO;
import org.backend.gcmd.dto.LigneCommandeDTO;
import org.backend.gcmd.entity.LigneCommandeEntity;
import org.backend.gcmd.exceptions.technical.ObjectNotFoundException;
import org.backend.gcmd.mapper.LigneCommandeMapper;
import org.backend.gcmd.repository.LigneCommandeRepository;
import org.backend.gcmd.validator.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Optional;
import static java.util.stream.Collectors.toList;
#Service
#Transactional
public class LigneCommandeService {
#Autowired
private LigneCommandeRepository ligneCommandeRepository;
#Autowired
private LigneCommandeMapper ligneCommandeMapper;
#Autowired
private LigneBpService ligneBpService;
private LigneBpDTO ligneBpDTO;
#Autowired
private LigneBpMapper ligneBpMapper;
public LigneCommandeDTO findById(Long id) {
Validate.notNull(id, "id mus be not null");
Optional<LigneCommandeEntity> entity = ligneCommandeRepository.findById(id);
if (entity.isPresent()) {
return ligneCommandeMapper.convertToDto(entity.get());
} else {
throw new ObjectNotFoundException("LigneCommandeDTO not found");
}
}
public LigneCommandeDTO save(LigneCommandeDTO dto) {
Validate.notNull(dto, "LigneCommandeDTO must be not null");
LigneCommandeEntity entity = ligneCommandeMapper.convertToEntity(dto);
LigneCommandeEntity saved = ligneCommandeRepository.save(entity);
return ligneCommandeMapper.convertToDto(saved);
}
public LigneCommandeDTO update(LigneCommandeDTO dto) {
Validate.notNull(dto, "LigneCommandeDTO must be not null");
Validate.notNull(dto.getId(), "LigneCommandeDTO id must be not null");
findById(dto.getId());
LigneCommandeEntity entity = ligneCommandeMapper.convertToEntity(dto);
LigneCommandeEntity saved = ligneCommandeRepository.save(entity);
return ligneCommandeMapper.convertToDto(saved);
}
public Page<LigneCommandeDTO> findAllByDeletedFalse(Pageable pageable) {
Page<LigneCommandeEntity> page = ligneCommandeRepository.findAllByDeletedFalse(pageable);
return ligneCommandeMapper.convertToPageDto(page);
}
public LigneCommandeDTO affecter(Long id, Boolean isAffected) {
Validate.notNull(id, "LigneCommandeDTO must be not null");
Validate.notNull(isAffected, "LigneCommandeDTO id must be not null");
LigneCommandeDTO lcdto = findById(id);
lcdto.setIsAffected(isAffected);
update(lcdto);
genererbp();
return null;
}
public LigneBpDTO genererbp(LigneCommandeDTO ligneCommandeDTO) {
if (ligneCommandeDTO.getIsAffected()) {
return ligneBpService.findAllByDeletedFalse((Pageable) ligneBpDTO)
.stream()
.map(ligneBpMapper::convertToDto)
.collect(toList());
}
return null;
}
}
the mapper class
package org.backend.gcmd.mapper;
import org.springframework.data.domain.Page;
import java.util.List;
public interface Mapper<D, E> {
Page<D> convertToPageDto(Page<E> page);
D convertToDto(E entity);
E convertToEntity(D dto);
List<D> convertToDtoList(List<E> entities);
List<E> convertToEntitiesList(List<D> entities);
}
package org.backend.gcmd.mapper;
import org.backend.gcmd.dto.LigneBpDTO;
import org.backend.gcmd.entity.LigneBpEntity;
import org.backend.gcmd.repository.BulltinPrestationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
#Component
public class LigneBpMapper implements Mapper<LigneBpDTO, LigneBpEntity> {
#Autowired
BulltinPrestationRepository bulltinprestationRepository;
#Override
public Page<LigneBpDTO> convertToPageDto(Page<LigneBpEntity> page) {
return page.map(this::convertToDto);
}
#Override
public LigneBpDTO convertToDto(LigneBpEntity entity) {
LigneBpDTO dto = new LigneBpDTO();
dto.setId(entity.getId());
dto.setDate(entity.getDate());
dto.setHeure(entity.getHeure());
dto.setNombre(entity.getNombre());
dto.setPrestation(entity.getPrestation());
dto.setProduit(entity.getProduit());
dto.setSensTrafic(entity.getSensTrafic());
dto.setTarifUnifie(entity.getTarifUnifie());
dto.setTcConv(entity.getTcConv());
dto.setTcSuppl(entity.getTcSuppl());
dto.setTonnageMinimum(entity.getTonnageMinimum());
dto.setTonnageReel(entity.getTonnageReel());
return dto;
}
#Override
public LigneBpEntity convertToEntity(LigneBpDTO dto) {
LigneBpEntity entity = new LigneBpEntity();
entity.setId(dto.getId());
entity.setDate(dto.getDate());
entity.setHeure(dto.getHeure());
entity.setNombre(dto.getNombre());
entity.setPrestation(dto.getPrestation());
entity.setProduit(dto.getProduit());
entity.setSensTrafic(dto.getSensTrafic());
entity.setTarifUnifie(dto.getTarifUnifie());
entity.setTcConv(dto.getTcConv());
entity.setTcSuppl(dto.getTcSuppl());
entity.setTonnageMinimum(dto.getTonnageMinimum());
entity.setTonnageReel(dto.getTonnageReel());
return entity;
}
#Override
public List<LigneBpDTO> convertToDtoList(List<LigneBpEntity> entities) {
return entities.stream().map(this::convertToDto).collect(Collectors.toCollection(ArrayList::new));
}
#Override
public List<LigneBpEntity> convertToEntitiesList(List<LigneBpDTO> dtos) {
return dtos.stream().map(this::convertToEntity).collect(Collectors.toCollection(ArrayList::new));
}
}
controller
package org.backend.gcmd.controller;
import org.backend.gcmd.dto.LigneBpDTO;
import org.backend.gcmd.dto.LigneCommandeDTO;
import org.backend.gcmd.service.LigneBpService;
import org.backend.gcmd.service.LigneCommandeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
#RestController
#RequestMapping("/api/gcmd/v1/ligneCommandes")
public class LigneCommandeController {
#Autowired
private LigneCommandeService ligneCommandeService;
#GetMapping("{id}")
public ResponseEntity<LigneCommandeDTO> findById(#PathVariable Long id) {
LigneCommandeDTO ligneCommandeDTO = ligneCommandeService.findById(id);
return ResponseEntity.status(HttpStatus.OK).body(ligneCommandeDTO);
}
#PostMapping
public ResponseEntity<LigneCommandeDTO> save(#RequestBody LigneCommandeDTO ligneCommandeDTO) {
return ResponseEntity.status(HttpStatus.CREATED).body(ligneCommandeService.save(ligneCommandeDTO));
}
#PutMapping("{id}")
public ResponseEntity<LigneCommandeDTO> update(#PathVariable Long id,
#RequestBody LigneCommandeDTO ligneCommandeDTO) {
ligneCommandeDTO.setId(id);
return ResponseEntity.status(HttpStatus.ACCEPTED).body(ligneCommandeService.update(ligneCommandeDTO));
}
#GetMapping
public ResponseEntity<Page<LigneCommandeDTO>> findAllByDeletedFalse(Pageable pageable) {
return ResponseEntity.status(HttpStatus.OK).body(ligneCommandeService.findAllByDeletedFalse(pageable));
}
}

what is the problem using java spring boot

I try to search all data using java-spring-boot with Mysql but when i run the code i got the this error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'emp0_.get_company_name' in 'field list'
This is my Controller code
package com.example.rest.controller;
import com.example.rest.Emp;
import com.example.rest.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
#RequestMapping("emp")
public class EmpController {
#Autowired
private EmpService empService;
#GetMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<List<Emp>> getAllEmps() {
List<Emp> emps = empService.findAll();
return new ResponseEntity<List<Emp>>(emps,HttpStatus.OK);
}
}
This is my EmpRepository code
package com.example.rest.repository;
import com.example.rest.Emp;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface EmpRepository extends JpaRepository<Emp, Integer> {
}
This is my EmpService Code
package com.example.rest.service;
import com.example.rest.Emp;
import java.util.List;
public interface EmpService {
List<Emp> findAll();
}
This is my EmpServiceImpl Code
package com.example.rest.service;
import com.example.rest.Emp;
import com.example.rest.repository.EmpRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
#Service
public class EmpServiceImpl implements EmpService {
#Autowired
private EmpRepository empRepository;
#Override
public List<Emp> findAll() {
List<Emp> emps= new ArrayList<>();
empRepository.findAll().forEach((e -> emps.add(e)));
return emps;
}
}
This is my Application Code
package com.example.rest;
import com.example.rest.repository.EmpRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
#SpringBootApplication
#Configuration
public class Application {
#Autowired
EmpRepository empRepository;
public static void main(String[] args) {
SpringApplication.run(Application.class , args);
}
}
This is my Emp Code
package com.example.rest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
#Entity
#Table(name = "company")
#Data
#AllArgsConstructor
#NoArgsConstructor
public class Emp implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue (strategy = GenerationType.IDENTITY)
private Integer idx;
private String company_id;
private String getCompany_name;
}
So if someone knows that what is the problem please tell me if you don't remind with solution
Thank you!
Use #Service annotation on the EmpServiceImpl class and #Repository annotation on the EmpRepository.
#Service
public class EmpServiceImpl implements EmpService {
#Autowired
private EmpRepository empRepository;
#Override
public List<Emp> findAll() {
List<Emp> emps= new ArrayList<>();
empRepository.findAll().forEach((e -> emps.add(e)));
return emps;
}
}
#Repository
public interface EmpRepository extends JpaRepository<Emp, Integer> {
}
Need to add annotation on below class:
#Service
class EmpServiceImpl implements EmpService {
}

MockMvc empty response/return

I just created a simple integration test with MockMvc for a controller. All works well, but there is no response provided even the controller method returns something.
Here is the Controller:
import depmgmt.service.deposit.HouseService;
import depmgmt.service.dto.deposit.HouseDTO;
import depmgmt.serviceweb.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
#Controller
#RequestMapping("/rest/house")
public class HouseRestController extends BaseController {
private HouseService houseService;
#Autowired
public HouseRestController(HouseService houseService) {
this.houseService = houseService;
}
#RequestMapping(value = "/", method = RequestMethod.GET)
public List<HouseDTO> getHouses(#RequestParam(value = "page", defaultValue = "1") int page,
#RequestParam(value = "size", defaultValue = "50") int size) {
validatePageRequest(page, size);
return houseService.getHouseList(page, size);
}
}
Here is the test controller:
import com.fasterxml.jackson.databind.ObjectMapper;
import depmgmt.service.deposit.HouseService;
import depmgmt.service.dto.deposit.HouseDTO;
import depmgmt.serviceweb.config.SpringServiceWebConfig;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
#ExtendWith(SpringExtension.class)
#ContextConfiguration(classes = {SpringServiceWebConfig.class})
#WebMvcTest(controllers = HouseRestController.class)
public class HouseRestControllerTest {
#Autowired
private MockMvc mockMvc;
#Autowired
private ObjectMapper objectMapper;
#MockBean
private HouseService houseService;
#Test
public void shouldGetHouses() throws Exception {
//given
List<HouseDTO> houseDTOS = new ArrayList<>();
HouseDTO houseDTO = HouseDTO.builder().country(null).city("City").owner(null).name("MyHouse").id(1l).address("HouseAddress").build();
houseDTOS.add(houseDTO);
when(houseService.getHouseList(1, 1)).thenReturn(houseDTOS);
//when
MvcResult mvcResult = mockMvc.perform(get("/rest/house/")
.contentType("application/json")
.param("page", "1")
.param("size", "1")).andReturn();
System.out.println(mvcResult.getResponse().getContentAsString());
}
}
When I execute the test the controller method is called successfully and it returns what it should:
HouseDTO houseDTO = HouseDTO.builder().country(null).city("City").owner(null).name("MyHouse").id(1l).address("HouseAddress").build();
houseDTOS.add(houseDTO);
when(houseService.getHouseList(1, 1)).thenReturn(houseDTOS);
However in the test Controller the: mvcResult.getResponse().getContentAsString() returns empty string:
What is wrong in the test?
The mistake what that there is no #ResponseBody on the method.
So either #RestController on the Class or #Controller on the class and #ResponseBody on the method.

How to write a unit test for OneToMany connection (testing Rest endpoint)?

I'm a beginner in programming, so please forgive me if I'm asking a trivial question.
My question is, how can I make my testing method be able to check the UserModel's OneToMany connection related to the CalendarModel. I want to check, that the list contains the proper Entity, so all the 4 fields of the UserModel were tested.
My UserModel:
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
#Entity
public class UserModel {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#OneToMany(mappedBy = "id")
#JsonIgnore
private List<CalendarModel> calendarModels;
private String username;
private String password;
public UserModel(long id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public UserModel() {
}
public long getId() {
return id;
}
//Other Getters and Setters
My CalendarModel:
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
#Entity
public class CalendarModel {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#ManyToOne(cascade = CascadeType.MERGE)
private UserModel userModel;
private String googleId;
private String summary;
public CalendarModel() {
}
public CalendarModel(UserModel userModel, String googleId, String summary) {
this.userModel = userModel;
this.googleId = googleId;
this.summary = summary;
}
public Long getId() {
return id;
}
//Other Getters and Setters
My RestController with the endpoint:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
public class UserController {
private UserServiceImpl service;
#Autowired
public UserController(UserServiceImpl service) {
this.service = service;
}
#GetMapping("/api/users")
public ResponseEntity<Object> allUser() {
List<UserModel> userModelList = service.listUsers();
if (!userModelList.isEmpty() && userModelList != null) {
return new ResponseEntity<>(userModelList, HttpStatus.OK);
}
return new ResponseEntity<>("Error: users not found!", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
My UserModelBuilder class for testing purposes:
public class UserModelBuilder {
long id = 1;
String userName = "user";
String password = "password";
public UserModelBuilder() {
}
public UserModelBuilder withId(long id) {
this.id = id;
return this;
}
public UserModelBuilder withUsername(String userName) {
this.userName = userName;
return this;
}
public UserModelBuilder withPassword(String password) {
this.password = password;
return this;
}
public UserModel build() {
return new UserModel(id, userName, password);
}
}
My test (currently working properly):
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.Arrays;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
#RunWith(SpringRunner.class)
#WebMvcTest(UserController.class)
public class UserControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private UserServiceImpl service;
#Test
public void listAllUserTest() throws Exception {
UserModel firstUser = new UserModelBuilder()
.withId(1)
.withPassword("password")
.withUsername("firstuser")
.build();
UserModel secondUser = new UserModelBuilder()
.withId(2)
.withPassword("otherpass")
.withUsername("seconduser")
.build();
Mockito.when(service.listUsers()).thenReturn(Arrays.asList(firstUser, secondUser));
MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.get("/api/users")
.accept(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].id", is(1)))
.andExpect(jsonPath("$[0].password", is("password")))
.andExpect(jsonPath("$[0].username", is("firstuser")))
.andExpect(jsonPath("$[1].id", is(2)))
.andExpect(jsonPath("$[1].password", is("otherpass")))
.andExpect(jsonPath("$[1].username", is("seconduser")))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
Mockito.verify(service).listUsers();
}
}
Thank you in advance! :)

Spring StandardMultipartHttpServletRequest validation

Is there any possibility to validate StandardMultipartHttpServletRequest using standard #Valid annotation and custom Validator?
I've implemented such validator, annotated method param in controller the validator is not invoked.
I've figured it out myself. To make it work you need a DTO:
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
#Getter
#Setter
public class NewOrderFilesDTO {
List<MultipartFile> files;
}
Then, a validator:
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import static org.springframework.util.CollectionUtils.isEmpty;
#Component
public class NewOrderFilesValidator implements Validator {
private static final String MIME_TYPE_PDF = "application/pdf";
private static final long ALLOWED_SIZE = 3 * 1024 * 1024;
#Override
public void validate(Object target, Errors errors) {
if (target == null) {
return;
}
NewOrderFilesDTO newOrderFilesDTO = (NewOrderFilesDTO) target;
List<MultipartFile> newOrderFiles = newOrderFilesDTO.getFiles();
if (isEmpty(newOrderFiles)) {
return;
}
for (MultipartFile file : newOrderFiles) {
if (!MIME_TYPE_PDF.equals(file.getContentType())) {
errors.rejectValue(file.getName(), file.getName(), "'application/pdf' files allowed only!");
}
if (file.getSize() > ALLOWED_SIZE) {
errors.rejectValue(file.getName(), file.getName(), "File size allowed up to 3MB!");
}
}
}
#Override
public boolean supports(Class<?> cls) {
return NewOrderFilesDTO.class.equals(cls);
}
}
And finally a controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.validation.Valid;
import static org.springframework.http.HttpStatus.NO_CONTENT;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
#Controller
class OrderController {
private final NewOrderFilesValidator newOrderFilesValidator;
#Autowired
OrderController(NewOrderFilesValidator newOrderFilesValidator) {
this.newOrderFilesValidator = newOrderFilesValidator;
}
#InitBinder("newOrderFiles")
void initOrderFilesBinder(WebDataBinder binder) {
binder.addValidators(newOrderFilesValidator);
}
#ResponseStatus(NO_CONTENT)
#RequestMapping(value = ORDERS_PATH, method = POST, consumes = MULTIPART_FORM_DATA_VALUE)
void createOrder(
#Valid #ModelAttribute NewOrderFilesDTO newOrderFiles
) {
}
}
With the configuration above the DTO will be validated automatically by spring.

Categories