LeaderInfoContributor implements InfoContributor { @Override public void contribute(Builder builder) { Map<String, Object> details = new HashMap<>(); Optional<Leader> leader = leadershipController.getLocalLeader(); if (leader.isPresent()) { Leader l = leader.get(); details.put("leaderId", l.getId()); details.put("role", l.getRole()); details.put("isLeader", l.isCandidate(candidate)); } else { details.put("leaderId", "Unknown"); } builder.withDetail("leaderElection", details); } LeaderInfoContributor(LeadershipController leadershipController, Candidate candidate); @Override void contribute(Builder builder);  }
@SuppressWarnings("unchecked") @Test public void infoWithoutLeader() { Info.Builder builder = new Info.Builder(); leaderInfoContributor.contribute(builder); Map<String, Object> details = (Map<String, Object>) builder.build().get("leaderElection"); assertThat(details).containsEntry("leaderId", "Unknown"); }
@SuppressWarnings("unchecked") @Test public void infoWhenLeader() { given(this.mockLeadershipController.getLocalLeader()).willReturn(Optional.of(this.mockLeader)); given(this.mockLeader.isCandidate(this.mockCandidate)).willReturn(true); given(this.mockLeader.getRole()).willReturn("testRole"); given(this.mockLeader.getId()).willReturn("id"); Info.Builder builder = new Info.Builder(); leaderInfoContributor.contribute(builder); Map<String, Object> details = (Map<String, Object>) builder.build().get("leaderElection"); assertThat(details).containsEntry("isLeader", true); assertThat(details).containsEntry("leaderId", "id"); assertThat(details).containsEntry("role", "testRole"); }
@SuppressWarnings("unchecked") @Test public void infoWhenAnotherIsLeader() { given(this.mockLeadershipController.getLocalLeader()).willReturn(Optional.of(this.mockLeader)); given(this.mockLeader.getRole()).willReturn("testRole"); given(this.mockLeader.getId()).willReturn("id"); Info.Builder builder = new Info.Builder(); leaderInfoContributor.contribute(builder); Map<String, Object> details = (Map<String, Object>) builder.build().get("leaderElection"); assertThat(details).containsEntry("isLeader", false); assertThat(details).containsEntry("leaderId", "id"); assertThat(details).containsEntry("role", "testRole"); }