RejectionDispatchKeys { static DispatchKey of(EventClass eventClass, Method rawMethod) { checkNotNull(eventClass); checkNotNull(rawMethod); Class<?>[] parameters = rawMethod.getParameterTypes(); String methodName = rawMethod.getName(); checkArgument(parameters.length >= 2, "The method `%s` should have at least 2 parameters, but has `%s`.", methodName, parameters.length); Class<?> secondParameter = parameters[1]; checkArgument(CommandMessage.class.isAssignableFrom(secondParameter), "The method `%s` should have the second parameter assignable from `CommandMessage`, but has `%s`.", methodName, secondParameter); @SuppressWarnings("unchecked") Class<? extends CommandMessage> commandMessageClass = (Class<? extends CommandMessage>) secondParameter; CommandClass commandClass = CommandClass.from(commandMessageClass); return new DispatchKey(eventClass.value(), null, commandClass.value()); } private  RejectionDispatchKeys();   }
@SuppressWarnings("ResultOfMethodCallIgnored") @Test @DisplayName("create a valid key") void createValidKey() { assertDoesNotThrow(() -> { Method method = ModelTests.getMethod(RejectionSubscriber.class, "rejectionWithCommand"); RejectionDispatchKeys.of(EventClass.from(SigCannotCreateProject.class), method); }); assertDoesNotThrow(() -> { Method method = ModelTests.getMethod(RejectionSubscriber.class, "rejectionWithCommandAndCtx"); RejectionDispatchKeys.of(EventClass.from(SigCannotCreateProject.class), method); }); }
@SuppressWarnings("ResultOfMethodCallIgnored") @Test @DisplayName("not create keys for rejections without a cause") void notCreateKeyForRejectionsWithoutCause() { assertThrows(IllegalArgumentException.class, () -> { Method method = ModelTests.getMethod(RejectionSubscriber.class, "rejectionWithoutCommand"); RejectionDispatchKeys.of(EventClass.from(SigCannotCreateProject.class), method); }); assertThrows(IllegalArgumentException.class, () -> { Method method = ModelTests.getMethod(RejectionSubscriber.class, "rejectionWithCtx"); RejectionDispatchKeys.of(EventClass.from(SigCannotCreateProject.class), method); }); }