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

don't delete all users in some integration tests #26250

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions generators/app/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,10 @@ exports[`generator - app with default config should match snapshot 1`] = `
"projectDescription": "Description for JHipster",
"projectVersion": "0.0.1-SNAPSHOT",
"reactive": false,
"reactorBlock": "",
"reactorBlockOptional": "",
"rememberMeKey": undefined,
"requiresDeleteAllUsers": false,
"searchEngine": "no",
"searchEngineAny": false,
"searchEngineCouchbase": false,
Expand Down Expand Up @@ -1328,7 +1331,10 @@ exports[`generator - app with gateway should match snapshot 1`] = `
"projectDescription": "Description for JHipster",
"projectVersion": "0.0.1-SNAPSHOT",
"reactive": true,
"reactorBlock": ".block()",
"reactorBlockOptional": ".blockOptional()",
"rememberMeKey": undefined,
"requiresDeleteAllUsers": true,
"routes": undefined,
"searchEngine": "no",
"searchEngineAny": false,
Expand Down Expand Up @@ -1864,7 +1870,10 @@ exports[`generator - app with microservice should match snapshot 1`] = `
"projectDescription": "Description for JHipster",
"projectVersion": "0.0.1-SNAPSHOT",
"reactive": false,
"reactorBlock": "",
"reactorBlockOptional": "",
"rememberMeKey": undefined,
"requiresDeleteAllUsers": false,
"searchEngine": "no",
"searchEngineAny": false,
"searchEngineCouchbase": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ import static org.mockito.Mockito.*;

import <%= packageName %>.IntegrationTest;
import <%= packageName %>.security.AuthoritiesConstants;
<%_ if (syncUserWithIdp) { _%>
import <%= packageName %>.repository.UserRepository;
<%_ } _%>

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
<%_ if (authenticationTypeOauth2) { _%>
import org.junit.jupiter.api.AfterEach;
<%_ } _%>
<%_ if (reactive) { _%>
import org.junit.jupiter.api.BeforeEach;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
Expand Down Expand Up @@ -74,6 +80,11 @@ import org.springframework.transaction.annotation.Transactional;
<%_ } _%>
@IntegrationTest
class AccountResourceIT {
<%_ if (syncUserWithIdp) { _%>

@Autowired
private UserRepository userRepository;
<%_ } _%>

<%_ if (reactive) { _%>
private Map<String, Object> claims;
Expand Down Expand Up @@ -106,6 +117,17 @@ class AccountResourceIT {
claims.put("email", "jane.doe@jhipster.com");
}

<%_ } _%>
<%_ if (syncUserWithIdp) { _%>
@AfterEach
public void cleanup() {
// Remove syncUserWithIdp users
<%_ if (databaseTypeSql && reactive) { _%>
userRepository.deleteAllUserAuthorities()<%- reactorBlock %>;
<%_ } _%>
userRepository.deleteAll()<%- reactorBlock %>;
}

<%_ } _%>
@Test
<%_ if (databaseTypeSql && !reactive) { _%>
Expand Down
3 changes: 3 additions & 0 deletions generators/server/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,7 @@ export type SpringBootApplication = JavaApplication &
databaseMigrationLiquibase: boolean;

communicationSpringWebsocket: boolean;
requiresDeleteAllUsers: boolean;
reactorBlock: string;
reactorBlockOptional: string;
};
9 changes: 9 additions & 0 deletions generators/spring-boot/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ 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()' : '';
},
registerSpringFactory({ source, application }) {
source.addTestSpringFactory = ({ key, value }) => {
const springFactoriesFile = `${application.srcTestResources}META-INF/spring.factories`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import tech.jhipster.security.RandomUtil;
<%_ if (!authenticationTypeOauth2) { _%>
import org.apache.commons.lang3.RandomStringUtils;
<%_ } _%>
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -137,9 +138,9 @@ import static org.mockito.Mockito.when;
<%_ } _%>
class UserServiceIT {

private static final String DEFAULT_LOGIN = "johndoe";
private static final String DEFAULT_LOGIN = "johndoe_service";

private static final String DEFAULT_EMAIL = "johndoe@localhost";
private static final String DEFAULT_EMAIL = "johndoe_service@localhost";

private static final String DEFAULT_FIRSTNAME = "john";

Expand Down Expand Up @@ -189,21 +190,21 @@ class UserServiceIT {
<%_ if (authenticationTypeOauth2) { _%>
private Map<String, Object> userDetails;

<%_ } _%>
<%_ if (!databaseTypeCassandra && !requiresDeleteAllUsers) { _%>
private Long numberOfUsers;

@BeforeEach
public void countUsers() {
numberOfUsers = userRepository.count()<%- reactorBlock %>;
}

<%_ } _%>
@BeforeEach
public void init() {
<%_ if (databaseTypeCouchbase) { _%>
mockAuthentication();
<%_ } _%>
<%_ if ((databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) && authenticationTypeSession && !reactive) { _%>
persistentTokenRepository.deleteAll();
<%_ } _%>
<%_ if (databaseTypeSql && reactive) { _%>
userRepository.deleteAllUserAuthorities().block();
<%_ } _%>
<%_ if ((reactive && !databaseTypeNo) || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCassandra || databaseTypeCouchbase) { _%>
userRepository.deleteAll()<% if (reactive) { %>.block()<% } %>;
<%_ } _%>
<%_ if (!databaseTypeNo) { _%>
user = new <%= user.persistClass %>();
<%_ if (databaseTypeCassandra) { _%>
Expand Down Expand Up @@ -240,6 +241,25 @@ class UserServiceIT {

when(dateTimeProvider.getNow()).thenReturn(Optional.of(LocalDateTime.now()));
auditingHandler.setDateTimeProvider(dateTimeProvider);
<%_ } _%>
}

@AfterEach
public void cleanupAndCheck() {
<%_ if ((databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) && authenticationTypeSession && !reactive) { _%>
persistentTokenRepository.deleteAll();
<%_ } _%>
<%_ if (requiresDeleteAllUsers) { _%>
<%_ if (databaseTypeSql && reactive) { _%>
userRepository.deleteAllUserAuthorities()<%- reactorBlock %>;
<%_ } _%>
userRepository.deleteAll()<%- reactorBlock %>;
<%_ } else { _%>
userService.deleteUser(DEFAULT_LOGIN)<%- reactorBlock %>;
<%_ } _%>
<%_ if (!databaseTypeCassandra && !requiresDeleteAllUsers) { _%>
assertThat(userRepository.count()<%- reactorBlock %>).isEqualTo(numberOfUsers);
numberOfUsers = null;
<%_ } _%>
}
<%_ if ((databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) && authenticationTypeSession && !reactive) { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
-%>
package <%= packageName %>.web.rest;

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

import <%= packageName %>.IntegrationTest;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.config.Constants;
Expand All @@ -27,13 +29,17 @@ import <%= packageName %>.config.TestSecurityConfiguration;
<%_ } _%>
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.repository.UserRepository;
<%_ if (!requiresDeleteAllUsers) { _%>
import <%= packageName %>.service.UserService;
<%_ } _%>
<%_ if (searchEngineElasticsearch) { _%>
import <%= packageName %>.repository.search.UserSearchRepository;
<%_ } _%>
import <%= packageName %>.security.AuthoritiesConstants;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.repository.EntityManager;
<%_ } _%>
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -77,9 +83,6 @@ import java.util.stream.Stream;
import java.util.UUID;
<%_ } _%>

<%_ if (reactive) { _%>
import static org.assertj.core.api.Assertions.assertThat;
<%_ } _%>
<%_ if (reactive && searchEngineElasticsearch) { _%>
import static org.mockito.Mockito.*;
<%_ } _%>
Expand Down Expand Up @@ -115,6 +118,11 @@ class PublicUserResourceIT {

@Autowired
private UserRepository userRepository;
<%_ if (!requiresDeleteAllUsers) { _%>

@Autowired
private UserService userService;
<%_ } _%>
<%_ if (searchEngineElasticsearch) { _%>

/**
Expand Down Expand Up @@ -145,6 +153,15 @@ class PublicUserResourceIT {

private <%= user.persistClass %> user;

<%_ if (!databaseTypeCassandra && !requiresDeleteAllUsers) { _%>
private Long numberOfUsers;

@BeforeEach
public void countUsers() {
numberOfUsers = userRepository.count()<%- reactorBlock %>;
}

<%_ } _%>
<%_ if (cacheProviderAny) { _%>
@BeforeEach
public void setup() {
Expand All @@ -162,7 +179,23 @@ class PublicUserResourceIT {
<%_ } _%>
@BeforeEach
public void initTest() {
user = UserResourceIT.initTestUser(userRepository<% if (databaseTypeSql) { %>, em<% } %>);
user = UserResourceIT.initTestUser(<% if (databaseTypeSql) { %>em<% } %>);
}

@AfterEach
public void cleanupAndCheck() {
<%_ if (requiresDeleteAllUsers) { _%>
<%_ if (databaseTypeSql && reactive) { _%>
userRepository.deleteAllUserAuthorities()<%- reactorBlock %>;
<%_ } _%>
userRepository.deleteAll()<%- reactorBlock %>;
<%_ } else { _%>
userService.deleteUser(DEFAULT_LOGIN)<%- reactorBlock %>;
<%_ } _%>
<%_ if (!databaseTypeCassandra && !requiresDeleteAllUsers) { _%>
assertThat(userRepository.count()<%- reactorBlock %>).isEqualTo(numberOfUsers);
numberOfUsers = null;
<%_ } _%>
}

@Test
Expand Down
Loading
Loading