|
| 1 | +/* |
| 2 | + * Copyright 2012-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.endpoint.jackson; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.time.Instant; |
| 21 | +import java.time.format.DateTimeFormatter; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import tools.jackson.databind.json.JsonMapper; |
| 28 | + |
| 29 | +import org.springframework.boot.actuate.endpoint.jackson.EndpointJsonMapper; |
| 30 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 31 | +import org.springframework.boot.test.context.FilteredClassLoader; |
| 32 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests for {@link Jackson2EndpointAutoConfiguration}. |
| 41 | + * |
| 42 | + * @author Phillip Webb |
| 43 | + * @author Andy Wilkinson |
| 44 | + * @deprecated since 4.0.0 for removal in 4.2.0 in favor of Jackson 3 |
| 45 | + */ |
| 46 | +@SuppressWarnings("removal") |
| 47 | +@Deprecated(since = "4.0.0", forRemoval = true) |
| 48 | +class Jackson2EndpointAutoConfigurationTests { |
| 49 | + |
| 50 | + private final ApplicationContextRunner runner = new ApplicationContextRunner() |
| 51 | + .withConfiguration(AutoConfigurations.of(Jackson2EndpointAutoConfiguration.class)); |
| 52 | + |
| 53 | + @Test |
| 54 | + void endpointObjectMapperWhenNoProperty() { |
| 55 | + this.runner.run((context) -> assertThat(context) |
| 56 | + .hasSingleBean(org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper.class)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void endpointObjectMapperWhenPropertyTrue() { |
| 61 | + this.runner.run((context) -> assertThat(context) |
| 62 | + .hasSingleBean(org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper.class)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void endpointObjectMapperWhenPropertyFalse() { |
| 67 | + this.runner.withPropertyValues("management.endpoints.jackson.isolated-object-mapper=false") |
| 68 | + .run((context) -> assertThat(context) |
| 69 | + .doesNotHaveBean(org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper.class)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void endpointObjectMapperWhenSpringWebIsAbsent() { |
| 74 | + this.runner.withClassLoader(new FilteredClassLoader(Jackson2ObjectMapperBuilder.class)) |
| 75 | + .run((context) -> assertThat(context) |
| 76 | + .doesNotHaveBean(org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper.class)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void endpointObjectMapperDoesNotSerializeDatesAsTimestamps() { |
| 81 | + this.runner.run((context) -> { |
| 82 | + ObjectMapper objectMapper = context |
| 83 | + .getBean(org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper.class) |
| 84 | + .get(); |
| 85 | + Instant now = Instant.now(); |
| 86 | + String json = objectMapper.writeValueAsString(Map.of("timestamp", now)); |
| 87 | + assertThat(json).contains(DateTimeFormatter.ISO_INSTANT.format(now)); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void endpointObjectMapperDoesNotSerializeDurationsAsTimestamps() { |
| 93 | + this.runner.run((context) -> { |
| 94 | + ObjectMapper objectMapper = context |
| 95 | + .getBean(org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper.class) |
| 96 | + .get(); |
| 97 | + Duration duration = Duration.ofSeconds(42); |
| 98 | + String json = objectMapper.writeValueAsString(Map.of("duration", duration)); |
| 99 | + assertThat(json).contains(duration.toString()); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void endpointObjectMapperDoesNotSerializeNullValues() { |
| 105 | + this.runner.run((context) -> { |
| 106 | + ObjectMapper objectMapper = context |
| 107 | + .getBean(org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper.class) |
| 108 | + .get(); |
| 109 | + HashMap<String, String> map = new HashMap<>(); |
| 110 | + map.put("key", null); |
| 111 | + String json = objectMapper.writeValueAsString(map); |
| 112 | + assertThat(json).isEqualTo("{}"); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + @Configuration(proxyBeanMethods = false) |
| 117 | + static class TestEndpointMapperConfiguration { |
| 118 | + |
| 119 | + @Bean |
| 120 | + TestEndpointJsonMapper testEndpointJsonMapper() { |
| 121 | + return new TestEndpointJsonMapper(); |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + static class TestEndpointJsonMapper implements EndpointJsonMapper { |
| 127 | + |
| 128 | + @Override |
| 129 | + public JsonMapper get() { |
| 130 | + return new JsonMapper(); |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments