Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup cache in tests #26271

Merged
merged 11 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions generators/spring-boot/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@ 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),
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
Loading