Skip to content

Commit

Permalink
fix: org units combobox (#393)
Browse files Browse the repository at this point in the history
Co-authored-by: Paulo Gomes da Cruz Junior <[email protected]>
  • Loading branch information
Ricardo Campos and paulushcgcj authored Oct 11, 2024
1 parent f1f9c4b commit 855cff3
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 88 deletions.
7 changes: 6 additions & 1 deletion backend/openshift.deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -94,7 +97,7 @@ objects:
metadata:
labels:
app: ${NAME}-${ZONE}
name: ${NAME}-${ZONE}-${COMPONENT}
name: ${NAME}-${ZONE}-cert
spec:
accessModes:
- ReadWriteMany
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,8 @@ public List<OpenCategoryCodeEntity> 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")
Expand All @@ -283,16 +282,8 @@ public List<OpenCategoryCodeEntity> getOpeningCategories(
description = "Access token is missing or invalid",
content = @Content(schema = @Schema(implementation = Void.class)))
})
public List<OrgUnitEntity> 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<OrgUnitEntity> getOpeningOrgUnits() {
return orgUnitService.findAllOrgUnits();
}

/**
Expand Down Expand Up @@ -322,7 +313,7 @@ public List<OrgUnitEntity> getOpeningOrgUnitsByCode(
in = ParameterIn.QUERY,
description = "Defines the org units that should be included in the search",
required = false)
List<String> codes) {
String[] codes) {
return orgUnitService.findAllOrgUnitsByCode(codes);
}
}
Original file line number Diff line number Diff line change
@@ -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<OrgUnitEntity, Long> {

List<OrgUnitEntity> findAllByExpiryDateAfter(LocalDate now);

List<OrgUnitEntity> findAllByOrgUnitCodeIn(List<String> orgUnitCodes);
List<OrgUnitEntity> findAllByOrgUnitCodeIn(String[] orgUnitCodes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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<OrgUnitEntity> findAllOrgUnits(boolean includeExpired) {
log.info("Getting all org units. Include expired: {}", includeExpired);
public List<OrgUnitEntity> findAllOrgUnits() {
log.info("Getting all org units for the search openings");

if (includeExpired) {
List<OrgUnitEntity> 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<OrgUnitEntity> orgUnits = orgUnitRepository.findAllByExpiryDateAfter(LocalDate.now());
log.info("Found {} org units (excluding expired)", orgUnits.size());
List<OrgUnitEntity> orgUnits = orgUnitRepository.findAllByOrgUnitCodeIn(orgUnitsFromProps);

log.info("Found {} org units by codes", orgUnits.size());
return orgUnits;
}

Expand All @@ -42,8 +46,8 @@ public List<OrgUnitEntity> findAllOrgUnits(boolean includeExpired) {
* @param orgUnitCodes Org Unit codes to search for.
* @return List of {@link OrgUnitEntity} with found categories.
*/
public List<OrgUnitEntity> findAllOrgUnitsByCode(List<String> orgUnitCodes) {
log.info("Getting all org units by codes: {}", orgUnitCodes);
public List<OrgUnitEntity> findAllOrgUnitsByCode(String[] orgUnitCodes) {
log.info("Getting all org units by codes: {}", Arrays.toString(orgUnitCodes));

List<OrgUnitEntity> orgUnits = orgUnitRepository.findAllByOrgUnitCodeIn(orgUnitCodes);
log.info("Found {} org units by codes", orgUnits.size());
Expand Down
5 changes: 4 additions & 1 deletion backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void getOpeningOrgUnits_happyPath_shouldSucceed() throws Exception {

List<OrgUnitEntity> orgUnitEntityList = List.of(orgUnit);

when(orgUnitService.findAllOrgUnits(false)).thenReturn(orgUnitEntityList);
when(orgUnitService.findAllOrgUnits()).thenReturn(orgUnitEntityList);

mockMvc
.perform(
Expand Down Expand Up @@ -257,7 +257,7 @@ void getOpeningOrgUnitsByCode_happyPath_shouldSucceed() throws Exception {

List<OrgUnitEntity> orgUnitEntityList = List.of(orgUnit);

when(orgUnitService.findAllOrgUnitsByCode(List.of("DAS"))).thenReturn(orgUnitEntityList);
when(orgUnitService.findAllOrgUnitsByCode(new String[]{"DAS"})).thenReturn(orgUnitEntityList);

mockMvc
.perform(
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrgUnitEntity> list = orgUnitRepository.findAllByExpiryDateAfter(LocalDate.now());
@DisplayName("Find all by org unit code unit in happy path should succeed")
void findAllByOrgUnitCodeIn_happyPath_shouldSucceed() {
List<OrgUnitEntity> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
Expand All @@ -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<OrgUnitEntity> entities = orgUnitService.findAllOrgUnits(false);
when(orgUnitRepository.findAllByOrgUnitCodeIn(new String[] {"DAS"}))
.thenReturn(List.of(orgUnit));
List<OrgUnitEntity> entities = orgUnitService.findAllOrgUnits();

Assertions.assertNotNull(entities);
Assertions.assertEquals(1, entities.size());
Expand All @@ -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<OrgUnitEntity> entities = orgUnitService.findAllOrgUnits(true);
when(orgUnitRepository.findAllByOrgUnitCodeIn(new String[] {"DAS"})).thenReturn(List.of());
List<OrgUnitEntity> 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
Expand All @@ -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<OrgUnitEntity> entities = orgUnitService.findAllOrgUnitsByCode(List.of("DAS"));
String[] units = new String[] {"DAS"};

when(orgUnitRepository.findAllByOrgUnitCodeIn(units)).thenReturn(List.of(orgUnit));
List<OrgUnitEntity> entities = orgUnitService.findAllOrgUnitsByCode(units);

Assertions.assertNotNull(entities);
Assertions.assertEquals(1, entities.size());
Expand All @@ -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<OrgUnitEntity> entities = orgUnitService.findAllOrgUnitsByCode(List.of("DAS"));
String[] units = new String[] {"DAS"};
when(orgUnitRepository.findAllByOrgUnitCodeIn(units)).thenReturn(List.of());
List<OrgUnitEntity> entities = orgUnitService.findAllOrgUnitsByCode(units);

Assertions.assertNotNull(entities);
Assertions.assertTrue(entities.isEmpty());
Expand Down
2 changes: 2 additions & 0 deletions backend/src/test/resources/application-default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down

0 comments on commit 855cff3

Please sign in to comment.