|
| 1 | +package graphql.kickstart.spring.webclient.boot; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 4 | +import static org.mockito.ArgumentMatchers.isA; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.times; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 14 | +import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository; |
| 15 | +import org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction; |
| 16 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 17 | +import org.springframework.web.reactive.function.client.WebClient; |
| 18 | +import reactor.core.publisher.Mono; |
| 19 | + |
| 20 | +class GraphQLWebClientAutoConfigurationTest { |
| 21 | + |
| 22 | + private GraphQLWebClientAutoConfiguration configuration; |
| 23 | + private WebClient.Builder mockClientBuilder; |
| 24 | + |
| 25 | + @BeforeEach |
| 26 | + void setup() { |
| 27 | + GraphQLClientProperties graphQLClientProperties = new GraphQLClientProperties(); |
| 28 | + configuration = new GraphQLWebClientAutoConfiguration(graphQLClientProperties); |
| 29 | + |
| 30 | + mockClientBuilder = mock(WebClient.Builder.class); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void webClient_nullClientRegistrations_doesNotAddFilter() { |
| 35 | + configuration.webClient(mockClientBuilder, null); |
| 36 | + verify(mockClientBuilder, times(0)) |
| 37 | + .filter(isA(ServerOAuth2AuthorizedClientExchangeFilterFunction.class)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void webClient_noGraphQLRegistration_doesNotAddFilter() { |
| 42 | + var registrationRepository = mock(ReactiveClientRegistrationRepository.class); |
| 43 | + when(registrationRepository.findByRegistrationId("graphql")).thenReturn(Mono.empty()); |
| 44 | + configuration.webClient(mockClientBuilder, registrationRepository); |
| 45 | + verify(mockClientBuilder, times(0)) |
| 46 | + .filter(isA(ServerOAuth2AuthorizedClientExchangeFilterFunction.class)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void webClient_withGraphQLRegistration_doesAddFilter() { |
| 51 | + var registrationRepository = mock(ReactiveClientRegistrationRepository.class); |
| 52 | + var registration = ClientRegistration.withRegistrationId("graphql") |
| 53 | + .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS) |
| 54 | + .clientId("clientId") |
| 55 | + .clientSecret("clientSecret") |
| 56 | + .tokenUri("tokenUri") |
| 57 | + .build(); |
| 58 | + when(registrationRepository.findByRegistrationId("graphql")).thenReturn(Mono.just(registration)); |
| 59 | + configuration.webClient(mockClientBuilder, registrationRepository); |
| 60 | + verify(mockClientBuilder, times(1)) |
| 61 | + .filter(isA(ServerOAuth2AuthorizedClientExchangeFilterFunction.class)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void webClient_getObjectMapper_returns() { |
| 66 | + assertNotNull(configuration.objectMapper()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void webClient_graphQLWebClient_returns() { |
| 71 | + assertNotNull(configuration.graphQLWebClient(WebClient.builder().build(), new ObjectMapper())); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments