### Question:
BlockTimeStampValidationRule implements BlockParentDependantValidationRule, BlockValidationRule { @Override public boolean isValid(Block block) { if (this.validPeriodLength == 0) { return true; } final long currentTime = System.currentTimeMillis() / 1000; final long blockTime = block.getTimestamp(); boolean result = blockTime - currentTime <= this.validPeriodLength; if(!result) { logger.warn("Error validating block. Invalid timestamp {}.", blockTime); } return result; } BlockTimeStampValidationRule(int validPeriodLength); @Override boolean isValid(Block block); @Override boolean isValid(Block block, Block parent);  }### Answer:
@Test public void blockInThePast() { int validPeriod = 540; BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod); Block block = Mockito.mock(Block.class); Mockito.when(block.getTimestamp()) .thenReturn((System.currentTimeMillis() / 1000) - 1000); Assert.assertTrue(validationRule.isValid(block)); }
@Test public void blockInTheFutureLimit() { int validPeriod = 540; BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod); Block block = Mockito.mock(Block.class); Mockito.when(block.getTimestamp()) .thenReturn((System.currentTimeMillis() / 1000) + validPeriod); Assert.assertTrue(validationRule.isValid(block)); }
@Test public void blockInTheFuture() { int validPeriod = 540; BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod); Block block = Mockito.mock(Block.class); Mockito.when(block.getTimestamp()) .thenReturn((System.currentTimeMillis() / 1000) + 2*validPeriod); Assert.assertFalse(validationRule.isValid(block)); }
@Test public void blockTimeLowerThanParentTime() { int validPeriod = 540; BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod); Block block = Mockito.mock(Block.class); Block parent = Mockito.mock(Block.class); Mockito.when(block.getTimestamp()) .thenReturn(System.currentTimeMillis() / 1000); Mockito.when(parent.getTimestamp()) .thenReturn((System.currentTimeMillis() / 1000) + 1000); Assert.assertFalse(validationRule.isValid(block, parent)); }
@Test public void blockTimeGreaterThanParentTime() { int validPeriod = 540; BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod); Block block = Mockito.mock(Block.class); Block parent = Mockito.mock(Block.class); Mockito.when(block.getTimestamp()) .thenReturn(System.currentTimeMillis() / 1000); Mockito.when(parent.getTimestamp()) .thenReturn((System.currentTimeMillis() / 1000) - 1000); Assert.assertTrue(validationRule.isValid(block, parent)); }
@Test public void blockTimeEqualsParentTime() { int validPeriod = 540; BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod); Block block = Mockito.mock(Block.class); Block parent = Mockito.mock(Block.class); Mockito.when(block.getTimestamp()) .thenReturn(System.currentTimeMillis() / 1000); Mockito.when(parent.getTimestamp()) .thenReturn(System.currentTimeMillis() / 1000); Assert.assertFalse(validationRule.isValid(block, parent)); }