@Test public void returnsFailureIfConstituencyNotFound() throws Exception { User user = mock(User.class); Constituency c = constituency().build(); given(constituencyRepository.findOne(c.getId())).willReturn(null); Try<UserRestrictedWards> result = underTest.findByConstituency(c.getId(), user); assertThat(result.isSuccess(), is(false)); }
@Transactional(readOnly = true) public Try<UserRestrictedWards> findByConstituency(UUID constituencyID, User user) { log.debug("Finding accessible wards for constituency={} user={}", constituencyID, user); return Optional.ofNullable(constituencyRepository.findOne(constituencyID)) .map(constituency -> { Set<Ward> wards = user.getAccessibleWards(); List<Ward> collect = wards.stream() .filter(ward -> Objects.equals(ward.getConstituency(), constituency)) .collect(toList()); UserRestrictedWards userRestrictedWards = new UserRestrictedWards(new HashSet<>(collect)); return Try.success(userRestrictedWards); }).orElseGet(() -> { log.debug("Could not find constituency={} for user={}", constituencyID, user); return Try.failure(new NotFoundFailure("No constituency with ID " + constituencyID)); }); }
WardService { @Transactional(readOnly = true) public Try<UserRestrictedWards> findByConstituency(UUID constituencyID, User user) { log.debug("Finding accessible wards for constituency={} user={}", constituencyID, user); return Optional.ofNullable(constituencyRepository.findOne(constituencyID)) .map(constituency -> { Set<Ward> wards = user.getAccessibleWards(); List<Ward> collect = wards.stream() .filter(ward -> Objects.equals(ward.getConstituency(), constituency)) .collect(toList()); UserRestrictedWards userRestrictedWards = new UserRestrictedWards(new HashSet<>(collect)); return Try.success(userRestrictedWards); }).orElseGet(() -> { log.debug("Could not find constituency={} for user={}", constituencyID, user); return Try.failure(new NotFoundFailure("No constituency with ID " + constituencyID)); }); } }
WardService { @Transactional(readOnly = true) public Try<UserRestrictedWards> findByConstituency(UUID constituencyID, User user) { log.debug("Finding accessible wards for constituency={} user={}", constituencyID, user); return Optional.ofNullable(constituencyRepository.findOne(constituencyID)) .map(constituency -> { Set<Ward> wards = user.getAccessibleWards(); List<Ward> collect = wards.stream() .filter(ward -> Objects.equals(ward.getConstituency(), constituency)) .collect(toList()); UserRestrictedWards userRestrictedWards = new UserRestrictedWards(new HashSet<>(collect)); return Try.success(userRestrictedWards); }).orElseGet(() -> { log.debug("Could not find constituency={} for user={}", constituencyID, user); return Try.failure(new NotFoundFailure("No constituency with ID " + constituencyID)); }); } @Autowired  WardService(WardSummaryConverter wardSummaryConverter,
                       WardRepository wardRepository,
                       ConstituencyRepository constituencyRepository,
                       UserService userService,
                       WardAssociationService wardAssociationService); }
WardService { @Transactional(readOnly = true) public Try<UserRestrictedWards> findByConstituency(UUID constituencyID, User user) { log.debug("Finding accessible wards for constituency={} user={}", constituencyID, user); return Optional.ofNullable(constituencyRepository.findOne(constituencyID)) .map(constituency -> { Set<Ward> wards = user.getAccessibleWards(); List<Ward> collect = wards.stream() .filter(ward -> Objects.equals(ward.getConstituency(), constituency)) .collect(toList()); UserRestrictedWards userRestrictedWards = new UserRestrictedWards(new HashSet<>(collect)); return Try.success(userRestrictedWards); }).orElseGet(() -> { log.debug("Could not find constituency={} for user={}", constituencyID, user); return Try.failure(new NotFoundFailure("No constituency with ID " + constituencyID)); }); } @Autowired  WardService(WardSummaryConverter wardSummaryConverter,
                       WardRepository wardRepository,
                       ConstituencyRepository constituencyRepository,
                       UserService userService,
                       WardAssociationService wardAssociationService); @Transactional(readOnly = true) List<Ward> searchUserRestrictedConstituencies(User user, String searchTerm); @Transactional(readOnly = true) Try<Ward> getByCode(String wardCode, User user); @Transactional(readOnly = true) Try<UserRestrictedWards> findByConstituency(UUID constituencyID, User user); @Transactional(readOnly = true) UserRestrictedWards getByUser(User user); @Transactional(readOnly = true) List<WardSummary> getSummaryByUser(User user); @Transactional(readOnly = true) UserRestrictedWards getVisibleWardsByUser(User user, int limit); @Transactional(readOnly = true) Try<List<Ward>> getAllByName(User user, String name, int limit); Try<User> associateToUserByUsername(User permissible, String wardCode, String username); }
WardService { @Transactional(readOnly = true) public Try<UserRestrictedWards> findByConstituency(UUID constituencyID, User user) { log.debug("Finding accessible wards for constituency={} user={}", constituencyID, user); return Optional.ofNullable(constituencyRepository.findOne(constituencyID)) .map(constituency -> { Set<Ward> wards = user.getAccessibleWards(); List<Ward> collect = wards.stream() .filter(ward -> Objects.equals(ward.getConstituency(), constituency)) .collect(toList()); UserRestrictedWards userRestrictedWards = new UserRestrictedWards(new HashSet<>(collect)); return Try.success(userRestrictedWards); }).orElseGet(() -> { log.debug("Could not find constituency={} for user={}", constituencyID, user); return Try.failure(new NotFoundFailure("No constituency with ID " + constituencyID)); }); } @Autowired  WardService(WardSummaryConverter wardSummaryConverter,
                       WardRepository wardRepository,
                       ConstituencyRepository constituencyRepository,
                       UserService userService,
                       WardAssociationService wardAssociationService); @Transactional(readOnly = true) List<Ward> searchUserRestrictedConstituencies(User user, String searchTerm); @Transactional(readOnly = true) Try<Ward> getByCode(String wardCode, User user); @Transactional(readOnly = true) Try<UserRestrictedWards> findByConstituency(UUID constituencyID, User user); @Transactional(readOnly = true) UserRestrictedWards getByUser(User user); @Transactional(readOnly = true) List<WardSummary> getSummaryByUser(User user); @Transactional(readOnly = true) UserRestrictedWards getVisibleWardsByUser(User user, int limit); @Transactional(readOnly = true) Try<List<Ward>> getAllByName(User user, String name, int limit); Try<User> associateToUserByUsername(User permissible, String wardCode, String username);  }