From 9282d80c1a2a993ef497f1cd7d068fcd16d2f1b5 Mon Sep 17 00:00:00 2001 From: Krishna Bottla <40598480+kbottla@users.noreply.github.com> Date: Wed, 1 Nov 2023 12:16:14 +0000 Subject: [PATCH] PP-11205 Add method to find services (#2214) - Added method to find services to check for archiving --- .../persistence/dao/ServiceDao.java | 15 ++++- .../fixtures/ServiceEntityFixture.java | 11 +++- .../persistence/dao/ServiceDaoIT.java | 56 ++++++++++++++++++- .../adminusers/utils/DatabaseTestHelper.java | 5 +- 4 files changed, 80 insertions(+), 7 deletions(-) diff --git a/src/main/java/uk/gov/pay/adminusers/persistence/dao/ServiceDao.java b/src/main/java/uk/gov/pay/adminusers/persistence/dao/ServiceDao.java index eaa7466c5..840fac106 100644 --- a/src/main/java/uk/gov/pay/adminusers/persistence/dao/ServiceDao.java +++ b/src/main/java/uk/gov/pay/adminusers/persistence/dao/ServiceDao.java @@ -7,6 +7,7 @@ import javax.inject.Inject; import javax.persistence.EntityManager; +import java.time.ZonedDateTime; import java.util.List; import java.util.Optional; @@ -24,7 +25,7 @@ public List listAll() { .createQuery(query, ServiceEntity.class) .getResultList(); } - + @SuppressWarnings("unchecked") public List findByENServiceName(String searchString) { String query = "SELECT * FROM services s WHERE s.id IN (SELECT service_id FROM service_names sn WHERE to_tsvector('english', sn.name) @@ plainto_tsquery('english', ?) AND sn.language = 'en')"; @@ -32,7 +33,7 @@ public List findByENServiceName(String searchString) { .setParameter(1, searchString) .getResultList(); } - + @SuppressWarnings("unchecked") public List findByServiceMerchantName(String searchString) { String query = "SELECT * FROM services s WHERE to_tsvector('english', s.merchant_name) @@ plainto_tsquery('english', ?)"; @@ -80,4 +81,14 @@ public Optional findByExternalId(String serviceExternalId) { .stream() .findFirst(); } + + public List findServicesToCheckForArchiving(ZonedDateTime archiveServicesBeforeDate) { + String query = "SELECT s FROM ServiceEntity as s" + + " WHERE s.createdDate < :archiveServicesBeforeDate" + + " AND NOT s.archived"; + return entityManager.get() + .createQuery(query, ServiceEntity.class) + .setParameter("archiveServicesBeforeDate", archiveServicesBeforeDate) + .getResultList(); + } } diff --git a/src/test/java/uk/gov/pay/adminusers/fixtures/ServiceEntityFixture.java b/src/test/java/uk/gov/pay/adminusers/fixtures/ServiceEntityFixture.java index f264d92cc..dd66bef99 100644 --- a/src/test/java/uk/gov/pay/adminusers/fixtures/ServiceEntityFixture.java +++ b/src/test/java/uk/gov/pay/adminusers/fixtures/ServiceEntityFixture.java @@ -11,8 +11,6 @@ import uk.gov.pay.adminusers.persistence.entity.service.ServiceNameEntity; import uk.gov.service.payments.commons.model.SupportedLanguage; -import static org.apache.commons.lang3.RandomUtils.nextInt; - import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.HashSet; @@ -20,6 +18,8 @@ import java.util.Map; import java.util.Set; +import static org.apache.commons.lang3.RandomUtils.nextInt; + public final class ServiceEntityFixture { private Integer id = nextInt(); private String externalId = RandomIdGenerator.randomUuid(); @@ -37,6 +37,7 @@ public final class ServiceEntityFixture { private ZonedDateTime wentLiveDate; private String sector; private PspTestAccountStage pspTestAccountStage = PspTestAccountStage.NOT_STARTED; + private boolean archived = false; private ServiceEntityFixture() { } @@ -127,6 +128,11 @@ public ServiceEntityFixture withPspTestAccountStage(PspTestAccountStage pspTestA return this; } + public ServiceEntityFixture withArchived(boolean archived) { + this.archived = archived; + return this; + } + public ServiceEntity build() { ServiceEntity serviceEntity = new ServiceEntity(); serviceEntity.setId(id); @@ -145,6 +151,7 @@ public ServiceEntity build() { serviceEntity.setWentLiveDate(wentLiveDate); serviceEntity.setSector(sector); serviceEntity.setCurrentPspTestAccountStage(pspTestAccountStage); + serviceEntity.setArchived(archived); return serviceEntity; } } diff --git a/src/test/java/uk/gov/pay/adminusers/persistence/dao/ServiceDaoIT.java b/src/test/java/uk/gov/pay/adminusers/persistence/dao/ServiceDaoIT.java index c569b7857..f1aafe25d 100644 --- a/src/test/java/uk/gov/pay/adminusers/persistence/dao/ServiceDaoIT.java +++ b/src/test/java/uk/gov/pay/adminusers/persistence/dao/ServiceDaoIT.java @@ -4,8 +4,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.lang3.RandomUtils; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.postgresql.util.PGobject; +import uk.gov.pay.adminusers.fixtures.ServiceEntityFixture; import uk.gov.pay.adminusers.fixtures.UserDbFixture; import uk.gov.pay.adminusers.model.GoLiveStage; import uk.gov.pay.adminusers.model.Permission; @@ -19,7 +21,6 @@ import uk.gov.pay.adminusers.persistence.entity.MerchantDetailsEntity; import uk.gov.pay.adminusers.persistence.entity.MerchantDetailsEntityBuilder; import uk.gov.pay.adminusers.persistence.entity.ServiceEntity; -import uk.gov.pay.adminusers.fixtures.ServiceEntityFixture; import uk.gov.pay.adminusers.persistence.entity.service.ServiceNameEntity; import uk.gov.service.payments.commons.model.SupportedLanguage; @@ -374,6 +375,59 @@ void shouldMergePSPTestAccountStage() { assertThat(optionalService.get().getCurrentPspTestAccountStage(), is(PspTestAccountStage.REQUEST_SUBMITTED)); } + @Nested + class TestFindServicesToCheckForArchiving { + @Test + void shouldReturnServicesOlderThanTheProvidedDate() { + ZonedDateTime archiveServicesBeforeDate = ZonedDateTime.parse("2022-01-01T00:00:00Z"); + ServiceEntity insertedServiceEntity = ServiceEntityFixture + .aServiceEntity() + .withCreatedDate(archiveServicesBeforeDate.minusDays(1)) + .build(); + + databaseHelper.insertServiceEntity(insertedServiceEntity); + + List services = serviceDao.findServicesToCheckForArchiving(archiveServicesBeforeDate); + assertThat(services.size(), is(1)); + assertThat(services.get(0).getExternalId(), is(insertedServiceEntity.getExternalId())); + } + + @Test + void shouldNotReturnServiceCreatedAfterTheProvidedDate() { + ZonedDateTime archiveServicesBeforeDate = ZonedDateTime.parse("2022-01-01T00:00:00Z"); + ServiceEntity insertedServiceEntity1 = ServiceEntityFixture + .aServiceEntity() + .withCreatedDate(archiveServicesBeforeDate) + .build(); + ServiceEntity insertedServiceEntity2 = ServiceEntityFixture + .aServiceEntity() + .withCreatedDate(archiveServicesBeforeDate.plusDays(1)) + .build(); + + databaseHelper.insertServiceEntity(insertedServiceEntity1); + databaseHelper.insertServiceEntity(insertedServiceEntity2); + + List services = serviceDao.findServicesToCheckForArchiving(archiveServicesBeforeDate); + assertThat(services.size(), is(0)); + } + + @Test + void shouldNotReturnServiceArchived() { + ZonedDateTime archiveServicesBeforeDate = ZonedDateTime.parse("2022-01-01T00:00:00Z"); + + ServiceEntity insertedServiceEntity = ServiceEntityFixture + .aServiceEntity() + .withCreatedDate(archiveServicesBeforeDate.minusDays(1)) + .withArchived(true) + .build(); + + databaseHelper.insertServiceEntity(insertedServiceEntity); + + List services = serviceDao.findServicesToCheckForArchiving(archiveServicesBeforeDate); + assertThat(services.size(), is(0)); + } + } + private void setupUsersForServiceAndRole(String externalId, int roleId, int noOfUsers) { Permission perm1 = aPermission(); Permission perm2 = aPermission(); diff --git a/src/test/java/uk/gov/pay/adminusers/utils/DatabaseTestHelper.java b/src/test/java/uk/gov/pay/adminusers/utils/DatabaseTestHelper.java index 21ee777bd..fcdb2f42c 100644 --- a/src/test/java/uk/gov/pay/adminusers/utils/DatabaseTestHelper.java +++ b/src/test/java/uk/gov/pay/adminusers/utils/DatabaseTestHelper.java @@ -385,10 +385,10 @@ public DatabaseTestHelper insertServiceEntity(ServiceEntity serviceEntity) { "id, custom_branding, " + "merchant_name, merchant_telephone_number, merchant_address_line1, merchant_address_line2, merchant_address_city, " + "merchant_address_postcode, merchant_address_country, merchant_email, merchant_url, external_id, redirect_to_service_immediately_on_terminal_state, " + - "current_go_live_stage, experimental_features_enabled, current_psp_test_account_stage, created_date) " + + "current_go_live_stage, experimental_features_enabled, current_psp_test_account_stage, created_date, archived) " + "VALUES (:id, :customBranding, :merchantName, :merchantTelephoneNumber, :merchantAddressLine1, :merchantAddressLine2, " + ":merchantAddressCity, :merchantAddressPostcode, :merchantAddressCountry, :merchantEmail, :merchantUrl, :externalId, :redirectToServiceImmediatelyOnTerminalState, " + - ":currentGoLiveStage, :experimentalFeaturesEnabled, :pspTestAccountStage, :createdDate)") + ":currentGoLiveStage, :experimentalFeaturesEnabled, :pspTestAccountStage, :createdDate, :archived)") .bind("id", serviceEntity.getId()) .bindBySqlType("customBranding", customBranding, OTHER) .bind("merchantName", merchantDetails.getName()) @@ -406,6 +406,7 @@ public DatabaseTestHelper insertServiceEntity(ServiceEntity serviceEntity) { .bind("experimentalFeaturesEnabled", serviceEntity.isExperimentalFeaturesEnabled()) .bind("pspTestAccountStage", serviceEntity.getCurrentPspTestAccountStage()) .bind("createdDate", serviceEntity.getCreatedDate()) + .bind("archived", serviceEntity.isArchived()) .execute(); }); serviceEntity.getGatewayAccountIds().forEach(gatewayAccount ->