@Test public void returnsFailureIfWardNotFoundWhenGettingWardByCode() throws Exception { Ward w = ward().build(); User user = user().withRole(Role.USER).withWards(newHashSet(w)).build(); given(wardRepository.findByCode(w.getCode())).willReturn(Collections.emptyList()); Try<Ward> result = underTest.getByCode(w.getCode(), user); assertThat(result, isFailure(instanceOf(NotFoundFailure.class))); }
@Transactional(readOnly = true) public Try<Ward> getByCode(String wardCode, User user) { List<Ward> wards = wardRepository.findByCode(wardCode); if (wards.isEmpty()) { String msg = String.format("No ward with code=%s", wardCode); log.debug(msg); return Try.failure(new NotFoundFailure(msg)); } else { if (wards.stream().anyMatch(user::hasWardPermission) || user.isAdmin()) { return Try.success(wards.get(0)); } else { String msg = String.format("User=%s tried to access ward=%s without permission", user, wardCode); log.warn(msg); return Try.failure(new NotAuthorizedFailure("Not Authorized")); } } }
WardService { @Transactional(readOnly = true) public Try<Ward> getByCode(String wardCode, User user) { List<Ward> wards = wardRepository.findByCode(wardCode); if (wards.isEmpty()) { String msg = String.format("No ward with code=%s", wardCode); log.debug(msg); return Try.failure(new NotFoundFailure(msg)); } else { if (wards.stream().anyMatch(user::hasWardPermission) || user.isAdmin()) { return Try.success(wards.get(0)); } else { String msg = String.format("User=%s tried to access ward=%s without permission", user, wardCode); log.warn(msg); return Try.failure(new NotAuthorizedFailure("Not Authorized")); } } } }
WardService { @Transactional(readOnly = true) public Try<Ward> getByCode(String wardCode, User user) { List<Ward> wards = wardRepository.findByCode(wardCode); if (wards.isEmpty()) { String msg = String.format("No ward with code=%s", wardCode); log.debug(msg); return Try.failure(new NotFoundFailure(msg)); } else { if (wards.stream().anyMatch(user::hasWardPermission) || user.isAdmin()) { return Try.success(wards.get(0)); } else { String msg = String.format("User=%s tried to access ward=%s without permission", user, wardCode); log.warn(msg); return Try.failure(new NotAuthorizedFailure("Not Authorized")); } } } @Autowired  WardService(WardSummaryConverter wardSummaryConverter,
                       WardRepository wardRepository,
                       ConstituencyRepository constituencyRepository,
                       UserService userService,
                       WardAssociationService wardAssociationService); }
WardService { @Transactional(readOnly = true) public Try<Ward> getByCode(String wardCode, User user) { List<Ward> wards = wardRepository.findByCode(wardCode); if (wards.isEmpty()) { String msg = String.format("No ward with code=%s", wardCode); log.debug(msg); return Try.failure(new NotFoundFailure(msg)); } else { if (wards.stream().anyMatch(user::hasWardPermission) || user.isAdmin()) { return Try.success(wards.get(0)); } else { String msg = String.format("User=%s tried to access ward=%s without permission", user, wardCode); log.warn(msg); return Try.failure(new NotAuthorizedFailure("Not Authorized")); } } } @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<Ward> getByCode(String wardCode, User user) { List<Ward> wards = wardRepository.findByCode(wardCode); if (wards.isEmpty()) { String msg = String.format("No ward with code=%s", wardCode); log.debug(msg); return Try.failure(new NotFoundFailure(msg)); } else { if (wards.stream().anyMatch(user::hasWardPermission) || user.isAdmin()) { return Try.success(wards.get(0)); } else { String msg = String.format("User=%s tried to access ward=%s without permission", user, wardCode); log.warn(msg); return Try.failure(new NotAuthorizedFailure("Not Authorized")); } } } @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);  }