2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.salus.internal.rest;
15 import static java.util.Objects.requireNonNull;
16 import static org.assertj.core.api.Assertions.assertThat;
17 import static org.assertj.core.api.Assertions.assertThatThrownBy;
18 import static org.mockito.BDDMockito.given;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.jupiter.api.DisplayName;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.openhab.binding.salus.internal.rest.RestClient.Content;
31 import org.openhab.binding.salus.internal.rest.RestClient.Header;
34 * @author Martin GrzeĊlowski - Initial contribution
36 @SuppressWarnings("DataFlowIssue")
38 @ExtendWith(MockitoExtension.class)
39 class RetryHttpClientTest {
42 RestClient restClient;
43 String url = "https://example.com";
44 Header header = new Header("Authorization", "Bearer token");
45 Header[] headers = new Header[] { header };
46 String response = "Success";
47 Content content = new Content("Request body");
50 @DisplayName("get method retries calling restClient.get up to maxRetries times until it succeeds")
51 void testGetMethodRetriesUntilSucceeds() throws Exception {
54 var retryHttpClient = new RetryHttpClient(requireNonNull(restClient), maxRetries);
56 given(restClient.get(url, headers))//
57 .willThrow(new SalusApiException("1")) //
58 .willThrow(new HttpSalusApiException(404, "2")) //
59 .willThrow(new SalusApiException("3")) //
60 .willReturn(response);
63 var result = retryHttpClient.get(url, headers);
66 assertThat(result).isEqualTo(response);
67 verify(restClient, times(4)).get(url, headers);
71 @DisplayName("post method retries calling restClient.post up to maxRetries times until it succeeds")
72 void testPostMethodRetriesUntilSucceeds() throws SalusApiException {
75 var retryHttpClient = new RetryHttpClient(requireNonNull(restClient), maxRetries);
77 given(restClient.post(url, content, headers))//
78 .willThrow(new SalusApiException("1")) //
79 .willThrow(new HttpSalusApiException(404, "2")) //
80 .willThrow(new SalusApiException("3")) //
81 .willReturn(response);
84 var result = retryHttpClient.post(url, content, headers);
87 assertThat(result).isEqualTo(response);
88 verify(restClient, times(4)).post(url, content, headers);
92 @DisplayName("get method logs debug messages when it fails and retries")
93 public void testGetMethodLogsDebugMessagesWhenFailsAndRetries() throws SalusApiException {
96 var retryHttpClient = new RetryHttpClient(requireNonNull(restClient), maxRetries);
98 given(restClient.get(url, headers)).willThrow(new RuntimeException("Error"));
101 assertThatThrownBy(() -> retryHttpClient.get(url, headers))//
102 .isInstanceOf(RuntimeException.class)//
103 .hasMessage("Error");
107 @DisplayName("post method logs debug messages when it fails and retries")
108 public void testPostMethodLogsDebugMessagesWhenFailsAndRetries() throws SalusApiException {
111 var retryHttpClient = new RetryHttpClient(requireNonNull(restClient), maxRetries);
113 given(restClient.post(url, content, headers)).willThrow(new RuntimeException("Error"));
116 assertThatThrownBy(() -> retryHttpClient.post(url, content, headers))//
117 .isInstanceOf(RuntimeException.class)//
118 .hasMessage("Error");
122 @DisplayName("RetryHttpClient throws an IllegalArgumentException if maxRetries is less than or equal to 0")
123 public void testThrowsIllegalArgumentExceptionIfMaxRetriesLessThanOrEqualTo0() {
125 RestClient restClient = mock(RestClient.class);
129 assertThatThrownBy(() -> new RetryHttpClient(restClient, maxRetries))//
130 .isInstanceOf(IllegalArgumentException.class)//
131 .hasMessage("maxRetries cannot be lower or equal to 0, but was " + maxRetries);