Skip to content

Commit

Permalink
KAA-1594: Fixed endpoint user re-attach
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Tkhir committed Jan 12, 2017
1 parent 70c1ef0 commit e6cf878
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 19 deletions.
Expand Up @@ -74,6 +74,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -385,36 +386,44 @@ public EndpointProfileDto saveEndpointProfile(EndpointProfileDto endpointProfile
}

@Override
public EndpointProfileDto attachEndpointToUser(String userExternalId, String tenantId,
EndpointProfileDto profile) {
validateString(userExternalId, "Incorrect userExternalId "
+ userExternalId);
EndpointUser endpointUser = endpointUserDao.findByExternalIdAndTenantId(userExternalId,
tenantId);
if (endpointUser
== null) {
LOG.info("Creating new endpoint user with external id: [{}] in context of [{}] tenant",
userExternalId, tenantId);
public EndpointProfileDto attachEndpointToUser(String userExternalId, String tenantId, EndpointProfileDto profile) {
validateString(userExternalId, "Incorrect userExternalId " + userExternalId);
EndpointUser endpointUser = endpointUserDao.findByExternalIdAndTenantId(userExternalId, tenantId);
String currentEndpointUserId = profile.getEndpointUserId();
String endpointId = profile.getId();
if (endpointUser == null) {
LOG.info("Creating new endpoint user with external id: [{}] in context of [{}] tenant", userExternalId, tenantId);
EndpointUserDto endpointUserDto = new EndpointUserDto();
endpointUserDto.setTenantId(tenantId);
endpointUserDto.setExternalId(userExternalId);
endpointUserDto.setUsername(userExternalId);
endpointUser = endpointUserDao.save(endpointUserDto);
}

List<String> endpointIds = endpointUser.getEndpointIds();
if (endpointIds
== null) {
if (endpointIds == null) {
endpointIds = new ArrayList<>();
endpointUser.setEndpointIds(endpointIds);
} else if (endpointIds
!= null
&& endpointIds.contains(profile.getId())) {
LOG.warn("Endpoint is already assigned to current user {}.", profile.getEndpointUserId());
} else if (endpointIds.contains(endpointId)) {
LOG.warn("Endpoint is already assigned to current user {}.", currentEndpointUserId);
return profile;
}
endpointIds.add(profile.getId());
endpointIds.add(endpointId);

if (currentEndpointUserId != null) {
// Remove the endpoint ID from current user collection
EndpointUser currentEndpointUser = endpointUserDao.findById(currentEndpointUserId);
if (currentEndpointUser != null) {
Collection<String> currentUserEndpointIds = currentEndpointUser.getEndpointIds();
if (currentUserEndpointIds != null && currentUserEndpointIds.remove(endpointId)) {
endpointUserDao.save(currentEndpointUser);
}
}
}

endpointUser = endpointUserDao.save(endpointUser);
profile.setEndpointUserId(endpointUser.getId());
String newEndpointUserId = endpointUser.getId();
profile.setEndpointUserId(newEndpointUserId);
while (true) {
try {
LOG.trace("Save endpoint user {} and endpoint profile {}", endpointUser, profile);
Expand All @@ -423,7 +432,7 @@ public EndpointProfileDto attachEndpointToUser(String userExternalId, String ten
LOG.warn("Optimistic lock detected in endpoint profile ", Arrays.toString(profile
.getEndpointKey()), ex);
profile = findEndpointProfileByKeyHash(profile.getEndpointKeyHash());
profile.setEndpointUserId(endpointUser.getId());
profile.setEndpointUserId(newEndpointUserId);
}
}
}
Expand Down
Expand Up @@ -36,13 +36,15 @@
import org.kaaproject.kaa.server.common.dao.exception.KaaOptimisticLockingFailureException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -264,6 +266,107 @@ public void attachEndpointToUserTest() {
Assert.assertEquals(1, endpointIds.size());
Assert.assertEquals(endpointProfileDto.getId(), endpointIds.get(0));
}


@Test
public void attachEndpointToNewUserByExternalIdTest() {
TenantDto tenant = generateTenantDto();
String endpointGroupId = "124";
EndpointProfileDto endpointProfile = generateEndpointProfileWithGroupIdDto(endpointGroupId);
String userExternalId = UUID.randomUUID().toString();
String tenantId = tenant.getId();

EndpointProfileDto savedEndpointProfile = endpointService.attachEndpointToUser(userExternalId, tenantId, endpointProfile);

Assert.assertNotNull(savedEndpointProfile);
Assert.assertNotNull(savedEndpointProfile.getEndpointUserId());
EndpointUserDto attachedEndpointUser = endpointService.findEndpointUserById(savedEndpointProfile.getEndpointUserId());
Assert.assertNotNull(attachedEndpointUser);
Assert.assertEquals(userExternalId, attachedEndpointUser.getExternalId());
Assert.assertEquals(userExternalId, attachedEndpointUser.getUsername());
Assert.assertEquals(tenantId, attachedEndpointUser.getTenantId());
List<String> endpointIds = attachedEndpointUser.getEndpointIds();
Assert.assertNotNull(endpointIds);
Assert.assertEquals(1, endpointIds.size());
Assert.assertEquals(endpointProfile.getId(), endpointIds.get(0));
}

@Test
public void attachEndpointToExistingUserByExternalIdTest() {
TenantDto tenant = generateTenantDto();
String endpointGroupId = "124";
EndpointProfileDto endpointProfile = generateEndpointProfileWithGroupIdDto(endpointGroupId);
String tenantId = tenant.getId();
EndpointUserDto endpointUserDto = generateEndpointUserDto(tenantId);
String userExternalId = endpointUserDto.getExternalId();

EndpointProfileDto savedEndpointProfile = endpointService.attachEndpointToUser(userExternalId, tenantId, endpointProfile);

Assert.assertNotNull(savedEndpointProfile);
Assert.assertNotNull(savedEndpointProfile.getEndpointUserId());
EndpointUserDto attachedEndpointUser = endpointService.findEndpointUserById(savedEndpointProfile.getEndpointUserId());
Assert.assertNotNull(attachedEndpointUser);
Assert.assertEquals(userExternalId, attachedEndpointUser.getExternalId());
Assert.assertEquals(tenantId, attachedEndpointUser.getTenantId());
List<String> endpointIds = attachedEndpointUser.getEndpointIds();
Assert.assertNotNull(endpointIds);
Assert.assertEquals(1, endpointIds.size());
Assert.assertEquals(endpointProfile.getId(), endpointIds.get(0));
}

@Test
public void attachEndpointToAlreadyAttachedUserByExternalIdTest() {
TenantDto tenant = generateTenantDto();
String endpointGroupId = "124";
EndpointProfileDto endpointProfile = generateEndpointProfileWithGroupIdDto(endpointGroupId);
String tenantId = tenant.getId();
EndpointUserDto endpointUserDto = generateEndpointUserDto(tenantId);
String userExternalId = endpointUserDto.getExternalId();

EndpointProfileDto savedEndpointProfile = endpointService.attachEndpointToUser(userExternalId, tenantId, endpointProfile);
savedEndpointProfile = endpointService.attachEndpointToUser(userExternalId, tenantId, savedEndpointProfile);

Assert.assertNotNull(savedEndpointProfile);
Assert.assertNotNull(savedEndpointProfile.getEndpointUserId());
EndpointUserDto attachedEndpointUser = endpointService.findEndpointUserById(savedEndpointProfile.getEndpointUserId());
Assert.assertNotNull(attachedEndpointUser);
Assert.assertEquals(userExternalId, attachedEndpointUser.getExternalId());
Assert.assertEquals(tenantId, attachedEndpointUser.getTenantId());
List<String> endpointIds = attachedEndpointUser.getEndpointIds();
Assert.assertNotNull(endpointIds);
Assert.assertEquals(1, endpointIds.size());
Assert.assertEquals(endpointProfile.getId(), endpointIds.get(0));
}

@Test
public void attachEndpointAlreadyAttachedToAnotherUserByExternalIdTest() {
TenantDto tenant = generateTenantDto();
String endpointGroupId = "124";
EndpointProfileDto endpointProfile = generateEndpointProfileWithGroupIdDto(endpointGroupId);
String tenantId = tenant.getId();
EndpointUserDto endpointUser1 = generateEndpointUserDto(tenantId);
String userExternalId1 = endpointUser1.getExternalId();

EndpointUserDto endpointUser2 = generateEndpointUserDto(tenantId);
String userExternalId2 = endpointUser2.getExternalId();


EndpointProfileDto savedEndpointProfile = endpointService.attachEndpointToUser(userExternalId1, tenantId, endpointProfile);
savedEndpointProfile = endpointService.attachEndpointToUser(userExternalId2, tenantId, savedEndpointProfile);

EndpointUserDto attachedUser = endpointService.findEndpointUserById(savedEndpointProfile.getEndpointUserId());
endpointUser1 = endpointService.findEndpointUserByExternalIdAndTenantId(userExternalId1, tenantId);
endpointUser2 = endpointService.findEndpointUserByExternalIdAndTenantId(userExternalId2, tenantId);
Assert.assertNotNull(attachedUser);
Assert.assertEquals(endpointUser2, attachedUser);
List<String> user1EndpointIds = endpointUser1.getEndpointIds();
List<String> user2EndpointIds = endpointUser2.getEndpointIds();
Assert.assertTrue(CollectionUtils.isEmpty(user1EndpointIds));
Assert.assertFalse(CollectionUtils.isEmpty(user2EndpointIds));
Assert.assertEquals(1, user2EndpointIds.size());
Assert.assertEquals(endpointProfile.getId(), user2EndpointIds.get(0));
Assert.assertEquals(endpointUser2.getId(), savedEndpointProfile.getEndpointUserId());
}

@Test
public void findEndpointProfilesByExternalUserIdTest() {
Expand Down

0 comments on commit e6cf878

Please sign in to comment.