View Javadoc
1   /*
2    * Copyright 2019-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.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.http.converter.json.Jackson2ObjectMapperBuilder;
35  import org.springframework.web.servlet.HandlerExceptionResolver;
36  
37  /**
38   * The api exception resolver autoconfiguration test.
39   *
40   * @author Christian Bremer
41   */
42  class ApiExceptionResolverAutoConfigurationTest {
43  
44    private ApiExceptionResolverAutoConfiguration target;
45  
46    /**
47     * Sets .
48     */
49    @BeforeEach
50    void setup() {
51      RestApiExceptionMapperBootProperties properties = new RestApiExceptionMapperBootProperties();
52  
53      //noinspection unchecked
54      ObjectProvider<RestApiExceptionMapper> apiExceptionMapper = mock(ObjectProvider.class);
55      when(apiExceptionMapper.getIfAvailable()).thenReturn(new RestApiExceptionMapperForWeb(
56          properties.toRestApiExceptionMapperProperties(),
57          "test"
58      ));
59  
60      //noinspection unchecked
61      ObjectProvider<Jackson2ObjectMapperBuilder> objectMapperBuilder = mock(ObjectProvider.class);
62      when(objectMapperBuilder.getIfAvailable(any()))
63          .thenReturn(new Jackson2ObjectMapperBuilder());
64  
65      //noinspection unchecked
66      ObjectProvider<HttpServletRequestIdProvider> restApiIdProvider = mock(ObjectProvider.class);
67      when(restApiIdProvider.getIfAvailable()).thenReturn(null);
68  
69      this.target = new ApiExceptionResolverAutoConfiguration(
70          properties, apiExceptionMapper, objectMapperBuilder, restApiIdProvider);
71    }
72  
73    /**
74     * Init.
75     */
76    @Test
77    void init() {
78      assertThatNoException().isThrownBy(() -> target.init());
79    }
80  
81    /**
82     * Extend handler exception resolvers.
83     */
84    @Test
85    void extendHandlerExceptionResolvers() {
86      List<HandlerExceptionResolver> exceptionResolvers = new ArrayList<>();
87      target.extendHandlerExceptionResolvers(exceptionResolvers);
88      assertThat(exceptionResolvers)
89          .isNotEmpty();
90    }
91  }