Merge pull request #7399 from pallavi-priyadarshani/BAEL-3005
BAEL-3005 | Validating List objects in Spring
This commit is contained in:
commit
7befda7f22
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.validation.listvalidation;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@ComponentScan(basePackages = "com.baeldung.validation.listvalidation")
|
||||
@Configuration
|
||||
@SpringBootApplication
|
||||
public class SpringListValidationApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringListValidationApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.validation.listvalidation.constraint;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
|
||||
@Constraint(validatedBy = MaxSizeConstraintValidator.class)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MaxSizeConstraint {
|
||||
|
||||
String message() default "The input list cannot contain more than 4 movies.";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.validation.listvalidation.constraint;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
import com.baeldung.validation.listvalidation.model.Movie;
|
||||
|
||||
public class MaxSizeConstraintValidator implements ConstraintValidator<MaxSizeConstraint, List<Movie>> {
|
||||
|
||||
@Override
|
||||
public boolean isValid(List<Movie> values, ConstraintValidatorContext context) {
|
||||
boolean isValid = true;
|
||||
if (values.size() > 4) {
|
||||
isValid = false;
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.validation.listvalidation.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.validation.listvalidation.constraint.MaxSizeConstraint;
|
||||
import com.baeldung.validation.listvalidation.model.Movie;
|
||||
import com.baeldung.validation.listvalidation.service.MovieService;
|
||||
|
||||
@Validated
|
||||
@RestController
|
||||
@RequestMapping("/movies")
|
||||
public class MovieController {
|
||||
|
||||
@Autowired
|
||||
private MovieService movieService;
|
||||
|
||||
@PostMapping
|
||||
public void addAll(@RequestBody @NotEmpty(message = "Input movie list cannot be empty.") @MaxSizeConstraint List<@Valid Movie> movies) {
|
||||
movieService.addAll(movies);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.validation.listvalidation.exception;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ConstraintViolationExceptionHandler {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ConstraintViolationExceptionHandler.class);
|
||||
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
public ResponseEntity<String> handle(ConstraintViolationException constraintViolationException) {
|
||||
Set<ConstraintViolation<?>> violations = constraintViolationException.getConstraintViolations();
|
||||
String errorMessage = "";
|
||||
if (!violations.isEmpty()) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
violations.forEach(violation -> builder.append("\n" + violation.getMessage()));
|
||||
errorMessage = builder.toString();
|
||||
} else {
|
||||
errorMessage = "ConstraintViolationException occured.";
|
||||
}
|
||||
|
||||
logger.error(errorMessage);
|
||||
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.validation.listvalidation.model;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
public class Movie {
|
||||
|
||||
private String id;
|
||||
|
||||
@NotEmpty(message = "Movie name cannot be empty.")
|
||||
private String name;
|
||||
|
||||
public Movie(String name) {
|
||||
this.id = UUID.randomUUID()
|
||||
.toString();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Movie(){
|
||||
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.validation.listvalidation.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.validation.listvalidation.model.Movie;
|
||||
|
||||
@Service
|
||||
public class MovieService {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(MovieService.class);
|
||||
|
||||
private static List<Movie> moviesData;
|
||||
|
||||
static {
|
||||
moviesData = new ArrayList<>();
|
||||
|
||||
Movie m1 = new Movie("MovieABC");
|
||||
moviesData.add(m1);
|
||||
|
||||
Movie m2 = new Movie("MovieDEF");
|
||||
moviesData.add(m2);
|
||||
|
||||
}
|
||||
|
||||
public Movie get(String name) {
|
||||
Movie movie = null;
|
||||
for (Movie m : moviesData) {
|
||||
if (name.equalsIgnoreCase(m.getName())) {
|
||||
movie = m;
|
||||
logger.info("Found movie with name {} : {} ", name, movie);
|
||||
}
|
||||
}
|
||||
|
||||
return movie;
|
||||
}
|
||||
|
||||
public void add(Movie movie) {
|
||||
if (get(movie.getName()) == null) {
|
||||
moviesData.add(movie);
|
||||
logger.info("Added new movie - {}", movie.getName());
|
||||
}
|
||||
}
|
||||
|
||||
public void addAll(List<Movie> movies) {
|
||||
for (Movie movie : movies) {
|
||||
add(movie);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
package com.baeldung.validation.listvalidation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
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.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
import com.baeldung.validation.listvalidation.model.Movie;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringListValidationApplication.class)
|
||||
@AutoConfigureMockMvc
|
||||
public class MovieControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
public void givenValidMovieList_whenAddingMovieList_thenIsOK() throws Exception {
|
||||
List<Movie> movies = new ArrayList<>();
|
||||
Movie movie = new Movie("Movie3");
|
||||
movies.add(movie);
|
||||
mvc.perform(MockMvcRequestBuilders.post("/movies")
|
||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.content(objectMapper.writeValueAsString(movies)))
|
||||
.andExpect(MockMvcResultMatchers.status()
|
||||
.isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyMovieList_whenAddingMovieList_thenThrowBadRequest() throws Exception {
|
||||
List<Movie> movies = new ArrayList<>();
|
||||
mvc.perform(MockMvcRequestBuilders.post("/movies")
|
||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.content(objectMapper.writeValueAsString(movies)))
|
||||
.andExpect(MockMvcResultMatchers.status()
|
||||
.isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyMovieName_whenAddingMovieList_thenThrowBadRequest() throws Exception {
|
||||
Movie movie = new Movie("");
|
||||
mvc.perform(MockMvcRequestBuilders.post("/movies")
|
||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.content(objectMapper.writeValueAsString(Arrays.asList(movie))))
|
||||
.andExpect(MockMvcResultMatchers.status()
|
||||
.isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given5MoviesInputList_whenAddingMovieList_thenThrowBadRequest() throws Exception {
|
||||
Movie movie1 = new Movie("Movie1");
|
||||
Movie movie2 = new Movie("Movie2");
|
||||
Movie movie3 = new Movie("Movie3");
|
||||
Movie movie4 = new Movie("Movie4");
|
||||
Movie movie5 = new Movie("Movie5");
|
||||
List<Movie> movies = new ArrayList<>();
|
||||
movies.add(movie1);
|
||||
movies.add(movie2);
|
||||
movies.add(movie3);
|
||||
movies.add(movie4);
|
||||
movies.add(movie5);
|
||||
mvc.perform(MockMvcRequestBuilders.post("/movies")
|
||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.content(objectMapper.writeValueAsString(movies)))
|
||||
.andExpect(MockMvcResultMatchers.status()
|
||||
.isBadRequest());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue