FactoryBaseValidator { protected void validateCurrentTimeAfterSinceUntil(FactoryDto factory) throws BadRequestException { final PoliciesDto policies = factory.getPolicies(); if (policies == null) { return; } final Long since = policies.getSince() == null ? 0L : policies.getSince(); final Long until = policies.getUntil() == null ? 0L : policies.getUntil(); if (since != 0 && until != 0 && since >= until) { throw new BadRequestException(FactoryConstants.INVALID_SINCEUNTIL_MESSAGE); } if (since != 0 && currentTimeMillis() > since) { throw new BadRequestException(FactoryConstants.INVALID_SINCE_MESSAGE); } if (until != 0 && currentTimeMillis() > until) { throw new BadRequestException(FactoryConstants.INVALID_UNTIL_MESSAGE); } }    }
@Test public void shouldValidateIfCurrentTimeBeforeSinceUntil() throws Exception { Long currentTime = new Date().getTime(); factory.withPolicies( newDto(PoliciesDto.class).withSince(currentTime + 10000L).withUntil(currentTime + 20000L)); validator.validateCurrentTimeAfterSinceUntil(factory); }
@Test( expectedExceptions = ApiException.class, expectedExceptionsMessageRegExp = FactoryConstants.INVALID_SINCE_MESSAGE) public void shouldNotValidateIfSinceBeforeCurrent() throws ApiException { factory.withPolicies(newDto(PoliciesDto.class).withSince(1L)); validator.validateCurrentTimeAfterSinceUntil(factory); }
@Test( expectedExceptions = ApiException.class, expectedExceptionsMessageRegExp = FactoryConstants.INVALID_UNTIL_MESSAGE) public void shouldNotValidateIfUntilBeforeCurrent() throws ApiException { factory.withPolicies(newDto(PoliciesDto.class).withUntil(1L)); validator.validateCurrentTimeAfterSinceUntil(factory); }
@Test( expectedExceptions = ApiException.class, expectedExceptionsMessageRegExp = FactoryConstants.INVALID_SINCEUNTIL_MESSAGE) public void shouldNotValidateIfUntilBeforeSince() throws ApiException { factory.withPolicies(newDto(PoliciesDto.class).withSince(2L).withUntil(1L)); validator.validateCurrentTimeAfterSinceUntil(factory); }