Skip to content

Commit

Permalink
Fixing (or partially fixing) issues with sending grants to open search (
Browse files Browse the repository at this point in the history
  • Loading branch information
GavCookCO authored Apr 16, 2024
1 parent 0b84eeb commit bd28ccc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package gov.cabinetoffice.gap.adminbackend.services;

import com.contentful.java.cma.model.CMAEntry;
import com.fasterxml.jackson.databind.ObjectMapper;
import gov.cabinetoffice.gap.adminbackend.config.ContentfulConfigProperties;
import gov.cabinetoffice.gap.adminbackend.config.OpenSearchConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static org.apache.http.HttpHeaders.AUTHORIZATION;
import static org.apache.http.HttpHeaders.CONTENT_TYPE;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
Expand All @@ -14,20 +17,17 @@
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import static org.apache.http.HttpHeaders.AUTHORIZATION;
import static org.apache.http.HttpHeaders.CONTENT_TYPE;

@Service
@RequiredArgsConstructor
@Slf4j
public class OpenSearchService {

private final WebClient.Builder webClientBuilder;
private final OpenSearchConfig openSearchConfig;
private final ObjectMapper objectMapper;
private final ContentfulConfigProperties contentfulProperties;

public void indexEntry(final CMAEntry contentfulEntry) {
final String body = contentfulEntryToJsonString(contentfulEntry);
final String body = getContentfulAdvertAsJson(contentfulEntry.getId());
webClientBuilder.build().put()
.uri(createUrl(contentfulEntry))
.body(Mono.just(body), String.class)
Expand All @@ -40,7 +40,7 @@ public void indexEntry(final CMAEntry contentfulEntry) {
}

public void removeIndexEntry(final CMAEntry contentfulEntry) {
final String body = contentfulEntryToJsonString(contentfulEntry.getSystem());
final String body = getContentfulAdvertAsJson(contentfulEntry.getId());
webClientBuilder.build().method(HttpMethod.DELETE)
.uri(createUrl(contentfulEntry))
.body(Mono.just(body), String.class)
Expand All @@ -61,8 +61,37 @@ private String createAuthHeader() {
return "Basic " + Base64.getEncoder().encodeToString(auth.getBytes());
}

private String contentfulEntryToJsonString(final Object contentfulEntry) {
final String contentfulObject = objectMapper.valueToTree(contentfulEntry).toString();
return contentfulObject.replace("\"system\":", "\"sys\":");
private String getContentfulAdvertAsJson(String entryId) {
final String contentfulUrl = String.format(
"https://api.contentful.com/spaces/%1$s/environments/%2$s/entries/%3$s",
contentfulProperties.getSpaceId(),
contentfulProperties.getEnvironmentId(),
entryId
);

return webClientBuilder.build()
.get()
.uri(contentfulUrl)
.headers(h ->
h.set("Authorization", String.format("Bearer %s", contentfulProperties.getAccessToken()))
)
.retrieve()
.onStatus(HttpStatus::isError, response -> {
log.error("Contentful response -------------------");
log.error(response.statusCode().toString());
log.error(response.bodyToMono(String.class).toString());
log.error("End Contentful response ---------------");

return Mono.empty();
})
.bodyToMono(String.class)
.doOnError(exception ->
log.error(
"getContentfulAdvertAsJson failed on GET to {}, with message: {}",
contentfulUrl,
exception
)
)
.block();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

import com.contentful.java.cma.model.CMAEntry;
import com.contentful.java.cma.model.CMASystem;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import gov.cabinetoffice.gap.adminbackend.config.ContentfulConfigProperties;
import gov.cabinetoffice.gap.adminbackend.config.OpenSearchConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import static org.mockito.Mockito.*;
import org.mockito.Spy;
import org.springframework.http.HttpMethod;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import static org.mockito.Mockito.*;

@SpringJUnitConfig
class OpenSearchServiceTest {
@Spy
Expand All @@ -31,7 +29,7 @@ class OpenSearchServiceTest {
private WebClient.Builder webClientBuilder;

@Mock
private ObjectMapper objectMapper;
private ContentfulConfigProperties contentfulProperties;

private CMAEntry contentfulEntry;

Expand All @@ -53,12 +51,23 @@ 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);
final WebClient.ResponseSpec mockResponseSpec = mock(WebClient.ResponseSpec.class);
final JsonNode mockJsonNode = mock(JsonNode.class);

when(objectMapper.valueToTree(any())).thenReturn(mockJsonNode);
when(mockJsonNode.toString()).thenReturn("{\"system\":{\"id\":\"testId\"},\"environmentId\":\"master\",\"id\":\"testId\",\"published\":false,\"archived\":false}");
when(contentfulProperties.getSpaceId()).thenReturn("Space");
when(contentfulProperties.getEnvironmentId()).thenReturn("environment");
when(contentfulProperties.getAccessToken()).thenReturn("accessToken");
when(contentfulProperties.getDeliveryAPIAccessToken()).thenReturn("deliveryAccessToken");

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

when(mockWebClient.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(mockRequestBodyUriSpec.uri("testUrl/testDomain/_doc/testId")).thenReturn(mockRequestBodyUriSpec);
when(mockRequestBodyUriSpec.body(any(), eq(String.class))).thenReturn(mockRequestHeadersSpec);
Expand All @@ -68,6 +77,12 @@ void indexEntry() {
when(mockResponseSpec.bodyToMono(void.class)).thenReturn(Mono.empty());

openSearchService.indexEntry(contentfulEntry);

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

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

@Test
Expand All @@ -76,11 +91,22 @@ void removeIndexEntry() {
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);
final JsonNode mockJsonNode = mock(JsonNode.class);
final WebClient.RequestHeadersUriSpec mockRequestHeadersUriSpec = mock(WebClient.RequestHeadersUriSpec.class);

when(contentfulProperties.getSpaceId()).thenReturn("Space");
when(contentfulProperties.getEnvironmentId()).thenReturn("environment");
when(contentfulProperties.getAccessToken()).thenReturn("accessToken");
when(contentfulProperties.getDeliveryAPIAccessToken()).thenReturn("deliveryAccessToken");

when(objectMapper.valueToTree(any())).thenReturn(mockJsonNode);
when(mockJsonNode.toString()).thenReturn("{\"system\":{\"id\":\"testId\"},\"environmentId\":\"master\",\"id\":\"testId\",\"published\":false,\"archived\":false}");
when(webClientBuilder.build()).thenReturn(mockWebClient);

when(mockWebClient.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(mockRequestBodyUriSpec.uri("testUrl/testDomain/_doc/testId")).thenReturn(mockRequestBodyUriSpec);
when(mockRequestBodyUriSpec.body(any(), eq(String.class))).thenReturn(mockRequestHeadersSpec);
Expand All @@ -90,5 +116,11 @@ void removeIndexEntry() {
when(mockResponseSpec.bodyToMono(void.class)).thenReturn(Mono.empty());

openSearchService.removeIndexEntry(contentfulEntry);

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

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

0 comments on commit bd28ccc

Please sign in to comment.