### Question:
IbftHelpers { public static Optional<PreparedCertificate> findLatestPreparedCertificate( final Collection<SignedData<RoundChangePayload>> msgs) { Optional<PreparedCertificate> result = Optional.empty(); for (SignedData<RoundChangePayload> roundChangeMsg : msgs) { final RoundChangePayload payload = roundChangeMsg.getPayload(); if (payload.getPreparedCertificate().isPresent()) { if (!result.isPresent()) { result = payload.getPreparedCertificate(); } else { final PreparedCertificate currentLatest = result.get(); final PreparedCertificate nextCert = payload.getPreparedCertificate().get(); if (currentLatest.getProposalPayload().getPayload().getRoundIdentifier().getRoundNumber() < nextCert.getProposalPayload().getPayload().getRoundIdentifier().getRoundNumber()) { result = Optional.of(nextCert); } } } } return result; }  static int calculateRequiredValidatorQuorum(final int validatorCount); static long prepareMessageCountForQuorum(final long quorum); static Block createSealedBlock(
      final Block block, final Collection<Signature> commitSeals); static Optional<PreparedCertificate> findLatestPreparedCertificate(
      final Collection<SignedData<RoundChangePayload>> msgs); static final Hash EXPECTED_MIX_HASH; }### Answer:
@Test public void latestPreparedCertificateIsExtractedFromRoundChangeCertificate() { final NodeKey proposerKey = NodeKeyUtils.generate(); final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey); final Block proposedBlock = mock(Block.class); when(proposedBlock.getHash()).thenReturn(Hash.fromHexStringLenient("1")); final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(1, 4); final ConsensusRoundIdentifier preparedRound = TestHelpers.createFrom(roundIdentifier, 0, -1); final Proposal differentProposal = proposerMessageFactory.createProposal(preparedRound, proposedBlock, Optional.empty()); final Optional<PreparedRoundArtifacts> latterPreparedRoundArtifacts = Optional.of( new PreparedRoundArtifacts( differentProposal, Lists.newArrayList( proposerMessageFactory.createPrepare(roundIdentifier, proposedBlock.getHash()), proposerMessageFactory.createPrepare( roundIdentifier, proposedBlock.getHash())))); final ConsensusRoundIdentifier earlierPreparedRound = TestHelpers.createFrom(roundIdentifier, 0, -2); final Proposal earlierProposal = proposerMessageFactory.createProposal( earlierPreparedRound, proposedBlock, Optional.empty()); final Optional<PreparedRoundArtifacts> earlierPreparedRoundArtifacts = Optional.of( new PreparedRoundArtifacts( earlierProposal, Lists.newArrayList( proposerMessageFactory.createPrepare( earlierPreparedRound, proposedBlock.getHash()), proposerMessageFactory.createPrepare( earlierPreparedRound, proposedBlock.getHash())))); final Optional<PreparedCertificate> newestCert = IbftHelpers.findLatestPreparedCertificate( Lists.newArrayList( proposerMessageFactory .createRoundChange(roundIdentifier, earlierPreparedRoundArtifacts) .getSignedPayload(), proposerMessageFactory .createRoundChange(roundIdentifier, latterPreparedRoundArtifacts) .getSignedPayload())); assertThat(newestCert.get()) .isEqualTo(latterPreparedRoundArtifacts.get().getPreparedCertificate()); }
@Test public void allRoundChangeHaveNoPreparedReturnsEmptyOptional() { final NodeKey proposerKey = NodeKeyUtils.generate(); final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey); final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(1, 4); final Optional<PreparedCertificate> newestCert = IbftHelpers.findLatestPreparedCertificate( Lists.newArrayList( proposerMessageFactory .createRoundChange(roundIdentifier, Optional.empty()) .getSignedPayload(), proposerMessageFactory .createRoundChange(roundIdentifier, Optional.empty()) .getSignedPayload())); assertThat(newestCert).isEmpty(); }