Skip to content

Commit

Permalink
SHIBUI-1740
Browse files Browse the repository at this point in the history
Testing cleanup
  • Loading branch information
chasegawa committed Jul 23, 2021
1 parent e7d89c3 commit 85a3fd6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,41 +112,43 @@ public Set<UserGroup> getUserGroups() {
return userGroups;
}

public void setGroup(Group g) {
groupId = g.getResourceId();
updateUserGroupsWithGroup(g);
}

public void setGroups(Set<Group> groups) {
oldUserGroups.addAll(getUserGroups());
getUserGroups().clear();
groups.forEach(g -> userGroups.add(new UserGroup(g, this)));
}

/**
* If we change groups, we have to manually manage the set of UserGroups so that we don't have group associations
* we didn't intend (thanks JPA!!).
*/
public void setGroup(Group assignedGroup) {
UserGroup theUserGroup = new UserGroup();
public void updateUserGroupsWithGroup(Group assignedGroup) {
final Set<UserGroup> setWithNewGroup= new HashSet<>();
// Go through the existing UserGroups:
// 1) If a UG doesn't match the incoming assignment, move it out of the list and into the old for deletion
// 2) If it DOES match, update the group object so hibernate doesn't have a cow
userGroups.forEach(ug -> {
if (ug.getGroup().getResourceId().equals(groupId)) {
theUserGroup.setGroup(assignedGroup);
theUserGroup.setUser(this);
theUserGroup.setId(ug.getId());
ug.setGroup(assignedGroup);
setWithNewGroup.add(ug);
} else {
oldUserGroups.add(ug);
}
});
userGroups.clear();
userGroups = setWithNewGroup;

// Assign the new group
if (theUserGroup.getUser() == null) {
if (userGroups.isEmpty()) {
UserGroup ug = new UserGroup(assignedGroup, this);
userGroups.add(ug);
} else {
userGroups.add(theUserGroup);
}

// Set reference for the UI
groupId = assignedGroup.getResourceId();
}

public void setGroups(Set<Group> groups) {
oldUserGroups.addAll(getUserGroups());
getUserGroups().clear();
groups.forEach(g -> userGroups.add(new UserGroup(g, this)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public User save(User user) {
} else {
g = groupService.find(user.getGroupId());
}
user.setGroup(g);
user.updateUserGroupsWithGroup(g);
} else {
user.getUserGroups().forEach(ug -> {
Group g = groupService.find(ug.getGroup().getResourceId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ class UserServiceTests extends Specification {
def User userInB = userService.save(user)

when:
userInB.setGroup(ga)
def User result = userService.save(user)
userInB.setGroupId("testingGroup") // changing groups will happen by updating the user's groupid
def User result = userService.save(userInB)
def List<UserGroup> usersGroups = userGroupRepository.findAllByUser(result)

then:
Expand Down

0 comments on commit 85a3fd6

Please sign in to comment.