Skip to content

Commit

Permalink
making web client a bean to see if this resolves connection closed er…
Browse files Browse the repository at this point in the history
…rors (#292)
  • Loading branch information
GavCookCO authored Apr 19, 2024
1 parent 673b76b commit 90cb945
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gov.cabinetoffice.gap.adminbackend.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class WebClientConfig {

@Bean
public WebClient getWebClient() {
return WebClient.builder().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class GrantAdvertService {
private final UserService userService;
private final OpenSearchService openSearchService;
private final WebClient.Builder webClientBuilder;

private final WebClient webClient;
private final Clock clock;
private final ContentfulConfigProperties contentfulProperties;
private final FeatureFlagsConfigurationProperties featureFlagsProperties;
Expand Down Expand Up @@ -436,8 +438,7 @@ private void createRichTextQuestionsInContentful(final GrantAdvert advert, final
}

final Instant now = Instant.now();
webClientBuilder.build()
.patch()
webClient.patch()
.uri(contentfulUrl)
.headers(h -> {
h.set("Authorization", String.format("Bearer %s", contentfulProperties.getAccessToken()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
@Slf4j
public class OpenSearchService {

private final WebClient.Builder webClientBuilder;
private final WebClient webClient;
private final OpenSearchConfig openSearchConfig;
private final ContentfulConfigProperties contentfulProperties;

public void indexEntry(final CMAEntry contentfulEntry) {
final String body = getContentfulAdvertAsJson(contentfulEntry.getId());
webClientBuilder.build().put()
webClient.put()
.uri(createUrl(contentfulEntry))
.body(Mono.just(body), String.class)
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE + "; " + StandardCharsets.UTF_8.name())
Expand All @@ -41,7 +41,7 @@ public void indexEntry(final CMAEntry contentfulEntry) {

public void removeIndexEntry(final CMAEntry contentfulEntry) {
final String body = getContentfulAdvertAsJson(contentfulEntry.getId());
webClientBuilder.build().method(HttpMethod.DELETE)
webClient.method(HttpMethod.DELETE)
.uri(createUrl(contentfulEntry))
.body(Mono.just(body), String.class)
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE + "; " + StandardCharsets.UTF_8.name())
Expand Down Expand Up @@ -69,8 +69,7 @@ private String getContentfulAdvertAsJson(String entryId) {
entryId
);

return webClientBuilder.build()
.get()
return webClient.get()
.uri(contentfulUrl)
.headers(h ->
h.set("Authorization", String.format("Bearer %s", contentfulProperties.getAccessToken()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class GrantAdvertServiceTest {
@Mock
private OpenSearchService openSearchService;

@Mock
private WebClient webClient;

@InjectMocks
@Spy
private GrantAdvertService grantAdvertService;
Expand Down Expand Up @@ -851,12 +854,10 @@ void publishAdvert_successfullyPublishedAdvert() {

doReturn(mockGrantAdvert).when(grantAdvertService).save(any());

final WebClient webClient = mock(WebClient.class);
final WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
final WebClient.RequestBodyUriSpec requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
final WebClient.ResponseSpec responseSpec = mock(WebClient.ResponseSpec.class);

when(webClientBuilder.build()).thenReturn(webClient);
when(webClient.patch()).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri(anyString())).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.headers(any())).thenReturn(requestBodyUriSpec);
Expand Down Expand Up @@ -927,12 +928,10 @@ void publishAdvert_updatesExistingAdvert_IfFirstPublishedDateHasBeenSet() {
//when(contentfulEntries.async()).thenReturn(async);
doReturn(grantAvertInDatabase).when(grantAdvertService).save(any());

final WebClient webClient = mock(WebClient.class);
final WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
final WebClient.RequestBodyUriSpec requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
final WebClient.ResponseSpec responseSpec = mock(WebClient.ResponseSpec.class);

when(webClientBuilder.build()).thenReturn(webClient);
when(webClient.patch()).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri(anyString())).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.headers(any())).thenReturn(requestBodyUriSpec);
Expand Down Expand Up @@ -977,12 +976,10 @@ void publishAdvertThroughLambda_successfullyPublishedAdvert() {
.grantAdvertName("Grant Advert Name").response(response).grantAdvertName("Homelessness Grant")
.build();

final WebClient webClient = mock(WebClient.class);
final WebClient.RequestHeadersSpec requestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
final WebClient.RequestBodyUriSpec requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
final WebClient.ResponseSpec responseSpec = mock(WebClient.ResponseSpec.class);

when(webClientBuilder.build()).thenReturn(webClient);
when(webClient.patch()).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri(anyString())).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.headers(any())).thenReturn(requestBodyUriSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class OpenSearchServiceTest {
private OpenSearchConfig openSearchConfig;

@Mock
private WebClient.Builder webClientBuilder;
private WebClient webClient;

@Mock
private ContentfulConfigProperties contentfulProperties;
Expand All @@ -48,7 +48,6 @@ void beforeEach() {

@Test
void indexEntry() {
final WebClient mockWebClient = mock(WebClient.class);
final WebClient.RequestBodyUriSpec mockRequestBodyUriSpec = Mockito.mock(WebClient.RequestBodyUriSpec.class);
final WebClient.RequestHeadersSpec mockRequestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
final WebClient.RequestHeadersUriSpec mockRequestHeadersUriSpec = mock(WebClient.RequestHeadersUriSpec.class);
Expand All @@ -59,16 +58,14 @@ void indexEntry() {
when(contentfulProperties.getAccessToken()).thenReturn("accessToken");
when(contentfulProperties.getDeliveryAPIAccessToken()).thenReturn("deliveryAccessToken");

when(webClientBuilder.build()).thenReturn(mockWebClient);

when(mockWebClient.get()).thenReturn(mockRequestHeadersUriSpec);
when(webClient.get()).thenReturn(mockRequestHeadersUriSpec);
when(mockRequestHeadersUriSpec.uri(anyString())).thenReturn(mockRequestHeadersUriSpec);
when(mockRequestHeadersUriSpec.headers(any())).thenReturn(mockRequestHeadersUriSpec);
when(mockRequestHeadersUriSpec.retrieve()).thenReturn(mockResponseSpec);
when(mockResponseSpec.onStatus(any(), any())).thenReturn(mockResponseSpec);
when(mockResponseSpec.bodyToMono(String.class)).thenReturn(Mono.just("{\"system\":{\"id\":\"testId\"},\"environmentId\":\"master\",\"id\":\"testId\",\"published\":false,\"archived\":false}"));

when(mockWebClient.put()).thenReturn(mockRequestBodyUriSpec);
when(webClient.put()).thenReturn(mockRequestBodyUriSpec);
when(mockRequestBodyUriSpec.uri("testUrl/testDomain/_doc/testId")).thenReturn(mockRequestBodyUriSpec);
when(mockRequestBodyUriSpec.body(any(), eq(String.class))).thenReturn(mockRequestHeadersSpec);
when(mockRequestHeadersSpec.header("Content-Type", "application/json; UTF-8")).thenReturn(mockRequestHeadersSpec);
Expand All @@ -78,16 +75,15 @@ void indexEntry() {

openSearchService.indexEntry(contentfulEntry);

verify(webClientBuilder.build().get(), times(1))
verify(webClient.get(), times(1))
.uri("https://api.contentful.com/spaces/Space/environments/environment/entries/testId");

verify(webClientBuilder.build().put(), times(1))
verify(webClient.put(), times(1))
.uri("testUrl/testDomain/_doc/testId");
}

@Test
void removeIndexEntry() {
final WebClient mockWebClient = mock(WebClient.class);
final WebClient.RequestBodyUriSpec mockRequestBodyUriSpec = Mockito.mock(WebClient.RequestBodyUriSpec.class);
final WebClient.RequestHeadersSpec mockRequestHeadersSpec = mock(WebClient.RequestHeadersSpec.class);
final WebClient.ResponseSpec mockResponseSpec = mock(WebClient.ResponseSpec.class);
Expand All @@ -98,16 +94,15 @@ void removeIndexEntry() {
when(contentfulProperties.getAccessToken()).thenReturn("accessToken");
when(contentfulProperties.getDeliveryAPIAccessToken()).thenReturn("deliveryAccessToken");

when(webClientBuilder.build()).thenReturn(mockWebClient);

when(mockWebClient.get()).thenReturn(mockRequestHeadersUriSpec);
when(webClient.get()).thenReturn(mockRequestHeadersUriSpec);
when(mockRequestHeadersUriSpec.uri(anyString())).thenReturn(mockRequestHeadersUriSpec);
when(mockRequestHeadersUriSpec.headers(any())).thenReturn(mockRequestHeadersUriSpec);
when(mockRequestHeadersUriSpec.retrieve()).thenReturn(mockResponseSpec);
when(mockResponseSpec.onStatus(any(), any())).thenReturn(mockResponseSpec);
when(mockResponseSpec.bodyToMono(String.class)).thenReturn(Mono.just("{\"system\":{\"id\":\"testId\"},\"environmentId\":\"master\",\"id\":\"testId\",\"published\":false,\"archived\":false}"));

when(mockWebClient.method(HttpMethod.DELETE)).thenReturn(mockRequestBodyUriSpec);
when(webClient.method(HttpMethod.DELETE)).thenReturn(mockRequestBodyUriSpec);
when(mockRequestBodyUriSpec.uri("testUrl/testDomain/_doc/testId")).thenReturn(mockRequestBodyUriSpec);
when(mockRequestBodyUriSpec.body(any(), eq(String.class))).thenReturn(mockRequestHeadersSpec);
when(mockRequestHeadersSpec.header("Content-Type", "application/json; UTF-8")).thenReturn(mockRequestHeadersSpec);
Expand All @@ -117,10 +112,10 @@ void removeIndexEntry() {

openSearchService.removeIndexEntry(contentfulEntry);

verify(webClientBuilder.build().get(), times(1))
verify(webClient.get(), times(1))
.uri("https://api.contentful.com/spaces/Space/environments/environment/entries/testId");

verify(webClientBuilder.build().method(HttpMethod.DELETE), times(1))
verify(webClient.method(HttpMethod.DELETE), times(1))
.uri("testUrl/testDomain/_doc/testId");
}
}

0 comments on commit 90cb945

Please sign in to comment.