Skip to content

Commit

Permalink
Merge pull request #25113 from mshima/jackson
Browse files Browse the repository at this point in the history
use spring boot injected ObjectMapper to serialize tests.
  • Loading branch information
DanielFran authored Feb 6, 2024
2 parents d1b86c2 + c8661f9 commit a21bdb7
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package <%= packageName %>;
import <%= packageName %>.<%= mainClass %>;
import <%= packageName %>.config.AsyncSyncConfiguration;
import <%= packageName %>.config.JacksonConfiguration;
<%_ if (databaseTypeCouchbase) { _%>
import org.springframework.test.context.ActiveProfiles;
import tech.jhipster.config.JHipsterConstants;
Expand Down Expand Up @@ -64,7 +65,7 @@ import java.lang.annotation.Target;
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@SpringBootTest(classes = {<%= mainClass %>.class, AsyncSyncConfiguration.class<% if (authenticationTypeOauth2) { %>, TestSecurityConfiguration.class<% } %>})
@SpringBootTest(classes = {<%= mainClass %>.class, JacksonConfiguration.class, AsyncSyncConfiguration.class<% if (authenticationTypeOauth2) { %>, TestSecurityConfiguration.class<% } %>})
<%_ if (cacheProviderRedis) { _%>
@EmbeddedRedis
<%_ } _%>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.repository.UserRepository;
<%_ } _%>
import <%= packageName %>.web.rest.vm.LoginVM;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
<%_ if (reactive) { _%>
Expand Down Expand Up @@ -73,6 +75,9 @@ import static org.hamcrest.Matchers.not;
@IntegrationTest
class AuthenticateControllerIT {
@Autowired
private ObjectMapper om;
<%_ if (generateUserManagement) { _%>
@Autowired
private UserRepository userRepository;
Expand Down Expand Up @@ -119,7 +124,7 @@ class AuthenticateControllerIT {
<%_ if (reactive) { _%>
webTestClient.post().uri("/api/authenticate")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(login))
.bodyValue(om.writeValueAsBytes(login))
.exchange()
.expectStatus().isOk()
.expectHeader().valueMatches("Authorization", "Bearer .+")
Expand All @@ -128,7 +133,7 @@ class AuthenticateControllerIT {
<%_ } else { _%>
mockMvc.perform(post("/api/authenticate")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(login)))
.content(om.writeValueAsBytes(login)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id_token").isString())
.andExpect(jsonPath("$.id_token").isNotEmpty())
Expand Down Expand Up @@ -169,7 +174,7 @@ class AuthenticateControllerIT {
<%_ if (reactive) { _%>
webTestClient.post().uri("/api/authenticate")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(login))
.bodyValue(om.writeValueAsBytes(login))
.exchange()
.expectStatus().isOk()
.expectHeader().valueMatches("Authorization", "Bearer .+")
Expand All @@ -178,7 +183,7 @@ class AuthenticateControllerIT {
<%_ } else { _%>
mockMvc.perform(post("/api/authenticate")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(login)))
.content(om.writeValueAsBytes(login)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id_token").isString())
.andExpect(jsonPath("$.id_token").isNotEmpty())
Expand All @@ -195,7 +200,7 @@ class AuthenticateControllerIT {
<%_ if (reactive) { _%>
webTestClient.post().uri("/api/authenticate")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(login))
.bodyValue(om.writeValueAsBytes(login))
.exchange()
.expectStatus().isUnauthorized()
.expectHeader().doesNotExist("Authorization")
Expand All @@ -204,7 +209,7 @@ class AuthenticateControllerIT {
<%_ } else { _%>
mockMvc.perform(post("/api/authenticate")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(login)))
.content(om.writeValueAsBytes(login)))
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.id_token").doesNotExist())
.andExpect(header().doesNotExist("Authorization"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
-%>
package <%= packageName %>.web.rest;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.TypeSafeMatcher;
Expand Down Expand Up @@ -64,27 +60,6 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public final class TestUtil {
private static final ObjectMapper mapper = createObjectMapper();
private static ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.registerModule(new JavaTimeModule());
return mapper;
}
/**
* Convert an object to JSON byte array.
*
* @param object the object to convert.
* @return the JSON byte array.
* @throws IOException
*/
public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
return mapper.writeValueAsBytes(object);
}
/**
* Create a byte array with a specific size filled with specified data.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ webTestClient
.patch()
.uri(ENTITY_API_URL_ID, partialUpdated<%= persistClass %>.get<%= primaryKey.nameCapitalized %>())
.contentType(MediaType.valueOf("application/merge-patch+json"))
.bodyValue(TestUtil.convertObjectToJsonBytes(<%= 'partialUpdated' + persistClass %>))
.bodyValue(om.writeValueAsBytes(<%= 'partialUpdated' + persistClass %>))
.exchange()
.expectStatus()
.isOk();
<%_ } else { _%>
rest<%= entityClass %>MockMvc.perform(patch(ENTITY_API_URL_ID, partialUpdated<%= persistClass %>.get<%= primaryKey.nameCapitalized %>())<% if (authenticationUsesCsrf) { %>.with(csrf())<% }%>
.contentType("application/merge-patch+json")
.content(TestUtil.convertObjectToJsonBytes(<%= 'partialUpdated' + persistClass %>)))
.content(om.writeValueAsBytes(<%= 'partialUpdated' + persistClass %>)))
.andExpect(status().isOk());
<%_ } _%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ if (!reactive && databaseTypeSql) {
}
_%>
import <%= packageName %>.IntegrationTest;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.config.Constants;
<%_ } _%>
Expand All @@ -44,6 +45,8 @@ import <%= packageName %>.service.dto.<%= user.adminUserDto %>;
import <%= packageName %>.repository.EntityManager;
<%_ } _%>
import <%= packageName %>.service.mapper.UserMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -160,6 +163,9 @@ class UserResourceIT {
private static final String UPDATED_LANGKEY = "fr";
<%_ } _%>

@Autowired
private ObjectMapper om;

@Autowired
private UserRepository userRepository;
<%_ if (databaseTypeSql && reactive) { _%>
Expand Down Expand Up @@ -299,13 +305,13 @@ class UserResourceIT {
<%_ if (reactive) { _%>
webTestClient.post().uri("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(user))
.bodyValue(om.writeValueAsBytes(user))
.exchange()
.expectStatus().isCreated();
<%_ } else { _%>
restUserMockMvc.perform(post("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
.content(om.writeValueAsBytes(user))<% if (authenticationUsesCsrf) { %>
.with(csrf())<% } %>)
.andExpect(status().isCreated());
<%_ } _%>
Expand Down Expand Up @@ -361,13 +367,13 @@ class UserResourceIT {
<%_ if (reactive) { _%>
webTestClient.post().uri("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(user))
.bodyValue(om.writeValueAsBytes(user))
.exchange()
.expectStatus().isBadRequest();
<%_ } else { _%>
restUserMockMvc.perform(post("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
.content(om.writeValueAsBytes(user))<% if (authenticationUsesCsrf) { %>
.with(csrf())<% } %>)
.andExpect(status().isBadRequest());
<%_ } _%>
Expand Down Expand Up @@ -410,13 +416,13 @@ class UserResourceIT {
<%_ if (reactive) { _%>
webTestClient.post().uri("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(user))
.bodyValue(om.writeValueAsBytes(user))
.exchange()
.expectStatus().isBadRequest();
<%_ } else { _%>
restUserMockMvc.perform(post("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
.content(om.writeValueAsBytes(user))<% if (authenticationUsesCsrf) { %>
.with(csrf())<% } %>)
.andExpect(status().isBadRequest());
<%_ } _%>
Expand Down Expand Up @@ -459,13 +465,13 @@ class UserResourceIT {
<%_ if (reactive) { _%>
webTestClient.post().uri("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(user))
.bodyValue(om.writeValueAsBytes(user))
.exchange()
.expectStatus().isBadRequest();
<%_ } else { _%>
restUserMockMvc.perform(post("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
.content(om.writeValueAsBytes(user))<% if (authenticationUsesCsrf) { %>
.with(csrf())<% } %>)
.andExpect(status().isBadRequest());
<%_ } _%>
Expand Down Expand Up @@ -654,13 +660,13 @@ class UserResourceIT {
<%_ if (reactive) { _%>
webTestClient.put().uri("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(user))
.bodyValue(om.writeValueAsBytes(user))
.exchange()
.expectStatus().isOk();
<%_ } else { _%>
restUserMockMvc.perform(put("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
.content(om.writeValueAsBytes(user))<% if (authenticationUsesCsrf) { %>
.with(csrf())<% } %>)
.andExpect(status().isOk());
<%_ } _%>
Expand Down Expand Up @@ -719,13 +725,13 @@ class UserResourceIT {
<%_ if (reactive) { _%>
webTestClient.put().uri("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(user))
.bodyValue(om.writeValueAsBytes(user))
.exchange()
.expectStatus().isOk();
<%_ } else { _%>
restUserMockMvc.perform(put("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
.content(om.writeValueAsBytes(user))<% if (authenticationUsesCsrf) { %>
.with(csrf())<% } %>)
.andExpect(status().isOk());
<%_ } _%>
Expand Down Expand Up @@ -808,13 +814,13 @@ class UserResourceIT {
<%_ if (reactive) { _%>
webTestClient.put().uri("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(user))
.bodyValue(om.writeValueAsBytes(user))
.exchange()
.expectStatus().isBadRequest();
<%_ } else { _%>
restUserMockMvc.perform(put("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
.content(om.writeValueAsBytes(user))<% if (authenticationUsesCsrf) { %>
.with(csrf())<% } %>)
.andExpect(status().isBadRequest());
<%_ } _%>
Expand Down Expand Up @@ -883,13 +889,13 @@ class UserResourceIT {
<%_ if (reactive) { _%>
webTestClient.put().uri("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(TestUtil.convertObjectToJsonBytes(user))
.bodyValue(om.writeValueAsBytes(user))
.exchange()
.expectStatus().isBadRequest();
<%_ } else { _%>
restUserMockMvc.perform(put("/api/admin/users")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
.content(om.writeValueAsBytes(user))<% if (authenticationUsesCsrf) { %>
.with(csrf())<% } %>)
.andExpect(status().isBadRequest());
<%_ } _%>
Expand Down
Loading

0 comments on commit a21bdb7

Please sign in to comment.