From 673b76b3504141a7e7ff0e288353d8ad1c158840 Mon Sep 17 00:00:00 2001 From: GavCookCO <99668051+GavCookCO@users.noreply.github.com> Date: Fri, 19 Apr 2024 13:49:49 +0100 Subject: [PATCH] Fixing (or partially fixing) issues with sending grants to open search (#291) --- .../adminbackend/services/GrantAdvertService.java | 15 ++++----------- .../services/GrantAdvertServiceTest.java | 12 ++++++------ 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java index 276be0a4..576e8921 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java @@ -4,7 +4,6 @@ import com.contentful.java.cda.CDAClient; import com.contentful.java.cda.CDAEntry; import com.contentful.java.cda.QueryOperation; -import com.contentful.java.cma.CMACallback; import com.contentful.java.cma.CMAClient; import com.contentful.java.cma.model.CMAEntry; import com.contentful.java.cma.model.rich.CMARichDocument; @@ -32,6 +31,7 @@ import gov.cabinetoffice.gap.adminbackend.repositories.SchemeRepository; import gov.cabinetoffice.gap.adminbackend.utils.CurrencyFormatter; import gov.cabinetoffice.gap.adminbackend.utils.HelperUtils; +import static gov.cabinetoffice.gap.adminbackend.validation.validators.AdvertPageResponseValidator.*; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.json.JSONArray; @@ -47,8 +47,6 @@ import java.time.format.DateTimeFormatter; import java.util.*; -import static gov.cabinetoffice.gap.adminbackend.validation.validators.AdvertPageResponseValidator.*; - @Service @RequiredArgsConstructor @Slf4j @@ -300,7 +298,6 @@ public void deleteGrantAdvert(UUID grantAdvertId) { } public GrantAdvert publishAdvert(UUID advertId) { - final Instant now = Instant.now(); final GrantAdvert advert = getAdvertById(advertId); CMAEntry contentfulAdvert; @@ -320,13 +317,8 @@ public GrantAdvert publishAdvert(UUID advertId) { advert.setContentfulEntryId(contentfulAdvert.getId()); if (Boolean.FALSE.equals(contentfulAdvert.isPublished())) { - contentfulManagementClient.entries().async().publish(contentfulAdvert, new CMACallback<>() { - @Override - protected void onSuccess(CMAEntry result) { - openSearchService.indexEntry(result); - log.debug("Took {} seconds to publish advert", Duration.between(now, Instant.now()).getSeconds()); - } - }); + final CMAEntry publishedAdvert = contentfulManagementClient.entries().publish(contentfulAdvert); + openSearchService.indexEntry(publishedAdvert); } updateGrantAdvertApplicationDates(advert); @@ -460,6 +452,7 @@ private void createRichTextQuestionsInContentful(final GrantAdvert advert, final log.info("Took {} seconds to try and update rich text questions in Contentful", Duration.between(now, Instant.now()).getSeconds()); }) .block(); + log.info("Took {} seconds to update rich text questions in Contentful", Duration.between(now, Instant.now()).getSeconds()); } diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertServiceTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertServiceTest.java index 2ffe88ba..eebe3618 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertServiceTest.java @@ -847,7 +847,7 @@ void publishAdvert_successfullyPublishedAdvert() { when(contentfulEntries.fetchOne(contentfulAdvertId)).thenReturn(publishedContentfulAdvert); - when(contentfulEntries.async()).thenReturn(async); + //when(contentfulEntries.async()).thenReturn(async); doReturn(mockGrantAdvert).when(grantAdvertService).save(any()); @@ -899,7 +899,7 @@ void publishAdvert_successfullyPublishedAdvert() { verify(contentfulEntries).fetchOne(contentfulAdvertId); // verify that we've published - verify(async).publish(eq(publishedContentfulAdvert), any()); + verify(contentfulEntries).publish(publishedContentfulAdvert); } @Test @@ -924,7 +924,7 @@ void publishAdvert_updatesExistingAdvert_IfFirstPublishedDateHasBeenSet() { when(contentfulEntries.update(Mockito.any())).thenReturn(publishedContentfulAdvert); when(contentfulEntries.fetchOne(contentfulAdvertId)).thenReturn(publishedContentfulAdvert, publishedContentfulAdvert); - when(contentfulEntries.async()).thenReturn(async); + //when(contentfulEntries.async()).thenReturn(async); doReturn(grantAvertInDatabase).when(grantAdvertService).save(any()); final WebClient webClient = mock(WebClient.class); @@ -965,7 +965,7 @@ void publishAdvert_updatesExistingAdvert_IfFirstPublishedDateHasBeenSet() { verify(contentfulEntries, atLeastOnce()).fetchOne(contentfulAdvertId); // verify that we've published - verify(async).publish(eq(publishedContentfulAdvert), any()); + verify(contentfulEntries).publish(publishedContentfulAdvert); } @Test @@ -1010,7 +1010,7 @@ void publishAdvertThroughLambda_successfullyPublishedAdvert() { when(contentfulEntries.fetchOne(contentfulAdvertId)).thenReturn(publishedContentfulAdvert); - when(contentfulEntries.async()).thenReturn(async); + //when(contentfulEntries.async()).thenReturn(async); final ArgumentCaptor entryCaptor = ArgumentCaptor.forClass(CMAEntry.class); @@ -1047,7 +1047,7 @@ void publishAdvertThroughLambda_successfullyPublishedAdvert() { verify(contentfulEntries).fetchOne(contentfulAdvertId); // verify that we've published - verify(async).publish(eq(publishedContentfulAdvert), any()); + verify(contentfulEntries).publish(publishedContentfulAdvert); } }