]> git.basschouten.com Git - openhab-addons.git/blob
5506f424b4532ea8c8ee5946afedbf69ff9393fe
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.salus.internal.rest;
14
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;
22
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;
32
33 /**
34  * @author Martin GrzeĊ›lowski - Initial contribution
35  */
36 @SuppressWarnings("DataFlowIssue")
37 @NonNullByDefault
38 @ExtendWith(MockitoExtension.class)
39 class RetryHttpClientTest {
40     @Mock
41     @Nullable
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");
48
49     @Test
50     @DisplayName("get method retries calling restClient.get up to maxRetries times until it succeeds")
51     void testGetMethodRetriesUntilSucceeds() throws Exception {
52         // Given
53         var maxRetries = 4;
54         var retryHttpClient = new RetryHttpClient(requireNonNull(restClient), maxRetries);
55
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);
61
62         // When
63         var result = retryHttpClient.get(url, headers);
64
65         // Then
66         assertThat(result).isEqualTo(response);
67         verify(restClient, times(4)).get(url, headers);
68     }
69
70     @Test
71     @DisplayName("post method retries calling restClient.post up to maxRetries times until it succeeds")
72     void testPostMethodRetriesUntilSucceeds() throws SalusApiException {
73         // Given
74         int maxRetries = 4;
75         var retryHttpClient = new RetryHttpClient(requireNonNull(restClient), maxRetries);
76
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);
82
83         // When
84         var result = retryHttpClient.post(url, content, headers);
85
86         // Then
87         assertThat(result).isEqualTo(response);
88         verify(restClient, times(4)).post(url, content, headers);
89     }
90
91     @Test
92     @DisplayName("get method logs debug messages when it fails and retries")
93     public void testGetMethodLogsDebugMessagesWhenFailsAndRetries() throws SalusApiException {
94         // Given
95         var maxRetries = 3;
96         var retryHttpClient = new RetryHttpClient(requireNonNull(restClient), maxRetries);
97
98         given(restClient.get(url, headers)).willThrow(new RuntimeException("Error"));
99
100         // When
101         assertThatThrownBy(() -> retryHttpClient.get(url, headers))//
102                 .isInstanceOf(RuntimeException.class)//
103                 .hasMessage("Error");
104     }
105
106     @Test
107     @DisplayName("post method logs debug messages when it fails and retries")
108     public void testPostMethodLogsDebugMessagesWhenFailsAndRetries() throws SalusApiException {
109         // Given
110         int maxRetries = 3;
111         var retryHttpClient = new RetryHttpClient(requireNonNull(restClient), maxRetries);
112
113         given(restClient.post(url, content, headers)).willThrow(new RuntimeException("Error"));
114
115         // When
116         assertThatThrownBy(() -> retryHttpClient.post(url, content, headers))//
117                 .isInstanceOf(RuntimeException.class)//
118                 .hasMessage("Error");
119     }
120
121     @Test
122     @DisplayName("RetryHttpClient throws an IllegalArgumentException if maxRetries is less than or equal to 0")
123     public void testThrowsIllegalArgumentExceptionIfMaxRetriesLessThanOrEqualTo0() {
124         // Given
125         RestClient restClient = mock(RestClient.class);
126         int maxRetries = 0;
127
128         // When/Then
129         assertThatThrownBy(() -> new RetryHttpClient(restClient, maxRetries))//
130                 .isInstanceOf(IllegalArgumentException.class)//
131                 .hasMessage("maxRetries cannot be lower or equal to 0, but was " + maxRetries);
132     }
133 }