diff --git a/charts/rd-judicial-api/values.preview.template.yaml b/charts/rd-judicial-api/values.preview.template.yaml index 5379ba163..8702306c0 100644 --- a/charts/rd-judicial-api/values.preview.template.yaml +++ b/charts/rd-judicial-api/values.preview.template.yaml @@ -19,7 +19,7 @@ java: ELINKS_URL: https://judiciary-middleware-futureehr.herokuapp.com/api/v5 LAST_UPDATED: 2014-01-01 SCHEDULER_ENABLED: true - CRON_EXPRESSION: "* 10 15 * * *" + CRON_EXPRESSION: "* 45 12 * * *" postgresql: enabled: true auth: diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/DeletedIntegrationTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/DeletedIntegrationTest.java index 02001c700..d078e754d 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/DeletedIntegrationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/DeletedIntegrationTest.java @@ -116,14 +116,14 @@ void verifyDeletedJrdUserProfile() { List userprofile = profileRepository.findAll(); - assertEquals(12, userprofile.size()); - assertEquals("4913085", userprofile.get(10).getPersonalCode()); - assertEquals(true, userprofile.get(10).getDeletedFlag()); - assertEquals("2023-07-13", userprofile.get(10).getDeletedOn().toLocalDate().toString()); + assertEquals(13, userprofile.size()); + assertEquals("4913085", userprofile.get(11).getPersonalCode()); + assertEquals(true, userprofile.get(11).getDeletedFlag()); + assertEquals("2023-07-13", userprofile.get(11).getDeletedOn().toLocalDate().toString()); - assertEquals("4913086", userprofile.get(11).getPersonalCode()); - assertEquals(false, userprofile.get(11).getDeletedFlag()); - assertEquals("2022-07-10", userprofile.get(11).getDeletedOn().toLocalDate().toString()); + assertEquals("4913086", userprofile.get(12).getPersonalCode()); + assertEquals(false, userprofile.get(12).getDeletedFlag()); + assertEquals("2022-07-10", userprofile.get(12).getDeletedOn().toLocalDate().toString()); @@ -142,14 +142,14 @@ void verifyDeletedJrdAuditFunctionality() { List userprofile = profileRepository.findAll(); - assertEquals(12, userprofile.size()); - assertEquals("4913085", userprofile.get(10).getPersonalCode()); - assertEquals(true, userprofile.get(10).getDeletedFlag()); - assertEquals("2023-07-13", userprofile.get(10).getDeletedOn().toLocalDate().toString()); + assertEquals(13, userprofile.size()); + assertEquals("4913085", userprofile.get(11).getPersonalCode()); + assertEquals(true, userprofile.get(11).getDeletedFlag()); + assertEquals("2023-07-13", userprofile.get(11).getDeletedOn().toLocalDate().toString()); - assertEquals("4913086", userprofile.get(11).getPersonalCode()); - assertEquals(false, userprofile.get(11).getDeletedFlag()); - assertEquals("2022-07-10", userprofile.get(11).getDeletedOn().toLocalDate().toString()); + assertEquals("4913086", userprofile.get(12).getPersonalCode()); + assertEquals(false, userprofile.get(12).getDeletedFlag()); + assertEquals("2022-07-10", userprofile.get(12).getDeletedOn().toLocalDate().toString()); List elinksAudit = elinkSchedularAuditRepository.findAll(); diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinkClientsCommonIntegrationTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinkClientsCommonIntegrationTest.java index 47a7703e8..860bb5f50 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinkClientsCommonIntegrationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinkClientsCommonIntegrationTest.java @@ -28,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.APPOINTMENT_TABLE; +import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.BASE_LOCATION_ID; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.DELETEDAPI; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.ELINKS_ERROR_RESPONSE_BAD_REQUEST; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.ELINKS_ERROR_RESPONSE_FORBIDDEN; @@ -375,7 +375,7 @@ void verifyPeoplesJrdExceptionRecordsBaseLocationNotFoundScenario() { List elinksException = elinkDataExceptionRepository.findAll(); ElinkDataExceptionRecords exceptionEntry = elinksException.get(0); - assertEquals(APPOINTMENT_TABLE, exceptionEntry.getFieldInError()); + assertEquals(BASE_LOCATION_ID, exceptionEntry.getFieldInError()); assertNotNull(exceptionEntry.getSchedulerStartTime()); } diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksEndToEndIntegrationForExistingObjectId.java b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksEndToEndIntegrationForExistingObjectId.java index 001220150..9d5457c57 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksEndToEndIntegrationForExistingObjectId.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksEndToEndIntegrationForExistingObjectId.java @@ -182,18 +182,18 @@ void test_elinks_end_to_end_success_scenario_with_partial_success_return_status_ assertEquals("1722",baseLocationList.get(4).getParentId()); List userprofile = profileRepository.findAll(); - assertEquals(11, userprofile.size()); - assertEquals("4913085", userprofile.get(10).getPersonalCode()); - assertEquals("Rachel", userprofile.get(10).getKnownAs()); - assertEquals("Jones", userprofile.get(10).getSurname()); - assertEquals("District Judge Rachel Jones", userprofile.get(10).getFullName()); - assertEquals(null, userprofile.get(10).getPostNominals()); + assertEquals(12, userprofile.size()); + assertEquals("4913085", userprofile.get(11).getPersonalCode()); + assertEquals("Rachel", userprofile.get(11).getKnownAs()); + assertEquals("Jones", userprofile.get(11).getSurname()); + assertEquals("District Judge Rachel Jones", userprofile.get(11).getFullName()); + assertEquals(null, userprofile.get(11).getPostNominals()); assertEquals("DJ.Rachel.Jones@ejudiciary.net", - userprofile.get(10).getEmailId()); - assertTrue(userprofile.get(10).getActiveFlag()); - assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cae", userprofile.get(10).getObjectId()); - assertNull(userprofile.get(10).getSidamId()); - assertEquals("RJ",userprofile.get(10).getInitials()); + userprofile.get(11).getEmailId()); + assertTrue(userprofile.get(11).getActiveFlag()); + assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cae", userprofile.get(11).getObjectId()); + assertNull(userprofile.get(11).getSidamId()); + assertEquals("RJ",userprofile.get(11).getInitials()); //asserting userprofile data for leaver api diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksEndToEndIntegrationForSidamIdisPresent.java b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksEndToEndIntegrationForSidamIdisPresent.java new file mode 100644 index 000000000..70739b47b --- /dev/null +++ b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksEndToEndIntegrationForSidamIdisPresent.java @@ -0,0 +1,202 @@ +package uk.gov.hmcts.reform.judicialapi.elinks; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.nimbusds.jose.JOSEException; +import org.junit.Assert; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.util.ReflectionTestUtils; +import uk.gov.hmcts.reform.judicialapi.elinks.configuration.IdamTokenConfigProperties; +import uk.gov.hmcts.reform.judicialapi.elinks.domain.BaseLocation; +import uk.gov.hmcts.reform.judicialapi.elinks.domain.DataloadSchedulerJob; +import uk.gov.hmcts.reform.judicialapi.elinks.domain.ElinkDataSchedularAudit; +import uk.gov.hmcts.reform.judicialapi.elinks.domain.Location; +import uk.gov.hmcts.reform.judicialapi.elinks.domain.UserProfile; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.AppointmentsRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.AuthorisationsRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.BaseLocationRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.DataloadSchedulerJobRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.ElinkDataExceptionRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.ElinkSchedularAuditRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.JudicialRoleTypeRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.LocationRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.ProfileRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.response.ElinkLocationWrapperResponse; +import uk.gov.hmcts.reform.judicialapi.elinks.scheduler.ElinksApiJobScheduler; +import uk.gov.hmcts.reform.judicialapi.elinks.service.PublishSidamIdService; +import uk.gov.hmcts.reform.judicialapi.elinks.servicebus.ElinkTopicPublisher; +import uk.gov.hmcts.reform.judicialapi.elinks.util.ElinksEnabledIntegrationTest; +import uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants; +import uk.gov.hmcts.reform.judicialapi.versions.V2; + +import java.util.List; +import java.util.Map; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.BASE_LOCATION_DATA_LOAD_SUCCESS; +import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.LOCATIONAPI; + +class ElinksEndToEndIntegrationForSidamIdisPresent extends ElinksEnabledIntegrationTest { + + @Autowired + LocationRepository locationRepository; + @Autowired + ProfileRepository profileRepository; + @Autowired + JudicialRoleTypeRepository judicialRoleTypeRepository; + @Autowired + BaseLocationRepository baseLocationRepository; + @Autowired + AuthorisationsRepository authorisationsRepository; + @Autowired + AppointmentsRepository appointmentsRepository; + @Autowired + IdamTokenConfigProperties tokenConfigProperties; + + + @Autowired + private ElinkSchedularAuditRepository elinkSchedularAuditRepository; + + @Autowired + private ElinksApiJobScheduler elinksApiJobScheduler; + + @Autowired + private DataloadSchedulerJobRepository dataloadSchedulerJobRepository; + + @Autowired + PublishSidamIdService publishSidamIdService; + + @MockBean + ElinkTopicPublisher elinkTopicPublisher; + + @Autowired + ElinkDataExceptionRepository elinkDataExceptionRepository; + + + + @BeforeAll + void loadElinksResponse() throws Exception { + + cleanupData(); + + String locationResponseValidationJson = + loadJson("src/integrationTest/resources/wiremock_responses/location.json"); + String baselocationResponseValidationJson = + loadJson("src/integrationTest/resources/wiremock_responses/base_location.json"); + String peopleResponseValidationJson = + loadJson("src/integrationTest/resources/wiremock_responses/testpeople_withoutsidam.json"); + + elinks.stubFor(get(urlPathMatching("/reference_data/location")) + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", V2.MediaType.SERVICE) + .withHeader("Connection", "close") + .withBody(locationResponseValidationJson))); + + elinks.stubFor(get(urlPathMatching("/reference_data/base_location")) + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", V2.MediaType.SERVICE) + .withHeader("Connection", "close") + .withBody(baselocationResponseValidationJson) + .withTransformers("user-token-response"))); + + elinks.stubFor(get(urlPathMatching("/people")) + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", V2.MediaType.SERVICE) + .withHeader("Connection", "close") + .withBody(peopleResponseValidationJson))); + } + + @BeforeEach + void before() { + cleanupData(); + } + + @AfterEach + void cleanUp() { + cleanupData(); + } + + @DisplayName("Elinks end to test sidam is not updated") + @Test + void test_elinks_end_to_end_success_scenario_with_partial_success_return_status_200() + throws JOSEException, JsonProcessingException { + + ReflectionTestUtils.setField(elinksApiJobScheduler, "isSchedulerEnabled", true); + ReflectionTestUtils.setField(publishSidamIdService, "elinkTopicPublisher", elinkTopicPublisher); + + dataloadSchedulerJobRepository.deleteAll(); + elinksApiJobScheduler.loadElinksJob(); + + List audits = dataloadSchedulerJobRepository.findAll(); + DataloadSchedulerJob jobDetails = audits.get(0); + + //assserting scheduler data + assertThat(jobDetails).isNotNull(); + assertThat(jobDetails.getPublishingStatus()).isNotNull(); + Assert.assertEquals(RefDataElinksConstants.JobStatus.SUCCESS.getStatus(),jobDetails.getPublishingStatus()); + + // asserting location data + List elinksAudit = elinkSchedularAuditRepository.findAll(); + Map locationResponse = elinksReferenceDataClient.getLocations(); + ElinkLocationWrapperResponse locations = (ElinkLocationWrapperResponse) locationResponse.get("body"); + ElinkDataSchedularAudit locationAuditEntry = elinksAudit.get(0); + + assertThat(locationResponse).containsEntry("http_status", "200 OK"); + assertEquals(BASE_LOCATION_DATA_LOAD_SUCCESS, locations.getMessage()); + assertEquals(LOCATIONAPI,locationAuditEntry.getApiName()); + assertEquals(RefDataElinksConstants.JobStatus.SUCCESS.getStatus(), locationAuditEntry.getStatus()); + + + List locationsList = locationRepository.findAll(); + assertEquals(11, locationsList.size()); + assertEquals("1", locationsList.get(1).getRegionId()); + assertEquals("London", locationsList.get(1).getRegionDescEn()); + + + //asserting baselocation data + List baseLocationList = baseLocationRepository.findAll(); + assertEquals(12, baseLocationList.size()); + assertEquals("Aberconwy",baseLocationList.get(4).getName()); + assertEquals("1",baseLocationList.get(4).getBaseLocationId()); + assertEquals("1722",baseLocationList.get(4).getParentId()); + + List userprofile = profileRepository.findAll(); + assertEquals(11, userprofile.size()); + assertEquals("123454", userprofile.get(10).getPersonalCode()); + assertEquals("Rachel", userprofile.get(10).getKnownAs()); + assertEquals("Jones", userprofile.get(10).getSurname()); + assertEquals("District Judge Rachel Jones", userprofile.get(10).getFullName()); + assertEquals(null, userprofile.get(10).getPostNominals()); + assertEquals("DJ.Rachel.Jones@ejudiciary.net", + userprofile.get(10).getEmailId()); + assertTrue(userprofile.get(10).getActiveFlag()); + assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cae", userprofile.get(10).getObjectId()); + assertEquals("3333333",userprofile.get(10).getSidamId()); + assertEquals("RJ",userprofile.get(10).getInitials()); + + + + //asserting userprofile data for leaver api + + //asserting userprofile data for deleted api + + } + + private void cleanupData() { + elinkSchedularAuditRepository.deleteAll(); + dataloadSchedulerJobRepository.deleteAll(); + } +} diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksFailedApiPublishingStatusEndToEndIntegrationTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksFailedApiPublishingStatusEndToEndIntegrationTest.java index b504becb0..00268bd50 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksFailedApiPublishingStatusEndToEndIntegrationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksFailedApiPublishingStatusEndToEndIntegrationTest.java @@ -47,10 +47,10 @@ import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyString; import static org.powermock.api.mockito.PowerMockito.doNothing; +import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.DELETEDAPI; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.LEAVERSAPI; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.LOCATION; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.LOCATIONAPI; -import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.PEOPLEAPI; public class ElinksFailedApiPublishingStatusEndToEndIntegrationTest extends ElinksEnabledIntegrationTest { @@ -150,10 +150,10 @@ void test_end_to_end_load_elinks_job_status_failure() //asserting userprofile data for people api Map peopleResponse = elinksReferenceDataClient.getPeoples(); ElinkPeopleWrapperResponse profiles = (ElinkPeopleWrapperResponse) peopleResponse.get("body"); - ElinkDataSchedularAudit peopleAuditEntry = elinksAudit.get(1); + ElinkDataSchedularAudit peopleAuditEntry = elinksAudit.get(2); assertThat(peopleResponse).containsEntry("http_status", "400"); - assertEquals(PEOPLEAPI,peopleAuditEntry.getApiName()); + assertEquals(LEAVERSAPI,peopleAuditEntry.getApiName()); assertEquals(RefDataElinksConstants.JobStatus.FAILED.getStatus(), peopleAuditEntry.getStatus()); List userprofile = profileRepository.findAll(); @@ -162,10 +162,10 @@ void test_end_to_end_load_elinks_job_status_failure() //asserting userprofile data for leaver api Map leaversResponse = elinksReferenceDataClient.getLeavers(); ElinkLeaversWrapperResponse leaversProfiles = (ElinkLeaversWrapperResponse) leaversResponse.get("body"); - ElinkDataSchedularAudit leaversAuditEntry = elinksAudit.get(2); + ElinkDataSchedularAudit leaversAuditEntry = elinksAudit.get(3); assertThat(leaversResponse).containsEntry("http_status", "400"); - assertEquals(LEAVERSAPI,leaversAuditEntry.getApiName()); + assertEquals(DELETEDAPI,leaversAuditEntry.getApiName()); assertEquals(RefDataElinksConstants.JobStatus.FAILED.getStatus(), leaversAuditEntry.getStatus()); List leaverUserProfile = profileRepository.findAll(); @@ -174,7 +174,7 @@ void test_end_to_end_load_elinks_job_status_failure() //assert elastic search api Map idamResponses = elinksReferenceDataClient.getIdamElasticSearch(); - assertEquals("403",idamResponses.get("http_status")); + assertEquals("500",idamResponses.get("http_status")); // asserting SIDAM publishing Map idamResponse = elinksReferenceDataClient.publishSidamIds(); diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksLoadPeopleDataWithDuplicate.java b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksLoadPeopleDataWithDuplicate.java index 71b1bcf5d..9f3467b3d 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksLoadPeopleDataWithDuplicate.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/ElinksLoadPeopleDataWithDuplicate.java @@ -151,7 +151,7 @@ void test_elinks_end_to_end_success_scenario_with_return_status_200() assertEquals(RefDataElinksConstants.JobStatus.PARTIAL_SUCCESS.getStatus(), peopleAuditEntry.getStatus()); List elinksException = elinkDataExceptionRepository.findAll(); - assertEquals("PageNumber:2-Personal Code : 4925319 is already loaded", + assertEquals("Personal Code : 4925319 is already loaded", elinksException.get(1).getErrorDescription()); assertThat(elinksException.size()).isEqualTo(2); diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/IdamElasticSearchIntegrationForSidamTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/IdamElasticSearchIntegrationForSidamTest.java new file mode 100644 index 000000000..109add030 --- /dev/null +++ b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/IdamElasticSearchIntegrationForSidamTest.java @@ -0,0 +1,184 @@ +package uk.gov.hmcts.reform.judicialapi.elinks; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import uk.gov.hmcts.reform.judicialapi.elinks.configuration.IdamTokenConfigProperties; +import uk.gov.hmcts.reform.judicialapi.elinks.domain.UserProfile; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.AppointmentsRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.AuthorisationsRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.ElinkSchedularAuditRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.ProfileRepository; +import uk.gov.hmcts.reform.judicialapi.elinks.response.IdamResponse; +import uk.gov.hmcts.reform.judicialapi.elinks.util.ElinksEnabledIntegrationTest; +import uk.gov.hmcts.reform.judicialapi.versions.V2; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@SuppressWarnings("unchecked") +class IdamElasticSearchIntegrationForSidamTest extends ElinksEnabledIntegrationTest { + + + @Autowired + private ProfileRepository profileRepository; + + @Autowired + IdamTokenConfigProperties tokenConfigProperties; + @Autowired + AuthorisationsRepository authorisationsRepository; + @Autowired + AppointmentsRepository appointmentsRepository; + + @Autowired + private ElinkSchedularAuditRepository elinkSchedularAuditRepository; + + + @BeforeEach + void setUp() { + + final String clientId = "234342332"; + final String redirectUri = "http://idam-api.aat.platform.hmcts.net"; + //The authorization and clientAuth is the dummy value which we can evaluate using BASE64 encoder. + final String authorization = "ZHVtbXl2YWx1ZUBobWN0cy5uZXQ6SE1DVFMxMjM0"; + final String clientAuth = "cmQteHl6LWFwaTp4eXo="; + final String url = "http://127.0.0.1:5000"; + tokenConfigProperties.setClientId(clientId); + tokenConfigProperties.setClientAuthorization(clientAuth); + tokenConfigProperties.setAuthorization(authorization); + tokenConfigProperties.setRedirectUri(redirectUri); + tokenConfigProperties.setUrl(url); + + cleanupData(); + } + + + @BeforeAll + void loadElinksResponse() throws Exception { + + cleanupData(); + + + String peopleResponseValidationJson = + loadJson("src/integrationTest/resources/wiremock_responses/people_part.json"); + + elinks.stubFor(get(urlPathMatching("/people")) + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", V2.MediaType.SERVICE) + .withHeader("Connection", "close") + .withBody(peopleResponseValidationJson))); + + String idamResponseValidationJson = + loadJson("src/integrationTest/resources/wiremock_responses/sidamid_update_for_matched_objectid.json"); + + sidamService.stubFor(get(urlPathMatching("/api/v1/users")) + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", "application/json") + .withHeader("Connection", "close") + .withBody(idamResponseValidationJson) + )); + } + + @AfterEach + void cleanUp() { + cleanupData(); + } + + + @DisplayName("Sidam Id should be updated for matched object Id") + @Test + void sidam_Id_Update_For_Matched_object_Id_From_Null_To_Value() { + + Map response = elinksReferenceDataClient.getPeoples(); + assertThat(response).containsEntry("http_status", "200 OK"); + + + List userprofile = profileRepository.findAll(); + assertEquals(2, userprofile.size()); + assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cae", userprofile.get(0).getObjectId()); + assertEquals(null,userprofile.get(0).getSidamId()); + + Map idamResponses = elinksReferenceDataClient.getIdamElasticSearch(); + assertEquals("200 OK",idamResponses.get("http_status")); + List idamResponse = (ArrayList) idamResponses.get("body"); + assertEquals(2,idamResponse.size()); + + userprofile = profileRepository.findAll(); + assertEquals(2, userprofile.size()); + assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cae", userprofile.get(0).getObjectId()); + assertEquals("6455c84c-e77d-4c4f-9759-bf4a93a8e972", userprofile.get(0).getSidamId()); + + assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cab", userprofile.get(1).getObjectId()); + assertEquals("6455c84c-e77d-4c4f-9759-bf4a93a8e974", userprofile.get(1).getSidamId()); + + } + + @DisplayName("Sidam Id should be updated for matched object Id from V1 to v2") + @Test + void sidam_Id_Update_For_Matched_object_Id_From_V1_To_V2() { + Map response = elinksReferenceDataClient.getPeoples(); + assertThat(response).containsEntry("http_status", "200 OK"); + + List userprofile = profileRepository.findAll(); + userprofile.get(1).setSidamId("6455c84c-e77d-4c4f-9759-bf4a93a8e970"); + assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cab", userprofile.get(1).getObjectId()); + assertEquals("6455c84c-e77d-4c4f-9759-bf4a93a8e970", userprofile.get(1).getSidamId()); + + Map idamResponses = elinksReferenceDataClient.getIdamElasticSearch(); + assertEquals("200 OK",idamResponses.get("http_status")); + List idamResponse = (ArrayList) idamResponses.get("body"); + assertEquals(2,idamResponse.size()); + + userprofile = profileRepository.findAll(); + assertEquals(2, userprofile.size()); + assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cab", userprofile.get(1).getObjectId()); + assertEquals("6455c84c-e77d-4c4f-9759-bf4a93a8e974", userprofile.get(1).getSidamId()); + + assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cae", userprofile.get(0).getObjectId()); + assertEquals("6455c84c-e77d-4c4f-9759-bf4a93a8e972", userprofile.get(0).getSidamId()); + + } + + + @DisplayName("Sidam Id should be updated for matched object Id No change") + @Test + void sidam_Id_Update_For_Matched_object_Id_No_Change() { + Map response = elinksReferenceDataClient.getPeoples(); + assertThat(response).containsEntry("http_status", "200 OK"); + + Map idamResponses = elinksReferenceDataClient.getIdamElasticSearch(); + assertEquals("200 OK",idamResponses.get("http_status")); + List idamResponse = (ArrayList) idamResponses.get("body"); + assertEquals(2,idamResponse.size()); + List userprofile = profileRepository.findAll(); + assertEquals(2, userprofile.size()); + assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cae", userprofile.get(0).getObjectId()); + assertEquals("6455c84c-e77d-4c4f-9759-bf4a93a8e972", userprofile.get(0).getSidamId()); + + assertEquals("5f8b26ba-0c8b-4192-b5c7-311d737f0cab", userprofile.get(1).getObjectId()); + assertEquals("6455c84c-e77d-4c4f-9759-bf4a93a8e974", userprofile.get(1).getSidamId()); + + + } + + + + private void cleanupData() { + authorisationsRepository.deleteAll(); + appointmentsRepository.deleteAll(); + profileRepository.deleteAll(); + elinkSchedularAuditRepository.deleteAll(); + } +} diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/LeaversIntegrationTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/LeaversIntegrationTest.java index 2c6ba900d..6992001fc 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/LeaversIntegrationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/LeaversIntegrationTest.java @@ -78,7 +78,7 @@ void verifyLeaversJrdUserProfile() { List userprofile = profileRepository.findAll(); - assertEquals(11, userprofile.size()); + assertEquals(12, userprofile.size()); assertEquals("28", userprofile.get(1).getPersonalCode()); assertEquals(true, userprofile.get(1).getActiveFlag()); assertEquals("1.11112E+12", userprofile.get(1).getObjectId()); @@ -97,7 +97,7 @@ void verifyLeaversJrdAuditFunctionality() { List userprofile = profileRepository.findAll(); - assertEquals(11, userprofile.size()); + assertEquals(12, userprofile.size()); assertEquals("28", userprofile.get(1).getPersonalCode()); assertEquals(true, userprofile.get(1).getActiveFlag()); assertEquals("1.11112E+12", userprofile.get(1).getObjectId()); diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/SchedulerElinksJobIntegrationTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/SchedulerElinksJobIntegrationTest.java index 33db06274..ab8e14e75 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/SchedulerElinksJobIntegrationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/judicialapi/elinks/SchedulerElinksJobIntegrationTest.java @@ -131,7 +131,7 @@ void test_load_elinks_job_status_failure() { } - @DisplayName("Elinks load eLinks scheduler status verification failure case for job ran already") + @DisplayName("Elinks load eLinks scheduler status verification failure case for job ran already") @Test @Order(3) void test_load_elinks_job_status_failure_job_ran_already() { diff --git a/src/integrationTest/resources/application-test.yml b/src/integrationTest/resources/application-test.yml index 19d8fd824..0c89f1a74 100644 --- a/src/integrationTest/resources/application-test.yml +++ b/src/integrationTest/resources/application-test.yml @@ -149,6 +149,8 @@ elinks: includePreviousAppointments : false threadPauseTime: 2000 threadRetriggerPauseTime: 1000 + retriggerStatus: ${RETRIGGER_STATUSCODE:503,429} + retriggerThreshold: ${RETRIGGER_THRESHOLD:5} scheduler: wrapperApiUrl: ${ELINKS_WRAPPER_API_URL:http://localhost:8093} cronExpression: ${CRON_EXPRESSION:* * */5 * * *} diff --git a/src/integrationTest/resources/db/testmigration/V1_81__elinks_dataload_exception_records.sql b/src/integrationTest/resources/db/testmigration/V1_81__elinks_dataload_exception_records.sql new file mode 100644 index 000000000..4921cbbeb --- /dev/null +++ b/src/integrationTest/resources/db/testmigration/V1_81__elinks_dataload_exception_records.sql @@ -0,0 +1 @@ +ALTER TABLE dbjudicialdata.dataload_exception_records ADD COLUMN page_id int8; \ No newline at end of file diff --git a/src/integrationTest/resources/db/testmigration/V1_81__judicial_service_code_mapping.sql b/src/integrationTest/resources/db/testmigration/V1_81__judicial_service_code_mapping.sql deleted file mode 100644 index 2cb88de41..000000000 --- a/src/integrationTest/resources/db/testmigration/V1_81__judicial_service_code_mapping.sql +++ /dev/null @@ -1,5 +0,0 @@ - -INSERT INTO judicial_service_code_mapping (service_id,ticket_code,service_code,service_description) VALUES - ('124','405','ABA5','Family Private Law'); - -COMMIT; diff --git a/src/integrationTest/resources/db/testmigration/V1_82__elinks_insert_judicial_location_mapping_tables.sql b/src/integrationTest/resources/db/testmigration/V1_82__elinks_insert_judicial_location_mapping_tables.sql new file mode 100644 index 000000000..f56adeddf --- /dev/null +++ b/src/integrationTest/resources/db/testmigration/V1_82__elinks_insert_judicial_location_mapping_tables.sql @@ -0,0 +1,37 @@ +-- insert dbjudicialdata.judicial_location_mapping +INSERT INTO dbjudicialdata.judicial_location_mapping (epimms_id, judicial_base_location_id, service_code) VALUES + ('450049', '866', ''), + ('774335', '1863', ''), + ('457273', '1915', ''), + ('457273', '1455', ''), + ('336348', '1920', ''), + ('117667', '1867', ''), + ('101633', '1122', ''), + ('257431', '969', ''), + ('101959', '2026', ''), + ('787030', '2016', ''), + ('288691', '1931', ''), + ('640119', '923', ''), + ('609320', '2054', ''), + ('361595', '2044', ''), + ('455368', '2068', ''), + ('448345', '2051', ''), + ('846055', '2070', ''), + ('200518', '2079', ''), + ('416742', '2067', ''), + ('416742', '2071', ''), + ('270253', '2076', ''), + ('659436', '2111', ''), + ('562808', '2116', ''), + ('20262', '1633', ''), + ('817113', '2109', ''), + ('563156', '2139', ''), + ('781139', '1456', ''), + ('213971', '839', ''), + ('852649', '1092', ''), + ('409795', '985', ''), + ('409795', '2105', ''), + ('624161', '2149', ''), + ('624161', '2150', ''); + +COMMIT; \ No newline at end of file diff --git a/src/integrationTest/resources/db/testmigration/V1_83__judicial_service_code_mapping.sql b/src/integrationTest/resources/db/testmigration/V1_83__judicial_service_code_mapping.sql new file mode 100644 index 000000000..6256aefcf --- /dev/null +++ b/src/integrationTest/resources/db/testmigration/V1_83__judicial_service_code_mapping.sql @@ -0,0 +1,12 @@ +INSERT INTO dbjudicialdata.judicial_user_profile (personal_code,known_as,surname,full_name,post_nominals,ejudiciary_email,active_flag,created_date,last_loaded_date,object_id,sidam_id,initials) +VALUES ('123454','recc2Kb','recc2Sb','recc2Fb','Mr','recc531@test.net','TRUE','2021-07-14 12:25:28.763','2021-07-14 12:25:28.763','5f8b26ba-0c8b-4192-b5c7-311d73qwert','3333333','J.K'); + + +INSERT INTO dbjudicialdata.judicial_office_appointment (personal_code,base_location_id,hmcts_region_id,is_prinicple_appointment,start_date,end_date,created_date,last_loaded_date,epimms_id,appointment,appointment_type,appointment_id,role_name_id,"type",contract_type_id,"location",jo_base_location_id) VALUES + ('123454','1032','1',true,'1995-03-27', NULL,'2023-08-03 13:28:10.188406','2023-08-03 13:28:10.188432',NULL,'Magistrate','Voluntary','234','1','LJA','2','South East','1032'); + + INSERT INTO dbjudicialdata.judicial_office_authorisation + (judicial_office_auth_id, jurisdiction, start_date, end_date, created_date, + last_updated, lower_level, personal_code, ticket_code,authorisation_id,jurisdiction_id,appointment_id) + VALUES(2344, 'Authorisation Magistrate', '2002-09-09 00:00:00.000', NULL, + '2021-08-11 09:14:30.054', '2021-08-11 09:14:30.054', 'Family Court', '123454', '368',7,1,'234'); diff --git a/src/integrationTest/resources/wiremock_responses/sidamid_update_for_matched_objectid.json b/src/integrationTest/resources/wiremock_responses/sidamid_update_for_matched_objectid.json new file mode 100644 index 000000000..3a090701f --- /dev/null +++ b/src/integrationTest/resources/wiremock_responses/sidamid_update_for_matched_objectid.json @@ -0,0 +1,60 @@ +[ + { + "id": "6455c84c-e77d-4c4f-9759-bf4a93a8e972", + "forename": "Service", + "surname": "Account", + "email": "HHJ.Leslie.Jones@judiciarystagingtest999.onmicrosoft.com", + "active": true, + "locked": false, + "pending": true, + "stale": true, + "pwdAccountLockedTime": "string", + "roles": [ + "caseworker-privatelaw-systemupdate", + "caseworker-privatelaw", + "hearing-manager", + "hearing-viewer", + "jrd-admin", + "listed-hearing-viewer", + "idam-service-account", + "judge", + "caseworker", + "judiciary", + "jrd-system-user", + "caseworker-privatelaw-courtadmin" + ], + "ssoProvider": "string", + "ssoId": "5f8b26ba-0c8b-4192-b5c7-311d737f0cae", + "lastModified": "2023-01-17T13:15:36.435Z", + "createDate": "2022-12-02T11:54:55.212Z" + }, + { + "id": "6455c84c-e77d-4c4f-9759-bf4a93a8e974", + "forename": "Service", + "surname": "Account", + "email": "HHJ.Yuriko.Koiko@judiciarystaging13232.onmicrosoft.com", + "active": true, + "locked": false, + "pending": true, + "stale": true, + "pwdAccountLockedTime": "string", + "roles": [ + "caseworker-privatelaw-systemupdate", + "caseworker-privatelaw", + "hearing-manager", + "hearing-viewer", + "jrd-admin", + "listed-hearing-viewer", + "idam-service-account", + "judge", + "caseworker", + "judiciary", + "jrd-system-user", + "caseworker-privatelaw-courtadmin" + ], + "ssoProvider": "string", + "ssoId": "5f8b26ba-0c8b-4192-b5c7-311d737f0cab", + "lastModified": "2023-01-17T13:15:36.435Z", + "createDate": "2022-12-02T11:54:55.212Z" + } +] \ No newline at end of file diff --git a/src/integrationTest/resources/wiremock_responses/testpeople_withoutsidam.json b/src/integrationTest/resources/wiremock_responses/testpeople_withoutsidam.json new file mode 100644 index 000000000..9838b0cbc --- /dev/null +++ b/src/integrationTest/resources/wiremock_responses/testpeople_withoutsidam.json @@ -0,0 +1,83 @@ +{ + "pagination": { + "results": 23376, + "pages": 468, + "current_page": 20, + "results_per_page": 50, + "more_pages": false + }, + "results": [ + { + "id": "5f8b26ba-0c8b-4192-b5c7-311d737f0cae", + "per_id": 37992, + "personal_code": "123454", + "title": "District Judge", + "known_as": "Rachel", + "surname": "Jones", + "fullname": "District Judge Rachel Jones", + "initials": "RJ", + "post_nominals": null, + "email": "DJ.Rachel.Jones@ejudiciary.net", + "sex": "female", + "work_phone": "07965 544394", + "disability": false, + "retirement_date": "2026-07-23", + "leaving_on": "2026-07-23", + "appointments": [ + { + "appointment_id": 453355, + "role_name": "District Judge", + "role_name_id": 45, + "type": "Courts", + "court_name": "Court of Protection", + "court_type": null, + "circuit": "London", + "bench": null, + "advisory_committee_area": null, + "location": "London", + "base_location": "Court of Protection", + "base_location_id": 768, + "is_principal": true, + "start_date": "2010-03-23", + "end_date": "2026-07-23", + "contract_type": "SPTW-50%", + "contract_type_id": 5 + } + ], + "judiciary_roles": [ + { + "name": "Course Director for COP (JC)", + "judiciary_role_id": 427, + "start_date": "2016-04-30T00:00:00.000Z", + "end_date": "2019-04-30T00:00:00.000Z" + } + ], + "authorisations": [ + { + "jurisdiction": "Family", + "tickets": [ + "Court of Protection" + ] + }, + { + "jurisdiction": "Tribunals", + "tickets": [ + "Criminal Injuries Compensations" + ] + } + ], + "authorisations_with_dates": [ + { + "authorisation_id": 9918, + "appointment_id": 453355, + "jurisdiction": "Family", + "jurisdiction_id": 26, + "ticket": "Court of Protection", + "ticket_id": 313, + "start_date": "2017-03-24", + "end_date": "2022-07-23" + } + ] + } + ] +} \ No newline at end of file diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/domain/ElinkDataExceptionRecords.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/domain/ElinkDataExceptionRecords.java index 54f22ff57..e4828e351 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/domain/ElinkDataExceptionRecords.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/domain/ElinkDataExceptionRecords.java @@ -55,4 +55,7 @@ public class ElinkDataExceptionRecords implements Serializable { @Column(name = "row_id") private String rowId; + @Column(name = "page_id") + private int pageId; + } \ No newline at end of file diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/repository/DataloadSchedularAuditRepository.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/repository/DataloadSchedularAuditRepository.java index e4ffe6cfd..94f3bfabc 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/repository/DataloadSchedularAuditRepository.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/repository/DataloadSchedularAuditRepository.java @@ -19,7 +19,7 @@ public interface DataloadSchedularAuditRepository extends JpaRepository locationResponse = retrieveLocationDetails(); } catch(Exception ex) { - log.info("ElinksApiJobScheduler.loadElinksData Job execution completed failure for Location Response"); - elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, - now(), - now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),LOCATIONAPI); + log.error("ElinksApiJobScheduler.loadElinksData Job execution completed failure for Location Response"); + if (ex instanceof HttpClientErrorException) + { + HttpClientErrorException exception=(HttpClientErrorException)ex; + if (exception.getRawStatusCode()==403 && exception.getMessage() + .contains("jrd-elinks-location".concat(SPACE).concat(FORBIDDEN_EXCEPTION_LD))) + { + + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + now(), + now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),LOCATIONAPI); + } + } } try{ ResponseEntity peopleResponse = retrievePeopleDetails(); } catch(Exception ex) { - elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, - now(), - now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),PEOPLEAPI); + if (ex instanceof HttpClientErrorException) + { + HttpClientErrorException exception=(HttpClientErrorException)ex; + if (exception.getRawStatusCode()==403 && exception.getMessage() + .contains("jrd-elinks-load-people".concat(SPACE).concat(FORBIDDEN_EXCEPTION_LD))) + { + + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + now(), + now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),PEOPLEAPI); + } + } } try{ ResponseEntity leaversResponse = retrieveLeaversDetails(); } catch(Exception ex) { - log.info("ElinksApiJobScheduler.loadElinksData Job execution completed failure for Leavers Response"); - elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, - now(), - now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),LEAVERSAPI); + log.error("ElinksApiJobScheduler.loadElinksData Job execution completed failure for Leavers Response"); + if (ex instanceof HttpClientErrorException) + { + HttpClientErrorException exception=(HttpClientErrorException)ex; + if (exception.getRawStatusCode()==403 && exception.getMessage() + .contains("jrd-elinks-leavers".concat(SPACE).concat(FORBIDDEN_EXCEPTION_LD))) + { + + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + now(), + now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),LEAVERSAPI); + } + } } try{ ResponseEntity deletedResponse = retrieveDeletedDetails(); } catch(Exception ex) { - log.info("ElinksApiJobScheduler.loadElinksData Job execution completed failure for Deleted Response"); + log.error("ElinksApiJobScheduler.loadElinksData Job execution completed failure for Deleted Response"); + if (ex instanceof HttpClientErrorException) + { + HttpClientErrorException exception=(HttpClientErrorException)ex; + if (exception.getRawStatusCode()==403 && exception.getMessage() + .contains("jrd-elinks-load-deleted".concat(SPACE).concat(FORBIDDEN_EXCEPTION_LD))) + { + + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + now(), + now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),DELETEDAPI); + } + } } try{ ResponseEntity idamSearchResponse = retrieveIdamElasticSearchDetails(); } catch(Exception ex) { - log.info("ElinksApiJobScheduler.loadElinksData Job execution completed failure for idamSearch Response"); - elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, - now(), - now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),IDAMSEARCH); + log.warn("ElinksApiJobScheduler.loadElinksData Job execution completed failure for idamSearch Response"); + if (ex instanceof HttpClientErrorException) + { + HttpClientErrorException exception=(HttpClientErrorException)ex; + if (exception.getRawStatusCode()==403 && exception.getMessage() + .contains("jrd-elinks-idam-elastic-search".concat(SPACE).concat(FORBIDDEN_EXCEPTION_LD))) + { + + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + now(), + now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),ELASTICSEARCH); + } + } } try{ ResponseEntity schedulerResponse = retrieveAsbPublishDetails(); } catch(Exception ex) { - log.info("ElinksApiJobScheduler.loadElinksData Job execution completed failure for Publish ASB Response"); - elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, - now(), - now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),PUBLISHASB); + log.warn("ElinksApiJobScheduler.loadElinksData Job execution completed failure for Publish ASB Response"); + jdbcTemplate.update(UPDATE_JOB_SQL, FAILED.getStatus(), + dataloadSchedulerJobRepository.findFirstByOrderByIdDesc().getId()); + + if (ex instanceof HttpClientErrorException) + { + HttpClientErrorException exception=(HttpClientErrorException)ex; + if (exception.getRawStatusCode()==403 && exception.getMessage() + .contains("jrd-elinks-publish-service-bus".concat(SPACE).concat(FORBIDDEN_EXCEPTION_LD))) + { + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + now(), + now(),RefDataElinksConstants.JobStatus.FAILED.getStatus(),PUBLISHSIDAM); + } + } } } diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/IdamElasticSearchService.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/IdamElasticSearchService.java index 5a093e82b..c3666a316 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/IdamElasticSearchService.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/IdamElasticSearchService.java @@ -2,9 +2,11 @@ import org.springframework.http.ResponseEntity; +import java.time.LocalDateTime; + public interface IdamElasticSearchService { - String getIdamBearerToken(); + String getIdamBearerToken(LocalDateTime schedulerStartTime); ResponseEntity getIdamElasticSearchSyncFeed(); } diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksServiceImpl.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksServiceImpl.java index ea5bfd815..680529f1c 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksServiceImpl.java @@ -4,6 +4,7 @@ import feign.Response; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.tuple.Triple; +import org.json.JSONException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.DataAccessException; @@ -135,13 +136,19 @@ public ResponseEntity retrieveLocation() { (ElinkBaseLocationResponse) responseEntity.getBody(); if (nonNull(responseEntity.getBody())) { - if (nonNull(elinkBaseLocationResponse)) { + if (nonNull(elinkBaseLocationResponse) && elinkBaseLocationResponse.getResults().size() > 0) { List baseLocationResponses = elinkBaseLocationResponse.getResults(); List baselocations = baseLocationResponses.stream() .map(BaseLocationResponse::toBaseLocationEntity) .toList(); result = loadBaseLocationData(baselocations); + } else { + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + now(), + RefDataElinksConstants.JobStatus.FAILED.getStatus(), LOCATIONAPI); + throw new ElinksException(HttpStatus.FORBIDDEN, ELINKS_ACCESS_ERROR, ELINKS_ACCESS_ERROR); } } else { elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, @@ -164,8 +171,19 @@ public ResponseEntity retrieveLocation() { } catch (FeignException ex) { throw new ElinksException(HttpStatus.FORBIDDEN, ELINKS_ACCESS_ERROR, ELINKS_ACCESS_ERROR); + } catch (JSONException ex) { + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + now(), + RefDataElinksConstants.JobStatus.FAILED.getStatus(), LOCATIONAPI); + throw new ElinksException(HttpStatus.FORBIDDEN, ELINKS_ACCESS_ERROR, ELINKS_ACCESS_ERROR); + } catch (Exception ex) { + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + now(), + RefDataElinksConstants.JobStatus.FAILED.getStatus(), LOCATIONAPI); + throw ex; } - elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, schedulerStartTime, now(), diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleDeleteServiceimpl.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleDeleteServiceimpl.java index e20d4719f..c093af932 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleDeleteServiceimpl.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleDeleteServiceimpl.java @@ -35,7 +35,6 @@ public class ElinksPeopleDeleteServiceimpl implements ELinksPeopleDeleteService @Override @Transactional(propagation = Propagation.REQUIRED) public void deleteAuth(ResultsRequest resultsRequest) { - log.info("entering into deleteAuth : "); authorisationsRepository.deleteByPersonalCode(resultsRequest.getPersonalCode()); appointmentsRepository.deleteByPersonalCode(resultsRequest.getPersonalCode()); judicialRoleTypeRepository.deleteByPersonalCode(resultsRequest.getPersonalCode()); diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleServiceImpl.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleServiceImpl.java index 6ead73346..5430b6d4a 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleServiceImpl.java @@ -61,7 +61,6 @@ import static java.time.LocalDateTime.now; import static java.util.Objects.isNull; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.APPOINTMENTID; -import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.APPOINTMENTIDFAILURE; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.APPOINTMENTIDNOTAVAILABLE; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.APPOINTMENTID_IS_NULL; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.APPOINTMENT_TABLE; @@ -86,6 +85,7 @@ import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.LOCATION; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.LOCATIONIDFAILURE; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.LOCATION_ID; +import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.OBJECTID; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.OBJECTIDISDUPLICATED; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.OBJECTIDISPRESENT; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.PEOPLEAPI; @@ -175,9 +175,15 @@ public class ElinksPeopleServiceImpl implements ElinksPeopleService { @Value("${elinks.people.threadRetriggerPauseTime}") private String threadRetriggerPauseTime; + @Value("${elinks.people.retriggerStatus}") + private List retriggerStatusCode; + @Value("${elinks.people.page}") private String page; + @Value("${elinks.people.retriggerThreshold}") + private int retriggerThreshold; + @Value("${elinks.people.includePreviousAppointments}") private String includePreviousAppointments; @@ -206,37 +212,48 @@ public ResponseEntity updatePeople() { RefDataElinksConstants.JobStatus.IN_PROGRESS.getStatus(), PEOPLEAPI); userProfilesSnapshot = profileRepository.findAll(); int pageValue = Integer.parseInt(page); - do { - Response peopleApiResponse = getPeopleResponseFromElinks(pageValue++, schedulerStartTime); - httpStatus = HttpStatus.valueOf(peopleApiResponse.status()); - ResponseEntity responseEntity; - - if (httpStatus.is2xxSuccessful()) { - responseEntity = JsonFeignResponseUtil.toResponseEntity(peopleApiResponse, PeopleRequest.class); - PeopleRequest elinkPeopleResponseRequest = (PeopleRequest) responseEntity.getBody(); - if (Optional.ofNullable(elinkPeopleResponseRequest).isPresent() + int retryCount = 0; + try { + do { + Response peopleApiResponse = getPeopleResponseFromElinks(pageValue++, schedulerStartTime); + httpStatus = HttpStatus.valueOf(peopleApiResponse.status()); + ResponseEntity responseEntity; + + if (httpStatus.is2xxSuccessful()) { + responseEntity = JsonFeignResponseUtil.toResponseEntity(peopleApiResponse, PeopleRequest.class); + PeopleRequest elinkPeopleResponseRequest = (PeopleRequest) responseEntity.getBody(); + if (Optional.ofNullable(elinkPeopleResponseRequest).isPresent() && Optional.ofNullable(elinkPeopleResponseRequest.getPagination()).isPresent() && Optional.ofNullable(elinkPeopleResponseRequest.getResultsRequests()).isPresent()) { - isMorePagesAvailable = elinkPeopleResponseRequest.getPagination().getMorePages(); - processPeopleResponse(elinkPeopleResponseRequest, schedulerStartTime,pageValue); + isMorePagesAvailable = elinkPeopleResponseRequest.getPagination().getMorePages(); + processPeopleResponse(elinkPeopleResponseRequest, schedulerStartTime, pageValue); + } else { + auditStatus(schedulerStartTime, RefDataElinksConstants.JobStatus.FAILED.getStatus()); + throw new ElinksException(HttpStatus.FORBIDDEN, ELINKS_ACCESS_ERROR, + ELINKS_ACCESS_ERROR); + } + } else if (retriggerStatusCode.contains(httpStatus.value()) && retryCount < retriggerThreshold) { + log.info(":::: Too Many Requests "); + pauseThread(Long.valueOf(threadRetriggerPauseTime), schedulerStartTime); + --pageValue; + retryCount++; + continue; } else { auditStatus(schedulerStartTime, RefDataElinksConstants.JobStatus.FAILED.getStatus()); - throw new ElinksException(HttpStatus.FORBIDDEN, ELINKS_ACCESS_ERROR, ELINKS_ACCESS_ERROR); + handleELinksErrorResponse(httpStatus); } - } else if (HttpStatus.TOO_MANY_REQUESTS.value() == httpStatus.value()) { - log.info(":::: Too Many Requests "); - pauseThread(Long.valueOf(threadRetriggerPauseTime),schedulerStartTime); - --pageValue; - continue; - } else { - auditStatus(schedulerStartTime, RefDataElinksConstants.JobStatus.FAILED.getStatus()); - handleELinksErrorResponse(httpStatus); - } - pauseThread(Long.valueOf(threadPauseTime),schedulerStartTime); - } while (isMorePagesAvailable); - - sendEmail(schedulerStartTime); - + pauseThread(Long.valueOf(threadPauseTime), schedulerStartTime); + } while (isMorePagesAvailable); + } catch (Exception ex) { + auditStatus(schedulerStartTime, RefDataElinksConstants.JobStatus.FAILED.getStatus()); + throw ex; + } + List list = elinkDataExceptionRepository + .findBySchedulerStartTime(schedulerStartTime); + if (!list.isEmpty()) { + sendEmail(new HashSet<>(list), "baselocation", + LocalDate.now().format(DateTimeFormatter.ofPattern(DATE_PATTERN))); + } if (partialSuccessFlag) { status = RefDataElinksConstants.JobStatus.PARTIAL_SUCCESS.getStatus(); } @@ -365,20 +382,20 @@ private void savePeopleDetails( .getObjectId(), resultsRequest.getAppointmentsRequests(),schedulerStartTime,pageValue); saveAuthorizationDetails(resultsRequest.getPersonalCode(), resultsRequest .getObjectId(), resultsRequest.getAuthorisationsRequests(),schedulerStartTime,pageValue); - saveRoleDetails(resultsRequest.getPersonalCode(), resultsRequest.getJudiciaryRoles()); + saveRoleDetails(resultsRequest.getPersonalCode(), resultsRequest.getJudiciaryRoles(),pageValue); } catch (Exception exception) { log.warn("saveUserProfile is failed " + resultsRequest.getPersonalCode()); partialSuccessFlag = true; elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, now(), resultsRequest.getPersonalCode(), - PERSONALCODE, exception.getMessage(), USER_PROFILE,resultsRequest.getPersonalCode()); + PERSONALCODE, exception.getMessage(), USER_PROFILE,resultsRequest.getPersonalCode(),pageValue); } } } - private void saveRoleDetails(String personalCode, List judiciaryRoles) { + private void saveRoleDetails(String personalCode, List judiciaryRoles, int pageValue) { for (RoleRequest roleRequest: judiciaryRoles) { @@ -396,7 +413,7 @@ private void saveRoleDetails(String personalCode, List judiciaryRol elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, now(), personalCode, - JUDICIALROLETYPE, e.getMessage(), JUDICIALROLETYPE,personalCode); + JUDICIALROLETYPE, e.getMessage(), JUDICIALROLETYPE,personalCode,pageValue); } } @@ -404,9 +421,17 @@ private void saveRoleDetails(String personalCode, List judiciaryRol private boolean saveUserProfile(ResultsRequest resultsRequest,LocalDateTime schedulerStartTime, int pageValue) { - + if (validateUserProfile(resultsRequest, schedulerStartTime,pageValue)) { try { + LocalDateTime createdOn = null; + String sidamId = null; + if (personalCodePresentInDb(resultsRequest).isEmpty()) { + createdOn = now(); + } else { + createdOn = personalCodePresentInDb(resultsRequest).get(0).getCreatedDate(); + sidamId = personalCodePresentInDb(resultsRequest).get(0).getSidamId(); + } UserProfile userProfile = UserProfile.builder() .personalCode(resultsRequest.getPersonalCode()) .knownAs(resultsRequest.getKnownAs()) @@ -416,11 +441,12 @@ private boolean saveUserProfile(ResultsRequest resultsRequest,LocalDateTime sche .emailId(resultsRequest.getEmail()) .lastWorkingDate(convertToLocalDate(resultsRequest.getLastWorkingDate())) .activeFlag(true) - .createdDate(now()) - .lastLoadedDate(now()) + .sidamId(sidamId) + .createdDate(createdOn) .objectId(resultsRequest.getObjectId()) .initials(resultsRequest.getInitials()) .title(resultsRequest.getTitle()) + .lastLoadedDate(now()) .retirementDate(convertToLocalDate(resultsRequest.getRetirementDate())) .build(); userProfileCache.put(resultsRequest.getPersonalCode(),userProfile); @@ -431,11 +457,11 @@ private boolean saveUserProfile(ResultsRequest resultsRequest,LocalDateTime sche partialSuccessFlag = true; String personalCode = resultsRequest.getPersonalCode(); String errorDescription = appendFieldWithErrorDescription( - USERPROFILEFAILURE, resultsRequest.getPersonalCode(),pageValue); + USERPROFILEFAILURE, resultsRequest.getPersonalCode()); elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, now(), resultsRequest.getPersonalCode(), - USER_PROFILE, errorDescription, USER_PROFILE,personalCode); + PERSONALCODE, errorDescription, USER_PROFILE,personalCode,pageValue); return false; } } @@ -448,22 +474,22 @@ private boolean validateUserProfile(ResultsRequest resultsRequest,LocalDateTime log.warn("Mapped Base location not found in base table " + resultsRequest.getPersonalCode()); partialSuccessFlag = true; String errorField = resultsRequest.getPersonalCode(); - String errorDescription = appendFieldWithErrorDescription(USERPROFILEEMAILID, errorField, pageValue); + String errorDescription = appendFieldWithErrorDescription(USERPROFILEEMAILID, errorField); elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, now(), resultsRequest.getPersonalCode(), - EMAILID, errorDescription, USER_PROFILE,resultsRequest.getPersonalCode()); + EMAILID, errorDescription, USER_PROFILE,resultsRequest.getPersonalCode(),pageValue); return false; } else if (!isNull(userProfileCache.get(resultsRequest.getPersonalCode()))) { log.warn("User Profile not loaded for " + resultsRequest.getPersonalCode()); partialSuccessFlag = true; String errorDescription = appendFieldWithErrorDescription( - USERPROFILEISPRESENT, resultsRequest.getPersonalCode(), pageValue); + USERPROFILEISPRESENT, resultsRequest.getPersonalCode()); String personalCode = resultsRequest.getPersonalCode(); elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, schedulerStartTime, resultsRequest.getPersonalCode(), - USER_PROFILE,errorDescription, USER_PROFILE,personalCode); + PERSONALCODE,errorDescription, USER_PROFILE,personalCode,pageValue); return false; } else if (!isNull(resultsRequest.getObjectId()) && !resultsRequest.getObjectId().isEmpty() && !objectIdisPresent(resultsRequest).isEmpty()) { @@ -473,7 +499,7 @@ private boolean validateUserProfile(ResultsRequest resultsRequest,LocalDateTime elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, schedulerStartTime, resultsRequest.getObjectId(), - USER_PROFILE,OBJECTIDISDUPLICATED, USER_PROFILE,personalCode); + OBJECTID,OBJECTIDISDUPLICATED, USER_PROFILE,personalCode,pageValue); return false; } else if (!isNull(resultsRequest.getObjectId()) && !resultsRequest.getObjectId().isEmpty() && objectIdisPresentInDb(resultsRequest)) { @@ -483,7 +509,7 @@ private boolean validateUserProfile(ResultsRequest resultsRequest,LocalDateTime elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, schedulerStartTime, resultsRequest.getPersonalCode(), - USER_PROFILE,OBJECTIDISPRESENT, USER_PROFILE,personalCode); + OBJECTID,OBJECTIDISPRESENT, USER_PROFILE,personalCode,pageValue); return false; } return true; @@ -495,6 +521,13 @@ private boolean objectIdisPresentInDb(ResultsRequest resultsRequest) { && !resultsRequest.getPersonalCode().equals(userProfile.getPersonalCode())); } + private List personalCodePresentInDb(ResultsRequest resultsRequest) { + return userProfilesSnapshot.stream() + .filter(userProfile -> resultsRequest.getPersonalCode() + .equals(userProfile.getPersonalCode())) + .toList(); + } + private List objectIdisPresent(ResultsRequest resultsRequest) { return userProfileCache.entrySet().stream().filter(userProfileEntry -> resultsRequest .getObjectId().equals(userProfileEntry.getValue().getObjectId())) @@ -538,12 +571,11 @@ private void saveAppointmentDetails(String personalCode, String objectId, } catch (Exception e) { log.warn("failed to load appointment details for " + appointmentsRequest.getAppointmentId()); partialSuccessFlag = true; - String errorDescription = appendFieldWithErrorDescription( - APPOINTMENTIDFAILURE, appointmentsRequest.getAppointmentId(), pageValue); + String errorDescription = appendFieldWithErrorDescription(LOCATIONIDFAILURE, baseLocationId); elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, now(), appointmentsRequest.getAppointmentId(), - APPOINTMENT_TABLE, errorDescription, APPOINTMENT_TABLE,personalCode); + BASE_LOCATION_ID, errorDescription, APPOINTMENT_TABLE,personalCode,pageValue); } } } @@ -577,12 +609,12 @@ private void saveAuthorizationDetails(String personalCode, String objectId, errorDescription = APPOINTMENTID_IS_NULL; } else { errorDescription = appendFieldWithErrorDescription( - APPOINTMENTIDNOTAVAILABLE, authorisationsRequest.getAppointmentId(), pageValue); + APPOINTMENTIDNOTAVAILABLE, authorisationsRequest.getAppointmentId()); } elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, schedulerStartTime, authorisationsRequest.getAuthorisationId(), - APPOINTMENTID, errorDescription, AUTHORISATION_TABLE,personalCode); + APPOINTMENTID, errorDescription, AUTHORISATION_TABLE,personalCode,pageValue); } } } @@ -603,31 +635,31 @@ private boolean validAppointments(AppointmentsRequest appointmentsRequest, Strin log.warn("Mapped Base location not found in base table " + appointmentsRequest.getBaseLocationId()); partialSuccessFlag = true; String baseLocationId = appointmentsRequest.getBaseLocationId(); - String errorDescription = appendFieldWithErrorDescription(LOCATIONIDFAILURE, baseLocationId, pageValue); + String errorDescription = appendFieldWithErrorDescription(LOCATIONIDFAILURE, baseLocationId); elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, schedulerStartTime, appointmentsRequest.getAppointmentId(), - BASE_LOCATION_ID, errorDescription, APPOINTMENT_TABLE,personalCode); + BASE_LOCATION_ID, errorDescription, APPOINTMENT_TABLE,personalCode,pageValue); return false; } else if (StringUtils.isEmpty(fetchRegionId(appointmentsRequest.getLocation()))) { log.warn("Mapped location not found in jrd lrd mapping table " + appointmentsRequest.getLocation()); partialSuccessFlag = true; String location = appointmentsRequest.getLocation(); - String errorDescription = appendFieldWithErrorDescription(CFTREGIONIDFAILURE, location, pageValue); + String errorDescription = appendFieldWithErrorDescription(CFTREGIONIDFAILURE, location); elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, schedulerStartTime, appointmentsRequest.getAppointmentId(), - LOCATION, errorDescription, APPOINTMENT_TABLE,personalCode); + LOCATION, errorDescription, APPOINTMENT_TABLE,personalCode,pageValue); return false; } else if (INVALID_ROLES.contains(appointmentsRequest.getRoleName())) { log.warn("Role Name is Invalid " + appointmentsRequest.getRoleName()); partialSuccessFlag = true; String errorDescription = appendFieldWithErrorDescription(INVALIDROLENAMES, - appointmentsRequest.getRoleName(), pageValue); + appointmentsRequest.getRoleName()); elinkDataExceptionHelper.auditException(JUDICIAL_REF_DATA_ELINKS, now(), appointmentsRequest.getAppointmentId(), - ROLENAME, errorDescription, APPOINTMENT_TABLE,personalCode); + ROLENAME, errorDescription, APPOINTMENT_TABLE,personalCode,pageValue); return false; } return true; @@ -658,16 +690,15 @@ private String fetchRegionId(String location) { } // Append the string to add error description for the given format - private String appendFieldWithErrorDescription(String errorDescription, String wordToAppend, int pageValue) { + private String appendFieldWithErrorDescription(String errorDescription, String wordToAppend) { String wordAfterWhichAppend = ":"; - String wordPageNumber = "PageNumber:"; - return wordPageNumber.concat(String.valueOf(pageValue)).concat("-").concat(errorDescription.substring(0, + return errorDescription.substring(0, errorDescription.indexOf(wordAfterWhichAppend) + wordAfterWhichAppend.length()) + " " + wordToAppend + " " + errorDescription.substring(errorDescription.indexOf(wordAfterWhichAppend) - + wordAfterWhichAppend.length(), errorDescription.length())); + + wordAfterWhichAppend.length(), errorDescription.length()); } diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/IdamElasticSearchServiceImpl.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/IdamElasticSearchServiceImpl.java index ec5fb3099..0f9175412 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/IdamElasticSearchServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/IdamElasticSearchServiceImpl.java @@ -19,6 +19,8 @@ import uk.gov.hmcts.reform.judicialapi.elinks.response.IdamOpenIdTokenResponse; import uk.gov.hmcts.reform.judicialapi.elinks.response.IdamResponse; import uk.gov.hmcts.reform.judicialapi.elinks.service.IdamElasticSearchService; +import uk.gov.hmcts.reform.judicialapi.elinks.util.ElinkDataIngestionSchedularAudit; +import uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants; import uk.gov.hmcts.reform.judicialapi.elinks.util.SqlConstants; import uk.gov.hmcts.reform.judicialapi.util.JsonFeignResponseUtil; @@ -35,9 +37,12 @@ import java.util.Map; import java.util.Set; +import static java.time.LocalDateTime.now; import static java.util.Objects.nonNull; +import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.ELASTICSEARCH; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.IDAM_ERROR_MESSAGE; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.IDAM_TOKEN_ERROR_MESSAGE; +import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.JUDICIAL_REF_DATA_ELINKS; @Slf4j @Component @@ -61,8 +66,11 @@ public class IdamElasticSearchServiceImpl implements IdamElasticSearchService { @Autowired JdbcTemplate jdbcTemplate; + @Autowired + ElinkDataIngestionSchedularAudit elinkDataIngestionSchedularAudit; + @Override - public String getIdamBearerToken() { + public String getIdamBearerToken(LocalDateTime schedulerStartTime) { IdamOpenIdTokenResponse idamOpenIdTokenResponse = null; try { @@ -86,6 +94,10 @@ public String getIdamBearerToken() { IDAM_TOKEN_ERROR_MESSAGE); } } catch (Exception e) { + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + now(), + RefDataElinksConstants.JobStatus.FAILED.getStatus(),ELASTICSEARCH); throw new ElinksException(HttpStatus.FORBIDDEN, IDAM_TOKEN_ERROR_MESSAGE, IDAM_TOKEN_ERROR_MESSAGE); } @@ -105,12 +117,18 @@ public ResponseEntity getIdamElasticSearchSyncFeed() { ResponseEntity responseEntity = null; Response response = null; + LocalDateTime schedulerStartTime = now(); + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + null, + RefDataElinksConstants.JobStatus.IN_PROGRESS.getStatus(), ELASTICSEARCH); + do { params.put("page", String.valueOf(count)); - String bearerToken = "Bearer ".concat(getIdamBearerToken()); - response = idamFeignClient.getUserFeed(bearerToken, params); - logIdamResponses(response); try { + String bearerToken = "Bearer ".concat(getIdamBearerToken(schedulerStartTime)); + response = idamFeignClient.getUserFeed(bearerToken, params); + logIdamResponses(response); responseEntity = JsonFeignResponseUtil.toResponseEntity(response, new TypeReference>() { }); @@ -135,6 +153,10 @@ public ResponseEntity getIdamElasticSearchSyncFeed() { } catch (Exception ex) { //There is No header. log.error("{}:: X-Total-Count header not return Idam Search Service::{}", loggingComponentName, ex); + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + now(), + RefDataElinksConstants.JobStatus.FAILED.getStatus(), ELASTICSEARCH); throw new ElinksException(HttpStatus.valueOf(response.status()), ex.getMessage(), IDAM_ERROR_MESSAGE); } @@ -142,6 +164,10 @@ public ResponseEntity getIdamElasticSearchSyncFeed() { log.debug("{}:: batch count :: ", count); } while (totalCount > 0 && recordsPerPage * count < totalCount); updateSidamIds(judicialUsers); + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + now(), + RefDataElinksConstants.JobStatus.SUCCESS.getStatus(), ELASTICSEARCH); return ResponseEntity .status(response.status()) @@ -173,7 +199,7 @@ public void updateSidamIds(Set sidamUsers) { List> sidamObjectId = new ArrayList<>(); String updateSidamIds = "UPDATE dbjudicialdata.judicial_user_profile SET sidam_id = ? " - + "WHERE object_id = ? AND (sidam_id IS NULL OR sidam_id <> ' ')"; + + "WHERE object_id = ? "; sidamUsers.stream().filter(user -> nonNull(user.getSsoId())).forEach(s -> sidamObjectId.add(Pair.of(s.getId(), s.getSsoId()))); log.debug("Insert Query batch Response from IDAM " + sidamObjectId.size()); @@ -188,6 +214,5 @@ public void setValues(PreparedStatement ps, Pair argument) throw } }); - } } \ No newline at end of file diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/PublishSidamIdServiceImpl.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/PublishSidamIdServiceImpl.java index 5e1862179..c1c27def5 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/PublishSidamIdServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/PublishSidamIdServiceImpl.java @@ -11,16 +11,21 @@ import org.springframework.stereotype.Component; import uk.gov.hmcts.reform.judicialapi.elinks.configuration.ElinkEmailConfiguration; import uk.gov.hmcts.reform.judicialapi.elinks.exception.ElinksException; +import uk.gov.hmcts.reform.judicialapi.elinks.repository.DataloadSchedularAuditRepository; import uk.gov.hmcts.reform.judicialapi.elinks.response.SchedulerJobStatusResponse; import uk.gov.hmcts.reform.judicialapi.elinks.service.IEmailService; import uk.gov.hmcts.reform.judicialapi.elinks.service.PublishSidamIdService; import uk.gov.hmcts.reform.judicialapi.elinks.service.dto.Email; import uk.gov.hmcts.reform.judicialapi.elinks.servicebus.ElinkTopicPublisher; +import uk.gov.hmcts.reform.judicialapi.elinks.util.ElinkDataIngestionSchedularAudit; import uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataConstants; +import uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; +import static java.time.LocalDateTime.now; import static org.apache.commons.lang3.ObjectUtils.isNotEmpty; import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.springframework.util.CollectionUtils.isEmpty; @@ -30,6 +35,8 @@ import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataConstants.CONTENT_TYPE_PLAIN; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.DATABASE_FETCH_ERROR; import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.JOB_DETAILS_UPDATE_ERROR; +import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.JUDICIAL_REF_DATA_ELINKS; +import static uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants.PUBLISHSIDAM; import static uk.gov.hmcts.reform.judicialapi.elinks.util.SqlContants.GET_DISTINCT_SIDAM_ID; import static uk.gov.hmcts.reform.judicialapi.elinks.util.SqlContants.SELECT_JOB_STATUS_SQL; import static uk.gov.hmcts.reform.judicialapi.elinks.util.SqlContants.UPDATE_JOB_SQL; @@ -56,6 +63,12 @@ public class PublishSidamIdServiceImpl implements PublishSidamIdService { @Autowired ElinkEmailConfiguration emailConfiguration; + @Autowired + ElinkDataIngestionSchedularAudit elinkDataIngestionSchedularAudit; + + @Autowired + private DataloadSchedularAuditRepository dataloadSchedularAuditRepository; + @Value("${launchdarkly.sdk.environment}") String environment; @@ -65,6 +78,11 @@ public ResponseEntity publishSidamIdToAsb() { //Get the job details from dataload_schedular_job table Pair jobDetails; + LocalDateTime schedulerStartTime = now(); + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + null, + RefDataElinksConstants.JobStatus.IN_PROGRESS.getStatus(), PUBLISHSIDAM); try { jobDetails = getJobDetails(SELECT_JOB_STATUS_SQL); } catch (Exception ex) { @@ -80,10 +98,10 @@ public ResponseEntity publishSidamIdToAsb() { if (isEmpty(sidamIds)) { log.warn("{}:: No Sidam id exists in JRD for publishing in ASB for JOB id: {} ", logComponentName, jobDetails.getLeft()); - updateAsbStatus(jobDetails.getLeft(), SUCCESS.getStatus()); + updateAsbStatus(jobDetails.getLeft(), SUCCESS.getStatus(),schedulerStartTime); } - publishMessage(jobDetails.getRight(), sidamIds, jobDetails.getLeft()); + publishMessage(jobDetails.getRight(), sidamIds, jobDetails.getLeft(),schedulerStartTime); jobDetails = getJobDetails(SELECT_JOB_STATUS_SQL); log.info("{}:: completed Publish SidamId to ASB with JOB Id: {} ", logComponentName, jobDetails.getLeft()); @@ -91,6 +109,10 @@ public ResponseEntity publishSidamIdToAsb() { SchedulerJobStatusResponse response = SchedulerJobStatusResponse.builder().id(jobDetails.getLeft()) .jobStatus(jobDetails.getRight()).sidamIdsCount(sidamIdcount).statusCode(HttpStatus.OK.value()).build(); + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + now(), + RefDataElinksConstants.JobStatus.SUCCESS.getStatus(), PUBLISHSIDAM); return ResponseEntity.status(HttpStatus.OK).body(response); } @@ -114,13 +136,14 @@ private Optional> getElinkJobStatus(String jobStatusQuery) } - public void publishMessage(String status, List sidamIds, String jobId) { + public void publishMessage(String status, List sidamIds, String jobId, + LocalDateTime schedulerStartTime) { try { if ((IN_PROGRESS.getStatus().equals(status)) && isNotEmpty(sidamIds)) { //Publish or retry Message in ASB log.info("{}:: Publishing/Retrying JRD messages in ASB for Job Id {}", logComponentName, jobId); elinkTopicPublisher.sendMessage(sidamIds, jobId); - updateAsbStatus(jobId, SUCCESS.getStatus()); + updateAsbStatus(jobId, SUCCESS.getStatus(),schedulerStartTime); log.info("{}:: Updated Total distinct Sidam Ids to ASB: {}", logComponentName, sidamIdcount); } } catch (Exception ex) { @@ -128,7 +151,7 @@ public void publishMessage(String status, List sidamIds, String jobId) { ElinkEmailConfiguration.MailTypeConfig mailTypeConfig = emailConfiguration.getMailTypes().get("asb"); final String logMessage = String.format(mailTypeConfig.getSubject(), jobId); log.error("{}:: {}", logComponentName, logMessage); - updateAsbStatus(jobId, FAILED.getStatus()); + updateAsbStatus(jobId, FAILED.getStatus(),schedulerStartTime); if (mailTypeConfig.isEnabled()) { Email email = Email.builder() .contentType(CONTENT_TYPE_PLAIN) @@ -139,15 +162,23 @@ public void publishMessage(String status, List sidamIds, String jobId) { .build(); emailService.sendEmail(email); } + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + now(), + RefDataElinksConstants.JobStatus.FAILED.getStatus(), PUBLISHSIDAM); throw ex; } } - private void updateAsbStatus(String jobId,String jobStatus) { + private void updateAsbStatus(String jobId,String jobStatus,LocalDateTime schedulerStartTime) { //Update elinks DB with Publishing Status try { jdbcTemplate.update(UPDATE_JOB_SQL, jobStatus, Integer.valueOf(jobId)); } catch (Exception ex) { + elinkDataIngestionSchedularAudit.auditSchedulerStatus(JUDICIAL_REF_DATA_ELINKS, + schedulerStartTime, + now(), + RefDataElinksConstants.JobStatus.FAILED.getStatus(), PUBLISHSIDAM); throw new ElinksException(HttpStatus.FORBIDDEN, JOB_DETAILS_UPDATE_ERROR, JOB_DETAILS_UPDATE_ERROR); } diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/servicebus/ElinkTopicPublisher.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/servicebus/ElinkTopicPublisher.java index 761cd1803..ef57699bb 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/servicebus/ElinkTopicPublisher.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/servicebus/ElinkTopicPublisher.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Component; import uk.gov.hmcts.reform.judicialapi.elinks.configuration.PublishingData; import uk.gov.hmcts.reform.judicialapi.elinks.exception.ElinksException; +import uk.gov.hmcts.reform.judicialapi.elinks.util.ElinkDataIngestionSchedularAudit; import java.util.ArrayList; import java.util.List; @@ -32,6 +33,8 @@ public class ElinkTopicPublisher { @Value("${jrd.publisher.azure.service.bus.topic}") String topic; + @Autowired + ElinkDataIngestionSchedularAudit elinkDataIngestionSchedularAudit; @Autowired private ServiceBusSenderClient elinkserviceBusSenderClient; diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataExceptionHelper.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataExceptionHelper.java index 869d9e1f3..10bf345a5 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataExceptionHelper.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataExceptionHelper.java @@ -25,7 +25,7 @@ public class ElinkDataExceptionHelper { @Transactional(propagation = Propagation.REQUIRES_NEW) public void auditException(String schedulerName, LocalDateTime schedulerStartTime, String key, String fieldInError, String errorDescription, - String tableName,String personalCode) { + String tableName,String personalCode,int pageValue) { ElinkDataExceptionRecords audit = new ElinkDataExceptionRecords(); try { @@ -38,6 +38,7 @@ public void auditException(String schedulerName, LocalDateTime schedulerStartTim audit.setTableName(tableName); audit.setUpdatedTimeStamp(LocalDateTime.now()); audit.setRowId(personalCode); + audit.setPageId(pageValue); elinkDataExceptionRepository.save(audit); } catch (Exception e) { log.error("{}:: Failure error Message {} in auditSchedulerStatus {} ", diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataIngestionSchedularAudit.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataIngestionSchedularAudit.java index 0f3c44373..3ac3b2d41 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataIngestionSchedularAudit.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataIngestionSchedularAudit.java @@ -29,10 +29,17 @@ public void auditSchedulerStatus(String schedulerName, LocalDateTime schedulerSt ElinkDataSchedularAudit audit = new ElinkDataSchedularAudit(); try { - if (nonNull(schedulerEndTime)) { + if (nonNull(schedulerEndTime) && nonNull(schedulerStartTime)) { audit = elinkSchedularAuditRepository.findBySchedulerStartTime(schedulerStartTime); - audit.setSchedulerEndTime(schedulerEndTime); + if (nonNull(audit)) { + audit.setSchedulerEndTime(schedulerEndTime); + } else { + /*No Audit entry in Scheduler Audit*/ + audit = new ElinkDataSchedularAudit(); + audit.setSchedulerEndTime(schedulerEndTime); + } } + audit.setSchedulerEndTime(schedulerEndTime); audit.setSchedulerName(schedulerName); audit.setSchedulerStartTime(schedulerStartTime); audit.setStatus(status); diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/RefDataElinksConstants.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/RefDataElinksConstants.java index da26c5bbd..47969728c 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/RefDataElinksConstants.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/RefDataElinksConstants.java @@ -57,6 +57,10 @@ private RefDataElinksConstants() { public static final String LEAVERSAPI = "Leavers"; + public static final String ELASTICSEARCH = "ElasticSearch"; + + public static final String PUBLISHSIDAM = "PublishSidamIds"; + public static final String DELETEDAPI = "Deleted"; public static final String LOCATIONAPI = "Location"; @@ -124,6 +128,8 @@ private RefDataElinksConstants() { public static final String INVALIDROLENAMES = "Role Name : " + " is invalid"; + public static final String OBJECTID = "Object_id"; + public static final String USERPROFILEFAILURE = "UserProfile : " + FAIL_LOAD; diff --git a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/SqlContants.java b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/SqlContants.java index 6c694c78f..ba3bf67ce 100644 --- a/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/SqlContants.java +++ b/src/main/java/uk/gov/hmcts/reform/judicialapi/elinks/util/SqlContants.java @@ -16,5 +16,5 @@ private SqlContants(){ + "dbjudicialdata.judicial_user_profile WHERE sidam_id IS NOT NULL"; public static final String UPDATE_JOB_SQL = "UPDATE dbjudicialdata.dataload_schedular_job " - + "SET job_end_time = NOW() AT TIME ZONE 'utc', publishing_status = ? WHERE id =?"; + + "SET job_end_time = NOW() , publishing_status = ? WHERE id =?"; } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index a4ed25ec9..a05494b3f 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -153,6 +153,8 @@ elinks: includePreviousAppointments : ${INCLUDE_PREVIOUS_APPOINTMENT:false} threadPauseTime: ${THREAD_PAUSE_TIME:2000} threadRetriggerPauseTime: ${THREAD_RETRIGGER_TIME:1000} + retriggerStatus: ${RETRIGGER_STATUSCODE:503,429} + retriggerThreshold: ${RETRIGGER_THRESHOLD:5} scheduler: wrapperApiUrl: ${ELINKS_WRAPPER_API_URL:http://localhost:8093} cronExpression: ${CRON_EXPRESSION:* 55 15 * * *} diff --git a/src/main/resources/db/migration/V1_80__elinks_insert_judicial_location_mapping_tables.sql b/src/main/resources/db/migration/V1_80__elinks_insert_judicial_location_mapping_tables.sql new file mode 100644 index 000000000..477656684 --- /dev/null +++ b/src/main/resources/db/migration/V1_80__elinks_insert_judicial_location_mapping_tables.sql @@ -0,0 +1,37 @@ +-- insert dbjudicialdata.judicial_location_mapping +INSERT INTO dbjudicialdata.judicial_location_mapping (epimms_id, judicial_base_location_id, service_code) VALUES + ('450049', '866', ''), + ('774335', '1863', ''), + ('457273', '1915', ''), + ('457273', '1455', ''), + ('336348', '1920', ''), + ('117667', '1867', ''), + ('101633', '1122', ''), + ('257431', '969', ''), + ('101959', '2026', ''), + ('787030', '2016', ''), + ('288691', '1931', ''), + ('640119', '923', ''), + ('609320', '2054', ''), + ('361595', '2044', ''), + ('455368', '2068', ''), + ('448345', '2051', ''), + ('846055', '2070', ''), + ('200518', '2079', ''), + ('416742', '2067', ''), + ('416742', '2071', ''), + ('270253', '2076', ''), + ('659436', '2111', ''), + ('562808', '2116', ''), + ('20262', '1633', ''), + ('817113', '2109', ''), + ('563156', '2139', ''), + ('781139', '1456', ''), + ('213971', '839', ''), + ('852649', '1092', ''), + ('409795', '985', ''), + ('409795', '2105', ''), + ('624161', '2149', ''), + ('624161', '2150', ''); + +COMMIT; diff --git a/src/main/resources/db/migration/V1_81__elinks_dataload_exception_records.sql b/src/main/resources/db/migration/V1_81__elinks_dataload_exception_records.sql new file mode 100644 index 000000000..4921cbbeb --- /dev/null +++ b/src/main/resources/db/migration/V1_81__elinks_dataload_exception_records.sql @@ -0,0 +1 @@ +ALTER TABLE dbjudicialdata.dataload_exception_records ADD COLUMN page_id int8; \ No newline at end of file diff --git a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksBaseLocationServiceImplTest.java b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksBaseLocationServiceImplTest.java index d83c758c5..274ec7e73 100644 --- a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksBaseLocationServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksBaseLocationServiceImplTest.java @@ -95,6 +95,32 @@ void elinksService_load_location_should_return_sucess_msg_with_status_200() thro } + @Test + void elinksService_load_location_should_return_sucess_msg_with_empty_response() throws JsonProcessingException { + + + + ElinkBaseLocationResponse elinkBaseLocationResponse = new ElinkBaseLocationResponse(); + + + ObjectMapper mapper = new ObjectMapper(); + + String body = mapper.writeValueAsString(elinkBaseLocationResponse); + + + when(elinksFeignClient.getLocationDetails()).thenReturn(Response.builder() + .request(mock(Request.class)).body(body, defaultCharset()).status(HttpStatus.OK.value()).build()); + + + ElinksException thrown = Assertions.assertThrows(ElinksException.class, () -> { + ResponseEntity responseEntity = eLinksServiceImpl.retrieveLocation(); + }); + + verify(elinkDataIngestionSchedularAudit,times(3)) + .auditSchedulerStatus(any(),any(),any(),any(),any()); + + } + @Test void elinksService_load_location_should_return_elinksException_when_DataAccessException() @@ -253,7 +279,7 @@ void elinksService_load_location_should_return_elinksException_when_http_NOT_FOU assertThat(thrown.getErrorMessage()).contains(ELINKS_ERROR_RESPONSE_NOT_FOUND); assertThat(thrown.getErrorDescription()).contains(ELINKS_ERROR_RESPONSE_NOT_FOUND); - verify(elinkDataIngestionSchedularAudit,times(2)) + verify(elinkDataIngestionSchedularAudit,times(3)) .auditSchedulerStatus(any(),any(),any(),any(),any()); } diff --git a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksServiceImplTest.java b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksServiceImplTest.java index 6b0e266f9..be6010095 100644 --- a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ELinksServiceImplTest.java @@ -123,6 +123,34 @@ void elinksService_load_location_should_return_elinksException_when_DataAccessEx assertThat(thrown.getErrorDescription()).contains(ELINKS_DATA_STORE_ERROR); + } + + @Test + void elinksService_load_location_should_return_elinksException_on_ZeroResult() + throws JsonProcessingException { + + + + ElinkBaseLocationResponse elinkLocationResponse = new ElinkBaseLocationResponse(); + + + ObjectMapper mapper = new ObjectMapper(); + + String body = mapper.writeValueAsString(elinkLocationResponse); + + when(elinksFeignClient.getLocationDetails()).thenReturn(Response.builder() + .request(mock(Request.class)).body(body, defaultCharset()).status(HttpStatus.OK.value()).build()); + + ElinksException thrown = Assertions.assertThrows(ElinksException.class, () -> { + ResponseEntity responseEntity = eLinksServiceImpl.retrieveLocation(); + }); + + assertThat(thrown.getStatus().value()).isEqualTo(HttpStatus.FORBIDDEN.value()); + + assertThat(thrown.getErrorMessage()).contains(ELINKS_ACCESS_ERROR); + assertThat(thrown.getErrorDescription()).contains(ELINKS_ACCESS_ERROR); + + } @Test diff --git a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleServiceImplTest.java b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleServiceImplTest.java index 72635b47f..9be6a3ba4 100644 --- a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/ElinksPeopleServiceImplTest.java @@ -53,14 +53,17 @@ import uk.gov.hmcts.reform.judicialapi.elinks.util.RefDataElinksConstants; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Optional; import static java.nio.charset.Charset.defaultCharset; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @@ -106,8 +109,6 @@ class ElinksPeopleServiceImplTest { final IEmailService emailService = spy(IEmailService.class); - final ElinksPeopleServiceImpl elinksPeopleService = mock(ElinksPeopleServiceImpl.class); - @Spy private LocationRepository locationRepository; @@ -175,6 +176,10 @@ void setUP() { "2000"); ReflectionTestUtils.setField(elinksPeopleServiceImpl, "threadRetriggerPauseTime", "1000"); + ReflectionTestUtils.setField(elinksPeopleServiceImpl, "retriggerThreshold", + 3); + ReflectionTestUtils.setField(elinksPeopleServiceImpl, "retriggerStatusCode", + List.of(503,429)); ReflectionTestUtils.setField(elinksPeopleServiceImpl, "lastUpdated", "Thu Jan 01 00:00:00 GMT 2015"); ReflectionTestUtils.setField(elinksPeopleServiceImpl, "page", @@ -372,6 +377,28 @@ void loadPeopleWhenAuditEntryPresentPartialSuccessFor429() throws JsonProcessing verify(elinkDataExceptionRepository, times(1)).save(any()); } + @Test + void should_notRetriggerMorethanThreshold() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + String body = mapper.writeValueAsString(elinksApiResponseFirstHit); + String body2 = mapper.writeValueAsString(elinksApiResponseSecondHit); + String body3 = mapper.writeValueAsString(elinksApiResponseThirdHit); + when(elinksFeignClient.getPeopleDetails(any(), any(), any(), + Boolean.parseBoolean(any()))).thenReturn(Response.builder() + .request(mock(Request.class)).body(body, defaultCharset()).status(429).build()) + .thenReturn(Response.builder().request(mock(Request.class)) + .body(body2, defaultCharset()).status(429).build()) + .thenReturn(Response.builder().request(mock(Request.class)) + .body(body3, defaultCharset()).status(429).build()); + + ElinksException thrown = Assertions.assertThrows(ElinksException.class, () -> { + ResponseEntity responseEntity = elinksPeopleServiceImpl.updatePeople(); + }); + verify(elinkDataIngestionSchedularAudit,times(3)) + .auditSchedulerStatus(any(),any(),any(),any(),any()); + } + @Test void loadPeopleWhenAuditEntryPresentSuccess() throws JsonProcessingException { @@ -409,7 +436,31 @@ void loadPeopleWhenAuditEntryPresentSuccess() throws JsonProcessingException { verify(judicialRoleTypeRepository, atLeastOnce()).save(any()); verify(authorisationsRepository, atLeastOnce()).save(any()); verify(elinkDataExceptionHelper,times(2)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); + } + + @Test + void loadPeopleWhenAuditEntryWithoutPagination() throws JsonProcessingException { + + PeopleRequest elinksApiResponseHit; + ResultsRequest result; + ObjectMapper mapper = new ObjectMapper(); + result = ResultsRequest.builder().personalCode("1234").knownAs("knownas").fullName("fullName") + .surname("surname").postNominals("postNOmi").email("email").lastWorkingDate("2022-12-20") + .objectId("objectId1").initials("initials").appointmentsRequests(null) + .authorisationsRequests(null).judiciaryRoles(null).build(); + elinksApiResponseHit = PeopleRequest.builder().resultsRequests(List.of(result)).pagination(null).build(); + String body = mapper.writeValueAsString(elinksApiResponseHit); + + when(elinksFeignClient.getPeopleDetails(any(), any(), any(), + Boolean.parseBoolean(any()))).thenReturn(Response.builder() + .request(mock(Request.class)).body(body, defaultCharset()).status(200).build()); + + ElinksException thrown = Assertions.assertThrows(ElinksException.class, () -> { + ResponseEntity responseEntity = elinksPeopleServiceImpl.updatePeople(); + }); + verify(elinkDataIngestionSchedularAudit,times(3)) + .auditSchedulerStatus(any(),any(),any(),any(),any()); } @Test @@ -498,7 +549,7 @@ void loadPeopleWithEmailIdBlank() throws JsonProcessingException { verify(judicialRoleTypeRepository, atLeastOnce()).save(any()); verify(authorisationsRepository, atLeastOnce()).save(any()); verify(elinkDataExceptionHelper,times(1)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); } @Test @@ -574,7 +625,7 @@ void loadPeopleWithDuplicateObjectId() throws JsonProcessingException { verify(authorisationsRepository, atLeastOnce()).save(any()); verify(elinkDataExceptionHelper,times(1)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); } @@ -614,8 +665,50 @@ void loadPeopleWithDuplicateObjectIdInDb() throws JsonProcessingException { verify(authorisationsRepository, atLeastOnce()).save(any()); verify(elinkDataExceptionHelper,times(1)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); + + } + + @Test + void loadPeopleWithSidamIdPresentinDb() throws JsonProcessingException { + + UserProfile userProfile = UserProfile.builder() + .personalCode("1234") + .objectId("objectId1") + .emailId("email@justice") + .sidamId("sidamId") + .createdDate(convertToLocalDateTime("2023-04-12T16:42:35Z")) + .build(); + when(profileRepository.findAll()).thenReturn(List.of(userProfile)); + when(dataloadSchedularAuditRepository.findLatestSchedularEndTime()).thenReturn(null); + BaseLocation location = new BaseLocation(); + location.setBaseLocationId("12345"); + location.setName("ABC"); + LocationMapping locationMapping = LocationMapping.builder() + .serviceCode("BHA1") + .epimmsId("1234").build(); + when(regionMappingRepository.fetchRegionIdfromRegion(any())).thenReturn("1"); + when(baseLocationRepository.fetchParentId(any())).thenReturn("1234"); + ObjectMapper mapper = new ObjectMapper(); + when(locationMapppingRepository.fetchEpimmsIdfromLocationId(any())).thenReturn("234"); + String body = mapper.writeValueAsString(elinksApiResponseSecondHit); + + when(elinksFeignClient.getPeopleDetails(any(), any(), any(), + Boolean.parseBoolean(any()))).thenReturn(Response.builder() + .request(mock(Request.class)).body(body, defaultCharset()).status(200).build()); + + ResponseEntity response = elinksPeopleServiceImpl.updatePeople(); + assertTrue(response.getStatusCode().is2xxSuccessful()); + assertThat(response.getBody().getMessage()).isEqualTo(PEOPLE_DATA_LOAD_SUCCESS); + + + verify(elinksFeignClient, times(1)).getPeopleDetails(any(), any(), any(), + Boolean.parseBoolean(any())); + verify(profileRepository, times(2)).save(any()); + + verify(appointmentsRepository, atLeastOnce()).save(any()); + verify(authorisationsRepository, atLeastOnce()).save(any()); } @Test @@ -652,7 +745,7 @@ void loadPeopleWithPartialSuccess() throws JsonProcessingException { verify(authorisationsRepository, atLeastOnce()).save(any()); verify(elinkDataExceptionHelper,times(6)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); } @@ -693,7 +786,7 @@ void loadPeopleWithPartialSuccessWithInvalidRoleNames() throws JsonProcessingExc verify(authorisationsRepository, atLeastOnce()).save(any()); verify(elinkDataExceptionHelper,times(1)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); } @@ -812,8 +905,8 @@ void load_people_should_return_elinksException_when_updating_peopleDb() when(profileRepository.save(any())).thenThrow(dataAccessException); ResponseEntity responseEntity = elinksPeopleServiceImpl.updatePeople(); - verify(elinkDataExceptionHelper,atLeastOnce()).auditException(any(),any(),any(),any(),any(),any(),any()); - + verify(elinkDataExceptionHelper,atLeastOnce()).auditException(any(),any(), + any(),any(),any(),any(),any(),anyInt()); } @@ -843,7 +936,7 @@ void load_people_should_return_elinksException_when_updating_appointmentsDb() ResponseEntity responseEntity = elinksPeopleServiceImpl.updatePeople(); verify(elinkDataExceptionHelper,times(4)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); } @Test @@ -872,7 +965,7 @@ void load_people_should_return_elinksException_when_updating_RoleDb() ResponseEntity responseEntity = elinksPeopleServiceImpl.updatePeople(); verify(elinkDataExceptionHelper,times(6)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); } @Test @@ -917,6 +1010,7 @@ void load_people_should_return_elinksException_when_updating_authorisationsDb() exceptionRecords1.setFieldInError(RefDataElinksConstants.APPOINTMENTID); exceptionRecords1.setSchedulerStartTime(LocalDateTime.now()); exceptionRecords1.setUpdatedTimeStamp(LocalDateTime.now()); + exceptionRecords1.setPageId(1); ElinkDataExceptionRecords exceptionRecords2 = new ElinkDataExceptionRecords(); exceptionRecords2.setId(2L); exceptionRecords2.setKey("key1"); @@ -927,13 +1021,14 @@ void load_people_should_return_elinksException_when_updating_authorisationsDb() exceptionRecords2.setFieldInError(RefDataElinksConstants.APPOINTMENTID); exceptionRecords2.setSchedulerStartTime(LocalDateTime.now()); exceptionRecords2.setUpdatedTimeStamp(LocalDateTime.now()); + exceptionRecords2.setPageId(2); List exceptionRecords = Arrays.asList(exceptionRecords1,exceptionRecords2); when(elinkDataExceptionRepository.findBySchedulerStartTime(any())).thenReturn(exceptionRecords); ResponseEntity responseEntity = elinksPeopleServiceImpl.updatePeople(); verify(elinkDataExceptionHelper,times(6)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); verify(emailService, atLeastOnce()).sendEmail(any()); } @@ -979,6 +1074,7 @@ void verify_send_email_negative_scenario_when_updating_authorisationsDb() exceptionRecords1.setFieldInError(RefDataElinksConstants.APPOINTMENTID); exceptionRecords1.setSchedulerStartTime(LocalDateTime.now()); exceptionRecords1.setUpdatedTimeStamp(LocalDateTime.now()); + exceptionRecords1.setPageId(1); ElinkDataExceptionRecords exceptionRecords2 = new ElinkDataExceptionRecords(); exceptionRecords2.setId(2L); exceptionRecords2.setKey("key1"); @@ -989,13 +1085,14 @@ void verify_send_email_negative_scenario_when_updating_authorisationsDb() exceptionRecords2.setFieldInError(RefDataElinksConstants.APPOINTMENTID); exceptionRecords2.setSchedulerStartTime(LocalDateTime.now()); exceptionRecords2.setUpdatedTimeStamp(LocalDateTime.now()); + exceptionRecords2.setPageId(2); List exceptionRecords = Arrays.asList(exceptionRecords1,exceptionRecords2); when(elinkDataExceptionRepository.findBySchedulerStartTime(any())).thenReturn(exceptionRecords); ResponseEntity responseEntity = elinksPeopleServiceImpl.updatePeople(); verify(elinkDataExceptionHelper,times(6)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); verify(emailService, times(0)).sendEmail(any()); } @@ -1040,6 +1137,7 @@ void load_people_should_return_elinksException_when_updating_locationsDb() exceptionRecords1.setFieldInError(RefDataElinksConstants.BASE_LOCATION_ID); exceptionRecords1.setSchedulerStartTime(LocalDateTime.now()); exceptionRecords1.setUpdatedTimeStamp(LocalDateTime.now()); + exceptionRecords1.setPageId(1); ElinkDataExceptionRecords exceptionRecords2 = new ElinkDataExceptionRecords(); exceptionRecords2.setId(2L); exceptionRecords2.setKey("key1"); @@ -1050,13 +1148,15 @@ void load_people_should_return_elinksException_when_updating_locationsDb() exceptionRecords2.setFieldInError(RefDataElinksConstants.LOCATION); exceptionRecords2.setSchedulerStartTime(LocalDateTime.now()); exceptionRecords2.setUpdatedTimeStamp(LocalDateTime.now()); + exceptionRecords2.setPageId(2); List exceptionRecords = Arrays.asList(exceptionRecords1,exceptionRecords2); when(elinkDataExceptionRepository.findBySchedulerStartTime(any())).thenReturn(exceptionRecords); ResponseEntity responseEntity = elinksPeopleServiceImpl.updatePeople(); + verify(emailService,times(1)).sendEmail(any()); verify(elinkDataExceptionHelper,times(6)) - .auditException(any(),any(),any(),any(),any(),any(),any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); verify(emailService, atLeastOnce()).sendEmail(any()); } @@ -1117,8 +1217,7 @@ void load_people_should_return_elinksException_when_updating_userprofileDb() ResponseEntity responseEntity = elinksPeopleServiceImpl.updatePeople(); verify(elinkDataExceptionHelper,times(6)) - .auditException(any(),any(),any(),any(),any(),any(),any()); - verify(emailService, atLeastOnce()).sendEmail(any()); + .auditException(any(),any(),any(),any(),any(),any(),any(),anyInt()); } @@ -1145,7 +1244,8 @@ void load_people_should_return_elinksException_when_updating_JudicialRoleTypeDb( .request(mock(Request.class)).body(body, defaultCharset()).status(200).build()); ResponseEntity responseEntity = elinksPeopleServiceImpl.updatePeople(); - verify(elinkDataExceptionHelper,atLeastOnce()).auditException(any(),any(),any(),any(),any(),any(),any()); + verify(elinkDataExceptionHelper,atLeastOnce()).auditException(any(), + any(),any(),any(),any(),any(),any(),anyInt()); } @Test @@ -1223,5 +1323,12 @@ void load_people_should_return_elinksException_when_http_not_found() { } + private static LocalDateTime convertToLocalDateTime(String date) { + if (Optional.ofNullable(date).isPresent()) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); + return LocalDateTime.parse(date, formatter); + } + return null; + } } \ No newline at end of file diff --git a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/IdamElasticSearchServiceImplTest.java b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/IdamElasticSearchServiceImplTest.java index 9323cd069..ffb00236e 100644 --- a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/IdamElasticSearchServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/IdamElasticSearchServiceImplTest.java @@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.ResponseEntity; import org.springframework.jdbc.core.JdbcTemplate; @@ -19,6 +20,7 @@ import uk.gov.hmcts.reform.judicialapi.elinks.feign.IdamFeignClient; import uk.gov.hmcts.reform.judicialapi.elinks.response.IdamOpenIdTokenResponse; import uk.gov.hmcts.reform.judicialapi.elinks.response.IdamResponse; +import uk.gov.hmcts.reform.judicialapi.elinks.util.ElinkDataIngestionSchedularAudit; import java.nio.charset.Charset; import java.sql.Timestamp; @@ -56,6 +58,9 @@ class IdamElasticSearchServiceImplTest { private IdamElasticSearchServiceImpl idamElasticSearchServiceImpl; JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class); + @Mock + ElinkDataIngestionSchedularAudit elinkDataIngestionSchedularAudit; + public static final String CLIENT_AUTHORIZATION = "eyjfddsfsdfsdfdj03903.dffkljfke932rjf032j02f3--fskfljdskls-fdkldskll"; @@ -82,7 +87,7 @@ void setUP() { void getBearerToken() { when(openIdTokenResponseMock.getAccessToken()).thenReturn(CLIENT_AUTHORIZATION); when(idamClientMock.getOpenIdToken(any())).thenReturn(openIdTokenResponseMock); - String actualToken = idamElasticSearchServiceImpl.getIdamBearerToken(); + String actualToken = idamElasticSearchServiceImpl.getIdamBearerToken(LocalDateTime.now()); assertThat(actualToken).isEqualTo(CLIENT_AUTHORIZATION); verify(openIdTokenResponseMock, times(1)).getAccessToken(); verify(idamClientMock, times(1)).getOpenIdToken(any()); @@ -91,7 +96,9 @@ void getBearerToken() { @Test void getBearerTokenWithException() { when(openIdTokenResponseMock.getAccessToken()).thenReturn(CLIENT_AUTHORIZATION); - assertThrows(ElinksException.class, () -> idamElasticSearchServiceImpl.getIdamBearerToken()); + assertThrows(ElinksException.class, () -> idamElasticSearchServiceImpl.getIdamBearerToken(LocalDateTime.now())); + verify(elinkDataIngestionSchedularAudit,times(1)) + .auditSchedulerStatus(any(),any(),any(),any(),any()); } @Test @@ -120,6 +127,8 @@ void testSyncFeed() throws JsonProcessingException { assertThat(useResponse.getEmail()).isEqualTo("some@some.com"); }); verify(idamClientMock, times(1)).getUserFeed(anyString(), any()); + verify(elinkDataIngestionSchedularAudit,times(2)) + .auditSchedulerStatus(any(),any(),any(),any(),any()); } @Test @@ -137,6 +146,8 @@ void testSyncFeedResponseError() throws JsonProcessingException { .status(500).build(); when(idamClientMock.getUserFeed(anyString(), any())).thenReturn(response); assertThrows(ElinksException.class,() -> idamElasticSearchServiceImpl.getIdamElasticSearchSyncFeed()); + verify(elinkDataIngestionSchedularAudit,times(2)) + .auditSchedulerStatus(any(),any(),any(),any(),any()); } diff --git a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/PublishSidamIdServiceImplTest.java b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/PublishSidamIdServiceImplTest.java index 9a6bb76c2..5f04d4943 100644 --- a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/PublishSidamIdServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/service/impl/PublishSidamIdServiceImplTest.java @@ -8,17 +8,22 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.dao.DataAccessException; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import uk.gov.hmcts.reform.judicialapi.elinks.configuration.ElinkEmailConfiguration; +import uk.gov.hmcts.reform.judicialapi.elinks.exception.ElinksException; import uk.gov.hmcts.reform.judicialapi.elinks.response.SchedulerJobStatusResponse; import uk.gov.hmcts.reform.judicialapi.elinks.service.dto.Email; import uk.gov.hmcts.reform.judicialapi.elinks.servicebus.ElinkTopicPublisher; +import uk.gov.hmcts.reform.judicialapi.elinks.util.ElinkDataIngestionSchedularAudit; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -30,6 +35,7 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -53,9 +59,15 @@ class PublishSidamIdServiceImplTest { @Mock ElinkTopicPublisher elinkTopicPublisher; + @Mock + ElinkDataIngestionSchedularAudit elinkDataIngestionSchedularAudit; + @Mock EmailServiceImpl emailService; + @Spy + ElinkEmailConfiguration emailConfiguration; + List sidamIds = new ArrayList<>(); @BeforeEach @@ -80,6 +92,8 @@ void should_publish_when_job_status_is_inprogress() { assertEquals("IN_PROGRESS", res.getJobStatus()); assertEquals(HttpStatus.OK.value(),res.getStatusCode()); verify(jdbcTemplate, times(1)).update(anyString(), any(), anyInt()); + verify(elinkDataIngestionSchedularAudit,times(2)) + .auditSchedulerStatus(any(),any(),any(),any(),any()); } @@ -153,10 +167,13 @@ void should_throw_exception_when_email_is_enabled() { publishSidamIdService.emailConfiguration = emailConfiguration; assertThrows(Exception.class, - () -> publishSidamIdService.publishMessage(IN_PROGRESS.getStatus(), sidamIds, "1")); + () -> publishSidamIdService + .publishMessage(IN_PROGRESS.getStatus(), sidamIds, "1", LocalDateTime.now())); verify(emailService, times(1)).sendEmail(any(Email.class)); verify(elinkTopicPublisher, times(1)).sendMessage(any(), anyString()); verify(jdbcTemplate, times(1)).update(anyString(), any(), anyInt()); + verify(elinkDataIngestionSchedularAudit,times(1)) + .auditSchedulerStatus(any(),any(),any(),any(),any()); } @SneakyThrows @@ -176,7 +193,8 @@ void should_throw_exception_when_email_is_not_enabled() { publishSidamIdService.emailConfiguration = emailConfiguration; assertThrows(Exception.class, - () -> publishSidamIdService.publishMessage(IN_PROGRESS.getStatus(), sidamIds, "1")); + () -> publishSidamIdService + .publishMessage(IN_PROGRESS.getStatus(), sidamIds, "1",LocalDateTime.now())); verify(emailService, times(0)).sendEmail(any(Email.class)); verify(elinkTopicPublisher, times(1)).sendMessage(any(), anyString()); verify(jdbcTemplate, times(1)).update(anyString(), any(), anyInt()); @@ -186,10 +204,34 @@ void should_throw_exception_when_email_is_not_enabled() { @Test @DisplayName("Positive scenario when status is in progress and sidam id is not null") void whould_send_messages_when_sidam_id_not_null() { - publishSidamIdService.publishMessage(IN_PROGRESS.getStatus(),sidamIds,"2"); + publishSidamIdService.publishMessage(IN_PROGRESS.getStatus(),sidamIds,"2",LocalDateTime.now()); verify(elinkTopicPublisher).sendMessage(sidamIds,"2"); verify(jdbcTemplate, times(1)).update(anyString(), any(), anyInt()); } + @SneakyThrows + @Test + @DisplayName("Positive scenario when status is in progress and sidam id is not null") + void whould_send_messages_when_sidam_id_not_null_throwsException() { + + DataAccessException exception = mock(DataAccessException.class); + when(jdbcTemplate.update(anyString(), any(), anyInt())).thenThrow(exception); + ElinkEmailConfiguration.MailTypeConfig mailTypeConfig = new ElinkEmailConfiguration.MailTypeConfig(); + mailTypeConfig.setEnabled(false); + mailTypeConfig.setSubject("%s :: Publishing of JRD messages to ASB failed"); + mailTypeConfig.setBody("Publishing of JRD messages to ASB failed for Job Id %s"); + mailTypeConfig.setFrom("test@test.com"); + mailTypeConfig.setTo(List.of("test@test.com")); + ElinkEmailConfiguration emailConfiguration = new ElinkEmailConfiguration(); + emailConfiguration.setMailTypes(Map.of("asb", mailTypeConfig)); + publishSidamIdService.emailConfiguration = emailConfiguration; + assertThrows(ElinksException.class,() -> publishSidamIdService + .publishMessage(IN_PROGRESS.getStatus(),sidamIds,"2",LocalDateTime.now())); + + verify(elinkTopicPublisher).sendMessage(sidamIds,"2"); + verify(elinkDataIngestionSchedularAudit,times(2)) + .auditSchedulerStatus(any(),any(),any(),any(),any()); + } + } \ No newline at end of file diff --git a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataExceptionHelperTest.java b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataExceptionHelperTest.java index 1ea22c8a5..44e4c28b2 100644 --- a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataExceptionHelperTest.java +++ b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataExceptionHelperTest.java @@ -35,7 +35,7 @@ void auditExceptionSuccess() { LocalDateTime.now(), "ElinksApiJobScheduler" + LocalDateTime.now(), "Schedular_Run_date", "JRD load failed since job has already ran for the day", - "ElinksApiJobScheduler", null); + "ElinksApiJobScheduler", null,1); verify(elinkDataExceptionRepository, times(1)) .save(any()); @@ -49,7 +49,7 @@ void auditExceptionFailure() { LocalDateTime.now(), "ElinksApiJobScheduler" + LocalDateTime.now(), "Schedular_Run_date", "JRD load failed since job has already ran for the day", - "ElinksApiJobScheduler",null)); + "ElinksApiJobScheduler",null,1)); } } \ No newline at end of file diff --git a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataIngestionSchedularAuditTest.java b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataIngestionSchedularAuditTest.java index a21b43a81..2f8ad07bf 100644 --- a/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataIngestionSchedularAuditTest.java +++ b/src/test/java/uk/gov/hmcts/reform/judicialapi/elinks/util/ElinkDataIngestionSchedularAuditTest.java @@ -89,7 +89,7 @@ void testSaveScheduleFailureAudit(String apiName, String status) { schedularAudit.getStatus(), schedularAudit.getApiName()); verify(schedularAudit,times(2)).setSchedulerName(any()); verify(schedularAudit,times(2)).setSchedulerStartTime(any()); - verify(schedularAudit,times(2)).setSchedulerEndTime(any()); + verify(schedularAudit,times(3)).setSchedulerEndTime(any()); verify(schedularAudit,times(2)).setStatus(any()); verify(schedularAudit,times(2)).setApiName(any()); verify(elinkSchedularAuditRepository, times(1))