package com.mycompany.myapp.service;
import com.mycompany.myapp.Application;
import com.mycompany.myapp.domain.PersistentToken;
import com.mycompany.myapp.domain.User;
import com.mycompany.myapp.repository.PersistentTokenRepository;
import com.mycompany.myapp.repository.UserRepository;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.inject.Inject;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
/**
* Test class for the UserResource REST controller.
*
* @see UserService
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@Transactional
public class UserServiceTest {
@Inject
private PersistentTokenRepository persistentTokenRepository;
@Inject
private UserRepository userRepository;
@Inject
private UserService userService;
@Test
public void testRemoveOldPersistentTokens() {
User admin = userRepository.findOne("admin");
int existingCount = persistentTokenRepository.findByUser(admin).size();
generateUserToken(admin, "1111-1111", new LocalDate());
LocalDate now = new LocalDate();
generateUserToken(admin, "2222-2222", now.minusDays(32));
assertThat(persistentTokenRepository.findByUser(admin)).hasSize(existingCount + 2);
userService.removeOldPersistentTokens();
assertThat(persistentTokenRepository.findByUser(admin)).hasSize(existingCount + 1);
}
@Test
public void testFindNotActivatedUsersByCreationDateBefore() {
userService.removeNotActivatedUsers();
DateTime now = new DateTime();
List<User> users = userRepository.findNotActivatedUsersByCreationDateBefore(now.minusDays(3));
assertThat(users).isEmpty();
}
private void generateUserToken(User user, String tokenSeries, LocalDate localDate) {
PersistentToken token = new PersistentToken();
token.setSeries(tokenSeries);
token.setUser(user);
token.setTokenValue(tokenSeries + "-data");
token.setTokenDate(localDate);
token.setIpAddress("127.0.0.1");
token.setUserAgent("Test agent");
persistentTokenRepository.saveAndFlush(token);
}
}