Search in sources :

Example 1 with ComparisonResult

use of org.forgerock.openam.authentication.modules.deviceprint.comparators.ComparisonResult in project OpenAM by OpenRock.

the class DevicePrintServiceTest method shouldGetBestMatchingUserProfile.

@Test
public void shouldGetBestMatchingUserProfile() {
    //Given
    DevicePrint devicePrint = mock(DevicePrint.class);
    List<UserProfile> userProfiles = new ArrayList<UserProfile>();
    UserProfile userProfileOne = mock(UserProfile.class);
    UserProfile userProfileTwo = mock(UserProfile.class);
    UserProfile userProfileThree = mock(UserProfile.class);
    DevicePrint userProfileOneDevicePrint = mock(DevicePrint.class);
    DevicePrint userProfileTwoDevicePrint = mock(DevicePrint.class);
    DevicePrint userProfileThreeDevicePrint = mock(DevicePrint.class);
    ComparisonResult userProfileOneResult = new ComparisonResult(30L);
    ComparisonResult userProfileThreeResult = new ComparisonResult(20L);
    userProfiles.add(userProfileOne);
    userProfiles.add(userProfileTwo);
    userProfiles.add(userProfileThree);
    given(userProfilesDao.getProfiles()).willReturn(userProfiles);
    given(userProfileOne.getLastSelectedDate()).willReturn(getDate(10));
    given(userProfileTwo.getLastSelectedDate()).willReturn(getDate(31));
    given(userProfileThree.getLastSelectedDate()).willReturn(getDate(29));
    given(userProfileOne.getDevicePrint()).willReturn(userProfileOneDevicePrint);
    given(userProfileTwo.getDevicePrint()).willReturn(userProfileTwoDevicePrint);
    given(userProfileThree.getDevicePrint()).willReturn(userProfileThreeDevicePrint);
    given(devicePrintComparator.compare(devicePrint, userProfileOneDevicePrint, devicePrintAuthenticationConfig)).willReturn(userProfileOneResult);
    given(devicePrintComparator.compare(devicePrint, userProfileThreeDevicePrint, devicePrintAuthenticationConfig)).willReturn(userProfileThreeResult);
    given(devicePrintAuthenticationConfig.getLong(DevicePrintAuthenticationConfig.MAX_TOLERATED_PENALTY_POINTS)).willReturn(50L);
    //When
    UserProfile selectedUserProfile = devicePrintService.getBestMatchingUserProfile(devicePrint);
    //Then
    assertEquals(selectedUserProfile, userProfileThree);
}
Also used : UserProfile(org.forgerock.openam.authentication.modules.deviceprint.model.UserProfile) ArrayList(java.util.ArrayList) DevicePrint(org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint) ComparisonResult(org.forgerock.openam.authentication.modules.deviceprint.comparators.ComparisonResult) Test(org.testng.annotations.Test)

Example 2 with ComparisonResult

use of org.forgerock.openam.authentication.modules.deviceprint.comparators.ComparisonResult in project OpenAM by OpenRock.

the class DevicePrintService method getBestMatchingUserProfile.

/**
     * Uses the given Device Print information to find the best matching stored Device Print information from stored
     * User Profiles. It uses the penalty points set in the authentication module settings to determine whether a stored
     * Device print matches the given one.
     *
     * If no match is found null is returned.
     *
     * @param devicePrint The Device Print to find a match for.
     * @return The matching User Profile or null.
     */
public UserProfile getBestMatchingUserProfile(DevicePrint devicePrint) {
    SortedMap<ComparisonResult, UserProfile> comparisonResultMap = new TreeMap<ComparisonResult, UserProfile>();
    for (UserProfile userProfile : getNotExpiredProfiles()) {
        DevicePrint storedDevicePrint = userProfile.getDevicePrint();
        comparisonResultMap.put(devicePrintComparator.compare(devicePrint, storedDevicePrint, devicePrintAuthenticationConfig), userProfile);
    }
    if (comparisonResultMap.isEmpty()) {
        return null;
    }
    ComparisonResult selectedComparisonResult = comparisonResultMap.firstKey();
    UserProfile selectedProfile = null;
    if (selectedComparisonResult.getPenaltyPoints() <= devicePrintAuthenticationConfig.getLong(DevicePrintAuthenticationConfig.MAX_TOLERATED_PENALTY_POINTS)) {
        selectedProfile = comparisonResultMap.get(selectedComparisonResult);
    }
    return selectedProfile;
}
Also used : UserProfile(org.forgerock.openam.authentication.modules.deviceprint.model.UserProfile) DevicePrint(org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint) TreeMap(java.util.TreeMap) ComparisonResult(org.forgerock.openam.authentication.modules.deviceprint.comparators.ComparisonResult)

Aggregations

ComparisonResult (org.forgerock.openam.authentication.modules.deviceprint.comparators.ComparisonResult)2 DevicePrint (org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint)2 UserProfile (org.forgerock.openam.authentication.modules.deviceprint.model.UserProfile)2 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 Test (org.testng.annotations.Test)1