EmailSchedulerProperties { @PostConstruct protected boolean validate() { if (enabled) { checkIsValid(this); } else { setValuesToNull(); } return true; }  static void checkIsValid(@NonNull final EmailSchedulerProperties emailSchedulerProperties);  }
@Test public void shouldValidateReturnTrueWhenNumberOfPriorityLevelsIsPositiveAndSchedulerIsEnabled() throws Exception { EmailSchedulerProperties emailSchedulerProperties = EmailSchedulerProperties.builder() .enabled(true) .priorityLevels(1) .build(); boolean givenValidationResult = emailSchedulerProperties.validate(); assertions.assertThat(givenValidationResult).isTrue(); }
@Test public void shouldValidateReturnTrueWhenSchedulerIsDisabledAndAllTheParamsAreWrong() throws Exception { final int negativeInt = -100; assertions.assertThat(negativeInt).isNegative(); EmailSchedulerProperties emailSchedulerProperties = EmailSchedulerProperties.builder() .enabled(false) .priorityLevels(negativeInt) .persistence(EmailSchedulerProperties.Persistence.builder() .desiredBatchSize(negativeInt) .minKeptInMemory(negativeInt) .maxKeptInMemory(negativeInt) .build()) .build(); boolean givenValidationResult = emailSchedulerProperties.validate(); assertions.assertThat(givenValidationResult).isTrue(); }
@Test public void shouldValidateThrowExceptionWhenNumberOfPriorityLevelsIsZeroAndSchedulerIsEnabled() throws Exception { EmailSchedulerProperties emailSchedulerProperties = EmailSchedulerProperties.builder().enabled(true).priorityLevels(0).build(); expectedException.expect(IllegalStateException.class); emailSchedulerProperties.validate(); fail("IllegalStateException expected"); }
@Test public void shouldValidateThrowExceptionWhenNumberOfPriorityLevelsIsNegativeAndSchedulerIsEnabled() throws Exception { EmailSchedulerProperties emailSchedulerProperties = EmailSchedulerProperties.builder() .enabled(true) .priorityLevels(-1) .build(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Expected at least one priority level. Review property 'spring.mail.scheduler.priorityLevels'."); emailSchedulerProperties.validate(); fail("IllegalStateException expected"); }
@Test public void shouldValidateThrowExceptionWhenDesiredBatchSizeIsZeroAndSchedulerIsEnabled() throws Exception { EmailSchedulerProperties emailSchedulerProperties = EmailSchedulerProperties.builder() .enabled(true) .priorityLevels(1) .persistence(EmailSchedulerProperties.Persistence.builder() .desiredBatchSize(0) .minKeptInMemory(100) .maxKeptInMemory(10) .build()) .build(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Expected at least a batch of size one, otherwise the persistence layer will not work. Review property 'spring.mail.scheduler.persistence.desiredBatchSize'."); emailSchedulerProperties.validate(); fail("IllegalStateException expected"); }
@Test public void shouldValidateThrowExceptionWhenMaxKeptInMemoryIsNegativeAndSchedulerIsEnabled() throws Exception { EmailSchedulerProperties emailSchedulerProperties = EmailSchedulerProperties.builder() .enabled(true) .priorityLevels(1) .persistence(EmailSchedulerProperties.Persistence.builder() .desiredBatchSize(1) .minKeptInMemory(-1) .maxKeptInMemory(10) .build()) .build(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Expected a non negative amount of email to be kept in memory. Review property 'spring.mail.scheduler.persistence.minKeptInMemory'."); emailSchedulerProperties.validate(); fail("IllegalStateException expected"); }
@Test public void shouldValidateThrowExceptionWhenMaxKeptInMemoryIsZeroAndSchedulerIsEnabled() throws Exception { EmailSchedulerProperties emailSchedulerProperties = EmailSchedulerProperties.builder() .enabled(true) .priorityLevels(1) .persistence(EmailSchedulerProperties.Persistence.builder() .desiredBatchSize(1) .minKeptInMemory(100) .maxKeptInMemory(0) .build()) .build(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Expected at least one email to be available in memory, otherwise the persistence layer will not work. Review property 'spring.mail.scheduler.persistence.maxKeptInMemory'."); emailSchedulerProperties.validate(); fail("IllegalStateException expected"); }
@Test public void shouldValidateThrowExceptionWhenMaxInMemoryIsSmallerThanMinInMemoryAndSchedulerIsEnabled() throws Exception { EmailSchedulerProperties emailSchedulerProperties = EmailSchedulerProperties.builder() .enabled(true) .priorityLevels(1) .persistence(EmailSchedulerProperties.Persistence.builder() .desiredBatchSize(1) .minKeptInMemory(100) .maxKeptInMemory(10) .build()) .build(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("The application properties key 'spring.mail.scheduler.persistence.maxKeptInMemory' should not have a value smaller than the value in property 'spring.mail.scheduler.persistence.minKeptInMemory'."); emailSchedulerProperties.validate(); fail("IllegalStateException expected"); }
@Test public void shouldValidateThrowExceptionWhenMaxInMemoryIsSmallerThanDesiredBatchSizeAndSchedulerIsEnabled() throws Exception { EmailSchedulerProperties emailSchedulerProperties = EmailSchedulerProperties.builder() .enabled(true) .priorityLevels(1) .persistence(EmailSchedulerProperties.Persistence.builder() .desiredBatchSize(100) .minKeptInMemory(1) .maxKeptInMemory(10) .build()) .build(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("The application properties key 'spring.mail.scheduler.persistence.maxKeptInMemory' should not have a value smaller than the value in property 'spring.mail.scheduler.persistence.desiredBatchSize'."); emailSchedulerProperties.validate(); fail("IllegalStateException expected"); }