AnnotatedServiceFactory { @VisibleForTesting static List<DecoratorAndOrder> collectDecorators(Class<?> clazz, Method method) { final List<DecoratorAndOrder> decorators = new ArrayList<>(); collectDecorators(decorators, AnnotationUtil.getAllAnnotations(clazz)); collectDecorators(decorators, AnnotationUtil.getAllAnnotations(method)); decorators.sort(Comparator.comparing(DecoratorAndOrder::order)); return decorators; } private  AnnotatedServiceFactory(); static List<AnnotatedServiceElement> find(
            String pathPrefix, Object object, boolean useBlockingTaskExecutor,
            List<RequestConverterFunction> requestConverterFunctions,
            List<ResponseConverterFunction> responseConverterFunctions,
            List<ExceptionHandlerFunction> exceptionHandlerFunctions);  }
@Test void ofNoOrdering() throws NoSuchMethodException { final List<DecoratorAndOrder> list = collectDecorators(TestClass.class, TestClass.class.getMethod("noOrdering")); assertThat(values(list)).containsExactly(Decorator1.class, LoggingDecoratorFactoryFunction.class, LoggingDecoratorFactoryFunction.class, Decorator2.class); assertThat(orders(list)).containsExactly(0, 0, 0, 0); final LoggingDecorator info = (LoggingDecorator) list.get(1).annotation(); assertThat(info.requestLogLevel()).isEqualTo(LogLevel.INFO); final LoggingDecorator trace = (LoggingDecorator) list.get(2).annotation(); assertThat(trace.requestLogLevel()).isEqualTo(LogLevel.TRACE); }
@Test void ofMethodScopeOrdering() throws NoSuchMethodException { final List<DecoratorAndOrder> list = collectDecorators(TestClass.class, TestClass.class.getMethod("methodScopeOrdering")); assertThat(values(list)).containsExactly(Decorator1.class, LoggingDecoratorFactoryFunction.class, RateLimitingDecoratorFactoryFunction.class, Decorator1.class, LoggingDecoratorFactoryFunction.class, Decorator2.class); assertThat(orders(list)).containsExactly(0, 0, 0, 1, 2, 3); final LoggingDecorator info = (LoggingDecorator) list.get(1).annotation(); assertThat(info.requestLogLevel()).isEqualTo(LogLevel.INFO); final RateLimitingDecorator limit = (RateLimitingDecorator) list.get(2).annotation(); assertThat(limit.value()).isEqualTo(1); final LoggingDecorator trace = (LoggingDecorator) list.get(4).annotation(); assertThat(trace.requestLogLevel()).isEqualTo(LogLevel.TRACE); }
@Test void ofGlobalScopeOrdering() throws NoSuchMethodException { final List<DecoratorAndOrder> list = collectDecorators(TestClass.class, TestClass.class.getMethod("globalScopeOrdering")); assertThat(values(list)).containsExactly(LoggingDecoratorFactoryFunction.class, Decorator1.class, LoggingDecoratorFactoryFunction.class, Decorator2.class); assertThat(orders(list)).containsExactly(-1, 0, 0, 1); final LoggingDecorator trace = (LoggingDecorator) list.get(0).annotation(); assertThat(trace.requestLogLevel()).isEqualTo(LogLevel.TRACE); final LoggingDecorator info = (LoggingDecorator) list.get(2).annotation(); assertThat(info.requestLogLevel()).isEqualTo(LogLevel.INFO); }
@Test void ofUserDefinedRepeatableDecorator() throws NoSuchMethodException { final List<DecoratorAndOrder> list = collectDecorators(TestClass.class, TestClass.class.getMethod("userDefinedRepeatableDecorator")); assertThat(values(list)).containsExactly(Decorator1.class, LoggingDecoratorFactoryFunction.class, UserDefinedRepeatableDecoratorFactory.class, Decorator2.class, UserDefinedRepeatableDecoratorFactory.class); assertThat(orders(list)).containsExactly(0, 0, 1, 2, 3); final LoggingDecorator info = (LoggingDecorator) list.get(1).annotation(); assertThat(info.requestLogLevel()).isEqualTo(LogLevel.INFO); final UserDefinedRepeatableDecorator udd1 = (UserDefinedRepeatableDecorator) list.get(2).annotation(); assertThat(udd1.value()).isEqualTo(1); final UserDefinedRepeatableDecorator udd2 = (UserDefinedRepeatableDecorator) list.get(4).annotation(); assertThat(udd2.value()).isEqualTo(2); }