|
| 1 | +/* |
| 2 | + * Copyright 2023 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.linecorp.bot.messaging.client; |
| 18 | + |
| 19 | +import okhttp3.Headers; |
| 20 | +import okhttp3.Request; |
| 21 | +import okhttp3.Response; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | +import static org.mockito.Mockito.when; |
| 31 | + |
| 32 | +import com.linecorp.bot.messaging.model.ErrorDetail; |
| 33 | +import com.linecorp.bot.messaging.model.SentMessage; |
| 34 | + |
| 35 | +public class MessagingApiClientExceptionTest { |
| 36 | + |
| 37 | + private Response response; |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void setUp() { |
| 41 | + response = mock(Response.class); |
| 42 | + Request request = new Request.Builder().url("https://example.com/").build(); |
| 43 | + |
| 44 | + when(response.request()).thenReturn(request); |
| 45 | + when(response.code()).thenReturn(200); |
| 46 | + |
| 47 | + Headers headers = Headers.of( |
| 48 | + "x-line-example", "headerValue", |
| 49 | + "x-line-request-id", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 50 | + ); |
| 51 | + when(response.headers()).thenReturn(headers); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void constructAndGetTest() { |
| 56 | + List<ErrorDetail> errorDetails = Collections.singletonList(new ErrorDetail("message", "property")); |
| 57 | + List<SentMessage> sentMessages = Collections.singletonList(new SentMessage("IDIDID", "QUOQUOQUO")); |
| 58 | + String error = "testError"; |
| 59 | + |
| 60 | + MessagingApiClientException exception = |
| 61 | + new MessagingApiClientException(response, error, errorDetails, sentMessages); |
| 62 | + |
| 63 | + assertThat(exception).isNotNull(); |
| 64 | + assertThat(exception.getMessage()).isEqualTo( |
| 65 | + "API returns error: code=" + response.code() + |
| 66 | + " requestUrl=" + response.request().url() + |
| 67 | + " x-line-example=headerValue x-line-request-id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx " + |
| 68 | + "error='testError' " + |
| 69 | + "details='[ErrorDetail[message=message, property=property]]'" + |
| 70 | + " sentMessages='[SentMessage[id=IDIDID, quoteToken=QUOQUOQUO]]'"); |
| 71 | + assertThat(exception.getError()).isEqualTo(error); |
| 72 | + assertThat(exception.getDetails()).isEqualTo(errorDetails); |
| 73 | + assertThat(exception.getSentMessages()).isEqualTo(sentMessages); |
| 74 | + } |
| 75 | +} |
0 commit comments