BAEL-4687: added test for each configuration

This commit is contained in:
Adina Rolea 2020-11-16 14:27:29 +02:00
parent 6e8b8c086d
commit 644939da07
16 changed files with 166 additions and 70 deletions

View File

@ -1,11 +1,12 @@
package com.baeldung.boot.jackson;
package com.baeldung.boot.jackson.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.baeldung.boot.jackson.controller")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

View File

@ -1,60 +0,0 @@
package com.baeldung.boot.jackson.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.time.format.DateTimeFormatter;
@Configuration
public class CoffeeConfiguration {
public static final String dateTimeFormat = "dd-MM-yyyy HH:mm";
private LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat));
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.serializers(localDateTimeSerializer)
.serializationInclusion(JsonInclude.Include.NON_NULL);
return new MappingJackson2HttpMessageConverter(builder.build());
}
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL)
.serializers(localDateTimeSerializer);
}
@Bean
@Primary
public ObjectMapper objectMapper() {
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(localDateTimeSerializer);
return new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.registerModule(module);
}
@Bean
@Primary
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
return new Jackson2ObjectMapperBuilder()
.serializers(localDateTimeSerializer)
.serializationInclusion(JsonInclude.Include.NON_NULL);
}
@Bean
public Module javaTimeModule() {
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(localDateTimeSerializer);
return module;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.boot.jackson.config;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.time.format.DateTimeFormatter;
public class CoffeeConstants {
public static final String dateTimeFormat = "dd-MM-yyyy HH:mm";
public static LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat));
}

View File

@ -0,0 +1,18 @@
package com.baeldung.boot.jackson.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer;
@Configuration
public class CoffeeCustomizerConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL)
.serializers(localDateTimeSerializer);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.boot.jackson.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer;
@Configuration
public class CoffeeHttpConverterConfiguration {
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.serializers(localDateTimeSerializer)
.serializationInclusion(JsonInclude.Include.NON_NULL);
return new MappingJackson2HttpMessageConverter(builder.build());
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.boot.jackson.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer;
@Configuration
public class CoffeeJacksonBuilderConfig {
@Bean
@Primary
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
return new Jackson2ObjectMapperBuilder()
.serializers(localDateTimeSerializer)
.serializationInclusion(JsonInclude.Include.NON_NULL);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.boot.jackson.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer;
@Configuration
public class CoffeeObjectMapperConfig {
@Bean
@Primary
public ObjectMapper objectMapper() {
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(localDateTimeSerializer);
return new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.registerModule(module);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.boot.jackson.config;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer;
@Configuration
@PropertySource("classpath:coffee.properties")
public class CoffeeRegisterModuleConfig {
@Bean
public Module javaTimeModule() {
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(localDateTimeSerializer);
return module;
}
}

View File

@ -1,2 +0,0 @@
spring.jackson.default-property-inclusion=non_null
spring.jackson.serialization.write-dates-as-timestamps=false

View File

@ -0,0 +1 @@
spring.jackson.default-property-inclusion=non_null

View File

@ -1,6 +1,6 @@
package com.baeldung.boot.jackson;
package com.baeldung.boot.jackson.app;
import com.baeldung.boot.jackson.config.CoffeeConfiguration;
import com.baeldung.boot.jackson.config.CoffeeConstants;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ -12,20 +12,20 @@ import java.time.format.DateTimeFormatter;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CoffeeIntegrationTest {
public abstract class AbstractCoffeeIntegrationTest {
@Autowired
TestRestTemplate restTemplate;
protected TestRestTemplate restTemplate;
@Test
public void whenGetCoffee_thenSerializedWithDateAndNonNull() {
String formattedDate = DateTimeFormatter.ofPattern(CoffeeConfiguration.dateTimeFormat)
String formattedDate = DateTimeFormatter.ofPattern(CoffeeConstants.dateTimeFormat)
.format(LocalDateTime.now());
String brand = "Lavazza";
String url = "/coffee?brand=" + brand;
String response = restTemplate.getForObject(url, String.class);
assertThat(response).isEqualTo(
"{\"brand\":\"" + brand + "\",\"date\":\"" + formattedDate + "\"}");
}

View File

@ -0,0 +1,8 @@
package com.baeldung.boot.jackson.app;
import com.baeldung.boot.jackson.config.CoffeeCustomizerConfig;
import org.springframework.context.annotation.Import;
@Import(CoffeeCustomizerConfig.class)
public class CoffeeCustomizerIntegrationTest extends AbstractCoffeeIntegrationTest {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.boot.jackson.app;
import com.baeldung.boot.jackson.config.CoffeeHttpConverterConfiguration;
import org.springframework.context.annotation.Import;
@Import(CoffeeHttpConverterConfiguration.class)
public class CoffeeHttpConverterIntegrationTest extends AbstractCoffeeIntegrationTest {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.boot.jackson.app;
import com.baeldung.boot.jackson.config.CoffeeJacksonBuilderConfig;
import org.springframework.context.annotation.Import;
@Import(CoffeeJacksonBuilderConfig.class)
public class CoffeeJacksonBuilderIntegrationTest extends AbstractCoffeeIntegrationTest {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.boot.jackson.app;
import com.baeldung.boot.jackson.config.CoffeeObjectMapperConfig;
import org.springframework.context.annotation.Import;
@Import(CoffeeObjectMapperConfig.class)
public class CoffeeObjectMapperIntegrationTest extends AbstractCoffeeIntegrationTest {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.boot.jackson.app;
import com.baeldung.boot.jackson.config.CoffeeRegisterModuleConfig;
import org.springframework.context.annotation.Import;
@Import(CoffeeRegisterModuleConfig.class)
public class CoffeeRegisterModuleIntegrationTest extends AbstractCoffeeIntegrationTest {
}