@Override public EndpointProfileDto attachEndpointToUser(EndpointProfileDto profile, String appToken, String userExternalId) { String tenantId = cacheService.getTenantIdByAppToken(appToken); return endpointService.attachEndpointToUser(userExternalId, tenantId, profile); }
@Override public EndpointProfileDto call() { EndpointProfileDto ep = null; try { ep = endpointService.attachEndpointToUser(endpointUserId, accessToken); } catch (Throwable t) { LOG.error("Error: " + t.getClass() + ": " + t.getMessage() + ". accessToken = " + accessToken); throw t; } return ep; } }));
@Override public EndpointAttachResponse attachEndpoint(EndpointProfileDto profile, EndpointAttachRequest endpointAttachRequest) { EndpointAttachResponse response = new EndpointAttachResponse(); response.setRequestId(endpointAttachRequest.getRequestId()); response.setResult(SyncStatus.FAILURE); String endpointUserId = profile.getEndpointUserId(); if (isNotEmpty(endpointUserId)) { try { EndpointProfileDto attachedEndpoint = endpointService.attachEndpointToUser( endpointUserId, endpointAttachRequest.getEndpointAccessToken()); response.setResult(SyncStatus.SUCCESS); response.setEndpointKeyHash(Base64Util.encode(attachedEndpoint.getEndpointKeyHash())); } catch (DatabaseProcessingException ex) { LOG.warn("[{}] failed to attach endpoint with access token {} and " + "user {}, exception catched: {}", Base64Util.encode(profile.getEndpointKeyHash()), endpointAttachRequest.getEndpointAccessToken(), profile.getEndpointUserId(), ex); } } else { LOG.warn("[{}] received attach endpoint request, but there is no user to attach.", Base64Util.encode(profile.getEndpointKeyHash())); } return response; }
@Test public void findEndpointProfilesByExternalUserIdTest() { TenantDto tenantDto = generateTenantDto(); EndpointUserDto endpointUserDto = generateEndpointUserDto(tenantDto.getId()); String endpointGroupId = "124"; String accessToken1 = "1111"; String accessToken2 = "2222"; EndpointProfileDto endpointProfileDto1 = generateEndpointProfileWithGroupIdDto(endpointGroupId); endpointProfileDto1.setAccessToken(accessToken1); EndpointProfileDto endpointProfileDto2 = generateEndpointProfileWithGroupIdDto(endpointGroupId); endpointProfileDto2.setAccessToken(accessToken2); endpointProfileDto1 = endpointService.saveEndpointProfile(endpointProfileDto1); endpointProfileDto2 = endpointService.saveEndpointProfile(endpointProfileDto2); endpointService.attachEndpointToUser(endpointUserDto.getId(), accessToken1); endpointService.attachEndpointToUser(endpointUserDto.getId(), accessToken2); List<EndpointProfileDto> endpointProfiles = endpointService.findEndpointProfilesByExternalIdAndTenantId(endpointUserDto.getExternalId(), tenantDto.getId()); Assert.assertEquals(2, endpointProfiles.size()); Comparator<EndpointProfileDto> endpointProfilesComparator = new Comparator<EndpointProfileDto>() { @Override public int compare(EndpointProfileDto o1, EndpointProfileDto o2) { return o1.getId().compareTo(o2.getId()); } }; List<EndpointProfileDto> expected = Arrays.asList(endpointProfileDto1, endpointProfileDto2); Collections.sort(expected, endpointProfilesComparator); Collections.sort(endpointProfiles, endpointProfilesComparator); Assert.assertEquals(expected, endpointProfiles); }
@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 attachEndpointToUserTest() { TenantDto tenantDto = generateTenantDto(); EndpointUserDto endpointUserDto = generateEndpointUserDto(tenantDto.getId()); String endpointGroupId = "124"; EndpointProfileDto endpointProfileDto = generateEndpointProfileWithGroupIdDto(endpointGroupId); String accessToken = "1111"; endpointProfileDto.setAccessToken(accessToken); endpointService.saveEndpointProfile(endpointProfileDto); endpointService.attachEndpointToUser(endpointUserDto.getId(), accessToken); endpointUserDto = endpointService.findEndpointUserById(endpointUserDto.getId()); List<String> endpointIds = endpointUserDto.getEndpointIds(); Assert.assertNotNull(endpointIds); Assert.assertEquals(1, endpointIds.size()); Assert.assertEquals(endpointProfileDto.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 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)); }