package <%=packageName%>.service;

import <%=packageName%>.Application;<% if (databaseType == 'nosql') { %>
import <%=packageName%>.config.MongoConfiguration;<% } %>
import <%=packageName%>.domain.PersistentToken;
import <%=packageName%>.domain.User;
import <%=packageName%>.repository.PersistentTokenRepository;
import <%=packageName%>.repository.UserRepository;
import org.joda.time.LocalDate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;<% if (databaseType == 'nosql') { %>
import org.springframework.context.annotation.Import;<% } %>
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.inject.Inject;

import static org.assertj.core.api.Assertions.*;

/**
 * Test class for the UserResource REST controller.
 *
 * @see UserService
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_CLASS)
@ActiveProfiles("dev")<% if (databaseType == 'nosql') { %>
@Import(MongoConfiguration.class)<% } %>
public class UserServiceTest {

    @Inject
    private PersistentTokenRepository persistentTokenRepository;

    @Inject
    private UserRepository userRepository;

    @Inject
    private UserService userService;

    @Test
    public void testRemoveOldPersistentTokens() {
        assertThat(persistentTokenRepository.findAll()).isEmpty();
        User admin = userRepository.findOne("admin");
        generateUserToken(admin, "1111-1111", new LocalDate());
        LocalDate now = new LocalDate();
        generateUserToken(admin, "2222-2222", now.minusDays(32));
        assertThat(persistentTokenRepository.findAll()).hasSize(2);
        userService.removeOldPersistentTokens();
        assertThat(persistentTokenRepository.findAll()).hasSize(1);
    }

    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");<% if (databaseType == 'sql') { %>
        persistentTokenRepository.saveAndFlush(token);<% } %><% if (databaseType == 'nosql') { %>
        persistentTokenRepository.save(token);<% } %>
    }
}

@@ -875,27 +875,115 @@
 ort 
-javax.inject.Inject
+ca.intelliware.ihtsdo.mlds.domain.User;%0A%0Aimport javax.inject.Inject;%0Aimport javax.transaction.Transactional
 ;%0A%0Ai
@@ -1240,73 +1240,8 @@
 ion%0A
-@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_CLASS)%0A
 @Act
@@ -1620,122 +1620,129 @@
     
-    assertThat(persistentTokenRepository.findAll()).isEmpty();%0A        User admin = userRepository.findOne(%22admin%22
+%09User admin = userRepository.findOne(%22admin%22);%0A    %09int existingCount = persistentTokenRepository.findByUser(admin).size(
 );%0A 
@@ -1960,31 +1960,55 @@
 ory.find
-All()).hasSize(
+ByUser(admin)).hasSize(existingCount + 
 2);%0A    
@@ -2105,23 +2105,47 @@
 find
-All()).hasSize(
+ByUser(admin)).hasSize(existingCount + 
 1);%0A

Change test to succeed in non-empty database