|
| 1 | +/* |
| 2 | + * Copyright (C) 2012 Slava Semushin <slava.semushin@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | + |
| 19 | +package ru.mystamps.web.service; |
| 20 | + |
| 21 | +import java.util.Calendar; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Date; |
| 24 | +import java.util.GregorianCalendar; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import org.apache.commons.lang3.time.DateUtils; |
| 28 | + |
| 29 | +import org.testng.annotations.BeforeMethod; |
| 30 | +import org.testng.annotations.Test; |
| 31 | + |
| 32 | +import org.mockito.ArgumentCaptor; |
| 33 | +import org.mockito.Captor; |
| 34 | +import org.mockito.InjectMocks; |
| 35 | +import org.mockito.Mock; |
| 36 | +import org.mockito.MockitoAnnotations; |
| 37 | + |
| 38 | +import static org.fest.assertions.Assertions.assertThat; |
| 39 | + |
| 40 | +import static org.mockito.Mockito.any; |
| 41 | +import static org.mockito.Mockito.eq; |
| 42 | +import static org.mockito.Mockito.never; |
| 43 | +import static org.mockito.Mockito.verify; |
| 44 | +import static org.mockito.Mockito.when; |
| 45 | + |
| 46 | +import ru.mystamps.web.dao.UsersActivationDao; |
| 47 | +import ru.mystamps.web.entity.UsersActivation; |
| 48 | + |
| 49 | +public class CronServiceTest { |
| 50 | + |
| 51 | + @Mock |
| 52 | + private UsersActivationDao usersActivationDao; |
| 53 | + |
| 54 | + @Captor |
| 55 | + private ArgumentCaptor<Date> dateCaptor; |
| 56 | + |
| 57 | + @InjectMocks |
| 58 | + private CronService service = new CronService(); |
| 59 | + |
| 60 | + @BeforeMethod |
| 61 | + public void setUp() { |
| 62 | + MockitoAnnotations.initMocks(this); |
| 63 | + } |
| 64 | + |
| 65 | + // |
| 66 | + // Tests for purgeUsersActivations() |
| 67 | + // |
| 68 | + |
| 69 | + @Test |
| 70 | + public void purgeUsersActivationsShouldGetExpiredActivationsFromDao() { |
| 71 | + when(usersActivationDao.findByCreatedAtLessThan(any(Date.class))) |
| 72 | + .thenReturn(Collections.<UsersActivation>emptyList()); |
| 73 | + |
| 74 | + service.purgeUsersActivations(); |
| 75 | + |
| 76 | + verify(usersActivationDao).findByCreatedAtLessThan(any(Date.class)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void purgeUsersActivationsShouldPassExpiredDateToDao() { |
| 81 | + when(usersActivationDao.findByCreatedAtLessThan(any(Date.class))) |
| 82 | + .thenReturn(Collections.<UsersActivation>emptyList()); |
| 83 | + |
| 84 | + service.purgeUsersActivations(); |
| 85 | + |
| 86 | + verify(usersActivationDao).findByCreatedAtLessThan(dateCaptor.capture()); |
| 87 | + |
| 88 | + final Calendar calendar = new GregorianCalendar().getInstance(); |
| 89 | + calendar.setTime(new Date()); |
| 90 | + calendar.add( |
| 91 | + Calendar.DAY_OF_MONTH, |
| 92 | + -(CronService.PURGE_UNACTIVATED_REQUEST_AFTER_DAYS) |
| 93 | + ); |
| 94 | + |
| 95 | + // Truncate seconds in dates to prevent test fail due to different milliseconds in dates |
| 96 | + final Date expectedDate = DateUtils.truncate(calendar.getTime(), Calendar.SECOND); |
| 97 | + final Date passedDate = DateUtils.truncate(dateCaptor.getValue(), Calendar.SECOND); |
| 98 | + |
| 99 | + assertThat(passedDate.equals(expectedDate)).isTrue(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test(expectedExceptions = IllegalStateException.class) |
| 103 | + public void purgeUsersActivationsShouldThrowExceptionWhenNullActivationsWasReturned() { |
| 104 | + when(usersActivationDao.findByCreatedAtLessThan(any(Date.class))).thenReturn(null); |
| 105 | + |
| 106 | + service.purgeUsersActivations(); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void purgeUsersActivationsShouldDeleteExpiredActivations() { |
| 111 | + final List<UsersActivation> expectedActivations = |
| 112 | + Collections.singletonList(UserServiceTest.getUsersActivation()); |
| 113 | + when(usersActivationDao.findByCreatedAtLessThan(any(Date.class))) |
| 114 | + .thenReturn(expectedActivations); |
| 115 | + |
| 116 | + service.purgeUsersActivations(); |
| 117 | + |
| 118 | + verify(usersActivationDao).delete(eq(expectedActivations)); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void purgeUsersActivationsShouldDoNothingIfNoActivations() { |
| 123 | + when(usersActivationDao.findByCreatedAtLessThan(any(Date.class))) |
| 124 | + .thenReturn(Collections.<UsersActivation>emptyList()); |
| 125 | + |
| 126 | + service.purgeUsersActivations(); |
| 127 | + |
| 128 | + verify(usersActivationDao, never()).delete(any(Iterable.class)); |
| 129 | + } |
| 130 | + |
| 131 | +} |
| 132 | + |
0 commit comments