View Javadoc
1   /*
2    * Copyright 2022 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    *      http://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.bremersee.geojson.spring.boot.autoconfigure.jackson;
18  
19  import static org.mockito.ArgumentMatchers.any;
20  import static org.mockito.Mockito.atLeast;
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.spy;
23  import static org.mockito.Mockito.verify;
24  import static org.mockito.Mockito.when;
25  
26  import java.util.function.Consumer;
27  import org.bremersee.geojson.GeoJsonGeometryFactory;
28  import org.bremersee.geojson.spring.boot.autoconfigure.GeoJsonProperties;
29  import org.junit.jupiter.api.Test;
30  import org.springframework.beans.factory.ObjectProvider;
31  import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
32  
33  /**
34   * The geo json jackson 2 object mapper builder customizer test.
35   *
36   * @author Christian Bremer
37   */
38  class GeoJsonJackson2ObjectMapperBuilderCustomizerTest {
39  
40    private GeoJsonJackson2ObjectMapperBuilderCustomizer newInstance(GeoJsonProperties properties) {
41      //noinspection unchecked
42      ObjectProvider<GeoJsonGeometryFactory> objectProvider = mock(ObjectProvider.class);
43      when(objectProvider.getIfAvailable(any())).thenReturn(new GeoJsonGeometryFactory());
44      return new GeoJsonJackson2ObjectMapperBuilderCustomizer(properties, objectProvider);
45    }
46  
47    /**
48     * Init.
49     */
50    @Test
51    void init() {
52      GeoJsonProperties properties = new GeoJsonProperties();
53      properties.setUseBigDecimal(true);
54      properties.setWithBoundingBox(true);
55      properties = spy(properties);
56      GeoJsonJackson2ObjectMapperBuilderCustomizer target = newInstance(properties);
57      target.init();
58      verify(properties, atLeast(1)).isUseBigDecimal();
59      verify(properties, atLeast(1)).isWithBoundingBox();
60    }
61  
62    /**
63     * Customize.
64     */
65    @Test
66    void customize() {
67      GeoJsonProperties properties = new GeoJsonProperties();
68      properties.setUseBigDecimal(false);
69      properties.setWithBoundingBox(false);
70      properties = spy(properties);
71      GeoJsonJackson2ObjectMapperBuilderCustomizer target = newInstance(properties);
72      Jackson2ObjectMapperBuilder builder = mock(Jackson2ObjectMapperBuilder.class);
73      target.customize(builder);
74      //noinspection unchecked
75      verify(builder).postConfigurer(any(Consumer.class));
76      verify(properties, atLeast(1)).isUseBigDecimal();
77      verify(properties, atLeast(1)).isWithBoundingBox();
78    }
79  }