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:
parent
077f38e318
commit
b3af5e65b7
|
@ -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<? extends Payload>[] payload() default {};
|
||||
}
|
|
@ -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,15 +17,10 @@ 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())) {
|
||||
isValid = false;
|
||||
break;
|
||||
}
|
||||
} else if (values.size() > 4) {
|
||||
isValid = false;
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue