Skip to content

Commit

Permalink
Merge pull request #26271 from jhipster/cleanup-tests
Browse files Browse the repository at this point in the history
cleanup cache in tests
  • Loading branch information
DanielFran authored May 26, 2024
2 parents 949ded3 + 273a59d commit 188b81d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
20 changes: 12 additions & 8 deletions generators/spring-boot/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,18 @@ export default class SpringBootGenerator extends BaseApplicationGenerator {
(application.backendType ?? 'Java') === 'Java' &&
(ADD_SPRING_MILESTONE_REPOSITORY || SPRING_BOOT_VERSION.includes('M') || SPRING_BOOT_VERSION.includes('RC'));
},
prepare({ application }) {
application.requiresDeleteAllUsers =
application.authenticationTypeOauth2 ||
(application.reactive && application.databaseTypeSql) ||
(!application.reactive && application.databaseTypeMongodb);

application.reactorBlock = application.reactive ? '.block()' : '';
application.reactorBlockOptional = application.reactive ? '.blockOptional()' : '';
prepare({ application, applicationDefaults }) {
const { reactive } = application;
applicationDefaults({
__override__: false,
requiresDeleteAllUsers: data =>
data.authenticationTypeOauth2 ||
(reactive && data.databaseTypeSql) ||
(!reactive && data.databaseTypeMongodb) ||
(!reactive && data.databaseTypeCassandra),
reactorBlock: reactive ? '.block()' : '',
reactorBlockOptional: reactive ? '.blockOptional()' : '',
});
},
registerSpringFactory({ source, application }) {
source.addTestSpringFactory = ({ key, value }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ import static org.mockito.Mockito.any;
<%_ if (databaseTypeSql && !reactive && !authenticationTypeOauth2) { _%>
import static org.mockito.Mockito.when;
<%_ } _%>
<%_ if (cacheProviderAny) { _%>
import org.springframework.cache.CacheManager;
import org.springframework.cache.Cache;
import java.util.Objects;
<%_ } _%>
/**
* Integration tests for {@link UserService}.
Expand All @@ -146,6 +151,11 @@ class UserServiceIT {

private static final String DEFAULT_LASTNAME = "doe";

<%_ if (cacheProviderAny) { _%>
@Autowired
private CacheManager cacheManager;

<%_ } _%>
<%_ if (!databaseTypeCassandra) { _%>
private static final String DEFAULT_IMAGEURL = "http://placehold.it/50x50";

Expand Down Expand Up @@ -246,6 +256,12 @@ class UserServiceIT {

@AfterEach
public void cleanupAndCheck() {
<%_ if (cacheProviderAny) { _%>
cacheManager.getCacheNames().stream()
.map(cacheName -> this.cacheManager.getCache(cacheName))
.filter(Objects::nonNull)
.forEach(Cache::clear);
<%_ } _%>
<%_ if ((databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) && authenticationTypeSession && !reactive) { _%>
persistentTokenRepository.deleteAll();
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
<%_ } _%>
<%_ if (cacheProviderAny) { _%>
import org.springframework.cache.CacheManager;
import org.springframework.cache.Cache;
import java.util.Objects;
<%_ } _%>
<%_ if (cacheProviderMemcached) { _%>
import org.springframework.cache.support.NoOpCacheManager;
Expand Down Expand Up @@ -161,14 +163,6 @@ class PublicUserResourceIT {
numberOfUsers = userRepository.count()<%- reactorBlock %>;
}
<%_ } _%>
<%_ if (cacheProviderAny) { _%>
@BeforeEach
public void setup() {
cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).clear();
cacheManager.getCache(UserRepository.USERS_BY_EMAIL_CACHE).clear();
}
<%_ } _%>
<%_ if (reactive && authenticationUsesCsrf) { _%>
@BeforeEach
Expand All @@ -184,6 +178,12 @@ class PublicUserResourceIT {
@AfterEach
public void cleanupAndCheck() {
<%_ if (cacheProviderAny) { _%>
cacheManager.getCacheNames().stream()
.map(cacheName -> this.cacheManager.getCache(cacheName))
.filter(Objects::nonNull)
.forEach(Cache::clear);
<%_ } _%>
<%_ if (requiresDeleteAllUsers) { _%>
<%_ if (databaseTypeSql && reactive) { _%>
userRepository.deleteAllUserAuthorities()<%- reactorBlock %>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
<%_ } _%>
<%_ if (cacheProviderAny) { _%>
import org.springframework.cache.CacheManager;
import org.springframework.cache.Cache;
import java.util.Objects;
<%_ } _%>
<%_ if (cacheProviderMemcached) { _%>
import org.springframework.cache.support.NoOpCacheManager;
Expand Down Expand Up @@ -215,14 +217,6 @@ class UserResourceIT {
numberOfUsers = userRepository.count()<%- reactorBlock %>;
}

<%_ } _%>
<%_ if (cacheProviderAny) { _%>
@BeforeEach
public void setup() {
cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).clear();
cacheManager.getCache(UserRepository.USERS_BY_EMAIL_CACHE).clear();
}

<%_ } _%>
<%_ if (reactive && authenticationUsesCsrf) { _%>
@BeforeEach
Expand Down Expand Up @@ -287,6 +281,12 @@ class UserResourceIT {

@AfterEach
public void cleanupAndCheck() {
<%_ if (cacheProviderAny) { _%>
cacheManager.getCacheNames().stream()
.map(cacheName -> this.cacheManager.getCache(cacheName))
.filter(Objects::nonNull)
.forEach(Cache::clear);
<%_ } _%>
<%_ if (requiresDeleteAllUsers) { _%>
<%_ if (databaseTypeSql && reactive) { _%>
userRepository.deleteAllUserAuthorities()<%- reactorBlock %>;
Expand Down

0 comments on commit 188b81d

Please sign in to comment.