RuleEntityServiceImpl implements RuleEntityService { @Override public void expire(final Long id, final DateTime newExpiryDate) throws NoSuchElementException { if (newExpiryDate != null) { final RuleEntity entity = ofNullable(ruleEntityRepository.findOne(id)) .orElseThrow(() -> new NoSuchElementException(format("No such Rule! Id: %s", id))); final DateTime now = DateTime.now(); if (newExpiryDate.isAfter(now) && entity.getExpiryDate().isAfter(now)) { invalidateRule(entity, newExpiryDate); } else { throw new IllegalArgumentException(format("Expiry dates lie in the past: new: %s old: %s", newExpiryDate, entity.getExpiryDate())); } } }  @Override RuleEntity save(final RuleDTO ruleDTO); @Override RuleEntity update(final RuleDTO ruleDTO, final Long id); @Override RuleEntity findById(final Long id); @Override List<RuleEntity> findByNotExpired(); @Override List<RuleEntity> findAll(); @Override void expire(final Long id, final DateTime newExpiryDate);  }
@Test public void testExpireWithNull() throws Exception { ruleEntityServiceImpl.expire(1L, null); }
@Test(expected = IllegalArgumentException.class) public void testExpireExpiryDateInPast() throws Exception { final DateTime now = DateTime.now().plusDays(1); ruleEntity.setExpiryDate(now); when(ruleEntityRepository.findOne(anyLong())).thenReturn(ruleEntity); try { final DateTime expiryDate = DateTime.now().minusDays(1); ruleEntityServiceImpl.expire(1L, expiryDate); } finally { verify(ruleEntityRepository).findOne(anyLong()); } }
@Test(expected = IllegalArgumentException.class) public void testExpireOldExpiryDateInPast() throws Exception { final DateTime now = DateTime.now().minusDays(1); ruleEntity.setExpiryDate(now); when(ruleEntityRepository.findOne(anyLong())).thenReturn(ruleEntity); try { final DateTime expiryDate = DateTime.now().plusDays(1); ruleEntityServiceImpl.expire(1L, expiryDate); } finally { verify(ruleEntityRepository).findOne(anyLong()); } }