From 855cff3a0f1bc6dab65d443fa39352f28326e59c Mon Sep 17 00:00:00 2001 From: Ricardo Campos Date: Fri, 11 Oct 2024 15:25:20 -0300 Subject: [PATCH] fix: org units combobox (#393) Co-authored-by: Paulo Gomes da Cruz Junior --- backend/openshift.deploy.yml | 7 +- .../endpoint/OpeningSearchEndpoint.java | 17 ++--- .../oracle/repository/OrgUnitRepository.java | 5 +- .../oracle/service/OrgUnitService.java | 30 +++++---- backend/src/main/resources/application.yml | 5 +- .../endpoint/OpeningSearchEndpointTest.java | 6 +- .../OrgUnitRepositoryIntegrationTest.java | 10 +-- .../oracle/service/OrgUnitServiceTest.java | 67 ++++++------------- .../test/resources/application-default.yml | 2 + .../oracle/V002__oracle_code_tables.sql | 2 +- 10 files changed, 63 insertions(+), 88 deletions(-) diff --git a/backend/openshift.deploy.yml b/backend/openshift.deploy.yml index 2a56251c..7219461e 100644 --- a/backend/openshift.deploy.yml +++ b/backend/openshift.deploy.yml @@ -81,6 +81,9 @@ parameters: - name: WMS_LAYERS_WHITELIST_USERS description: List of users that can see active layers on the map view required: true + - name: OPENING_SEARCH_ORG_UNITS + description: List of Org Units code to be displayed as options for the opening search + value: DCK,DSQ,DVA,DKM,DSC,DFN,DSI,DCR,DMK,DQC,DKA,DCS,DOS,DSE,DCC,DMH,DQU,DNI,DND,DRM,DPG,DSS,DPC - name: RANDOM_EXPRESSION description: Random expression to make sure deployments update from: "[a-zA-Z0-9]{32}" @@ -94,7 +97,7 @@ objects: metadata: labels: app: ${NAME}-${ZONE} - name: ${NAME}-${ZONE}-${COMPONENT} + name: ${NAME}-${ZONE}-cert spec: accessModes: - ReadWriteMany @@ -183,6 +186,8 @@ objects: value: ${ALLOWED_ORIGINS} - name: WMS_LAYERS_WHITELIST_USERS value: ${WMS_LAYERS_WHITELIST_USERS} + - name: OPENING_SEARCH_ORG_UNITS + value: ${OPENING_SEARCH_ORG_UNITS} - name: FORESTCLIENTAPI_KEY valueFrom: secretKeyRef: diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningSearchEndpoint.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningSearchEndpoint.java index d6fa1035..0a12031a 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningSearchEndpoint.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningSearchEndpoint.java @@ -264,9 +264,8 @@ public List getOpeningCategories( } /** - * Get all org units. + * Get the Org units list for the openings search API. * - * @param includeExpired Query param to include expired org units. * @return List of OrgUnitEntity with found org units. */ @GetMapping("/org-units") @@ -283,16 +282,8 @@ public List getOpeningCategories( description = "Access token is missing or invalid", content = @Content(schema = @Schema(implementation = Void.class))) }) - public List getOpeningOrgUnits( - @RequestParam(value = "includeExpired", required = false) - @Parameter( - name = "includeExpired", - in = ParameterIn.QUERY, - description = "Defines if the API should include expired org units", - required = false) - Boolean includeExpired) { - boolean addExpired = Boolean.TRUE.equals(includeExpired); - return orgUnitService.findAllOrgUnits(addExpired); + public List getOpeningOrgUnits() { + return orgUnitService.findAllOrgUnits(); } /** @@ -322,7 +313,7 @@ public List getOpeningOrgUnitsByCode( in = ParameterIn.QUERY, description = "Defines the org units that should be included in the search", required = false) - List codes) { + String[] codes) { return orgUnitService.findAllOrgUnitsByCode(codes); } } diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepository.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepository.java index 2cf454e3..2f48ba89 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepository.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepository.java @@ -1,14 +1,11 @@ package ca.bc.gov.restapi.results.oracle.repository; import ca.bc.gov.restapi.results.oracle.entity.OrgUnitEntity; -import java.time.LocalDate; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; /** This interface provides methods to get, save, and manage org unit data in the database. */ public interface OrgUnitRepository extends JpaRepository { - List findAllByExpiryDateAfter(LocalDate now); - - List findAllByOrgUnitCodeIn(List orgUnitCodes); + List findAllByOrgUnitCodeIn(String[] orgUnitCodes); } diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitService.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitService.java index 18109ed9..507b84f9 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitService.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitService.java @@ -2,10 +2,12 @@ import ca.bc.gov.restapi.results.oracle.entity.OrgUnitEntity; import ca.bc.gov.restapi.results.oracle.repository.OrgUnitRepository; -import java.time.LocalDate; +import java.util.Arrays; import java.util.List; +import java.util.Objects; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** This class contains methods to handle Org Units. */ @@ -16,23 +18,25 @@ public class OrgUnitService { private final OrgUnitRepository orgUnitRepository; + @Value("${nr.results.config.opening-search.org-units}") + private String[] orgUnitsFromProps; + /** - * Find all Org Units. Option to include expired ones. + * Find all Org Units for the Openings Search. * - * @param includeExpired True to include expired, false otherwise. * @return List of {@link OrgUnitEntity} with found categories. */ - public List findAllOrgUnits(boolean includeExpired) { - log.info("Getting all org units. Include expired: {}", includeExpired); + public List findAllOrgUnits() { + log.info("Getting all org units for the search openings"); - if (includeExpired) { - List orgUnits = orgUnitRepository.findAll(); - log.info("Found {} org units (including expired)", orgUnits.size()); - return orgUnits; + if (Objects.isNull(orgUnitsFromProps) || orgUnitsFromProps.length == 0) { + log.warn("No Org Units from the properties file."); + return List.of(); } - List orgUnits = orgUnitRepository.findAllByExpiryDateAfter(LocalDate.now()); - log.info("Found {} org units (excluding expired)", orgUnits.size()); + List orgUnits = orgUnitRepository.findAllByOrgUnitCodeIn(orgUnitsFromProps); + + log.info("Found {} org units by codes", orgUnits.size()); return orgUnits; } @@ -42,8 +46,8 @@ public List findAllOrgUnits(boolean includeExpired) { * @param orgUnitCodes Org Unit codes to search for. * @return List of {@link OrgUnitEntity} with found categories. */ - public List findAllOrgUnitsByCode(List orgUnitCodes) { - log.info("Getting all org units by codes: {}", orgUnitCodes); + public List findAllOrgUnitsByCode(String[] orgUnitCodes) { + log.info("Getting all org units by codes: {}", Arrays.toString(orgUnitCodes)); List orgUnits = orgUnitRepository.findAllByOrgUnitCodeIn(orgUnitCodes); log.info("Found {} org units by codes", orgUnits.size()); diff --git a/backend/src/main/resources/application.yml b/backend/src/main/resources/application.yml index 3c32cadd..852728b5 100644 --- a/backend/src/main/resources/application.yml +++ b/backend/src/main/resources/application.yml @@ -102,7 +102,10 @@ nr: # Users allowed to see and download WMS layers information config: - wms-layers.whitelist: ${WMS_LAYERS_WHITELIST_USERS:NONE} + wms-layers: + whitelist: ${WMS_LAYERS_WHITELIST_USERS:NONE} + opening-search: + org-units: ${OPENING_SEARCH_ORG_UNITS:DCK} # Forest Client API forest-client-api: diff --git a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningSearchEndpointTest.java b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningSearchEndpointTest.java index 048218dc..d616cc82 100644 --- a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningSearchEndpointTest.java +++ b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningSearchEndpointTest.java @@ -203,7 +203,7 @@ void getOpeningOrgUnits_happyPath_shouldSucceed() throws Exception { List orgUnitEntityList = List.of(orgUnit); - when(orgUnitService.findAllOrgUnits(false)).thenReturn(orgUnitEntityList); + when(orgUnitService.findAllOrgUnits()).thenReturn(orgUnitEntityList); mockMvc .perform( @@ -257,7 +257,7 @@ void getOpeningOrgUnitsByCode_happyPath_shouldSucceed() throws Exception { List orgUnitEntityList = List.of(orgUnit); - when(orgUnitService.findAllOrgUnitsByCode(List.of("DAS"))).thenReturn(orgUnitEntityList); + when(orgUnitService.findAllOrgUnitsByCode(new String[]{"DAS"})).thenReturn(orgUnitEntityList); mockMvc .perform( @@ -287,7 +287,7 @@ void getOpeningOrgUnitsByCode_happyPath_shouldSucceed() throws Exception { @Test @DisplayName("Get Opening Org Units By Code not Found should Succeed") void getOpeningOrgUnitsByCode_notFound_shouldSucceed() throws Exception { - when(orgUnitService.findAllOrgUnitsByCode(List.of("DAS"))).thenReturn(List.of()); + when(orgUnitService.findAllOrgUnitsByCode(new String[]{"DAS"})).thenReturn(List.of()); mockMvc .perform( diff --git a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepositoryIntegrationTest.java b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepositoryIntegrationTest.java index 70afeea1..8c49ba44 100644 --- a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepositoryIntegrationTest.java +++ b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepositoryIntegrationTest.java @@ -15,16 +15,16 @@ class OrgUnitRepositoryIntegrationTest extends AbstractTestContainerIntegrationT private OrgUnitRepository orgUnitRepository; @Test - @DisplayName("Find all by expiry date after happy path should succeed") - void findAllByExpiryDateAfter_happyPath_shouldSucceed() { - List list = orgUnitRepository.findAllByExpiryDateAfter(LocalDate.now()); + @DisplayName("Find all by org unit code unit in happy path should succeed") + void findAllByOrgUnitCodeIn_happyPath_shouldSucceed() { + List list = orgUnitRepository.findAllByOrgUnitCodeIn(new String[]{"DAS"}); Assertions.assertNotNull(list); - Assertions.assertEquals(2, list.size()); + Assertions.assertEquals(1, list.size()); OrgUnitEntity orgUnit = list.get(0); Assertions.assertEquals(1L, orgUnit.getOrgUnitNo()); - Assertions.assertEquals("ONE", orgUnit.getOrgUnitCode()); + Assertions.assertEquals("DAS", orgUnit.getOrgUnitCode()); Assertions.assertEquals("Org one", orgUnit.getOrgUnitName()); Assertions.assertEquals("001", orgUnit.getLocationCode()); Assertions.assertEquals("AAA", orgUnit.getAreaCode()); diff --git a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitServiceTest.java b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitServiceTest.java index 6d2acafe..e3e2a618 100644 --- a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitServiceTest.java +++ b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitServiceTest.java @@ -1,6 +1,5 @@ package ca.bc.gov.restapi.results.oracle.service; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -26,12 +25,12 @@ class OrgUnitServiceTest { @BeforeEach void setup() { - orgUnitService = new OrgUnitService(orgUnitRepository); + orgUnitService = new OrgUnitService(orgUnitRepository, new String[] {"DAS"}); } @Test - @DisplayName("Find all org units include expired false should succeed") - void findAllOrgUnits_includeExpiredFalse_shouldSucceed() { + @DisplayName("Find all org units happy path should succeed") + void findAllOrgUnits_happyPath_shouldSucceed() { OrgUnitEntity orgUnit = new OrgUnitEntity(); orgUnit.setOrgUnitNo(22L); orgUnit.setOrgUnitCode("DAS"); @@ -49,8 +48,9 @@ void findAllOrgUnits_includeExpiredFalse_shouldSucceed() { orgUnit.setExpiryDate(LocalDate.now().plusYears(3L)); orgUnit.setUpdateTimestamp(LocalDate.now()); - when(orgUnitRepository.findAllByExpiryDateAfter(any())).thenReturn(List.of(orgUnit)); - List entities = orgUnitService.findAllOrgUnits(false); + when(orgUnitRepository.findAllByOrgUnitCodeIn(new String[] {"DAS"})) + .thenReturn(List.of(orgUnit)); + List entities = orgUnitService.findAllOrgUnits(); Assertions.assertNotNull(entities); Assertions.assertEquals(1, entities.size()); @@ -74,47 +74,17 @@ void findAllOrgUnits_includeExpiredFalse_shouldSucceed() { } @Test - @DisplayName("Find all org units include expired true should succeed") - void findAllOrgUnits_includeExpiredTrue_shouldSucceed() { - OrgUnitEntity orgUnit = new OrgUnitEntity(); - orgUnit.setOrgUnitNo(22L); - orgUnit.setOrgUnitCode("DAS"); - orgUnit.setOrgUnitName("DAS Name"); - orgUnit.setLocationCode("123"); - orgUnit.setAreaCode("1"); - orgUnit.setTelephoneNo("25436521"); - orgUnit.setOrgLevelCode('R'); - orgUnit.setOfficeNameCode("RR"); - orgUnit.setRollupRegionNo(12L); - orgUnit.setRollupRegionCode("19"); - orgUnit.setRollupDistNo(13L); - orgUnit.setRollupDistCode("25"); - orgUnit.setEffectiveDate(LocalDate.now().minusYears(3L)); - orgUnit.setExpiryDate(LocalDate.now().minusYears(1L)); - orgUnit.setUpdateTimestamp(LocalDate.now()); + @DisplayName("Find all org units empty response should succeed") + void findAllOrgUnits_emptyResponse_shouldSucceed() { + orgUnitService = new OrgUnitService(orgUnitRepository, new String[] {}); - when(orgUnitRepository.findAll()).thenReturn(List.of(orgUnit)); - List entities = orgUnitService.findAllOrgUnits(true); + when(orgUnitRepository.findAllByOrgUnitCodeIn(new String[] {"DAS"})).thenReturn(List.of()); + List entities = orgUnitService.findAllOrgUnits(); Assertions.assertNotNull(entities); - Assertions.assertEquals(1, entities.size()); - - OrgUnitEntity responseOrg = entities.get(0); - Assertions.assertTrue(responseOrg.getExpiryDate().isBefore(LocalDate.now())); - Assertions.assertEquals(22L, responseOrg.getOrgUnitNo()); - Assertions.assertEquals("DAS", responseOrg.getOrgUnitCode()); - Assertions.assertEquals("DAS Name", responseOrg.getOrgUnitName()); - Assertions.assertEquals("123", responseOrg.getLocationCode()); - Assertions.assertEquals("1", responseOrg.getAreaCode()); - Assertions.assertEquals("25436521", responseOrg.getTelephoneNo()); - Assertions.assertEquals('R', responseOrg.getOrgLevelCode()); - Assertions.assertEquals("RR", responseOrg.getOfficeNameCode()); - Assertions.assertEquals(12L, responseOrg.getRollupRegionNo()); - Assertions.assertEquals("19", responseOrg.getRollupRegionCode()); - Assertions.assertEquals(13L, responseOrg.getRollupDistNo()); - Assertions.assertEquals("25", responseOrg.getRollupDistCode()); + Assertions.assertTrue(entities.isEmpty()); - verify(orgUnitRepository, times(0)).findAllByExpiryDateAfter(any()); + verify(orgUnitRepository, times(0)).findAll(); } @Test @@ -137,8 +107,10 @@ void findAllOrgUnitsByCode_happyPath_shouldSucceed() { orgUnit.setExpiryDate(LocalDate.now().minusYears(1L)); orgUnit.setUpdateTimestamp(LocalDate.now()); - when(orgUnitRepository.findAllByOrgUnitCodeIn(List.of("DAS"))).thenReturn(List.of(orgUnit)); - List entities = orgUnitService.findAllOrgUnitsByCode(List.of("DAS")); + String[] units = new String[] {"DAS"}; + + when(orgUnitRepository.findAllByOrgUnitCodeIn(units)).thenReturn(List.of(orgUnit)); + List entities = orgUnitService.findAllOrgUnitsByCode(units); Assertions.assertNotNull(entities); Assertions.assertEquals(1, entities.size()); @@ -162,8 +134,9 @@ void findAllOrgUnitsByCode_happyPath_shouldSucceed() { @Test @DisplayName("Find all org units by code not found should succeed") void findAllOrgUnitsByCode_notFound_shouldSucceed() { - when(orgUnitRepository.findAllByOrgUnitCodeIn(List.of("DAS"))).thenReturn(List.of()); - List entities = orgUnitService.findAllOrgUnitsByCode(List.of("DAS")); + String[] units = new String[] {"DAS"}; + when(orgUnitRepository.findAllByOrgUnitCodeIn(units)).thenReturn(List.of()); + List entities = orgUnitService.findAllOrgUnitsByCode(units); Assertions.assertNotNull(entities); Assertions.assertTrue(entities.isEmpty()); diff --git a/backend/src/test/resources/application-default.yml b/backend/src/test/resources/application-default.yml index d24c031b..9e9be1e6 100644 --- a/backend/src/test/resources/application-default.yml +++ b/backend/src/test/resources/application-default.yml @@ -15,6 +15,8 @@ nr: config: wms-layers: whitelist: ${WMS_LAYERS_WHITELIST_USERS:NONE} + opening-search: + org-units: ${OPENING_SEARCH_ORG_UNITS:DCK} # Forest Client API #forest-client-api: diff --git a/backend/src/test/resources/migration/oracle/V002__oracle_code_tables.sql b/backend/src/test/resources/migration/oracle/V002__oracle_code_tables.sql index 08579b5e..e340fb82 100644 --- a/backend/src/test/resources/migration/oracle/V002__oracle_code_tables.sql +++ b/backend/src/test/resources/migration/oracle/V002__oracle_code_tables.sql @@ -1434,7 +1434,7 @@ INSERT INTO THE.TREE_SPECIES_CODE VALUES('YC', 'yellow-cedar', TO_DATE('1905-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_DATE('9999-12-31 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_DATE('2023-07-17 09:06:00', 'YYYY-MM-DD HH24:MI:SS')); INSERT INTO THE.ORG_UNIT (ORG_UNIT_NO,ORG_UNIT_CODE,ORG_UNIT_NAME,LOCATION_CODE,AREA_CODE,TELEPHONE_NO,ORG_LEVEL_CODE,OFFICE_NAME_CODE,ROLLUP_REGION_NO,ROLLUP_REGION_CODE,ROLLUP_DIST_NO,ROLLUP_DIST_CODE,EFFECTIVE_DATE,EXPIRY_DATE,UPDATE_TIMESTAMP) VALUES -(1, 'ONE', 'Org one', '001', 'AAA', '1122334', 'H', 'VI', 111, '1Code', 222, '22Code', TO_DATE('2020-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_DATE('9999-12-31 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_DATE('2024-09-03 00:00:00', 'YYYY-MM-DD HH24:MI:SS')), +(1, 'DAS', 'Org one', '001', 'AAA', '1122334', 'H', 'VI', 111, '1Code', 222, '22Code', TO_DATE('2020-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_DATE('9999-12-31 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_DATE('2024-09-03 00:00:00', 'YYYY-MM-DD HH24:MI:SS')), (2, 'TWO', 'Org two', '002', 'BBB', '2233445', 'R', 'CA', 333, '3Code', 444, '44Code', TO_DATE('2020-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_DATE('2022-12-31 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), TO_DATE('2022-12-31 00:00:00', 'YYYY-MM-DD HH24:MI:SS')), (70,'HVA','Timber Pricing Branch','010','250','3871701','H','VA',0,' ',0,' ',TO_DATE('1905-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),TO_DATE('9999-12-31 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),TO_DATE('2011-08-29 15:49:08', 'YYYY-MM-DD HH24:MI:SS'));