### Question:
AcceptanceRateTracker implements ProposalListener { public double getAcceptanceRate(VariableReference variableReference) { if (!numApplied.keySet().contains(variableReference)) { throw new IllegalStateException("No proposals have been registered for " + variableReference); } return 1. - (double) numRejected.getOrDefault(variableReference, new Counter()).getValue() / numApplied.get(variableReference).getValue(); }  @Override void onProposalCreated(Proposal proposal); @Override void onProposalRejected(Proposal proposal); double getAcceptanceRate(VariableReference variableReference);  }### Answer:
@Test public void itThrowsIfYouAskForTheAcceptanceRateForAnUnrecognisedSetOfVertices() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("No proposals have been registered for [1]"); acceptanceRateTracker.getAcceptanceRate(vertex1.getId()); }
@Test public void youCanTrackTheAcceptanceRateForASingleVertex() { Proposal proposal = new Proposal(); proposal.setProposal(vertex1, 1.); notifier.notifyProposalCreated(proposal); notifier.notifyProposalRejected(); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex1.getId()), equalTo(0.)); proposal = new Proposal(); proposal.setProposal(vertex1, 2.); notifier.notifyProposalCreated(proposal); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex1.getId()), equalTo(0.5)); }
@Test public void youCanTrackTheAcceptanceRateForDifferentSetsOfVertices() { Proposal proposal = new Proposal(); proposal.setProposal(vertex1, 1.); notifier.notifyProposalCreated(proposal); notifier.notifyProposalRejected(); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex1.getId()), equalTo(0.)); expectRateToBeMissing(vertex2); proposal = new Proposal(); proposal.setProposal(vertex2, 1.); notifier.notifyProposalCreated(proposal); notifier.notifyProposalRejected(); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex1.getId()), equalTo(0.)); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex2.getId()), equalTo(0.)); proposal = new Proposal(); proposal.setProposal(vertex1, 2.); notifier.notifyProposalCreated(proposal); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex1.getId()), equalTo(0.5)); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex2.getId()), equalTo(0.)); proposal = new Proposal(); proposal.setProposal(vertex2, 2.); notifier.notifyProposalCreated(proposal); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex1.getId()), equalTo(0.5)); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex2.getId()), equalTo(0.5)); }
@Test public void youCanTrackTheAcceptanceRateOfASetOfMultipleVertices() { Proposal proposal = new Proposal(); proposal.setProposal(vertex1, 1.); proposal.setProposal(vertex2, 2.); notifier.notifyProposalCreated(proposal); notifier.notifyProposalRejected(); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex1.getId()), equalTo(0.)); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex2.getId()), equalTo(0.)); proposal = new Proposal(); proposal.setProposal(vertex1, 3.); proposal.setProposal(vertex2, 4.); notifier.notifyProposalCreated(proposal); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex1.getId()), equalTo(0.5)); assertThat(acceptanceRateTracker.getAcceptanceRate(vertex2.getId()), equalTo(0.5)); }