All Downloads are FREE. Search and download functionalities are using the official Maven repository.

generator.server.springboot.mvc.web.test.BeanValidationTest.mustache Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package {{packageName}};

import static org.assertj.core.api.Assertions.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

@UnitTest
class BeanValidationTest {

  private static final String ROOT_PACKAGE = "{{packageName}}";
  private static final Set EXCLUDED_CONTROLLERS = Set.of(
    "ExceptionTranslatorTestController",
    "AuthenticationResource",
    "DummyResource"
  );
  private static final Set OBJECT_METHODS = Arrays.stream(Object.class.getMethods()).collect(Collectors.toUnmodifiableSet());
  private static final Set> controllers = new Reflections(
    new ConfigurationBuilder()
      .setUrls(ClasspathHelper.forPackage(ROOT_PACKAGE))
      .setScanners(Scanners.TypesAnnotated, Scanners.SubTypes)
      .filterInputsBy(new FilterBuilder().includePackage(ROOT_PACKAGE))
  ).getTypesAnnotatedWith(RestController.class);

  @Test
  void shouldHaveValidatedAnnotationForAllParameters() {
    controllers
      .stream()
      .filter(controller -> !EXCLUDED_CONTROLLERS.contains(controller.getSimpleName()))
      .flatMap(toMethods())
      .filter(visibleMethods())
      .filter(controllerMethods())
      .forEach(checkValidatedAnnotation());
  }

  private Function, Stream> toMethods() {
    return controller -> Arrays.stream(controller.getMethods());
  }

  private Predicate visibleMethods() {
    return method -> !Modifier.isPrivate(method.getModifiers());
  }

  private Predicate controllerMethods() {
    return method -> !OBJECT_METHODS.contains(method);
  }

  private Consumer checkValidatedAnnotation() {
    return method ->
      Arrays.stream(method.getParameters())
        .filter(checkedTypes())
        .forEach(parameter ->
          assertThat(parameter.getAnnotations())
            .as(errorMessage(method, parameter))
            .extracting(Annotation::annotationType)
            .anyMatch(Validated.class::equals)
        );
  }

  private String errorMessage(Method method, Parameter parameter) {
    return (
      "Missing @Validated annotation in " +
      method.getDeclaringClass().getSimpleName() +
      " on method " +
      method.getName() +
      " parameter of type " +
      parameter.getType().getSimpleName()
    );
  }

  private Predicate checkedTypes() {
    return parameter -> {
      Class parameterClass = parameter.getType();
      return !parameterClass.isPrimitive() && parameterClass.getName().startsWith(ROOT_PACKAGE);
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy