@Test public void returnsNotFoundIfConstituencyNotFoundWhenAssociateConstituencyToUser() throws Exception { User admin = user().withRole(Role.ADMIN).build(); User user = user().withConstituencies(new HashSet<>()).build(); Constituency constituency = constituency().build(); given(constituencyRepository.findOne(constituency.getId())).willReturn(null); given(userRepository.findOne(user.getId())).willReturn(user); Try<User> userTry = underTest.associateToUser(admin, constituency.getId(), user.getId()); assertThat(userTry.isSuccess(), is(false)); assertThat(userTry.getFailure(), is(instanceOf(NotFoundFailure.class))); }
@Transactional public Try<User> associateToUser(User permissible, UUID constituencyID, UUID userID) { if (!permissible.isAdmin()) { log.error("Non admin attempted to associate user={} to constituency={}. loggedInUser={}", userID, constituencyID, permissible); return Try.failure(new NotAuthorizedFailure("Forbidden content")); } Constituency constituency = constituencyRepository.findOne(constituencyID); if (constituency == null) { String msg = "No constituency=" + constituencyID; log.debug(msg); return Try.failure(new NotFoundFailure(msg)); } User foundUser = userRepository.findOne(userID); if (foundUser == null) { return Try.failure(new NotFoundFailure("No user with ID " + userID)); } foundUser.getConstituencies().add(constituency); User updatedUser = userRepository.saveAndFlush(foundUser); log.debug("Added association constituency={}, user={}", constituencyID, userID); return Try.success(updatedUser); }
ConstituencyAssociationService { @Transactional public Try<User> associateToUser(User permissible, UUID constituencyID, UUID userID) { if (!permissible.isAdmin()) { log.error("Non admin attempted to associate user={} to constituency={}. loggedInUser={}", userID, constituencyID, permissible); return Try.failure(new NotAuthorizedFailure("Forbidden content")); } Constituency constituency = constituencyRepository.findOne(constituencyID); if (constituency == null) { String msg = "No constituency=" + constituencyID; log.debug(msg); return Try.failure(new NotFoundFailure(msg)); } User foundUser = userRepository.findOne(userID); if (foundUser == null) { return Try.failure(new NotFoundFailure("No user with ID " + userID)); } foundUser.getConstituencies().add(constituency); User updatedUser = userRepository.saveAndFlush(foundUser); log.debug("Added association constituency={}, user={}", constituencyID, userID); return Try.success(updatedUser); } }
ConstituencyAssociationService { @Transactional public Try<User> associateToUser(User permissible, UUID constituencyID, UUID userID) { if (!permissible.isAdmin()) { log.error("Non admin attempted to associate user={} to constituency={}. loggedInUser={}", userID, constituencyID, permissible); return Try.failure(new NotAuthorizedFailure("Forbidden content")); } Constituency constituency = constituencyRepository.findOne(constituencyID); if (constituency == null) { String msg = "No constituency=" + constituencyID; log.debug(msg); return Try.failure(new NotFoundFailure(msg)); } User foundUser = userRepository.findOne(userID); if (foundUser == null) { return Try.failure(new NotFoundFailure("No user with ID " + userID)); } foundUser.getConstituencies().add(constituency); User updatedUser = userRepository.saveAndFlush(foundUser); log.debug("Added association constituency={}, user={}", constituencyID, userID); return Try.success(updatedUser); } @Autowired  ConstituencyAssociationService(ConstituencyRepository constituencyRepository,
                                          ConstituencyService constituencyService,
                                          UserRepository userRepository,
                                          UserService userService); }
ConstituencyAssociationService { @Transactional public Try<User> associateToUser(User permissible, UUID constituencyID, UUID userID) { if (!permissible.isAdmin()) { log.error("Non admin attempted to associate user={} to constituency={}. loggedInUser={}", userID, constituencyID, permissible); return Try.failure(new NotAuthorizedFailure("Forbidden content")); } Constituency constituency = constituencyRepository.findOne(constituencyID); if (constituency == null) { String msg = "No constituency=" + constituencyID; log.debug(msg); return Try.failure(new NotFoundFailure(msg)); } User foundUser = userRepository.findOne(userID); if (foundUser == null) { return Try.failure(new NotFoundFailure("No user with ID " + userID)); } foundUser.getConstituencies().add(constituency); User updatedUser = userRepository.saveAndFlush(foundUser); log.debug("Added association constituency={}, user={}", constituencyID, userID); return Try.success(updatedUser); } @Autowired  ConstituencyAssociationService(ConstituencyRepository constituencyRepository,
                                          ConstituencyService constituencyService,
                                          UserRepository userRepository,
                                          UserService userService); @Transactional Try<User> associateToUser(User permissible, UUID constituencyID, UUID userID); @Transactional Try<User> removeUserAssociation(User user, UUID constituencyID, UUID userID); Try<User> associateToUserByUsername(User loggedInUser, String constituencyCode, String username); }
ConstituencyAssociationService { @Transactional public Try<User> associateToUser(User permissible, UUID constituencyID, UUID userID) { if (!permissible.isAdmin()) { log.error("Non admin attempted to associate user={} to constituency={}. loggedInUser={}", userID, constituencyID, permissible); return Try.failure(new NotAuthorizedFailure("Forbidden content")); } Constituency constituency = constituencyRepository.findOne(constituencyID); if (constituency == null) { String msg = "No constituency=" + constituencyID; log.debug(msg); return Try.failure(new NotFoundFailure(msg)); } User foundUser = userRepository.findOne(userID); if (foundUser == null) { return Try.failure(new NotFoundFailure("No user with ID " + userID)); } foundUser.getConstituencies().add(constituency); User updatedUser = userRepository.saveAndFlush(foundUser); log.debug("Added association constituency={}, user={}", constituencyID, userID); return Try.success(updatedUser); } @Autowired  ConstituencyAssociationService(ConstituencyRepository constituencyRepository,
                                          ConstituencyService constituencyService,
                                          UserRepository userRepository,
                                          UserService userService); @Transactional Try<User> associateToUser(User permissible, UUID constituencyID, UUID userID); @Transactional Try<User> removeUserAssociation(User user, UUID constituencyID, UUID userID); Try<User> associateToUserByUsername(User loggedInUser, String constituencyCode, String username);  }