Skip to content

Commit

Permalink
Merge pull request #26312 from mshima/public-user
Browse files Browse the repository at this point in the history
rework PublicUserResourceIT to don’t depend on User order
  • Loading branch information
DanielFran authored May 30, 2024
2 parents 2d94655 + f4bcd72 commit 00b413b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import <%= user.entityAbsoluteClass %>;
<%_ } _%>
import java.io.Serializable;
import java.util.Objects;
<%_ if (user.primaryKey.hasUUID) { _%>
import java.util.UUID;
<%_ } _%>
Expand Down Expand Up @@ -76,10 +77,32 @@ public class <%= user.dtoClass %> implements Serializable {
public String get<%= field.propertyJavaBeanName %>() {
return <%= field.fieldName %>;
}
public void set<%= field.propertyJavaBeanName %>(String <%= field.fieldName %>) {
this.<%= field.fieldName %> = <%= field.fieldName %>;
}
<%_ } _%>
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
<%= user.dtoClass %> userDTO = (<%= user.dtoClass %>) o;
if (userDTO.getId() == null || getId() == null) {
return false;
}
return Objects.equals(getId(), userDTO.getId()) &&
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
Objects.equals(get<%= field.propertyJavaBeanName %>(), userDTO.get<%= field.propertyJavaBeanName %>()) &&
<%_ } _%>
Objects.equals(getLogin(), userDTO.getLogin());
}
// prettier-ignore
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,14 @@ import java.util.stream.Stream;
<%_ if (databaseTypeCassandra) { _%>
import java.util.UUID;
<%_ } _%>
import java.util.Set;
<%_ if (reactive && searchEngineElasticsearch) { _%>
import static org.mockito.Mockito.*;
<%_ } _%>
<%_ if (reactive && authenticationUsesCsrf) { _%>
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf;
<%_ } else if (!reactive) { _%>
<%_ if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) { _%>
import static org.hamcrest.Matchers.hasItems;
<%_ } _%>
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
<%_ } _%>
Expand Down Expand Up @@ -149,10 +146,11 @@ class PublicUserResourceIT {
@Autowired
<%_ if (reactive) { _%>
private WebTestClient webTestClient;
<%_ } else { _%>
private MockMvc restUserMockMvc;
<%_ } _%>
<%_ } _%>
private <%= user.persistClass %> user;
<%_ if (!databaseTypeCassandra && !requiresDeleteAllUsers) { _%>
private Long numberOfUsers;
Expand Down Expand Up @@ -189,7 +187,7 @@ class PublicUserResourceIT {
<%_ } _%>
userRepository.deleteAll()<%- reactorBlock %>;
<%_ } else { _%>
userService.deleteUser(DEFAULT_LOGIN)<%- reactorBlock %>;
userService.deleteUser(user.getLogin())<%- reactorBlock %>;
<%_ } _%>
<%_ if (!databaseTypeCassandra && !requiresDeleteAllUsers) { _%>
assertThat(userRepository.count()<%- reactorBlock %>).isEqualTo(numberOfUsers);
Expand All @@ -207,32 +205,38 @@ class PublicUserResourceIT {
// Get all the users
<%_ if (reactive) { _%>
<%= user.dtoClass %> foundUser = webTestClient.get().uri("/api/users?sort=id,desc")
webTestClient.get().uri("/api/users?sort=id,desc")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.returnResult(<%= user.dtoClass %>.class).getResponseBody().blockFirst();
assertThat(foundUser.getLogin()).isEqualTo(DEFAULT_LOGIN);
.expectBody()
.jsonPath("$.[?(@.id == <%- primaryKey.typeNumeric ? '%d' : "'%s'" %>)].login", user.getId()).isEqualTo(user.getLogin())
.jsonPath("$.[?(@.id == <%- primaryKey.typeNumeric ? '%d' : "'%s'" %>)].keys()", user.getId()).isEqualTo(Set.of("id", "login"))
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
assertThat(foundUser.get<%= field.propertyJavaBeanName %>()).isEqualTo(DEFAULT_<%= field.fieldName.toUpperCase() %>);
.jsonPath("$.[?(@.id == <%- primaryKey.typeNumeric ? '%d' : "'%s'" %>)].<%= field.fieldName %>", user.getId()).isEqualTo(user.get<%= field.propertyJavaBeanName %>())
<%_ } _%>
.jsonPath("$.[*].email").doesNotHaveJsonPath()
<%_ if (!databaseTypeCassandra) { _%>
.jsonPath("$.[*].imageUrl").doesNotHaveJsonPath()
<%_ } _%>
.jsonPath("$.[*].langKey").doesNotHaveJsonPath();
<%_ } else { _%>
restUserMockMvc.perform(get("/api/users<% if (databaseTypeSql) { %>?sort=id,desc<% } %>")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.[*].login").value(hasItem(DEFAULT_LOGIN)))
.andExpect(jsonPath("$.[?(@.id == <%- primaryKey.typeNumeric ? '%d' : "'%s'" %>)].login", user.getId()).value(user.getLogin()))
.andExpect(jsonPath("$.[?(@.id == <%- primaryKey.typeNumeric ? '%d' : "'%s'" %>)].keys()", user.getId()).value(Set.of("id", "login")))
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
.andExpect(jsonPath("$.[*].<%= field.fieldName %>").value(hasItem(DEFAULT_<%= field.fieldName.toUpperCase() %>)))
.andExpect(jsonPath("$.[?(@.id == <%- primaryKey.typeNumeric ? '%d' : "'%s'" %>)].<%= field.fieldName %>", user.getId()).value(user.get<%= field.propertyJavaBeanName %>()))
<%_ } _%>
.andExpect(jsonPath("$.[*].email").doesNotExist())
.andExpect(jsonPath("$.[*].email").doesNotHaveJsonPath())
<%_ if (!databaseTypeCassandra) { _%>
.andExpect(jsonPath("$.[*].imageUrl").doesNotExist())
.andExpect(jsonPath("$.[*].imageUrl").doesNotHaveJsonPath())
<%_ } _%>
.andExpect(jsonPath("$.[*].langKey").doesNotExist());
<% } _%>
.andExpect(jsonPath("$.[*].langKey").doesNotHaveJsonPath());
<%_ } _%>
}
<%_ if (databaseTypeSql && !authenticationTypeOauth2) { _%>
Expand Down

0 comments on commit 00b413b

Please sign in to comment.