diff --git a/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/CustomConstraint.java b/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/MaxSizeConstraint.java similarity index 64% rename from spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/CustomConstraint.java rename to spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/MaxSizeConstraint.java index 033df5a25b..b5adab5d86 100644 --- a/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/CustomConstraint.java +++ b/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/MaxSizeConstraint.java @@ -6,13 +6,13 @@ import java.lang.annotation.RetentionPolicy; import javax.validation.Constraint; import javax.validation.Payload; -@Constraint(validatedBy = CustomConstraintValidator.class) +@Constraint(validatedBy = MaxSizeConstraintValidator.class) @Retention(RetentionPolicy.RUNTIME) -public @interface CustomConstraint { - - String message() default "Movie name should start with an alphanumeric character."; +public @interface MaxSizeConstraint { + String message() default "The input list cannot contain more than 4 movies."; + Class[] groups() default {}; - + Class[] payload() default {}; } diff --git a/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/CustomConstraintValidator.java b/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/MaxSizeConstraintValidator.java similarity index 65% rename from spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/CustomConstraintValidator.java rename to spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/MaxSizeConstraintValidator.java index 3e80a79afe..813ea3e657 100644 --- a/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/CustomConstraintValidator.java +++ b/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/constraint/MaxSizeConstraintValidator.java @@ -1,14 +1,13 @@ package com.baeldung.validation.listvalidation.constraint; import java.util.List; -import java.util.regex.Pattern; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import com.baeldung.validation.listvalidation.model.Movie; -public class CustomConstraintValidator implements ConstraintValidator> { +public class MaxSizeConstraintValidator implements ConstraintValidator> { @Override public boolean isValid(List values, ConstraintValidatorContext context) { @@ -18,15 +17,10 @@ public class CustomConstraintValidator implements ConstraintValidator 4) { + isValid = false; } return isValid; } - + } diff --git a/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/controller/MovieController.java b/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/controller/MovieController.java index b8db850809..45639cf303 100644 --- a/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/controller/MovieController.java +++ b/spring-mvc-simple-2/src/main/java/com/baeldung/validation/listvalidation/controller/MovieController.java @@ -12,7 +12,7 @@ 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.CustomConstraint; +import com.baeldung.validation.listvalidation.constraint.MaxSizeConstraint; import com.baeldung.validation.listvalidation.model.Movie; import com.baeldung.validation.listvalidation.service.MovieService; @@ -25,7 +25,7 @@ public class MovieController { private MovieService movieService; @PostMapping - public void addAll(@RequestBody @NotEmpty(message = "Input movie list cannot be empty.") @CustomConstraint List<@Valid Movie> movie) { - movieService.addAll(movie); + public void addAll(@RequestBody @NotEmpty(message = "Input movie list cannot be empty.") @MaxSizeConstraint List<@Valid Movie> movies) { + movieService.addAll(movies); } } diff --git a/spring-mvc-simple-2/src/test/java/com/baeldung/validation/listvalidation/MovieControllerIntegrationTest.java b/spring-mvc-simple-2/src/test/java/com/baeldung/validation/listvalidation/MovieControllerIntegrationTest.java index dd8685b54c..cddc6c6bd9 100644 --- a/spring-mvc-simple-2/src/test/java/com/baeldung/validation/listvalidation/MovieControllerIntegrationTest.java +++ b/spring-mvc-simple-2/src/test/java/com/baeldung/validation/listvalidation/MovieControllerIntegrationTest.java @@ -61,11 +61,21 @@ public class MovieControllerIntegrationTest { } @Test - public void givenInvalidMovieName_whenAddingMovieList_thenThrowBadRequest() throws Exception { - Movie movie = new Movie("$Movie2"); + 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 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(Arrays.asList(movie)))) + .content(objectMapper.writeValueAsString(movies))) .andExpect(MockMvcResultMatchers.status() .isBadRequest()); }