|
1 | 1 | package graphql.kickstart.spring.webclient.boot;
|
2 | 2 |
|
| 3 | +import java.time.Duration; |
| 4 | + |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
3 | 7 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
| 8 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
4 | 10 | import static org.mockito.ArgumentMatchers.isA;
|
5 | 11 | import static org.mockito.Mockito.mock;
|
6 | 12 | import static org.mockito.Mockito.times;
|
|
16 | 22 | import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
17 | 23 | import org.springframework.web.reactive.function.client.WebClient;
|
18 | 24 | import reactor.core.publisher.Mono;
|
| 25 | +import reactor.util.retry.RetryBackoffSpec; |
| 26 | +import reactor.util.retry.RetrySpec; |
19 | 27 |
|
20 | 28 | class GraphQLWebClientAutoConfigurationTest {
|
21 | 29 |
|
22 | 30 | private GraphQLWebClientAutoConfiguration configuration;
|
| 31 | + private GraphQLClientRetryProperties graphQLClientRetryProperties; |
23 | 32 | private WebClient.Builder mockClientBuilder;
|
24 | 33 |
|
25 | 34 | @BeforeEach
|
26 | 35 | void setup() {
|
27 | 36 | GraphQLClientProperties graphQLClientProperties = new GraphQLClientProperties();
|
28 |
| - configuration = new GraphQLWebClientAutoConfiguration(graphQLClientProperties); |
| 37 | + graphQLClientRetryProperties = new GraphQLClientRetryProperties(); |
| 38 | + configuration = new GraphQLWebClientAutoConfiguration(graphQLClientProperties, graphQLClientRetryProperties); |
29 | 39 |
|
30 | 40 | mockClientBuilder = mock(WebClient.Builder.class);
|
31 | 41 | }
|
@@ -68,7 +78,126 @@ void webClient_getObjectMapper_returns() {
|
68 | 78 |
|
69 | 79 | @Test
|
70 | 80 | void webClient_graphQLWebClient_returns() {
|
71 |
| - assertNotNull(configuration.graphQLWebClient(WebClient.builder().build(), new ObjectMapper())); |
| 81 | + assertNotNull(configuration.graphQLWebClient(WebClient.builder().build(), new ObjectMapper(), () -> null)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void webClient_graphQLWebClientRetryErrorFilterPredicate_returns() { |
| 86 | + var predicate = configuration.graphQLWebClientRetryErrorFilterPredicate(); |
| 87 | + assertNotNull(predicate); |
| 88 | + assertTrue(predicate.test(mock())); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void webClient_graphQLWebClientRetryProvider_noneStrategy() { |
| 93 | + graphQLClientRetryProperties.setStrategy(GraphQLClientRetryProperties.RetryStrategy.NONE); |
| 94 | + var provider = configuration.graphQLWebClientRetryProvider(mock()); |
| 95 | + assertNotNull(provider); |
| 96 | + assertNull(provider.get()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void webClient_graphQLWebClientRetryProvider_backoffStrategy() { |
| 101 | + graphQLClientRetryProperties.setStrategy(GraphQLClientRetryProperties.RetryStrategy.BACKOFF); |
| 102 | + graphQLClientRetryProperties.getBackoff().setMaxAttempts(42); |
| 103 | + graphQLClientRetryProperties.getBackoff().setMinBackoff(Duration.ofMillis(50)); |
| 104 | + graphQLClientRetryProperties.getBackoff().setMaxBackoff(Duration.ofMinutes(30)); |
| 105 | + |
| 106 | + var mockedPredicate = mock(GraphQLWebClientRetryErrorFilterPredicate.class); |
| 107 | + when(mockedPredicate.test(isA(Throwable.class))).thenReturn(true); |
| 108 | + |
| 109 | + var provider = configuration.graphQLWebClientRetryProvider(mockedPredicate); |
| 110 | + assertNotNull(provider); |
| 111 | + assertNotNull(provider.get()); |
| 112 | + assertInstanceOf(RetryBackoffSpec.class, provider.get()); |
| 113 | + |
| 114 | + var castedProviderValue = (RetryBackoffSpec) provider.get(); |
| 115 | + assertEquals(42, castedProviderValue.maxAttempts); |
| 116 | + assertEquals(Duration.ofMillis(50), castedProviderValue.minBackoff); |
| 117 | + assertEquals(Duration.ofMinutes(30), castedProviderValue.maxBackoff); |
| 118 | + |
| 119 | + castedProviderValue.errorFilter.test(new Throwable()); |
| 120 | + verify(mockedPredicate, times(1)).test(isA(Throwable.class)); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void webClient_graphQLWebClientRetryProvider_fixedDelayStrategy() { |
| 125 | + graphQLClientRetryProperties.setStrategy(GraphQLClientRetryProperties.RetryStrategy.FIXED_DELAY); |
| 126 | + graphQLClientRetryProperties.getFixedDelay().setMaxAttempts(42); |
| 127 | + graphQLClientRetryProperties.getFixedDelay().setDelay(Duration.ofMillis(50)); |
| 128 | + |
| 129 | + var mockedPredicate = mock(GraphQLWebClientRetryErrorFilterPredicate.class); |
| 130 | + when(mockedPredicate.test(isA(Throwable.class))).thenReturn(true); |
| 131 | + |
| 132 | + var provider = configuration.graphQLWebClientRetryProvider(mockedPredicate); |
| 133 | + assertNotNull(provider); |
| 134 | + assertNotNull(provider.get()); |
| 135 | + assertInstanceOf(RetryBackoffSpec.class, provider.get()); |
| 136 | + |
| 137 | + var castedProviderValue = (RetryBackoffSpec) provider.get(); |
| 138 | + assertEquals(42, castedProviderValue.maxAttempts); |
| 139 | + assertEquals(Duration.ofMillis(50), castedProviderValue.minBackoff); |
| 140 | + |
| 141 | + castedProviderValue.errorFilter.test(new Throwable()); |
| 142 | + verify(mockedPredicate, times(1)).test(isA(Throwable.class)); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void webClient_graphQLWebClientRetryProvider_indefinitelyStrategy() { |
| 147 | + graphQLClientRetryProperties.setStrategy(GraphQLClientRetryProperties.RetryStrategy.INDEFINITELY); |
| 148 | + |
| 149 | + var mockedPredicate = mock(GraphQLWebClientRetryErrorFilterPredicate.class); |
| 150 | + when(mockedPredicate.test(isA(Throwable.class))).thenReturn(true); |
| 151 | + |
| 152 | + var provider = configuration.graphQLWebClientRetryProvider(mockedPredicate); |
| 153 | + assertNotNull(provider); |
| 154 | + assertNotNull(provider.get()); |
| 155 | + assertInstanceOf(RetrySpec.class, provider.get()); |
| 156 | + |
| 157 | + var castedProviderValue = (RetrySpec) provider.get(); |
| 158 | + |
| 159 | + castedProviderValue.errorFilter.test(new Throwable()); |
| 160 | + verify(mockedPredicate, times(1)).test(isA(Throwable.class)); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void webClient_graphQLWebClientRetryProvider_maxStrategy() { |
| 165 | + graphQLClientRetryProperties.setStrategy(GraphQLClientRetryProperties.RetryStrategy.MAX); |
| 166 | + graphQLClientRetryProperties.getMax().setMaxAttempts(42); |
| 167 | + |
| 168 | + var mockedPredicate = mock(GraphQLWebClientRetryErrorFilterPredicate.class); |
| 169 | + when(mockedPredicate.test(isA(Throwable.class))).thenReturn(true); |
| 170 | + |
| 171 | + var provider = configuration.graphQLWebClientRetryProvider(mockedPredicate); |
| 172 | + assertNotNull(provider); |
| 173 | + assertNotNull(provider.get()); |
| 174 | + assertInstanceOf(RetrySpec.class, provider.get()); |
| 175 | + |
| 176 | + var castedProviderValue = (RetrySpec) provider.get(); |
| 177 | + assertEquals(42, castedProviderValue.maxAttempts); |
| 178 | + |
| 179 | + castedProviderValue.errorFilter.test(new Throwable()); |
| 180 | + verify(mockedPredicate, times(1)).test(isA(Throwable.class)); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + void webClient_graphQLWebClientRetryProvider_maxInRowStrategy() { |
| 185 | + graphQLClientRetryProperties.setStrategy(GraphQLClientRetryProperties.RetryStrategy.MAX_IN_ROW); |
| 186 | + graphQLClientRetryProperties.getMaxInRow().setMaxAttempts(42); |
| 187 | + |
| 188 | + var mockedPredicate = mock(GraphQLWebClientRetryErrorFilterPredicate.class); |
| 189 | + when(mockedPredicate.test(isA(Throwable.class))).thenReturn(true); |
| 190 | + |
| 191 | + var provider = configuration.graphQLWebClientRetryProvider(mockedPredicate); |
| 192 | + assertNotNull(provider); |
| 193 | + assertNotNull(provider.get()); |
| 194 | + assertInstanceOf(RetrySpec.class, provider.get()); |
| 195 | + |
| 196 | + var castedProviderValue = (RetrySpec) provider.get(); |
| 197 | + assertEquals(42, castedProviderValue.maxAttempts); |
| 198 | + |
| 199 | + castedProviderValue.errorFilter.test(new Throwable()); |
| 200 | + verify(mockedPredicate, times(1)).test(isA(Throwable.class)); |
72 | 201 | }
|
73 | 202 |
|
74 | 203 | }
|
0 commit comments