Skip to content

Commit

Permalink
Sonar: Replace these 3 tests with a single Parameterized one (#25225)
Browse files Browse the repository at this point in the history
  • Loading branch information
qmonmert authored Feb 16, 2024
1 parent c532c70 commit 01d7f5d
Showing 1 changed file with 20 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
<%_ } _%>
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
<%_ if (reactive) { _%>
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
Expand Down Expand Up @@ -76,6 +78,7 @@ import java.time.Instant;
import java.time.LocalDate;
<%_ } _%>
import java.util.*;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
<%_ if (reactive && searchEngineElasticsearch) { _%>
Expand Down Expand Up @@ -340,61 +343,20 @@ class AccountResourceIT {
assertThat(user).isEmpty();
}

@Test
<%_ if (databaseTypeSql && !reactive) { _%>
@Transactional
<%_ } _%>
void testRegisterInvalidEmail() throws Exception {
ManagedUserVM invalidUser = new ManagedUserVM();
invalidUser.setLogin("bob");
invalidUser.setPassword("password");
invalidUser.setFirstName("Bob");
invalidUser.setLastName("Green");
invalidUser.setEmail("invalid");// <-- invalid
invalidUser.setActivated(true);
<%_ if (user.hasImageField) { _%>
invalidUser.setImageUrl("http://placehold.it/50x50");
<%_ } _%>
invalidUser.setLangKey(Constants.DEFAULT_LANGUAGE);
invalidUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

<%_ if (reactive) { _%>
accountWebTestClient.post().uri("/api/register")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(om.writeValueAsBytes(invalidUser))
.exchange()
.expectStatus().isBadRequest();
<%_ } else { _%>
restAccountMockMvc.perform(
post("/api/register")
.contentType(MediaType.APPLICATION_JSON)
.content(om.writeValueAsBytes(invalidUser))<% if (authenticationUsesCsrf) { %>
.with(csrf())<% } %>)
.andExpect(status().isBadRequest());
<%_ } _%>

Optional<<%= user.persistClass %>> user = userRepository.findOneByLogin("bob")<% if (reactive) { %>.blockOptional()<% } %>;
assertThat(user).isEmpty();
static Stream<ManagedUserVM> invalidUsers() {
return Stream.of(
createInvalidUser("bob", "password", "Bob", "Green", "invalid", true),// <-- invalid
createInvalidUser("bob", "123", "Bob", "Green", "[email protected]", true),// password with only 3 digits
createInvalidUser("bob", null, "Bob", "Green", "[email protected]", true)// invalid null password
);
}

@Test
@ParameterizedTest
@MethodSource("invalidUsers")
<%_ if (databaseTypeSql && !reactive) { _%>
@Transactional
<%_ } _%>
void testRegisterInvalidPassword() throws Exception {
ManagedUserVM invalidUser = new ManagedUserVM();
invalidUser.setLogin("bob");
invalidUser.setPassword("123");// password with only 3 digits
invalidUser.setFirstName("Bob");
invalidUser.setLastName("Green");
invalidUser.setEmail("[email protected]");
invalidUser.setActivated(true);
<%_ if (user.hasImageField) { _%>
invalidUser.setImageUrl("http://placehold.it/50x50");
<%_ } _%>
invalidUser.setLangKey(Constants.DEFAULT_LANGUAGE);
invalidUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

void testRegisterInvalidUsers(ManagedUserVM invalidUser) throws Exception {
<%_ if (reactive) { _%>
accountWebTestClient.post().uri("/api/register")
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -414,41 +376,20 @@ class AccountResourceIT {
assertThat(user).isEmpty();
}

@Test
<%_ if (databaseTypeSql && !reactive) { _%>
@Transactional
<%_ } _%>
void testRegisterNullPassword() throws Exception {
private static ManagedUserVM createInvalidUser(String login, String password, String firstName, String lastName, String email, boolean activated) {
ManagedUserVM invalidUser = new ManagedUserVM();
invalidUser.setLogin("bob");
invalidUser.setPassword(null);// invalid null password
invalidUser.setFirstName("Bob");
invalidUser.setLastName("Green");
invalidUser.setEmail("[email protected]");
invalidUser.setActivated(true);
invalidUser.setLogin(login);
invalidUser.setPassword(password);
invalidUser.setFirstName(firstName);
invalidUser.setLastName(lastName);
invalidUser.setEmail(email);
invalidUser.setActivated(activated);
<%_ if (user.hasImageField) { _%>
invalidUser.setImageUrl("http://placehold.it/50x50");
<%_ } _%>
invalidUser.setLangKey(Constants.DEFAULT_LANGUAGE);
invalidUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

<%_ if (reactive) { _%>
accountWebTestClient.post().uri("/api/register")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(om.writeValueAsBytes(invalidUser))
.exchange()
.expectStatus().isBadRequest();
<%_ } else { _%>
restAccountMockMvc.perform(
post("/api/register")
.contentType(MediaType.APPLICATION_JSON)
.content(om.writeValueAsBytes(invalidUser))<% if (authenticationUsesCsrf) { %>
.with(csrf())<% } %>)
.andExpect(status().isBadRequest());
<%_ } _%>

Optional<<%= user.persistClass %>> user = userRepository.findOneByLogin("bob")<% if (reactive) { %>.blockOptional()<% } %>;
assertThat(user).isEmpty();
return invalidUser;
}

@Test
Expand Down

0 comments on commit 01d7f5d

Please sign in to comment.