From f4bcd720b5ca3f99d29beff51ca78ab0668277ef Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Wed, 29 May 2024 13:16:46 -0300 Subject: [PATCH] PublicUserResourceIT adjusts --- .../_package_/service/dto/UserDTO.java.ejs | 23 +++++++++++ .../web/rest/PublicUserResourceIT.java.ejs | 38 ++++++++++--------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/generators/server/templates/src/main/java/_package_/service/dto/UserDTO.java.ejs b/generators/server/templates/src/main/java/_package_/service/dto/UserDTO.java.ejs index ee8d2098cc11..f70aadc6dc9c 100644 --- a/generators/server/templates/src/main/java/_package_/service/dto/UserDTO.java.ejs +++ b/generators/server/templates/src/main/java/_package_/service/dto/UserDTO.java.ejs @@ -23,6 +23,7 @@ import <%= user.entityAbsoluteClass %>; <%_ } _%> import java.io.Serializable; +import java.util.Objects; <%_ if (user.primaryKey.hasUUID) { _%> import java.util.UUID; <%_ } _%> @@ -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 diff --git a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/PublicUserResourceIT.java.ejs b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/PublicUserResourceIT.java.ejs index 618e5135ad51..16d075d43d26 100644 --- a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/PublicUserResourceIT.java.ejs +++ b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/PublicUserResourceIT.java.ejs @@ -84,6 +84,7 @@ import java.util.stream.Stream; <%_ if (databaseTypeCassandra) { _%> import java.util.UUID; <%_ } _%> +import java.util.Set; <%_ if (reactive && searchEngineElasticsearch) { _%> import static org.mockito.Mockito.*; @@ -91,10 +92,6 @@ 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.*; <%_ } _%> @@ -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; @@ -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); @@ -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) { _%>