1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington/*
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington *
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington *
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * The contents of this file are subject to the terms
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * of the Common Development and Distribution License
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * (the License). You may not use this file except in
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * compliance with the License.
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington *
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * You can obtain a copy of the License at
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * https://opensso.dev.java.net/public/CDDLv1.0.html or
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * opensso/legal/CDDLv1.0.txt
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * See the License for the specific language governing
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * permission and limitations under the License.
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington *
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * When distributing Covered Code, include this CDDL
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * Header Notice in each file and include the License file
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * at opensso/legal/CDDLv1.0.txt.
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * If applicable, add the following below the CDDL Header,
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * with the fields enclosed by brackets [] replaced by
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * your own identifying information:
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * "Portions Copyrighted [year] [name of copyright owner]"
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington *
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington */
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington/*
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * Portions Copyrighted 2013 Syntegrity.
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * Portions Copyrighted 2013 ForgeRock Inc.
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington */
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonpackage org.forgerock.openam.authentication.modules.deviceprint;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.forgerock.openam.authentication.modules.deviceprint.comparators.ComparisonResult;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.forgerock.openam.authentication.modules.deviceprint.comparators.DevicePrintComparator;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.forgerock.openam.authentication.modules.deviceprint.exceptions.NotUniqueUserProfileException;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.forgerock.openam.authentication.modules.deviceprint.extractors.DevicePrintExtractorFactory;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.forgerock.openam.authentication.modules.deviceprint.extractors.Extractor;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.forgerock.openam.authentication.modules.deviceprint.model.UserProfile;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.mockito.ArgumentCaptor;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.mockito.Matchers;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.testng.annotations.BeforeMethod;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport org.testng.annotations.Test;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport javax.servlet.http.HttpServletRequest;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport java.util.ArrayList;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport java.util.Calendar;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport java.util.Date;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport java.util.HashSet;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport java.util.List;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport java.util.Set;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport static org.mockito.BDDMockito.given;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport static org.mockito.Mockito.*;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport static org.testng.Assert.assertEquals;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport static org.testng.AssertJUnit.assertNotNull;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonimport static org.testng.AssertJUnit.assertNull;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunningtonpublic class DevicePrintServiceTest {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington private DevicePrintService devicePrintService;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington private DevicePrintAuthenticationConfig devicePrintAuthenticationConfig;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington private UserProfilesDao userProfilesDao;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington private DevicePrintExtractorFactory extractorFactory;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington private DevicePrintComparator devicePrintComparator;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington @BeforeMethod
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington public void setUpMethod() {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington devicePrintAuthenticationConfig = mock(DevicePrintAuthenticationConfig.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington userProfilesDao = mock(UserProfilesDao.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington extractorFactory = mock(DevicePrintExtractorFactory.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington devicePrintComparator = mock(DevicePrintComparator.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(devicePrintAuthenticationConfig.getInt(
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrintAuthenticationConfig.PROFILE_EXPIRATION_DAYS)).willReturn(30);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(devicePrintAuthenticationConfig.getInt(
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrintAuthenticationConfig.MAX_STORED_PROFILES)).willReturn(2);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington devicePrintService = new DevicePrintService(devicePrintAuthenticationConfig, userProfilesDao, extractorFactory,
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington devicePrintComparator);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington }
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington @Test
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington public void shouldCheckHasRequiredAttributes() {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Given
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrint devicePrint = mock(DevicePrint.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //When
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington devicePrintService.hasRequiredAttributes(devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Then
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(devicePrintAuthenticationConfig).hasRequiredAttributes(devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington }
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington @Test
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington public void shouldGetCurrentDevicePrint() {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Given
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington HttpServletRequest request = mock(HttpServletRequest.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington Set<Extractor> extractors = new HashSet<Extractor>();
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington Extractor extractorOne = mock(Extractor.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington Extractor extractorTwo = mock(Extractor.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(extractorFactory.getExtractors()).willReturn(extractors);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington extractors.add(extractorOne);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington extractors.add(extractorTwo);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //When
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrint devicePrint = devicePrintService.getDevicePrint(request);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Then
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(extractorOne).extractData(Matchers.<DevicePrint>anyObject(), eq(request));
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(extractorTwo).extractData(Matchers.<DevicePrint>anyObject(), eq(request));
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington assertNotNull(devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington }
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington @Test
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington public void shouldGetBestMatchingUserProfileWithNoStoredProfiles() {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Given
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrint devicePrint = mock(DevicePrint.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington List<UserProfile> userProfiles = new ArrayList<UserProfile>();
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfilesDao.getProfiles()).willReturn(userProfiles);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //When
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile selectedUserProfile = devicePrintService.getBestMatchingUserProfile(devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Then
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington assertNull(selectedUserProfile);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington }
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington @Test
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington public void shouldGetBestMatchingUserProfile() {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Given
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrint devicePrint = mock(DevicePrint.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington List<UserProfile> userProfiles = new ArrayList<UserProfile>();
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile userProfileOne = mock(UserProfile.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile userProfileTwo = mock(UserProfile.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile userProfileThree = mock(UserProfile.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrint userProfileOneDevicePrint = mock(DevicePrint.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrint userProfileTwoDevicePrint = mock(DevicePrint.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrint userProfileThreeDevicePrint = mock(DevicePrint.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington ComparisonResult userProfileOneResult = new ComparisonResult(30L);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington ComparisonResult userProfileThreeResult = new ComparisonResult(20L);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington userProfiles.add(userProfileOne);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington userProfiles.add(userProfileTwo);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington userProfiles.add(userProfileThree);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfilesDao.getProfiles()).willReturn(userProfiles);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfileOne.getLastSelectedDate()).willReturn(getDate(10));
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfileTwo.getLastSelectedDate()).willReturn(getDate(31));
91c44b7f7277190e648c7e9a8d744fb71491aa20Phill Cunnington given(userProfileThree.getLastSelectedDate()).willReturn(getDate(29));
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfileOne.getDevicePrint()).willReturn(userProfileOneDevicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfileTwo.getDevicePrint()).willReturn(userProfileTwoDevicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfileThree.getDevicePrint()).willReturn(userProfileThreeDevicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(devicePrintComparator.compare(devicePrint, userProfileOneDevicePrint,
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington devicePrintAuthenticationConfig)).willReturn(userProfileOneResult);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(devicePrintComparator.compare(devicePrint, userProfileThreeDevicePrint,
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington devicePrintAuthenticationConfig)).willReturn(userProfileThreeResult);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
6c1db9f0cd43d3c9f8a32eaf54718d59f5f071d6Phill Cunnington given(devicePrintAuthenticationConfig.getLong(
6c1db9f0cd43d3c9f8a32eaf54718d59f5f071d6Phill Cunnington DevicePrintAuthenticationConfig.MAX_TOLERATED_PENALTY_POINTS)).willReturn(50L);
6c1db9f0cd43d3c9f8a32eaf54718d59f5f071d6Phill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //When
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile selectedUserProfile = devicePrintService.getBestMatchingUserProfile(devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Then
91c44b7f7277190e648c7e9a8d744fb71491aa20Phill Cunnington assertEquals(selectedUserProfile, userProfileThree);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington }
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington private Date getDate(int daysAgo) {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington Calendar calendar = Calendar.getInstance();
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington calendar.add(Calendar.DAY_OF_YEAR, -daysAgo);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington return calendar.getTime();
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington }
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington @Test
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington public void shouldCreateNewProfile() throws NotUniqueUserProfileException {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Given
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrint devicePrint = mock(DevicePrint.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfilesDao.getProfiles()).willReturn(new ArrayList<UserProfile>());
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //When
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington devicePrintService.createNewProfile(devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Then
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfilesDao).removeProfile(anyString());
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington ArgumentCaptor<UserProfile> userProfileCaptor = ArgumentCaptor.forClass(UserProfile.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfilesDao).addProfile(userProfileCaptor.capture());
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile userProfile = userProfileCaptor.getValue();
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington assertEquals(userProfile.getDevicePrint(), devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfilesDao).saveProfiles();
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington }
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington @Test
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington public void shouldCreateNewProfileAndDeleteOlderOnes() throws NotUniqueUserProfileException {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Given
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrint devicePrint = mock(DevicePrint.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington List<UserProfile> userProfiles = spy(new ArrayList<UserProfile>());
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile userProfileOne = mock(UserProfile.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile userProfileTwo = mock(UserProfile.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile userProfileThree = mock(UserProfile.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington userProfiles.add(userProfileOne);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington userProfiles.add(userProfileTwo);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington userProfiles.add(userProfileThree);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfilesDao.getProfiles()).willReturn(userProfiles);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfileOne.getLastSelectedDate()).willReturn(getDate(10));
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfileTwo.getLastSelectedDate()).willReturn(getDate(31));
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfileThree.getLastSelectedDate()).willReturn(getDate(30));
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //When
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington devicePrintService.createNewProfile(devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Then
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfilesDao).removeProfile(anyString());
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfiles).remove(userProfileTwo);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfiles).remove(userProfileThree);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington ArgumentCaptor<UserProfile> userProfileCaptor = ArgumentCaptor.forClass(UserProfile.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfilesDao).addProfile(userProfileCaptor.capture());
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile userProfile = userProfileCaptor.getValue();
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington assertEquals(userProfile.getDevicePrint(), devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfilesDao).saveProfiles();
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington }
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington @Test
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington public void shouldUpdateProfile() throws NotUniqueUserProfileException {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Given
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington UserProfile userProfile = mock(UserProfile.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington DevicePrint devicePrint = mock(DevicePrint.class);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfile.getUuid()).willReturn("USER_PROFILE_UUID");
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington given(userProfilesDao.getProfiles()).willReturn(new ArrayList<UserProfile>());
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //When
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington devicePrintService.updateProfile(userProfile, devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington //Then
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfile).setSelectionCounter(anyLong());
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfile).setLastSelectedDate(Matchers.<Date>anyObject());
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfile).setDevicePrint(devicePrint);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfilesDao).removeProfile(anyString());
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfilesDao).addProfile(userProfile);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington verify(userProfilesDao).saveProfiles();
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington }
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington}