View Javadoc
1   /*
2   * Copyright 2019-2026 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.exception.spring.boot.autoconfigure.servlet;
18  
19  import static org.assertj.core.api.Assertions.assertThat;
20  import static org.assertj.core.api.Assertions.assertThatNoException;
21  import static org.mockito.ArgumentMatchers.any;
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.when;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  import org.bremersee.exception.RestApiExceptionMapper;
28  import org.bremersee.exception.RestApiExceptionMapperForWeb;
29  import org.bremersee.exception.servlet.HttpServletRequestIdProvider;
30  import org.bremersee.exception.spring.boot.autoconfigure.RestApiExceptionMapperBootProperties;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  import org.springframework.beans.factory.ObjectProvider;
34  import org.springframework.web.servlet.HandlerExceptionResolver;
35  import tools.jackson.databind.json.JsonMapper;
36  import tools.jackson.dataformat.xml.XmlMapper;
37  
38  /**
39   * The api exception resolver autoconfiguration test.
40   *
41   * @author Christian Bremer
42   */
43  class ApiExceptionResolverAutoConfigurationTest {
44  
45    private ApiExceptionResolverAutoConfiguration target;
46  
47    /**
48     * Sets .
49     */
50    @BeforeEach
51    void setup() {
52      RestApiExceptionMapperBootProperties properties = new RestApiExceptionMapperBootProperties();
53  
54      //noinspection unchecked
55      ObjectProvider<RestApiExceptionMapper> apiExceptionMapper = mock(ObjectProvider.class);
56      when(apiExceptionMapper.getIfAvailable()).thenReturn(new RestApiExceptionMapperForWeb(
57          properties.toRestApiExceptionMapperProperties(),
58          "test"
59      ));
60  
61      //noinspection unchecked
62      ObjectProvider<JsonMapper.Builder> jsonMapperBuilder = mock(ObjectProvider.class);
63      when(jsonMapperBuilder.getIfAvailable(any()))
64          .thenReturn(JsonMapper.builder());
65  
66      //noinspection unchecked
67      ObjectProvider<XmlMapper.Builder> xmlMapperBuilder = mock(ObjectProvider.class);
68      when(xmlMapperBuilder.getIfAvailable(any()))
69          .thenReturn(XmlMapper.builder());
70  
71      //noinspection unchecked
72      ObjectProvider<HttpServletRequestIdProvider> restApiIdProvider = mock(ObjectProvider.class);
73      when(restApiIdProvider.getIfAvailable()).thenReturn(null);
74  
75      this.target = new ApiExceptionResolverAutoConfiguration(
76          properties, apiExceptionMapper, jsonMapperBuilder, xmlMapperBuilder, restApiIdProvider);
77    }
78  
79    /**
80     * Init.
81     */
82    @Test
83    void init() {
84      assertThatNoException().isThrownBy(() -> target.init());
85    }
86  
87    /**
88     * Extend handler exception resolvers.
89     */
90    @Test
91    void extendHandlerExceptionResolvers() {
92      List<HandlerExceptionResolver> exceptionResolvers = new ArrayList<>();
93      target.extendHandlerExceptionResolvers(exceptionResolvers);
94      assertThat(exceptionResolvers)
95          .isNotEmpty();
96    }
97  }