@Test public void convertsTheConstituenciesStats() throws Exception { when(mappingService.getRegionByConstituency("E14000649")).thenReturn("West Midlands"); StatsIntention intention = ImmutableStatsIntention.builder() .withProbablyRemain(1) .withRemain(2) .withLeave(3) .withUndecided(4) .withProbablyLeave(5) .build(); StatsVoted voted = ImmutableStatsVoted.builder() .withTotal(6) .withPledged(7) .build(); ConstituenciesStats stats = ImmutableConstituenciesStats.builder() .withCanvassed(1) .withCode("E14000649") .withName("Coventry North East") .withIntention(intention) .withVoted(voted) .withPledged(8) .build(); LocalDateTime updated = LocalDateTime.now(); AllConstituenciesStatsResponse statsResponse = ImmutableAllConstituenciesStatsResponse.builder() .withUpdated(updated) .withConstituencies(singletonList(stats)) .build(); StatsOverview statsOverview = underTest.apply(statsResponse); assertThat(statsOverview.constituencies(), hasItem(ImmutableConstituenciesStatsResponse.builder() .withRegion("West Midlands") .withStats(ImmutableConstituenciesStats.builder() .withIntention(intention) .withVoted(voted) .withName("Coventry North East") .withCode("E14000649") .withPledged(8) .withCanvassed(1) .build()) .build())); }
@Override public StatsOverview apply(AllConstituenciesStatsResponse constituenciesStats) { int canvassed = 0; int pledges = 0; int voted = 0; int votedPledges = 0; List<ConstituenciesStatsResponse> transformedStats = new ArrayList<>(); for (ConstituenciesStats stats : constituenciesStats.constituencies()) { canvassed += stats.canvassed(); pledges += stats.pledged(); voted += stats.voted().total(); votedPledges += stats.voted().pledged(); String regionName = regionMappingService.getRegionByConstituency(stats.code()); ConstituenciesStatsResponse constituencies = ImmutableConstituenciesStatsResponse.builder() .withRegion(regionName) .withStats(stats) .build(); transformedStats.add(constituencies); } return ImmutableStatsOverview.builder() .withConstituencies(transformedStats) .withTotal(ImmutableOverallStats.builder() .withCanvassed(canvassed) .withPledges(pledges) .withVoted(voted) .withPledgesVoted(votedPledges) .build()) .withUpdated(constituenciesStats.updated()) .build(); }
ConstituenciesStatsConverter implements Function<AllConstituenciesStatsResponse, StatsOverview> { @Override public StatsOverview apply(AllConstituenciesStatsResponse constituenciesStats) { int canvassed = 0; int pledges = 0; int voted = 0; int votedPledges = 0; List<ConstituenciesStatsResponse> transformedStats = new ArrayList<>(); for (ConstituenciesStats stats : constituenciesStats.constituencies()) { canvassed += stats.canvassed(); pledges += stats.pledged(); voted += stats.voted().total(); votedPledges += stats.voted().pledged(); String regionName = regionMappingService.getRegionByConstituency(stats.code()); ConstituenciesStatsResponse constituencies = ImmutableConstituenciesStatsResponse.builder() .withRegion(regionName) .withStats(stats) .build(); transformedStats.add(constituencies); } return ImmutableStatsOverview.builder() .withConstituencies(transformedStats) .withTotal(ImmutableOverallStats.builder() .withCanvassed(canvassed) .withPledges(pledges) .withVoted(voted) .withPledgesVoted(votedPledges) .build()) .withUpdated(constituenciesStats.updated()) .build(); } }
ConstituenciesStatsConverter implements Function<AllConstituenciesStatsResponse, StatsOverview> { @Override public StatsOverview apply(AllConstituenciesStatsResponse constituenciesStats) { int canvassed = 0; int pledges = 0; int voted = 0; int votedPledges = 0; List<ConstituenciesStatsResponse> transformedStats = new ArrayList<>(); for (ConstituenciesStats stats : constituenciesStats.constituencies()) { canvassed += stats.canvassed(); pledges += stats.pledged(); voted += stats.voted().total(); votedPledges += stats.voted().pledged(); String regionName = regionMappingService.getRegionByConstituency(stats.code()); ConstituenciesStatsResponse constituencies = ImmutableConstituenciesStatsResponse.builder() .withRegion(regionName) .withStats(stats) .build(); transformedStats.add(constituencies); } return ImmutableStatsOverview.builder() .withConstituencies(transformedStats) .withTotal(ImmutableOverallStats.builder() .withCanvassed(canvassed) .withPledges(pledges) .withVoted(voted) .withPledgesVoted(votedPledges) .build()) .withUpdated(constituenciesStats.updated()) .build(); } @Autowired  ConstituenciesStatsConverter(ConstituencyRegionMappingService regionMappingService); }
ConstituenciesStatsConverter implements Function<AllConstituenciesStatsResponse, StatsOverview> { @Override public StatsOverview apply(AllConstituenciesStatsResponse constituenciesStats) { int canvassed = 0; int pledges = 0; int voted = 0; int votedPledges = 0; List<ConstituenciesStatsResponse> transformedStats = new ArrayList<>(); for (ConstituenciesStats stats : constituenciesStats.constituencies()) { canvassed += stats.canvassed(); pledges += stats.pledged(); voted += stats.voted().total(); votedPledges += stats.voted().pledged(); String regionName = regionMappingService.getRegionByConstituency(stats.code()); ConstituenciesStatsResponse constituencies = ImmutableConstituenciesStatsResponse.builder() .withRegion(regionName) .withStats(stats) .build(); transformedStats.add(constituencies); } return ImmutableStatsOverview.builder() .withConstituencies(transformedStats) .withTotal(ImmutableOverallStats.builder() .withCanvassed(canvassed) .withPledges(pledges) .withVoted(voted) .withPledgesVoted(votedPledges) .build()) .withUpdated(constituenciesStats.updated()) .build(); } @Autowired  ConstituenciesStatsConverter(ConstituencyRegionMappingService regionMappingService); @Override StatsOverview apply(AllConstituenciesStatsResponse constituenciesStats); }
ConstituenciesStatsConverter implements Function<AllConstituenciesStatsResponse, StatsOverview> { @Override public StatsOverview apply(AllConstituenciesStatsResponse constituenciesStats) { int canvassed = 0; int pledges = 0; int voted = 0; int votedPledges = 0; List<ConstituenciesStatsResponse> transformedStats = new ArrayList<>(); for (ConstituenciesStats stats : constituenciesStats.constituencies()) { canvassed += stats.canvassed(); pledges += stats.pledged(); voted += stats.voted().total(); votedPledges += stats.voted().pledged(); String regionName = regionMappingService.getRegionByConstituency(stats.code()); ConstituenciesStatsResponse constituencies = ImmutableConstituenciesStatsResponse.builder() .withRegion(regionName) .withStats(stats) .build(); transformedStats.add(constituencies); } return ImmutableStatsOverview.builder() .withConstituencies(transformedStats) .withTotal(ImmutableOverallStats.builder() .withCanvassed(canvassed) .withPledges(pledges) .withVoted(voted) .withPledgesVoted(votedPledges) .build()) .withUpdated(constituenciesStats.updated()) .build(); } @Autowired  ConstituenciesStatsConverter(ConstituencyRegionMappingService regionMappingService); @Override StatsOverview apply(AllConstituenciesStatsResponse constituenciesStats);  }