From ced46dcd7f2ee7ab29fdf6e89eed85b88e6e0fd5 Mon Sep 17 00:00:00 2001 From: Ricardo Campos Date: Fri, 30 Aug 2024 15:23:48 -0300 Subject: [PATCH 1/2] feat: add open category api --- .../endpoint/OpeningSearchEndpoint.java | 37 ++++++++++++++++++ .../oracle/entity/OpenCategoryCodeEntity.java | 32 +++++++++++++++ .../OpenCategoryCodeRepository.java | 12 ++++++ .../service/OpenCategoryCodeService.java | 39 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OpenCategoryCodeEntity.java create mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepository.java create mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeService.java 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 a1ff0515..b6b2dcb0 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 @@ -5,6 +5,8 @@ import ca.bc.gov.restapi.results.common.pagination.PaginationParameters; import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchFiltersDto; import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto; +import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity; +import ca.bc.gov.restapi.results.oracle.service.OpenCategoryCodeService; import ca.bc.gov.restapi.results.oracle.service.OpeningService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -14,6 +16,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -31,6 +34,8 @@ public class OpeningSearchEndpoint { private final OpeningService openingService; + private final OpenCategoryCodeService openCategoryCodeService; + /** * Search for Openings with different filters. * @@ -188,4 +193,36 @@ public PaginatedResult openingSearch( mainSearchTerm); return openingService.openingSearch(filtersDto, paginationParameters); } + + /** + * Get all opening categories. Optionally you can ask for the expired ones. + * + * @param includeExpired Query param to include expired categories. + * @return List of OpenCategoryCodeEntity with found categories. + */ + @GetMapping("/categories") + @Operation( + summary = "Get all opening categories", + description = "Get all opening categories. Optionally you can ask for the expired ones.", + responses = { + @ApiResponse( + responseCode = "200", + description = "An array with found objects, or an empty array.", + content = @Content(mediaType = "application/json")), + @ApiResponse( + responseCode = "401", + description = "Access token is missing or invalid", + content = @Content(schema = @Schema(implementation = Void.class))) + }) + public List getOpeningCategories( + @RequestParam(value = "includeExpired", required = false) + @Parameter( + name = "includeExpired", + in = ParameterIn.QUERY, + description = "Defines if the API should include expired categories", + required = false) + Boolean includeExpired) { + boolean addExpired = Boolean.TRUE.equals(includeExpired); + return openCategoryCodeService.findAllCategories(addExpired); + } } diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OpenCategoryCodeEntity.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OpenCategoryCodeEntity.java new file mode 100644 index 00000000..1fff4ff8 --- /dev/null +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OpenCategoryCodeEntity.java @@ -0,0 +1,32 @@ +package ca.bc.gov.restapi.results.oracle.entity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import java.time.LocalDateTime; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@Entity +@Table(name = "OPEN_CATEGORY_CODE") +public class OpenCategoryCodeEntity { + + @Id + @Column(name = "OPEN_CATEGORY_CODE") + private String code; + + @Column(name = "DESCRIPTION", length = 120, nullable = false) + private String description; + + @Column(name = "EFFECTIVE_DATE", nullable = false) + private LocalDateTime effectiveDate; + + @Column(name = "EXPIRY_DATE", nullable = false) + private LocalDateTime expiryDate; + + @Column(name = "UPDATE_TIMESTAMP", nullable = false) + private LocalDateTime updateTimestamp; +} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepository.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepository.java new file mode 100644 index 00000000..5048a526 --- /dev/null +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepository.java @@ -0,0 +1,12 @@ +package ca.bc.gov.restapi.results.oracle.repository; + +import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity; +import java.time.LocalDateTime; +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; + +/** This interface provides methods to get, save, and manage data in the database. */ +public interface OpenCategoryCodeRepository extends JpaRepository { + + List findAllByExpiryDateAfter(LocalDateTime now); +} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeService.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeService.java new file mode 100644 index 00000000..c10ffc3f --- /dev/null +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeService.java @@ -0,0 +1,39 @@ +package ca.bc.gov.restapi.results.oracle.service; + +import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity; +import ca.bc.gov.restapi.results.oracle.repository.OpenCategoryCodeRepository; +import java.time.LocalDateTime; +import java.util.List; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** This class contains methods to handle Opening Categories. */ +@Slf4j +@Service +@RequiredArgsConstructor +public class OpenCategoryCodeService { + + private final OpenCategoryCodeRepository openCategoryCodeRepository; + + /** + * Find all Opening categories. Option to include expired ones. + * + * @param includeExpired True to include expired, false otherwise. + * @return List of {@link OpenCategoryCodeEntity} with found categories. + */ + public List findAllCategories(boolean includeExpired) { + log.info("Getting all open category codes. Include expired: {}", includeExpired); + + if (includeExpired) { + List openCategoryCodes = openCategoryCodeRepository.findAll(); + log.info("Found {} open category codes (including expired)", openCategoryCodes.size()); + return openCategoryCodes; + } + + List openCategoryCodes = + openCategoryCodeRepository.findAllByExpiryDateAfter(LocalDateTime.now()); + log.info("Found {} open category codes (excluding expired)", openCategoryCodes.size()); + return openCategoryCodes; + } +} From 9ba6dd6a7c2b8dc25caadab0b082e657b7e646b0 Mon Sep 17 00:00:00 2001 From: Ricardo Campos Date: Tue, 3 Sep 2024 16:46:20 -0300 Subject: [PATCH 2/2] feat: add api to get opening category and org units --- .../endpoint/OpeningSearchEndpoint.java | 67 +++++++ .../oracle/entity/OpenCategoryCodeEntity.java | 9 +- .../results/oracle/entity/OrgUnitEntity.java | 40 +++- .../OpenCategoryCodeRepository.java | 4 +- .../oracle/repository/OrgUnitRepository.java | 14 ++ .../service/OpenCategoryCodeService.java | 4 +- .../oracle/service/OrgUnitService.java | 52 +++++ .../endpoint/OpeningSearchEndpointTest.java | 180 ++++++++++++++++++ ...CategoryCodeRepositoryIntegrationTest.java | 36 ++++ .../OrgUnitRepositoryIntegrationTest.java | 46 +++++ .../service/OpenCategoryCodeServiceTest.java | 71 +++++++ .../oracle/service/OrgUnitServiceTest.java | 171 +++++++++++++++++ ...nCategoryCodeRepositoryIntegrationTest.sql | 9 + .../OrgUnitRepositoryIntegrationTest.sql | 19 ++ 14 files changed, 710 insertions(+), 12 deletions(-) create mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepository.java create mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitService.java create mode 100644 backend/src/test/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepositoryIntegrationTest.java create mode 100644 backend/src/test/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepositoryIntegrationTest.java create mode 100644 backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeServiceTest.java create mode 100644 backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitServiceTest.java create mode 100644 backend/src/test/resources/sql_scripts/OpenCategoryCodeRepositoryIntegrationTest.sql create mode 100644 backend/src/test/resources/sql_scripts/OrgUnitRepositoryIntegrationTest.sql 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 b6b2dcb0..0458c937 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 @@ -6,8 +6,10 @@ import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchFiltersDto; import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto; import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity; +import ca.bc.gov.restapi.results.oracle.entity.OrgUnitEntity; import ca.bc.gov.restapi.results.oracle.service.OpenCategoryCodeService; import ca.bc.gov.restapi.results.oracle.service.OpeningService; +import ca.bc.gov.restapi.results.oracle.service.OrgUnitService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.enums.ParameterIn; @@ -36,6 +38,8 @@ public class OpeningSearchEndpoint { private final OpenCategoryCodeService openCategoryCodeService; + private final OrgUnitService orgUnitService; + /** * Search for Openings with different filters. * @@ -225,4 +229,67 @@ public List getOpeningCategories( boolean addExpired = Boolean.TRUE.equals(includeExpired); return openCategoryCodeService.findAllCategories(addExpired); } + + /** + * Get all org units. + * + * @param includeExpired Query param to include expired org units. + * @return List of OrgUnitEntity with found org units. + */ + @GetMapping("/org-units") + @Operation( + summary = "Get all opening org units", + description = "Get all opening org units. Optionally you can ask for the expired ones.", + responses = { + @ApiResponse( + responseCode = "200", + description = "An array with found objects, or an empty array.", + content = @Content(mediaType = "application/json")), + @ApiResponse( + responseCode = "401", + 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); + } + + /** + * Get all org units by code. + * + * @param codes Org Unit codes to search for. + * @return List of OrgUnitEntity with found org units. + */ + @GetMapping("/org-units-by-code") + @Operation( + summary = "Get all opening org units by code", + description = "Get all opening org units by code.", + responses = { + @ApiResponse( + responseCode = "200", + description = "An array with found objects, or an empty array.", + content = @Content(mediaType = "application/json")), + @ApiResponse( + responseCode = "401", + description = "Access token is missing or invalid", + content = @Content(schema = @Schema(implementation = Void.class))) + }) + public List getOpeningOrgUnitsByCode( + @RequestParam(value = "orgUnitCodes", required = true) + @Parameter( + name = "orgUnitCodes", + in = ParameterIn.QUERY, + description = "Defines the org units that should be included in the search", + required = false) + List codes) { + return orgUnitService.findAllOrgUnitsByCode(codes); + } } diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OpenCategoryCodeEntity.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OpenCategoryCodeEntity.java index 1fff4ff8..a783936f 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OpenCategoryCodeEntity.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OpenCategoryCodeEntity.java @@ -4,10 +4,11 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; -import java.time.LocalDateTime; +import java.time.LocalDate; import lombok.Getter; import lombok.Setter; +/** This class represents an Opening Category in the database. */ @Getter @Setter @Entity @@ -22,11 +23,11 @@ public class OpenCategoryCodeEntity { private String description; @Column(name = "EFFECTIVE_DATE", nullable = false) - private LocalDateTime effectiveDate; + private LocalDate effectiveDate; @Column(name = "EXPIRY_DATE", nullable = false) - private LocalDateTime expiryDate; + private LocalDate expiryDate; @Column(name = "UPDATE_TIMESTAMP", nullable = false) - private LocalDateTime updateTimestamp; + private LocalDate updateTimestamp; } diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OrgUnitEntity.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OrgUnitEntity.java index 8589fa4b..66ac3f5f 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OrgUnitEntity.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/entity/OrgUnitEntity.java @@ -4,6 +4,7 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; +import java.time.LocalDate; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @@ -28,8 +29,39 @@ public class OrgUnitEntity { @Column(name = "ORG_UNIT_NAME", length = 100, nullable = false) private String orgUnitName; - // An Org Unit belongs to one Opening - // Foreign key referencing the opening table - // @OneToOne(mappedBy = "adminDistrict") - // private OpeningEntity openingEntity; + @Column(name = "LOCATION_CODE", length = 3, nullable = false) + private String locationCode; + + @Column(name = "AREA_CODE", length = 3, nullable = false) + private String areaCode; + + @Column(name = "TELEPHONE_NO", length = 7, nullable = false) + private String telephoneNo; + + @Column(name = "ORG_LEVEL_CODE", length = 1, nullable = false) + private Character orgLevelCode; + + @Column(name = "OFFICE_NAME_CODE", length = 2, nullable = false) + private String officeNameCode; + + @Column(name = "ROLLUP_REGION_NO", nullable = false) + private Long rollupRegionNo; + + @Column(name = "ROLLUP_REGION_CODE", length = 6, nullable = false) + private String rollupRegionCode; + + @Column(name = "ROLLUP_DIST_NO", nullable = false) + private Long rollupDistNo; + + @Column(name = "ROLLUP_DIST_CODE", length = 6, nullable = false) + private String rollupDistCode; + + @Column(name = "EFFECTIVE_DATE", nullable = false) + private LocalDate effectiveDate; + + @Column(name = "EXPIRY_DATE", nullable = false) + private LocalDate expiryDate; + + @Column(name = "UPDATE_TIMESTAMP", nullable = false) + private LocalDate updateTimestamp; } diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepository.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepository.java index 5048a526..4803d287 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepository.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepository.java @@ -1,12 +1,12 @@ package ca.bc.gov.restapi.results.oracle.repository; import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity; -import java.time.LocalDateTime; +import java.time.LocalDate; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; /** This interface provides methods to get, save, and manage data in the database. */ public interface OpenCategoryCodeRepository extends JpaRepository { - List findAllByExpiryDateAfter(LocalDateTime now); + List findAllByExpiryDateAfter(LocalDate now); } 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 new file mode 100644 index 00000000..2cf454e3 --- /dev/null +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepository.java @@ -0,0 +1,14 @@ +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); +} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeService.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeService.java index c10ffc3f..e6b4db83 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeService.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeService.java @@ -2,7 +2,7 @@ import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity; import ca.bc.gov.restapi.results.oracle.repository.OpenCategoryCodeRepository; -import java.time.LocalDateTime; +import java.time.LocalDate; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -32,7 +32,7 @@ public List findAllCategories(boolean includeExpired) { } List openCategoryCodes = - openCategoryCodeRepository.findAllByExpiryDateAfter(LocalDateTime.now()); + openCategoryCodeRepository.findAllByExpiryDateAfter(LocalDate.now()); log.info("Found {} open category codes (excluding expired)", openCategoryCodes.size()); return openCategoryCodes; } 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 new file mode 100644 index 00000000..18109ed9 --- /dev/null +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitService.java @@ -0,0 +1,52 @@ +package ca.bc.gov.restapi.results.oracle.service; + +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.List; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** This class contains methods to handle Org Units. */ +@Slf4j +@Service +@AllArgsConstructor +public class OrgUnitService { + + private final OrgUnitRepository orgUnitRepository; + + /** + * Find all Org Units. Option to include expired ones. + * + * @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); + + if (includeExpired) { + List orgUnits = orgUnitRepository.findAll(); + log.info("Found {} org units (including expired)", orgUnits.size()); + return orgUnits; + } + + List orgUnits = orgUnitRepository.findAllByExpiryDateAfter(LocalDate.now()); + log.info("Found {} org units (excluding expired)", orgUnits.size()); + return orgUnits; + } + + /** + * Find all Org Units by code. + * + * @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); + + List orgUnits = orgUnitRepository.findAllByOrgUnitCodeIn(orgUnitCodes); + log.info("Found {} org units by codes", orgUnits.size()); + return orgUnits; + } +} 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 0d1665b4..051b54f4 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 @@ -9,11 +9,17 @@ import ca.bc.gov.restapi.results.common.pagination.PaginatedResult; import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto; +import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity; +import ca.bc.gov.restapi.results.oracle.entity.OrgUnitEntity; import ca.bc.gov.restapi.results.oracle.enums.OpeningCategoryEnum; import ca.bc.gov.restapi.results.oracle.enums.OpeningStatusEnum; +import ca.bc.gov.restapi.results.oracle.service.OpenCategoryCodeService; import ca.bc.gov.restapi.results.oracle.service.OpeningService; +import ca.bc.gov.restapi.results.oracle.service.OrgUnitService; import java.math.BigDecimal; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.List; import org.hamcrest.Matchers; import org.junit.jupiter.api.DisplayName; @@ -33,6 +39,10 @@ class OpeningSearchEndpointTest { @MockBean private OpeningService openingService; + @MockBean private OpenCategoryCodeService openCategoryCodeService; + + @MockBean private OrgUnitService orgUnitService; + @Test @DisplayName("Opening search happy path should succeed") void openingSearch_happyPath_shouldSucceed() throws Exception { @@ -131,4 +141,174 @@ void openingSearch_noRecordsFound_shouldSucceed() throws Exception { .andExpect(jsonPath("$.data", Matchers.empty())) .andReturn(); } + + @Test + @DisplayName("Get Opening Categories happy Path should Succeed") + void getOpeningCategories_happyPath_shouldSucceed() throws Exception { + OpenCategoryCodeEntity category = new OpenCategoryCodeEntity(); + category.setCode("FTML"); + category.setDescription("Free Growing"); + category.setEffectiveDate(LocalDate.now().minusYears(3L)); + category.setExpiryDate(LocalDate.now().plusYears(3L)); + category.setUpdateTimestamp(LocalDate.now()); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + String effectiveDateStr = category.getEffectiveDate().format(formatter); + String expiryDateStr = category.getExpiryDate().format(formatter); + String updateTimestampStr = category.getUpdateTimestamp().format(formatter); + + List openCategoryCodeEntityList = List.of(category); + + when(openCategoryCodeService.findAllCategories(false)).thenReturn(openCategoryCodeEntityList); + + mockMvc + .perform( + get("/api/opening-search/categories") + .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/json")) + .andExpect(jsonPath("$[0].code").value(category.getCode())) + .andExpect(jsonPath("$[0].description").value(category.getDescription())) + .andExpect(jsonPath("$[0].effectiveDate").value(effectiveDateStr)) + .andExpect(jsonPath("$[0].expiryDate").value(expiryDateStr)) + .andExpect(jsonPath("$[0].updateTimestamp").value(updateTimestampStr)) + .andReturn(); + } + + @Test + @DisplayName("Get Opening Org Units happy Path should Succeed") + void getOpeningOrgUnits_happyPath_shouldSucceed() throws Exception { + 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().plusYears(3L)); + orgUnit.setUpdateTimestamp(LocalDate.now()); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + String effectiveDateStr = orgUnit.getEffectiveDate().format(formatter); + String expiryDateStr = orgUnit.getExpiryDate().format(formatter); + String updateTimestampStr = orgUnit.getUpdateTimestamp().format(formatter); + + List orgUnitEntityList = List.of(orgUnit); + + when(orgUnitService.findAllOrgUnits(false)).thenReturn(orgUnitEntityList); + + mockMvc + .perform( + get("/api/opening-search/org-units") + .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/json")) + .andExpect(jsonPath("$[0].orgUnitNo").value(orgUnit.getOrgUnitNo())) + .andExpect(jsonPath("$[0].orgUnitCode").value(orgUnit.getOrgUnitCode())) + .andExpect(jsonPath("$[0].orgUnitName").value(orgUnit.getOrgUnitName())) + .andExpect(jsonPath("$[0].locationCode").value(orgUnit.getLocationCode())) + .andExpect(jsonPath("$[0].areaCode").value(orgUnit.getAreaCode())) + .andExpect(jsonPath("$[0].telephoneNo").value(orgUnit.getTelephoneNo())) + .andExpect(jsonPath("$[0].orgLevelCode").value(orgUnit.getOrgLevelCode().toString())) + .andExpect(jsonPath("$[0].officeNameCode").value(orgUnit.getOfficeNameCode())) + .andExpect(jsonPath("$[0].rollupRegionNo").value(orgUnit.getRollupRegionNo())) + .andExpect(jsonPath("$[0].rollupRegionCode").value(orgUnit.getRollupRegionCode())) + .andExpect(jsonPath("$[0].rollupDistNo").value(orgUnit.getRollupDistNo())) + .andExpect(jsonPath("$[0].rollupDistCode").value(orgUnit.getRollupDistCode())) + .andExpect(jsonPath("$[0].effectiveDate").value(effectiveDateStr)) + .andExpect(jsonPath("$[0].expiryDate").value(expiryDateStr)) + .andExpect(jsonPath("$[0].updateTimestamp").value(updateTimestampStr)) + .andReturn(); + } + + @Test + @DisplayName("Get Opening Org Units By Code happy Path should Succeed") + void getOpeningOrgUnitsByCode_happyPath_shouldSucceed() throws Exception { + 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().plusYears(3L)); + orgUnit.setUpdateTimestamp(LocalDate.now()); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + String effectiveDateStr = orgUnit.getEffectiveDate().format(formatter); + String expiryDateStr = orgUnit.getExpiryDate().format(formatter); + String updateTimestampStr = orgUnit.getUpdateTimestamp().format(formatter); + + List orgUnitEntityList = List.of(orgUnit); + + when(orgUnitService.findAllOrgUnitsByCode(List.of("DAS"))).thenReturn(orgUnitEntityList); + + mockMvc + .perform( + get("/api/opening-search/org-units-by-code?orgUnitCodes=DAS") + .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/json")) + .andExpect(jsonPath("$[0].orgUnitNo").value(orgUnit.getOrgUnitNo())) + .andExpect(jsonPath("$[0].orgUnitCode").value(orgUnit.getOrgUnitCode())) + .andExpect(jsonPath("$[0].orgUnitName").value(orgUnit.getOrgUnitName())) + .andExpect(jsonPath("$[0].locationCode").value(orgUnit.getLocationCode())) + .andExpect(jsonPath("$[0].areaCode").value(orgUnit.getAreaCode())) + .andExpect(jsonPath("$[0].telephoneNo").value(orgUnit.getTelephoneNo())) + .andExpect(jsonPath("$[0].orgLevelCode").value(orgUnit.getOrgLevelCode().toString())) + .andExpect(jsonPath("$[0].officeNameCode").value(orgUnit.getOfficeNameCode())) + .andExpect(jsonPath("$[0].rollupRegionNo").value(orgUnit.getRollupRegionNo())) + .andExpect(jsonPath("$[0].rollupRegionCode").value(orgUnit.getRollupRegionCode())) + .andExpect(jsonPath("$[0].rollupDistNo").value(orgUnit.getRollupDistNo())) + .andExpect(jsonPath("$[0].rollupDistCode").value(orgUnit.getRollupDistCode())) + .andExpect(jsonPath("$[0].effectiveDate").value(effectiveDateStr)) + .andExpect(jsonPath("$[0].expiryDate").value(expiryDateStr)) + .andExpect(jsonPath("$[0].updateTimestamp").value(updateTimestampStr)) + .andReturn(); + } + + @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()); + + mockMvc + .perform( + get("/api/opening-search/org-units-by-code?orgUnitCodes=DAS") + .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/json")) + .andExpect(jsonPath("$", Matchers.empty())) + .andReturn(); + } + + @Test + @DisplayName("Get Opening Org Units By Code bad Request should Succeed") + void getOpeningOrgUnitsByCode_badRequest_shouldFail() throws Exception { + mockMvc + .perform( + get("/api/opening-search/org-units-by-code?") + .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()) + .andReturn(); + } } diff --git a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepositoryIntegrationTest.java b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepositoryIntegrationTest.java new file mode 100644 index 00000000..01990c00 --- /dev/null +++ b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/repository/OpenCategoryCodeRepositoryIntegrationTest.java @@ -0,0 +1,36 @@ +package ca.bc.gov.restapi.results.oracle.repository; + +import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity; +import java.time.LocalDate; +import java.util.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.jdbc.Sql; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = Replace.NONE) +@Sql(scripts = {"classpath:sql_scripts/OpenCategoryCodeRepositoryIntegrationTest.sql"}) +class OpenCategoryCodeRepositoryIntegrationTest { + + @Autowired private OpenCategoryCodeRepository openCategoryCodeRepository; + + @Test + @DisplayName("Find all by expiry date after happy path should succeed") + void findAllByExpiryDateAfter_happyPath_shouldSucceed() { + List list = + openCategoryCodeRepository.findAllByExpiryDateAfter(LocalDate.now()); + + Assertions.assertNotNull(list); + Assertions.assertEquals(1, list.size()); + + OpenCategoryCodeEntity category = list.get(0); + Assertions.assertEquals("AAA", category.getCode()); + Assertions.assertEquals("AAA description valid", category.getDescription()); + Assertions.assertTrue(category.getExpiryDate().isAfter(LocalDate.now())); + } +} 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 new file mode 100644 index 00000000..eab69cad --- /dev/null +++ b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/repository/OrgUnitRepositoryIntegrationTest.java @@ -0,0 +1,46 @@ +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.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.jdbc.Sql; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = Replace.NONE) +@Sql(scripts = {"classpath:sql_scripts/OrgUnitRepositoryIntegrationTest.sql"}) +class OrgUnitRepositoryIntegrationTest { + + @Autowired 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()); + + Assertions.assertNotNull(list); + Assertions.assertEquals(1, list.size()); + + OrgUnitEntity orgUnit = list.get(0); + Assertions.assertEquals(1L, orgUnit.getOrgUnitNo()); + Assertions.assertEquals("ONE", orgUnit.getOrgUnitCode()); + Assertions.assertEquals("Org one", orgUnit.getOrgUnitName()); + Assertions.assertEquals("001", orgUnit.getLocationCode()); + Assertions.assertEquals("AAA", orgUnit.getAreaCode()); + Assertions.assertEquals("1122334", orgUnit.getTelephoneNo()); + Assertions.assertEquals('H', orgUnit.getOrgLevelCode()); + Assertions.assertEquals("VI", orgUnit.getOfficeNameCode()); + Assertions.assertEquals(111L, orgUnit.getRollupRegionNo()); + Assertions.assertEquals("1Code", orgUnit.getRollupRegionCode()); + Assertions.assertEquals(222L, orgUnit.getRollupDistNo()); + Assertions.assertEquals("22Code", orgUnit.getRollupDistCode()); + Assertions.assertTrue(orgUnit.getEffectiveDate().isBefore(LocalDate.now())); + Assertions.assertTrue(orgUnit.getExpiryDate().isAfter(LocalDate.now())); + } +} diff --git a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeServiceTest.java b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeServiceTest.java new file mode 100644 index 00000000..8a2561df --- /dev/null +++ b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OpenCategoryCodeServiceTest.java @@ -0,0 +1,71 @@ +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; + +import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity; +import ca.bc.gov.restapi.results.oracle.repository.OpenCategoryCodeRepository; +import java.time.LocalDate; +import java.util.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +class OpenCategoryCodeServiceTest { + + @Mock OpenCategoryCodeRepository openCategoryCodeRepository; + + private OpenCategoryCodeService openCategoryCodeService; + + @BeforeEach + void setup() { + openCategoryCodeService = new OpenCategoryCodeService(openCategoryCodeRepository); + } + + @Test + @DisplayName("Find all categories include expired false should succeed") + void findAllCategories_includeExpiredFalse_shouldSucceed() { + OpenCategoryCodeEntity category = new OpenCategoryCodeEntity(); + category.setCode("FTML"); + category.setDescription("Free Growing"); + category.setEffectiveDate(LocalDate.now().minusYears(3L)); + category.setExpiryDate(LocalDate.now().plusYears(3L)); + category.setUpdateTimestamp(LocalDate.now()); + + when(openCategoryCodeRepository.findAllByExpiryDateAfter(any())).thenReturn(List.of(category)); + List entities = openCategoryCodeService.findAllCategories(false); + + Assertions.assertNotNull(entities); + Assertions.assertEquals(1, entities.size()); + Assertions.assertTrue(entities.get(0).getExpiryDate().isAfter(LocalDate.now())); + + verify(openCategoryCodeRepository, times(0)).findAll(); + } + + @Test + @DisplayName("Find all categories include expired true should succeed") + void findAllCategories_includeExpiredTrue_shouldSucceed() { + OpenCategoryCodeEntity category = new OpenCategoryCodeEntity(); + category.setCode("FTML"); + category.setDescription("Free Growing"); + category.setEffectiveDate(LocalDate.now().minusYears(3L)); + category.setExpiryDate(LocalDate.now().minusYears(1L)); + category.setUpdateTimestamp(LocalDate.now()); + + when(openCategoryCodeRepository.findAll()).thenReturn(List.of(category)); + List entities = openCategoryCodeService.findAllCategories(true); + + Assertions.assertNotNull(entities); + Assertions.assertEquals(1, entities.size()); + Assertions.assertTrue(entities.get(0).getExpiryDate().isBefore(LocalDate.now())); + + verify(openCategoryCodeRepository, times(0)).findAllByExpiryDateAfter(any()); + } +} 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 new file mode 100644 index 00000000..6d2acafe --- /dev/null +++ b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OrgUnitServiceTest.java @@ -0,0 +1,171 @@ +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; + +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.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +class OrgUnitServiceTest { + + @Mock OrgUnitRepository orgUnitRepository; + + private OrgUnitService orgUnitService; + + @BeforeEach + void setup() { + orgUnitService = new OrgUnitService(orgUnitRepository); + } + + @Test + @DisplayName("Find all org units include expired false should succeed") + void findAllOrgUnits_includeExpiredFalse_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().plusYears(3L)); + orgUnit.setUpdateTimestamp(LocalDate.now()); + + when(orgUnitRepository.findAllByExpiryDateAfter(any())).thenReturn(List.of(orgUnit)); + List entities = orgUnitService.findAllOrgUnits(false); + + Assertions.assertNotNull(entities); + Assertions.assertEquals(1, entities.size()); + + OrgUnitEntity responseOrg = entities.get(0); + Assertions.assertTrue(responseOrg.getExpiryDate().isAfter(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()); + + verify(orgUnitRepository, times(0)).findAll(); + } + + @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()); + + when(orgUnitRepository.findAll()).thenReturn(List.of(orgUnit)); + List entities = orgUnitService.findAllOrgUnits(true); + + 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()); + + verify(orgUnitRepository, times(0)).findAllByExpiryDateAfter(any()); + } + + @Test + @DisplayName("Find all org units by code happy path should succeed") + void findAllOrgUnitsByCode_happyPath_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()); + + when(orgUnitRepository.findAllByOrgUnitCodeIn(List.of("DAS"))).thenReturn(List.of(orgUnit)); + List entities = orgUnitService.findAllOrgUnitsByCode(List.of("DAS")); + + 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()); + } + + @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")); + + Assertions.assertNotNull(entities); + Assertions.assertTrue(entities.isEmpty()); + } +} diff --git a/backend/src/test/resources/sql_scripts/OpenCategoryCodeRepositoryIntegrationTest.sql b/backend/src/test/resources/sql_scripts/OpenCategoryCodeRepositoryIntegrationTest.sql new file mode 100644 index 00000000..256d1e63 --- /dev/null +++ b/backend/src/test/resources/sql_scripts/OpenCategoryCodeRepositoryIntegrationTest.sql @@ -0,0 +1,9 @@ +insert into OPEN_CATEGORY_CODE ( + OPEN_CATEGORY_CODE, + DESCRIPTION, + EFFECTIVE_DATE, + EXPIRY_DATE, + UPDATE_TIMESTAMP +) values + ('AAA', 'AAA description valid', '2020-01-01', '9999-12-31', '2024-09-03'), + ('BBB', 'BBB description expired', '2020-01-01', '2022-12-31', '2022-12-31'); diff --git a/backend/src/test/resources/sql_scripts/OrgUnitRepositoryIntegrationTest.sql b/backend/src/test/resources/sql_scripts/OrgUnitRepositoryIntegrationTest.sql new file mode 100644 index 00000000..a14dd1e3 --- /dev/null +++ b/backend/src/test/resources/sql_scripts/OrgUnitRepositoryIntegrationTest.sql @@ -0,0 +1,19 @@ +insert into 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', '2020-01-01', '9999-12-31', '2024-09-03'), + (2, 'TWO', 'Org two', '002', 'BBB', '2233445', 'R', 'CA', 333, '3Code', 444, '44Code', '2020-01-01', '2022-12-31', '2022-12-31');