ScimServiceImpl implements ScimService { @Override public void addUserToGroups(UserCreation userCreation) { userCreation.getUserType().getScopes().forEach(scope -> addUserToGroup(userCreation, scope, findGroupIdByDisplayName(scope.getScope()))); } @Autowired  ScimServiceImpl(@Value("${c2s.patient-user.scim.url}") String uaaBaseUrl); @Override ScimUser save(ScimUser scimUser); @Override String findGroupIdByDisplayName(String groupDisplayName); @Override String findUserIdByUserName(String username); @Override @Transactional ScimGroupMember addUserToGroup(UserCreation userCreation, Scope scope, String groupId); @Override void addUserToGroups(UserCreation userCreation); @Override void updateUserWithNewGroup(UserCreation userCreation, Scope scope);  }
@Test public void testAddUserToGroups() throws Exception { final String userId = "userId"; final String groupId1 = "groupId1"; final String groupId2 = "groupId2"; final String groupDisplayName1 = "groupDisplayName1"; final String groupDisplayName2 = "groupDisplayName2"; final UserCreation userCreation = mock(UserCreation.class); final UserType userType = mock(UserType.class); final Scope scope1 = mock(Scope.class); when(scope1.getScope()).thenReturn(groupDisplayName1); final Scope scope2 = mock(Scope.class); when(scope2.getScope()).thenReturn(groupDisplayName2); when(userType.getScopes()).thenReturn(Arrays.asList(scope1, scope2)); when(userCreation.getUserType()).thenReturn(userType); when(userCreation.getUserId()).thenReturn(userId); final ScimGroupMember scimGroupMemberResponse1 = mock(ScimGroupMember.class); final ScimGroupMember scimGroupMemberResponse2 = mock(ScimGroupMember.class); when(restTemplate.postForObject(eq(groupsEndpoint + "/{groupId}/members"), argThat(matching( (ScimGroupMember member) -> userId.equals(member.getMemberId()) )), eq(ScimGroupMember.class), eq(groupId1))).thenReturn(scimGroupMemberResponse1); when(restTemplate.postForObject(eq(groupsEndpoint + "/{groupId}/members"), argThat(matching( (ScimGroupMember member) -> userId.equals(member.getMemberId()) )), eq(ScimGroupMember.class), eq(groupId2))).thenReturn(scimGroupMemberResponse2); final String id1 = "id1"; final String id2 = "id2"; final SearchResultsWrapperWithId searchResultsMock1 = mock(SearchResultsWrapperWithId.class); final IdentifierDto identifier1 = new IdentifierDto(); identifier1.setId(id1); final SearchResultsWrapperWithId searchResultsMock2 = mock(SearchResultsWrapperWithId.class); final IdentifierDto identifier2 = new IdentifierDto(); identifier2.setId(id2); when(searchResultsMock1.getResources()).thenReturn(Arrays.asList(identifier1)); when(searchResultsMock2.getResources()).thenReturn(Arrays.asList(identifier2)); when(restTemplate.getForObject(groupsEndpoint + "?filter=displayName eq \"" + groupDisplayName1 + "\"&attributes=id", SearchResultsWrapperWithId.class)).thenReturn(searchResultsMock1); when(restTemplate.getForObject(groupsEndpoint + "?filter=displayName eq \"" + groupDisplayName2 + "\"&attributes=id", SearchResultsWrapperWithId.class)).thenReturn(searchResultsMock2); sut.addUserToGroups(userCreation); verify(userScopeAssignmentRepository, times(1)).save(argThat(matching( (UserScopeAssignment userScopeAssignment) -> userScopeAssignment.getScope().equals(scope1) && userScopeAssignment.getUserCreation().equals(userCreation) ))); verify(userScopeAssignmentRepository, times(1)).save(argThat(matching( (UserScopeAssignment userScopeAssignment) -> userScopeAssignment.getScope().equals(scope2) && userScopeAssignment.getUserCreation().equals(userCreation) ))); }