private EndpointProfileDto updateProfile( EndpointProfileDto update, BiFunction<EndpointProfileDto, EndpointProfileDto, EndpointProfileDto> mergeFunction, int retryCount) { LOG.debug("Updating profile {} ", update); try { return endpointService.saveEndpointProfile(update); } catch (KaaOptimisticLockingFailureException ex) { LOG.warn("Failed to update profile {} ", update, ex); if (retryCount > 0) { EndpointProfileDto stored = endpointService.findEndpointProfileByKeyHash( update.getEndpointKeyHash()); LOG.warn("Going to merge it with stored profile {}", stored); EndpointProfileDto merged = mergeFunction.apply(stored, update); LOG.warn("Merge result: {}", merged); return updateProfile(merged, mergeFunction, retryCount - 1); } else { throw ex; } } }
@Test(expected = KaaOptimisticLockingFailureException.class) public void createDuplicateProfileTest() { EndpointProfileDto endpointProfileDto = generateEndpointProfileWithGroupIdDto("124"); endpointProfileDto.setVersion(null); endpointService.saveEndpointProfile(endpointProfileDto); }
protected EndpointProfileDto generateEndpointProfileDtoWithSchemaVersion(String appId, int schemaVersion, String srvProfileBody) { EndpointProfileDto profileDto = new EndpointProfileDto(); profileDto.setApplicationId(appId); profileDto.setServerProfileVersion(schemaVersion); profileDto.setEndpointKeyHash("TEST_KEY_HASH".getBytes()); profileDto.setEpsConfigurationHash(EPS_CONFIG_HASH); try { profileDto.setClientProfileBody(readSchemaFileAsString(TEST_PROFILE_BODY_PATH)); } catch (IOException e) { LOG.error("Can't set client-side EP body {}", e); } profileDto.setSdkToken(UUID.randomUUID().toString()); profileDto.setServerProfileBody(srvProfileBody); return endpointService.saveEndpointProfile(profileDto); }
protected EndpointProfileDto generateEndpointProfileWithGroupIdDto(String endpointGroupId) { EndpointProfileDto profileDto = new EndpointProfileDto(); profileDto.setEndpointKeyHash(generateString("TEST_KEY_HASH").getBytes()); String appId = generateApplicationDto().getId(); profileDto.setApplicationId(appId); List<EndpointGroupStateDto> groupState = new ArrayList<>(); groupState.add(new EndpointGroupStateDto(endpointGroupId, null, null)); profileDto.setGroupState(groupState); try { profileDto.setClientProfileBody(readSchemaFileAsString(TEST_PROFILE_BODY_PATH)); } catch (IOException e) { LOG.error("Can't set client-side EP body {}", e); } profileDto.setServerProfileBody("{\"serverTitle\": \"SERVER_TEST\"}"); profileDto.setSdkToken(UUID.randomUUID().toString()); return endpointService.saveEndpointProfile(profileDto); }
String accessToken = "" + i; endpointProfileDto.setAccessToken(accessToken); endpointService.saveEndpointProfile(endpointProfileDto); accessTokens.add(accessToken); endpointIds.add(endpointProfileDto.getId());
protected EndpointProfileDto generateEndpointProfileDto(String appId, List<String> topicIds) { if (isBlank(appId)) { appId = generateApplicationDto().getId(); } EndpointProfileDto profileDto = new EndpointProfileDto(); profileDto.setApplicationId(appId); profileDto.setSubscriptions(topicIds); profileDto.setEndpointKeyHash("TEST_KEY_HASH".getBytes()); profileDto.setServerProfileBody("{\"serverTitle\": \"SERVER_TEST\"}"); profileDto.setEpsConfigurationHash(EPS_CONFIG_HASH); try { profileDto.setClientProfileBody(readSchemaFileAsString(TEST_PROFILE_BODY_PATH)); } catch (IOException e) { LOG.error("Can't set client-side EP body {}", e); } profileDto.setSdkToken(UUID.randomUUID().toString()); return endpointService.saveEndpointProfile(profileDto); }
@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); }
return endpointService.saveEndpointProfile(dto); } else { return updateProfile(new UpdateProfileRequest(
@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)); }