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.reactive;
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 org.bremersee.exception.RestApiExceptionMapper;
26  import org.bremersee.exception.RestApiExceptionMapperForWebFlux;
27  import org.bremersee.exception.spring.boot.autoconfigure.RestApiExceptionMapperBootProperties;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  import org.springframework.beans.factory.ObjectProvider;
31  import org.springframework.boot.autoconfigure.web.WebProperties;
32  import org.springframework.boot.web.reactive.error.ErrorAttributes;
33  import org.springframework.context.ApplicationContext;
34  import org.springframework.http.codec.ServerCodecConfigurer;
35  
36  /**
37   * The api exception handler autoconfiguration test.
38   *
39   * @author Christian Bremer
40   */
41  class ApiExceptionHandlerAutoConfigurationTest {
42  
43    private ApiExceptionHandlerAutoConfiguration target;
44  
45    /**
46     * Setup.
47     */
48    @BeforeEach
49    void setup() {
50      RestApiExceptionMapperBootProperties properties = new RestApiExceptionMapperBootProperties();
51      target = new ApiExceptionHandlerAutoConfiguration(properties);
52    }
53  
54    /**
55     * Init.
56     */
57    @Test
58    void init() {
59      assertThatNoException().isThrownBy(() -> target.init());
60    }
61  
62    /**
63     * Api exception handler.
64     */
65    @Test
66    void apiExceptionHandler() {
67      //noinspection unchecked
68      ObjectProvider<ErrorAttributes> errorAttributes = mock(ObjectProvider.class);
69      when(errorAttributes.getIfAvailable()).thenReturn(mock(ErrorAttributes.class));
70  
71      //noinspection unchecked
72      ObjectProvider<WebProperties> webProperties = mock(ObjectProvider.class);
73      when(webProperties.getIfAvailable(any())).thenReturn(new WebProperties());
74  
75      ApplicationContext applicationContext = mock(ApplicationContext.class);
76      when(applicationContext.getClassLoader())
77          .thenReturn(ApplicationContext.class.getClassLoader());
78  
79      //noinspection unchecked
80      ObjectProvider<ServerCodecConfigurer> serverCodecConfigurer = mock(ObjectProvider.class);
81      when(serverCodecConfigurer.getIfAvailable()).thenReturn(mock(ServerCodecConfigurer.class));
82  
83      //noinspection unchecked
84      ObjectProvider<RestApiExceptionMapper> restApiExceptionMapper = mock(ObjectProvider.class);
85      when(restApiExceptionMapper.getIfAvailable()).thenReturn(new RestApiExceptionMapperForWebFlux(
86          new RestApiExceptionMapperBootProperties().toRestApiExceptionMapperProperties(),
87          "test"
88      ));
89  
90      ApiExceptionHandler actual = target.apiExceptionHandler(
91          errorAttributes,
92          webProperties,
93          applicationContext,
94          serverCodecConfigurer,
95          restApiExceptionMapper);
96  
97      assertThat(actual)
98          .isNotNull();
99    }
100 }