/** * Create new instance of <code>MongoEndpointUser</code>. * @param dto data transfer object contain data that * assign on fields of new instance */ public MongoEndpointUser(EndpointUserDto dto) { this.id = dto.getId(); this.username = dto.getUsername(); this.externalId = dto.getExternalId(); this.tenantId = dto.getTenantId(); this.accessToken = dto.getAccessToken(); this.endpointIds = dto.getEndpointIds(); this.version = dto.getVersion(); }
@Override public EndpointUserDto toDto() { EndpointUserDto dto = new EndpointUserDto(); dto.setId(id); dto.setUsername(username); dto.setExternalId(externalId); dto.setTenantId(tenantId); dto.setAccessToken(accessToken); dto.setEndpointIds(endpointIds); dto.setVersion(version); return dto; }
protected EndpointUserDto generateEndpointUserDto(String tenantId) { EndpointUserDto endpointUser = new EndpointUserDto(); endpointUser.setExternalId(ENDPOINT_USER_EXTERNAL_ID + UUID.randomUUID().toString()); endpointUser.setUsername(ENDPOINT_USER_NAME + UUID.randomUUID().toString()); if (tenantId == null) { tenantId = UUID.randomUUID().toString(); } endpointUser.setTenantId(tenantId); endpointUser = endpointService.saveEndpointUser(endpointUser); return endpointUser; }
@Test public void generateEndpointUserAccessTokenTest() { TenantDto tenantDto = generateTenantDto(); EndpointUserDto savedEndpointUserDto = generateEndpointUserDto(tenantDto.getId()); Assert.assertNull(savedEndpointUserDto.getAccessToken()); String generatedAccessToken = endpointService.generateEndpointUserAccessToken(savedEndpointUserDto.getExternalId(), savedEndpointUserDto.getTenantId()); EndpointUserDto endpointUser = endpointService.findEndpointUserById(savedEndpointUserDto.getId()); Assert.assertNotNull(generatedAccessToken); Assert.assertEquals(generatedAccessToken, endpointUser.getAccessToken()); }
private void removeAllEndpointUsers() { List<EndpointUserDto> endpointUsers = endpointService.findAllEndpointUsers(); for (EndpointUserDto endpointUser : endpointUsers) { endpointService.removeEndpointUserById(endpointUser.getId()); } }
@Override public EndpointUserDto saveEndpointUser(EndpointUserDto endpointUserDto) { EndpointUserDto endpointUser = null; if (isValidObject(endpointUserDto)) { EndpointUser user = endpointUserDao.findByExternalIdAndTenantId(endpointUserDto .getExternalId(), endpointUserDto.getTenantId()); if (user == null || user.getId().equals(endpointUserDto.getId())) { endpointUser = getDto(endpointUserDao.save(endpointUserDto)); } else { throw new IncorrectParameterException("Can't save endpoint user with same external id"); } } return endpointUser; }
@Test public void testCheckAccessToken() throws Exception { EndpointUserDto expected = generateEndpointUser(); String accessToken = endpointUserDao.generateAccessToken(expected.getExternalId(), expected.getTenantId()); Boolean result = endpointUserDao.checkAccessToken(expected.getExternalId(), expected.getTenantId(), accessToken); Assert.assertTrue(result); result = endpointUserDao.checkAccessToken(expected.getExternalId(), expected.getTenantId(), ""); Assert.assertFalse(result); } }
@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 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 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()); }
final String endpointUserId = endpointUserDto.getId(); for (int i = 0; i < accessTokens.size(); i++) { final String accessToken = accessTokens.get(i); List<String> attachedEndpointIds = endpointUserDto.getEndpointIds(); Assert.assertNotNull(attachedEndpointIds); Collections.sort(endpointIds); attachedEndpointIds = endpointUserDto.getEndpointIds(); Assert.assertTrue(attachedEndpointIds == null || attachedEndpointIds.isEmpty());
@Test public void findUserConfigurationByExternalUIdAndAppTokenAndSchemaVersionTest() throws IOException { ApplicationDto appDto = generateApplicationDto(); EndpointUserDto userDto = generateEndpointUserDto(appDto.getTenantId()); EndpointUserConfigurationDto userConfigurationDto = generateEndpointUserConfigurationDto(userDto, appDto, null, readSchemaFileAsString(OVERRIDE_USER_DATA_JSON)); EndpointUserConfigurationDto endpointUserConfigurationDto = userConfigurationService.findUserConfigurationByExternalUIdAndAppTokenAndSchemaVersion( userDto.getExternalId(), appDto.getApplicationToken(), userConfigurationDto.getSchemaVersion(), appDto.getTenantId()); Assert.assertNotNull(endpointUserConfigurationDto); }
protected EndpointUserDto generateEndpointUser(List<String> endpointIds) { EndpointUserDto endpointUserDto = new EndpointUserDto(); endpointUserDto.setExternalId(UUID.randomUUID().toString()); endpointUserDto.setUsername("Test username"); endpointUserDto.setTenantId(UUID.randomUUID().toString()); endpointUserDto.setEndpointIds(endpointIds); return endpointUserDao.save(new CassandraEndpointUser(endpointUserDto)).toDto(); }
@Test public void removeEndpointUserByIdTest() { TenantDto tenantDto = generateTenantDto(); EndpointUserDto savedEndpointUserDto = generateEndpointUserDto(tenantDto.getId()); endpointService.removeEndpointUserById(savedEndpointUserDto.getId()); EndpointUserDto endpointUser = endpointService.findEndpointUserById(savedEndpointUserDto.getId()); Assert.assertNull(endpointUser); }
@Test public void generateAccessTokenTest() { TenantDto tenant = generateTenantDto(); EndpointUserDto dto = generateEndpointUserDto(tenant.getId()); Assert.assertNotNull(dto); String accessToken = endpointUserDao.generateAccessToken(dto.getExternalId(), dto.getTenantId().toString()); MongoEndpointUser found = endpointUserDao.findById(dto.getId()); Assert.assertEquals(accessToken, found.getAccessToken()); Assert.assertTrue(endpointUserDao.checkAccessToken(found.getExternalId(), found.getTenantId().toString(), accessToken)); Assert.assertFalse(endpointUserDao.checkAccessToken(found.getTenantId().toString(), found.getExternalId(), "invalid")); }
@Test public void testRemoveByExternalIdAndTenantId() throws Exception { EndpointUserDto expected = generateEndpointUser(); endpointUserDao.removeByExternalIdAndTenantId(expected.getExternalId(), expected.getTenantId()); CassandraEndpointUser found = endpointUserDao.findByExternalIdAndTenantId(expected.getExternalId(), expected.getTenantId()); Assert.assertNull(found); }
@Test public void findUserConfigurationByExternalUIdAndAppTokenAndSchemaVersionNullTest() throws IOException { ApplicationDto appDto = generateApplicationDto(); EndpointUserDto userDto = generateEndpointUserDto(appDto.getTenantId()); EndpointUserConfigurationDto userConfigurationDto = generateEndpointUserConfigurationDto(userDto, appDto, null, readSchemaFileAsString(OVERRIDE_USER_DATA_JSON)); userConfigurationService.removeByUserIdAndAppTokenAndSchemaVersion(userDto.getId(), appDto.getApplicationToken(), userConfigurationDto.getSchemaVersion()); EndpointUserConfigurationDto endpointUserConfigurationDto = userConfigurationService.findUserConfigurationByExternalUIdAndAppTokenAndSchemaVersion( userDto.getExternalId(), appDto.getApplicationToken(), userConfigurationDto.getSchemaVersion(), appDto.getTenantId()); Assert.assertNull(endpointUserConfigurationDto); }
@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 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)); }