3005 | updated custom constraint and test case

* Create MaxSizeConstraint.java

* Delete CustomConstraintValidator.java

* Delete CustomConstraint.java

* Create MaxSizeConstraintValidator.java

* Update MovieController.java

* Update MovieControllerIntegrationTest.java
This commit is contained in:
Pallavi Priyadarshani 2019-08-10 14:58:43 +05:30 committed by GitHub
parent 077f38e318
commit b3af5e65b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 21 deletions

View File

@ -6,11 +6,11 @@ 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 {
public @interface MaxSizeConstraint {
String message() default "Movie name should start with an alphanumeric character.";
String message() default "The input list cannot contain more than 4 movies.";
Class<?>[] groups() default {};

View File

@ -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<CustomConstraint, List<Movie>> {
public class MaxSizeConstraintValidator implements ConstraintValidator<MaxSizeConstraint, List<Movie>> {
@Override
public boolean isValid(List<Movie> values, ConstraintValidatorContext context) {
@ -18,13 +17,8 @@ public class CustomConstraintValidator implements ConstraintValidator<CustomCons
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("Movie list cannot be empty.")
.addConstraintViolation();
}
String regex = "[^A-Za-z0-9].*";
for (Movie movie : values) {
if (Pattern.matches(regex, movie.getName())) {
} else if (values.size() > 4) {
isValid = false;
break;
}
}
return isValid;
}

View File

@ -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);
}
}

View File

@ -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<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(Arrays.asList(movie))))
.content(objectMapper.writeValueAsString(movies)))
.andExpect(MockMvcResultMatchers.status()
.isBadRequest());
}