From 59d2e40dc44bcb13bbfe4e1f65c4a8f1ab9b75f7 Mon Sep 17 00:00:00 2001 From: Paulo Gomes da Cruz Junior Date: Fri, 20 Dec 2024 05:34:19 -0800 Subject: [PATCH] chore: removing unused dashboard code --- .../common/dto/OracleExtractionDto.java | 27 -- .../common/dto/OracleExtractionParamsDto.java | 13 - .../common/job/DashboardExtractionJob.java | 26 -- .../service/DashboardExtractionService.java | 57 ---- .../oracle/dto/DashboardActionCodeDto.java | 17 - .../oracle/dto/DashboardOpeningDto.java | 37 --- .../dto/DashboardOpeningSubmissionDto.java | 17 - .../oracle/dto/DashboardOrgUnitDto.java | 20 -- .../oracle/dto/DashboardResultsAuditDto.java | 31 -- .../oracle/dto/DashboardStockingEventDto.java | 31 -- .../service/OracleExtractionService.java | 257 --------------- .../service/DashboardInsertionService.java | 268 ---------------- .../DashboardExtractionServiceTest.java | 87 ----- .../service/OracleExtractionServiceTest.java | 289 ----------------- .../DashboardInsertionServiceTest.java | 300 ------------------ 15 files changed, 1477 deletions(-) delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/common/dto/OracleExtractionDto.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/common/dto/OracleExtractionParamsDto.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/common/job/DashboardExtractionJob.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/common/service/DashboardExtractionService.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardActionCodeDto.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOpeningDto.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOpeningSubmissionDto.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOrgUnitDto.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardResultsAuditDto.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardStockingEventDto.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OracleExtractionService.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/postgres/service/DashboardInsertionService.java delete mode 100644 backend/src/test/java/ca/bc/gov/restapi/results/common/service/DashboardExtractionServiceTest.java delete mode 100644 backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OracleExtractionServiceTest.java delete mode 100644 backend/src/test/java/ca/bc/gov/restapi/results/postgres/service/DashboardInsertionServiceTest.java diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/common/dto/OracleExtractionDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/common/dto/OracleExtractionDto.java deleted file mode 100644 index 12f5f44d8..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/common/dto/OracleExtractionDto.java +++ /dev/null @@ -1,27 +0,0 @@ -package ca.bc.gov.restapi.results.common.dto; - -import ca.bc.gov.restapi.results.oracle.dto.DashboardActionCodeDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOpeningDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOpeningSubmissionDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOrgUnitDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardResultsAuditDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardStockingEventDto; -import java.util.List; -import lombok.Builder; -import lombok.With; - -/** - * This record holds all data extracted from Oracle to be inserted on Postgres. - */ -@Builder -@With -public record OracleExtractionDto( - List mainOpenings, - List openingSubmissions, - List resultsAudits, - List stockingEvents, - List orgUnits, - List actionCodes, - List logMessages) { - -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/common/dto/OracleExtractionParamsDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/common/dto/OracleExtractionParamsDto.java deleted file mode 100644 index 8ed3b49e9..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/common/dto/OracleExtractionParamsDto.java +++ /dev/null @@ -1,13 +0,0 @@ -package ca.bc.gov.restapi.results.common.dto; - -import lombok.Builder; -import lombok.With; - -/** - * This record represents the filters for the oracle extraction process. - */ -@Builder -@With -public record OracleExtractionParamsDto(Integer months, Boolean debug, Boolean manuallyTriggered) { - -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/common/job/DashboardExtractionJob.java b/backend/src/main/java/ca/bc/gov/restapi/results/common/job/DashboardExtractionJob.java deleted file mode 100644 index 550605ba4..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/common/job/DashboardExtractionJob.java +++ /dev/null @@ -1,26 +0,0 @@ -package ca.bc.gov.restapi.results.common.job; - -import ca.bc.gov.restapi.results.common.service.DashboardExtractionService; -import lombok.RequiredArgsConstructor; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -/** This class contains the job responsible for extracting data from oracle for the dashboard. */ -@Component -@RequiredArgsConstructor -public class DashboardExtractionJob { - - private final DashboardExtractionService dashboardExtractionService; - - /** - * Scheduled job for extracting data from oracle and adding into Postgres. Spring Boot uses a - * different cron expression, having: 1 - second (0-59), 2 - minute (0-59) 3, - hour (0-23), 4 - - * day of month (1-31), 5 - month (1-12) or (JAN-DEC), 6 - day of the week (0-7) [0 or 7 is - * Sunday] or (MON-SUN) More: - * https://docs.spring.io/spring-framework/reference/integration/scheduling.html#scheduling-cron-expression - */ - @Scheduled(cron = "0 0 23 * * MON-FRI") - public void extractDataForTheDashboard() { - dashboardExtractionService.extractDataForTheDashboard(null, null, null); - } -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/common/service/DashboardExtractionService.java b/backend/src/main/java/ca/bc/gov/restapi/results/common/service/DashboardExtractionService.java deleted file mode 100644 index af01199e1..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/common/service/DashboardExtractionService.java +++ /dev/null @@ -1,57 +0,0 @@ -package ca.bc.gov.restapi.results.common.service; - -import ca.bc.gov.restapi.results.common.dto.OracleExtractionDto; -import ca.bc.gov.restapi.results.common.dto.OracleExtractionParamsDto; -import ca.bc.gov.restapi.results.common.dto.OracleLogDto; -import ca.bc.gov.restapi.results.oracle.service.OracleExtractionService; -import ca.bc.gov.restapi.results.postgres.service.DashboardInsertionService; -import java.time.LocalDateTime; -import java.util.Objects; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.scheduling.annotation.Async; -import org.springframework.stereotype.Service; - -/** - * This class contains the service responsible for extracting data from oracle for the dashboard. - */ -@Slf4j -@Service -@RequiredArgsConstructor -public class DashboardExtractionService { - - private final OracleExtractionService oracleExtractionService; - - private final DashboardInsertionService dashboardInsertionService; - - /** - * Service for extracting data from oracle and adding into Postgres. - * - * @param months Optional number of months. - * @param debug Optional debug mode enabled. - * @param manuallyTriggered Optional option. - */ - @Async - public void extractDataForTheDashboard(Integer months, Boolean debug, Boolean manuallyTriggered) { - OracleExtractionParamsDto params = getParams(months, debug, manuallyTriggered); - - LocalDateTime startDateTime = LocalDateTime.now(); - String message = "Starting extraction"; - log.info(message); - - OracleExtractionDto extractionDto = oracleExtractionService.getOpeningActivities(params); - - // Add the first log message - extractionDto.logMessages().add(0, new OracleLogDto(message, startDateTime)); - - dashboardInsertionService.loadDashboardData(extractionDto, startDateTime, params); - } - - private OracleExtractionParamsDto getParams(Integer months, Boolean debug, Boolean manuallyTriggered) { - return new OracleExtractionParamsDto( - Objects.requireNonNullElse(months, 24), - Objects.requireNonNullElse(debug, Boolean.FALSE), - Objects.requireNonNullElse(manuallyTriggered, Boolean.FALSE) - ); -} -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardActionCodeDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardActionCodeDto.java deleted file mode 100644 index 61d9551e8..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardActionCodeDto.java +++ /dev/null @@ -1,17 +0,0 @@ -package ca.bc.gov.restapi.results.oracle.dto; - -/** This interface represent an action code in the database. */ -public interface DashboardActionCodeDto { - - String getResultsAuditActionCode(); - - String getDescription(); - - /** Default method for logging the interface. */ - default String toLogString() { - StringBuilder logDto = new StringBuilder("{"); - logDto.append("resultsAuditActionCode='").append(getResultsAuditActionCode()); - logDto.append("', getDescription='").append(getDescription()).append("'}"); - return logDto.toString(); - } -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOpeningDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOpeningDto.java deleted file mode 100644 index 2f47c863f..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOpeningDto.java +++ /dev/null @@ -1,37 +0,0 @@ -package ca.bc.gov.restapi.results.oracle.dto; - -import java.time.LocalDateTime; - -/** This interface holds all opening columns required for the dashboard metrics. */ -public interface DashboardOpeningDto { - - Long getOpeningId(); - - String getOpeningStatusCode(); - - String getEntryUserId(); - - LocalDateTime getEntryTimestamp(); - - LocalDateTime getUpdateTimestamp(); - - Long getAdminDistrictNo(); - - Long getResultsSubmissionId(); - - LocalDateTime getActionTimestamp(); - - /** Default method for logging the interface. */ - default String toLogString() { - StringBuilder logDto = new StringBuilder("{"); - logDto.append("openingId=").append(getOpeningId()).append(", "); - logDto.append("openingStatusCode='").append(getOpeningStatusCode()); - logDto.append("', entryUserId='").append(getEntryUserId()).append("', "); - logDto.append("entryTimestamp='").append(getEntryTimestamp()).append("', "); - logDto.append("updateTimestamp='").append(getUpdateTimestamp()); - logDto.append("', adminDistrictNo=").append(getAdminDistrictNo()); - logDto.append(", resultsSubmissionId=").append(getResultsSubmissionId()); - logDto.append(", actionTimestamp='").append(getActionTimestamp()).append("'}"); - return logDto.toString(); - } -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOpeningSubmissionDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOpeningSubmissionDto.java deleted file mode 100644 index bea29d262..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOpeningSubmissionDto.java +++ /dev/null @@ -1,17 +0,0 @@ -package ca.bc.gov.restapi.results.oracle.dto; - -/** This interface holds openings submission ids with client numbers. */ -public interface DashboardOpeningSubmissionDto { - - Long getResultsSubmissionId(); - - String getClientNumber(); - - /** Default method for logging the interface. */ - default String toLogString() { - StringBuilder logDto = new StringBuilder("{"); - logDto.append("resultsSubmissionId=").append(getResultsSubmissionId()); - logDto.append(", clientNumber='").append(getClientNumber()).append("'}"); - return logDto.toString(); - } -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOrgUnitDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOrgUnitDto.java deleted file mode 100644 index 8e98dfd56..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardOrgUnitDto.java +++ /dev/null @@ -1,20 +0,0 @@ -package ca.bc.gov.restapi.results.oracle.dto; - -/** This interface represents a org unit record in the database. */ -public interface DashboardOrgUnitDto { - - Long getOrgUnitNo(); - - String getOrgUnitCode(); - - String getOrgUnitName(); - - /** Default method for logging the interface. */ - default String toLogString() { - StringBuilder logDto = new StringBuilder("{"); - logDto.append("orgUnitNo=").append(getOrgUnitNo()).append(", "); - logDto.append("orgUnitCode='").append(getOrgUnitCode()).append("', "); - logDto.append("orgUnitName='").append(getOrgUnitName()).append("'}"); - return logDto.toString(); - } -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardResultsAuditDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardResultsAuditDto.java deleted file mode 100644 index 5fbcd2cb6..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardResultsAuditDto.java +++ /dev/null @@ -1,31 +0,0 @@ -package ca.bc.gov.restapi.results.oracle.dto; - -import java.time.LocalDateTime; - -/** This record holds columns for the results audit table. */ -public interface DashboardResultsAuditDto { - - String getResultsAuditActionCode(); - - LocalDateTime getActionDate(); - - LocalDateTime getEntryTimestamp(); - - String getEntryUserid(); - - Long getOpeningId(); - - LocalDateTime getActionTimestamp(); - - /** Default method for logging the interface. */ - default String toLogString() { - StringBuilder logDto = new StringBuilder("{"); - logDto.append("resultsAuditActionCode='").append(getResultsAuditActionCode()); - logDto.append("', actionDate='").append(getActionDate()).append(", "); - logDto.append("entryTimestamp='").append(getEntryTimestamp()).append("', "); - logDto.append("entryUserid='").append(getEntryUserid()).append("', "); - logDto.append("openingId=").append(getOpeningId()).append(", "); - logDto.append("actionTimestamp=").append(getActionTimestamp()).append("'}"); - return logDto.toString(); - } -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardStockingEventDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardStockingEventDto.java deleted file mode 100644 index 2ccc4e260..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/DashboardStockingEventDto.java +++ /dev/null @@ -1,31 +0,0 @@ -package ca.bc.gov.restapi.results.oracle.dto; - -import java.time.LocalDateTime; - -/** This interface represents a stocking event history in the database. */ -public interface DashboardStockingEventDto { - - String getResultsAuditActionCode(); - - String getEntryUserid(); - - Long getOpeningId(); - - LocalDateTime getEntryTimestamp(); - - LocalDateTime getAmendEventTimestamp(); - - LocalDateTime getActionTimestamp(); - - /** Default method for logging the interface. */ - default String toLogString() { - StringBuilder logDto = new StringBuilder("{"); - logDto.append("resultsAuditActionCode='").append(getResultsAuditActionCode()); - logDto.append("', entryUserid='").append(getEntryUserid()).append("', "); - logDto.append("openingId=").append(getOpeningId()).append(", "); - logDto.append("entryTimestamp='").append(getEntryTimestamp()).append("', "); - logDto.append("amendEventTimestamp='").append(getAmendEventTimestamp()); - logDto.append("', actionTimestamp='").append(getActionTimestamp()).append("'}"); - return logDto.toString(); - } -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OracleExtractionService.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OracleExtractionService.java deleted file mode 100644 index e17b864e7..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/service/OracleExtractionService.java +++ /dev/null @@ -1,257 +0,0 @@ -package ca.bc.gov.restapi.results.oracle.service; - -import ca.bc.gov.restapi.results.common.dto.OracleExtractionDto; -import ca.bc.gov.restapi.results.common.dto.OracleExtractionParamsDto; -import ca.bc.gov.restapi.results.common.dto.OracleLogDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardActionCodeDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOpeningDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOpeningSubmissionDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOrgUnitDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardResultsAuditDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardStockingEventDto; -import ca.bc.gov.restapi.results.oracle.repository.OpeningRepository; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; - -/** This class contains methods for extracting data from Oracle THE schema. */ -@Slf4j -@Service -@RequiredArgsConstructor -public class OracleExtractionService { - - private final OpeningRepository openingRepository; - - /** - * Get all data from Oracle required for the dashboard metrics page. - * - * @return A {@link OracleExtractionDto} containing all data extracted. - */ - public OracleExtractionDto getOpeningActivities(OracleExtractionParamsDto params) { - List logMessages = new ArrayList<>(); - - // Openings main table - logAndSave( - logMessages, - "Querying Openings (on THE.OPENING main table) from the last {} months", - params.months()); - List mainOpenings = - openingRepository.findAllDashboardOpenings(params.months()); - logAndSave(logMessages, "{} record(s) found (on THE.OPENING main table)", mainOpenings.size()); - - if (Boolean.TRUE.equals(params.debug())) { - logAndSave(logMessages, "DEBUG mode ON! Logging each found record on THE.OPENING"); - for (DashboardOpeningDto openingDto : mainOpenings) { - logAndSave(logMessages, "DashboardOpeningDto={}", openingDto.toLogString()); - } - } - - // Opening Submissions - List submissionsIds = - mainOpenings.stream() - .filter(x -> !Objects.isNull(x.getResultsSubmissionId())) - .map(DashboardOpeningDto::getResultsSubmissionId) - .toList(); - - if (Boolean.TRUE.equals(params.debug())) { - logAndSave(logMessages, "DEBUG mode ON! Logging all Submission IDs for the next query"); - - if (submissionsIds.size() > 800) { - int perPage = 800; - int size = (submissionsIds.size() / perPage) + 1; - boolean stop = false; - for (int i = 0; i < size; i++) { - int start = i * perPage; - int end = i * perPage + perPage; - - if (end > submissionsIds.size() - 1) { - end = submissionsIds.size() - 1; - } - - List slice = submissionsIds.subList(start, end); - if (slice.isEmpty() || stop) { - break; - } - - logAndSave(logMessages, "IDs: {}", slice); - } - } else { - logAndSave(logMessages, "IDs: {}", submissionsIds); - } - } - - logAndSave( - logMessages, - "Querying Submissions (on THE.RESULTS_ELECTRONIC_SUBMISSION table) using {} Opening IDs" - + " found in the previous query", - submissionsIds.size()); - - List openingSubmissions = - openingRepository.findAllDashboardOpeningSubmissions(submissionsIds); - logAndSave( - logMessages, - "{} record(s) found (on THE.RESULTS_ELECTRONIC_SUBMISSION table)", - openingSubmissions.size()); - - if (Boolean.TRUE.equals(params.debug())) { - logAndSave( - logMessages, - "DEBUG mode ON! Logging each found record on THE.RESULTS_ELECTRONIC_SUBMISSION"); - for (DashboardOpeningSubmissionDto submissionDto : openingSubmissions) { - logAndSave(logMessages, "DashboardOpeningSubmissionDto={}", submissionDto.toLogString()); - } - } - - // RESULTS Audit - logAndSave( - logMessages, - "Querying Audit Events (on THE.RESULTS_AUDIT_EVENT table) from the last {} months", - params.months()); - List resultsAudits = - openingRepository.findAllDashboardAuditEvents(params.months()); - logAndSave( - logMessages, "{} record(s) found (on THE.RESULTS_AUDIT_EVENT table)", resultsAudits.size()); - - if (Boolean.TRUE.equals(params.debug())) { - logAndSave( - logMessages, "DEBUG mode ON! Logging each found record on THE.RESULTS_AUDIT_EVENT"); - for (DashboardResultsAuditDto resultsAudit : resultsAudits) { - logAndSave(logMessages, "DashboardResultsAuditDto={}", resultsAudit.toLogString()); - } - } - - // Stocking Event History - logAndSave( - logMessages, - "Querying Stocking Events (on THE.STOCKING_EVENT_HISTORY table) from the last {} months", - params.months()); - List stockingEvents = - openingRepository.findAllDashboardStockingEventHistory(params.months()); - logAndSave( - logMessages, - "{} record(s) found (on THE.STOCKING_EVENT_HISTORY table)", - stockingEvents.size()); - - if (Boolean.TRUE.equals(params.debug())) { - logAndSave( - logMessages, "DEBUG mode ON! Logging each found record on THE.STOCKING_EVENT_HISTORY"); - for (DashboardStockingEventDto stockingEvent : stockingEvents) { - logAndSave(logMessages, "DashboardStockingEventDto={}", stockingEvent.toLogString()); - } - } - - // Org Unit - Code Table - List orgUnitIds = - mainOpenings.stream().map(DashboardOpeningDto::getAdminDistrictNo).toList(); - logAndSave( - logMessages, - "Gathered {} district(s) (Org Units) found on the Openings", - orgUnitIds.size()); - - if (Boolean.TRUE.equals(params.debug())) { - logAndSave(logMessages, "DEBUG mode ON! Logging all Org Unit Codes for the next query"); - - if (orgUnitIds.size() > 800) { - int perPage = 800; - int size = (orgUnitIds.size() / perPage) + 1; - boolean stop = false; - for (int i = 0; i < size; i++) { - int start = i * perPage; - int end = i * perPage + perPage; - - if (end > orgUnitIds.size() - 1) { - end = orgUnitIds.size() - 1; - } - - List slice = orgUnitIds.subList(start, end); - if (slice.isEmpty() || stop) { - break; - } - - logAndSave(logMessages, "IDs: {}", slice); - } - } else { - logAndSave(logMessages, "IDs: {}", orgUnitIds); - } - } - - logAndSave( - logMessages, - "Querying Org Units (on THE.ORG_UNIT table) using {} Codes found in the previous query", - orgUnitIds.size()); - List orgUnits = openingRepository.findAllDashboardOrgUnits(orgUnitIds); - logAndSave(logMessages, "{} record(s) found (on THE.ORG_UNIT table)", orgUnits.size()); - - if (Boolean.TRUE.equals(params.debug())) { - logAndSave(logMessages, "DEBUG mode ON! Logging all Org Units found"); - - for (DashboardOrgUnitDto orgUnit : orgUnits) { - logAndSave(logMessages, "DashboardOrgUnitDto={}", orgUnit.toLogString()); - } - } - - // Action Codes - Code Table - Set codesSet = new HashSet<>(); - codesSet.addAll( - resultsAudits.stream().map(DashboardResultsAuditDto::getResultsAuditActionCode).toList()); - codesSet.addAll( - stockingEvents.stream().map(DashboardStockingEventDto::getResultsAuditActionCode).toList()); - - logAndSave( - logMessages, - "Gathered {} unique action codes found on both Results Audit and Stocking Events", - codesSet.size()); - - if (Boolean.TRUE.equals(params.debug())) { - logAndSave(logMessages, "DEBUG mode ON! Logging all Action Codes for the next query"); - logAndSave(logMessages, "Codes: {}", codesSet); - } - - logAndSave( - logMessages, - "Querying Action Codes (on THE.RESULTS_AUDIT_ACTION_CODE table) using {} Codes found in the" - + " previous query", - codesSet.size()); - - List codes = new ArrayList<>(codesSet); - List actionCodes = openingRepository.findAllDashboardActionCodes(codes); - logAndSave( - logMessages, - "{} record(s) found (on THE.RESULTS_AUDIT_ACTION_CODE table)", - actionCodes.size()); - - if (Boolean.TRUE.equals(params.debug())) { - logAndSave(logMessages, "DEBUG mode ON! Logging all Action Codes found"); - - for (DashboardActionCodeDto actionCode : actionCodes) { - logAndSave(logMessages, "DashboardActionCodeDto={}", actionCode.toLogString()); - } - } - - return new OracleExtractionDto( - mainOpenings, - openingSubmissions, - resultsAudits, - stockingEvents, - orgUnits, - actionCodes, - logMessages); - } - - private void logAndSave(List logMessages, String message, Object... params) { - log.info(message, params); - - if (params.length > 0) { - for (int i = 0, len = params.length; i < len; i++) { - message = message.replaceFirst("\\{\\}", String.valueOf(params[i])); - } - } - logMessages.add(new OracleLogDto(message, LocalDateTime.now())); - } -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/service/DashboardInsertionService.java b/backend/src/main/java/ca/bc/gov/restapi/results/postgres/service/DashboardInsertionService.java deleted file mode 100644 index 5299166f6..000000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/service/DashboardInsertionService.java +++ /dev/null @@ -1,268 +0,0 @@ -package ca.bc.gov.restapi.results.postgres.service; - -import ca.bc.gov.restapi.results.common.dto.OracleExtractionDto; -import ca.bc.gov.restapi.results.common.dto.OracleExtractionParamsDto; -import ca.bc.gov.restapi.results.common.dto.OracleLogDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOpeningDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOpeningSubmissionDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardResultsAuditDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardStockingEventDto; -import ca.bc.gov.restapi.results.postgres.entity.OpeningsActivityEntity; -import ca.bc.gov.restapi.results.postgres.entity.OpeningsActivityEntityId; -import ca.bc.gov.restapi.results.postgres.entity.OpeningsLastYearEntity; -import ca.bc.gov.restapi.results.postgres.entity.OracleExtractionLogsEntity; -import ca.bc.gov.restapi.results.postgres.repository.OpeningsActivityRepository; -import ca.bc.gov.restapi.results.postgres.repository.OpeningsLastYearRepository; -import ca.bc.gov.restapi.results.postgres.repository.OracleExtractionLogsRepository; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -/** This class contains methods for loading data extracted from oracle into postgres. */ -@Slf4j -@Service -@RequiredArgsConstructor -public class DashboardInsertionService { - - private final OpeningsLastYearRepository openingsLastYearRepository; - - private final OpeningsActivityRepository openingsActivityRepository; - - private final OracleExtractionLogsRepository oracleExtractionLogsRepository; - - private static final String INCONSISTENCY = "DEBUG mode ON! Possible data inconsistency found!"; - - /** - * Loads all data extracted from Oracle into Postgres. - * - * @param oracleDto A {@link OracleExtractionDto} containing all data - */ - @Transactional(transactionManager = "postgresTransactionManager") - public void loadDashboardData( - OracleExtractionDto oracleDto, - LocalDateTime startDateTime, - OracleExtractionParamsDto params) { - saveOpeningsLastYear(oracleDto, params.debug()); - saveOpeningsActivities(oracleDto, params.debug()); - saveLogs(oracleDto.logMessages(), startDateTime, params); - } - - private void saveLogs( - List logMessages, - LocalDateTime startDateTime, - OracleExtractionParamsDto params) { - long starting = startDateTime.toInstant(ZoneOffset.UTC).toEpochMilli(); - long ending = Instant.now().toEpochMilli(); - long spent = ending - starting; - String message = "Extraction finished! All Postgres tables loaded! Time spent in ms " + spent; - log.info(message); - logMessages.add(new OracleLogDto(message, LocalDateTime.now())); - - List entities = new ArrayList<>(); - logMessages.forEach( - log -> { - OracleExtractionLogsEntity entity = new OracleExtractionLogsEntity(); - entity.setLogMessage(log.message()); - entity.setLoggedAt(log.eventTime()); - entity.setManuallyTriggered(params.manuallyTriggered()); - entities.add(entity); - }); - - oracleExtractionLogsRepository.deleteAll(); - oracleExtractionLogsRepository.flush(); - - entities.sort(Comparator.comparing(OracleExtractionLogsEntity::getLoggedAt)); - oracleExtractionLogsRepository.saveAll(entities); - } - - private void saveOpeningsActivities(OracleExtractionDto oracleDto, Boolean debug) { - logAndSave( - oracleDto.logMessages(), - "Cleaning up opening activities (SILVA.OPENINGS_ACTIVITY on Postgres)"); - openingsActivityRepository.deleteAll(); - openingsActivityRepository.flush(); - - // Map of code-descriptions - Map actionCodesMap = new HashMap<>(); - oracleDto - .actionCodes() - .forEach( - actionCode -> - actionCodesMap.put( - actionCode.getResultsAuditActionCode(), actionCode.getDescription())); - - Map openingActivityMap = new HashMap<>(); - - // Load from: resultsAudits - logAndSave(oracleDto.logMessages(), "Reading extracted data from the Audit table"); - Integer activityId = 0; - for (DashboardResultsAuditDto resultsAudit : oracleDto.resultsAudits()) { - if (Objects.isNull(resultsAudit.getOpeningId())) { - if (Boolean.TRUE.equals(debug)) { - logAndSave(oracleDto.logMessages(), INCONSISTENCY); - - logAndSave( - oracleDto.logMessages(), - "Opening ID column is null on the audit table! resultsAudit={}", - resultsAudit.toLogString()); - } - continue; - } - OpeningsActivityEntityId openingActId = - new OpeningsActivityEntityId(++activityId, resultsAudit.getOpeningId()); - - openingActivityMap.putIfAbsent(openingActId, new OpeningsActivityEntity()); - OpeningsActivityEntity activityEntity = openingActivityMap.get(openingActId); - activityEntity.setActivityId(activityId); - activityEntity.setOpeningId(resultsAudit.getOpeningId()); - - String actionCode = resultsAudit.getResultsAuditActionCode(); - activityEntity.setActivityTypeCode(actionCode); - activityEntity.setActivityTypeDesc(actionCodesMap.getOrDefault(actionCode, "")); - activityEntity.setStatusCode(null); // ?? missing - activityEntity.setStatusDesc(null); // ?? missing - activityEntity.setLastUpdated(resultsAudit.getActionTimestamp()); - activityEntity.setEntryUserid(resultsAudit.getEntryUserid()); - } - - // Load from: stockingEvents - logAndSave(oracleDto.logMessages(), "Reading extracted data from the Stocking Events table"); - for (DashboardStockingEventDto stockingEvent : oracleDto.stockingEvents()) { - if (Objects.isNull(stockingEvent.getOpeningId())) { - if (Boolean.TRUE.equals(debug)) { - logAndSave(oracleDto.logMessages(), INCONSISTENCY); - - logAndSave( - oracleDto.logMessages(), - "Opening ID column is null on the stocking events table! stockingEvent={}", - stockingEvent.toLogString()); - } - continue; - } - OpeningsActivityEntityId openingActId = - new OpeningsActivityEntityId(++activityId, stockingEvent.getOpeningId()); - - openingActivityMap.putIfAbsent(openingActId, new OpeningsActivityEntity()); - OpeningsActivityEntity activityEntity = openingActivityMap.get(openingActId); - activityEntity.setActivityId(activityId); - activityEntity.setOpeningId(stockingEvent.getOpeningId()); - - String actionCode = stockingEvent.getResultsAuditActionCode(); - activityEntity.setActivityTypeCode(actionCode); - activityEntity.setActivityTypeDesc(actionCodesMap.getOrDefault(actionCode, "")); - activityEntity.setStatusCode(null); // ?? missing - activityEntity.setStatusDesc(null); // ?? missing - activityEntity.setLastUpdated(stockingEvent.getActionTimestamp()); - activityEntity.setEntryUserid(stockingEvent.getEntryUserid()); - } - - openingsActivityRepository.saveAllAndFlush(openingActivityMap.values()); - - logAndSave( - oracleDto.logMessages(), - "Loaded {} records into the activities table (SILVA.openings_activity on Postgres)", - openingActivityMap.values().size()); - } - - private void saveOpeningsLastYear(OracleExtractionDto oracleDto, Boolean debug) { - logAndSave( - oracleDto.logMessages(), - "Cleaning up the past openings table (SILVA.OPENINGS_LAST_YEAR on Postgres)"); - - openingsLastYearRepository.deleteAll(); - openingsLastYearRepository.flush(); - - // Map of org units - Map orgUnitsMap = new HashMap<>(); - oracleDto - .orgUnits() - .forEach(orgUnit -> orgUnitsMap.put(orgUnit.getOrgUnitCode(), orgUnit.getOrgUnitName())); - - // Inserting data - Map openingsLastYearMap = new HashMap<>(); - Map openingSubmissionMap = new HashMap<>(); - - // Load from: mainOpenings - for (DashboardOpeningDto openingDto : oracleDto.mainOpenings()) { - openingsLastYearMap.putIfAbsent(openingDto.getOpeningId(), new OpeningsLastYearEntity()); - - OpeningsLastYearEntity openingEntity = openingsLastYearMap.get(openingDto.getOpeningId()); - openingEntity.setOpeningId(openingDto.getOpeningId()); - openingEntity.setUserId(openingDto.getEntryUserId()); - openingEntity.setEntryTimestamp(openingDto.getEntryTimestamp()); - openingEntity.setUpdateTimestamp(openingDto.getUpdateTimestamp()); - openingEntity.setStatus(openingDto.getOpeningStatusCode()); - - String orgUnitCode = null; - String orgUnitName = null; - if (!Objects.isNull(openingDto.getAdminDistrictNo())) { - orgUnitCode = openingDto.getAdminDistrictNo().toString(); - orgUnitName = orgUnitsMap.get(orgUnitCode); - } - openingEntity.setOrgUnitCode(orgUnitCode); - openingEntity.setOrgUnitName(orgUnitName); - // clientNumber: down below - - openingSubmissionMap.put(openingDto.getResultsSubmissionId(), openingDto.getOpeningId()); - } - - // Load from: openingSubmissions - for (DashboardOpeningSubmissionDto submissionDto : oracleDto.openingSubmissions()) { - OpeningsLastYearEntity openingEntity = null; - - Long openingId = openingSubmissionMap.get(submissionDto.getResultsSubmissionId()); - if (Objects.isNull(openingId)) { - if (Boolean.TRUE.equals(debug)) { - logAndSave(oracleDto.logMessages(), INCONSISTENCY); - logAndSave( - oracleDto.logMessages(), - "No opening ID found for submission ID {}", - submissionDto.getResultsSubmissionId()); - } - } else { - openingEntity = openingsLastYearMap.get(openingId); - } - - if (Objects.isNull(openingEntity)) { - if (Boolean.TRUE.equals(debug)) { - logAndSave(oracleDto.logMessages(), INCONSISTENCY); - logAndSave( - oracleDto.logMessages(), - "No opening entity for ID {} (from the openings last year table map)", - openingId); - } - } else { - // clientNumber - openingEntity.setClientNumber(submissionDto.getClientNumber()); - } - } - - openingsLastYearRepository.saveAllAndFlush(openingsLastYearMap.values()); - - logAndSave( - oracleDto.logMessages(), - "Loaded {} records into the opening table (SILVA.OPENINGS_LAST_YEAR on Postgres)", - openingsLastYearMap.values().size()); - } - - private void logAndSave(List logMessages, String message, Object... params) { - log.info(message, params); - - if (params.length > 0) { - for (int i = 0, len = params.length; i < len; i++) { - message = message.replaceFirst("\\{\\}", String.valueOf(params[i])); - } - } - logMessages.add(new OracleLogDto(message, LocalDateTime.now())); - } -} diff --git a/backend/src/test/java/ca/bc/gov/restapi/results/common/service/DashboardExtractionServiceTest.java b/backend/src/test/java/ca/bc/gov/restapi/results/common/service/DashboardExtractionServiceTest.java deleted file mode 100644 index 8b42dacc0..000000000 --- a/backend/src/test/java/ca/bc/gov/restapi/results/common/service/DashboardExtractionServiceTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package ca.bc.gov.restapi.results.common.service; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.when; - -import ca.bc.gov.restapi.results.common.dto.OracleExtractionDto; -import ca.bc.gov.restapi.results.common.dto.OracleExtractionParamsDto; -import ca.bc.gov.restapi.results.oracle.service.OracleExtractionService; -import ca.bc.gov.restapi.results.postgres.service.DashboardInsertionService; -import java.util.ArrayList; -import java.util.stream.Stream; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -@DisplayName("Unit Test | Dashboard Extraction Service") -class DashboardExtractionServiceTest { - - @Mock - OracleExtractionService oracleExtractionService; - - @Mock - DashboardInsertionService dashboardInsertionService; - - private DashboardExtractionService dashboardExtractionService; - - @BeforeEach - void setup() { - dashboardExtractionService = - new DashboardExtractionService(oracleExtractionService, dashboardInsertionService); - } - - @ParameterizedTest - @MethodSource("basicValues") - @DisplayName("extract data for the dashboard happy path should succeed") - void extractDataForTheDashboard_happyPath_shouldSucceed( - Integer months, - Boolean debug, - Boolean manuallyTriggered - ) { - OracleExtractionParamsDto params = new OracleExtractionParamsDto( - 24, - false, - false - ); - OracleExtractionDto extractionDto = - new OracleExtractionDto( - null, - null, - null, - null, - null, - null, - new ArrayList<>() - ); - - when(oracleExtractionService.getOpeningActivities(params)).thenReturn(extractionDto); - - doNothing().when(dashboardInsertionService).loadDashboardData(any(), any(), any()); - - dashboardExtractionService.extractDataForTheDashboard(months, debug, manuallyTriggered); - - Assertions.assertFalse(extractionDto.logMessages().isEmpty()); - } - - private static Stream basicValues() { - return Stream.of( - Arguments.of(24, false, false), - Arguments.of(24, false, null), - Arguments.of(24, null, false), - Arguments.of(24, null, null), - Arguments.of(null, false, false), - Arguments.of(null, null, false), - Arguments.of(null, null, null), - Arguments.of(null, false, null) - ); - } - -} diff --git a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OracleExtractionServiceTest.java b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OracleExtractionServiceTest.java deleted file mode 100644 index 2469d3fd1..000000000 --- a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/service/OracleExtractionServiceTest.java +++ /dev/null @@ -1,289 +0,0 @@ -package ca.bc.gov.restapi.results.oracle.service; - -import static org.mockito.Mockito.when; - -import ca.bc.gov.restapi.results.common.dto.OracleExtractionDto; -import ca.bc.gov.restapi.results.common.dto.OracleExtractionParamsDto; -import ca.bc.gov.restapi.results.common.dto.OracleLogDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardActionCodeDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOpeningDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOpeningSubmissionDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOrgUnitDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardResultsAuditDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardStockingEventDto; -import ca.bc.gov.restapi.results.oracle.repository.OpeningRepository; -import java.time.LocalDateTime; -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 OracleExtractionServiceTest { - - @Mock OpeningRepository openingRepository; - - private OracleExtractionService oracleExtractionService; - - private static final Long OPENING_ID = 112233L; - - private static final String TEST_USER = "USER"; - - private static final Long ADMIN_DISTRICT_NO = 112L; - - private static final String AUDIT_ACTION_CODE = "UPD"; - - @BeforeEach - void setup() { - oracleExtractionService = new OracleExtractionService(openingRepository); - } - - private DashboardOpeningDto mockDashboardOpeningDto() { - return new DashboardOpeningDto() { - - @Override - public Long getOpeningId() { - return OPENING_ID; - } - - @Override - public String getOpeningStatusCode() { - return "APP"; - } - - @Override - public String getEntryUserId() { - return TEST_USER; - } - - @Override - public LocalDateTime getEntryTimestamp() { - return LocalDateTime.now().minusMonths(1L); - } - - @Override - public LocalDateTime getUpdateTimestamp() { - return LocalDateTime.now().minusDays(3); - } - - @Override - public Long getAdminDistrictNo() { - return ADMIN_DISTRICT_NO; - } - - @Override - public Long getResultsSubmissionId() { - return 22L; - } - - @Override - public LocalDateTime getActionTimestamp() { - return LocalDateTime.now().minusDays(3); - } - }; - } - - private DashboardOpeningSubmissionDto mockDashboardOpeningSubmissionDto() { - return new DashboardOpeningSubmissionDto() { - - @Override - public Long getResultsSubmissionId() { - return 22L; - } - - @Override - public String getClientNumber() { - return "0012797"; - } - }; - } - - private DashboardResultsAuditDto mockDashboardResultsAuditDto() { - return new DashboardResultsAuditDto() { - - @Override - public String getResultsAuditActionCode() { - return AUDIT_ACTION_CODE; - } - - @Override - public LocalDateTime getActionDate() { - return LocalDateTime.now().minusDays(1L); - } - - @Override - public LocalDateTime getEntryTimestamp() { - return LocalDateTime.now().minusDays(20L); - } - - @Override - public String getEntryUserid() { - return TEST_USER; - } - - @Override - public Long getOpeningId() { - return OPENING_ID; - } - - @Override - public LocalDateTime getActionTimestamp() { - return LocalDateTime.now().minusDays(1L); - } - }; - } - - private DashboardStockingEventDto mockDashboardStockingEventDto() { - return new DashboardStockingEventDto() { - - @Override - public String getResultsAuditActionCode() { - return AUDIT_ACTION_CODE; - } - - @Override - public String getEntryUserid() { - return TEST_USER; - } - - @Override - public Long getOpeningId() { - return OPENING_ID; - } - - @Override - public LocalDateTime getEntryTimestamp() { - return LocalDateTime.now().minusDays(20L); - } - - @Override - public LocalDateTime getAmendEventTimestamp() { - return LocalDateTime.now().minusDays(10L); - } - - @Override - public LocalDateTime getActionTimestamp() { - return LocalDateTime.now().minusDays(10L); - } - }; - } - - private DashboardOrgUnitDto mockDashboardOrgUnitDto() { - return new DashboardOrgUnitDto() { - - @Override - public Long getOrgUnitNo() { - return ADMIN_DISTRICT_NO; - } - - @Override - public String getOrgUnitCode() { - return "DCR"; - } - - @Override - public String getOrgUnitName() { - return "DCR Name"; - } - }; - } - - private DashboardActionCodeDto mockDashboardActionCodeDto() { - return new DashboardActionCodeDto() { - - @Override - public String getResultsAuditActionCode() { - return AUDIT_ACTION_CODE; - } - - @Override - public String getDescription() { - return AUDIT_ACTION_CODE; - } - }; - } - - @Test - @DisplayName("Get opening activities happy path should succeed") - void getOpeningActivities_happyPath_shouldSucceed() { - Integer months = 24; - - DashboardOpeningDto openingDto = mockDashboardOpeningDto(); - when(openingRepository.findAllDashboardOpenings(months)).thenReturn(List.of(openingDto)); - - DashboardOpeningSubmissionDto submissionDto = mockDashboardOpeningSubmissionDto(); - when(openingRepository.findAllDashboardOpeningSubmissions(List.of(22L))) - .thenReturn(List.of(submissionDto)); - - DashboardResultsAuditDto auditDto = mockDashboardResultsAuditDto(); - when(openingRepository.findAllDashboardAuditEvents(months)).thenReturn(List.of(auditDto)); - - DashboardStockingEventDto stockingEventDto = mockDashboardStockingEventDto(); - when(openingRepository.findAllDashboardStockingEventHistory(months)) - .thenReturn(List.of(stockingEventDto)); - - DashboardOrgUnitDto orgUnitDto = mockDashboardOrgUnitDto(); - when(openingRepository.findAllDashboardOrgUnits(List.of(ADMIN_DISTRICT_NO))) - .thenReturn(List.of(orgUnitDto)); - - DashboardActionCodeDto actionCodeDto = mockDashboardActionCodeDto(); - when(openingRepository.findAllDashboardActionCodes(List.of(AUDIT_ACTION_CODE))) - .thenReturn(List.of(actionCodeDto)); - - OracleExtractionParamsDto paramsDto = new OracleExtractionParamsDto(months, true, false); - OracleExtractionDto extractionDto = oracleExtractionService.getOpeningActivities(paramsDto); - - Assertions.assertNotNull(extractionDto); - Assertions.assertEquals(1, extractionDto.mainOpenings().size()); - DashboardOpeningDto openingDto2 = extractionDto.mainOpenings().get(0); - Assertions.assertEquals(OPENING_ID, openingDto2.getOpeningId()); - Assertions.assertEquals("APP", openingDto2.getOpeningStatusCode()); - Assertions.assertEquals(TEST_USER, openingDto2.getEntryUserId()); - Assertions.assertEquals(ADMIN_DISTRICT_NO, openingDto2.getAdminDistrictNo()); - Assertions.assertEquals(22L, openingDto2.getResultsSubmissionId()); - Assertions.assertFalse(openingDto2.toLogString().isBlank()); - - Assertions.assertEquals(1, extractionDto.openingSubmissions().size()); - DashboardOpeningSubmissionDto submissionDto2 = extractionDto.openingSubmissions().get(0); - Assertions.assertEquals(22L, submissionDto2.getResultsSubmissionId()); - Assertions.assertEquals("0012797", submissionDto2.getClientNumber()); - Assertions.assertFalse(submissionDto2.toLogString().isBlank()); - - Assertions.assertEquals(1, extractionDto.resultsAudits().size()); - DashboardResultsAuditDto auditDto2 = extractionDto.resultsAudits().get(0); - Assertions.assertEquals(AUDIT_ACTION_CODE, auditDto2.getResultsAuditActionCode()); - Assertions.assertEquals(TEST_USER, auditDto2.getEntryUserid()); - Assertions.assertEquals(OPENING_ID, auditDto2.getOpeningId()); - Assertions.assertFalse(auditDto2.toLogString().isBlank()); - - Assertions.assertEquals(1, extractionDto.stockingEvents().size()); - DashboardStockingEventDto stockingEventDto2 = extractionDto.stockingEvents().get(0); - Assertions.assertEquals(AUDIT_ACTION_CODE, stockingEventDto2.getResultsAuditActionCode()); - Assertions.assertEquals(AUDIT_ACTION_CODE, stockingEventDto2.getResultsAuditActionCode()); - Assertions.assertEquals(OPENING_ID, stockingEventDto2.getOpeningId()); - Assertions.assertFalse(stockingEventDto2.toLogString().isBlank()); - - Assertions.assertEquals(1, extractionDto.orgUnits().size()); - DashboardOrgUnitDto orgUnitDto2 = extractionDto.orgUnits().get(0); - Assertions.assertEquals(ADMIN_DISTRICT_NO, orgUnitDto2.getOrgUnitNo()); - Assertions.assertEquals("DCR", orgUnitDto2.getOrgUnitCode()); - Assertions.assertEquals("DCR Name", orgUnitDto2.getOrgUnitName()); - Assertions.assertFalse(orgUnitDto2.toLogString().isBlank()); - - Assertions.assertEquals(1, extractionDto.actionCodes().size()); - DashboardActionCodeDto actionCodeDto2 = extractionDto.actionCodes().get(0); - Assertions.assertEquals(AUDIT_ACTION_CODE, actionCodeDto2.getResultsAuditActionCode()); - Assertions.assertEquals(AUDIT_ACTION_CODE, actionCodeDto2.getDescription()); - Assertions.assertFalse(actionCodeDto2.toLogString().isBlank()); - - Assertions.assertFalse(extractionDto.logMessages().isEmpty()); - OracleLogDto logDto = extractionDto.logMessages().get(0); - String message = "Querying Openings (on THE.OPENING main table) from the last 24 months"; - Assertions.assertEquals(message, logDto.message()); - Assertions.assertNotNull(logDto.eventTime()); - } -} diff --git a/backend/src/test/java/ca/bc/gov/restapi/results/postgres/service/DashboardInsertionServiceTest.java b/backend/src/test/java/ca/bc/gov/restapi/results/postgres/service/DashboardInsertionServiceTest.java deleted file mode 100644 index 413dfb81f..000000000 --- a/backend/src/test/java/ca/bc/gov/restapi/results/postgres/service/DashboardInsertionServiceTest.java +++ /dev/null @@ -1,300 +0,0 @@ -package ca.bc.gov.restapi.results.postgres.service; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.when; - -import ca.bc.gov.restapi.results.common.dto.OracleExtractionDto; -import ca.bc.gov.restapi.results.common.dto.OracleExtractionParamsDto; -import ca.bc.gov.restapi.results.common.dto.OracleLogDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardActionCodeDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOpeningDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOpeningSubmissionDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardOrgUnitDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardResultsAuditDto; -import ca.bc.gov.restapi.results.oracle.dto.DashboardStockingEventDto; -import ca.bc.gov.restapi.results.postgres.repository.OpeningsActivityRepository; -import ca.bc.gov.restapi.results.postgres.repository.OpeningsLastYearRepository; -import ca.bc.gov.restapi.results.postgres.repository.OracleExtractionLogsRepository; -import java.time.LocalDateTime; -import java.util.ArrayList; -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.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class DashboardInsertionServiceTest { - - @Mock OpeningsLastYearRepository openingsLastYearRepository; - - @Mock OpeningsActivityRepository openingsActivityRepository; - - @Mock OracleExtractionLogsRepository oracleExtractionLogsRepository; - - private DashboardInsertionService dashboardInsertionService; - - private static final Long OPENING_ID = 112233L; - - private static final String TEST_USER = "USER"; - - private static final Long ADMIN_DISTRICT_NO = 112L; - - private static final String AUDIT_ACTION_CODE = "UPD"; - - private DashboardOpeningDto mockDashboardOpeningDto() { - return new DashboardOpeningDto() { - - @Override - public Long getOpeningId() { - return OPENING_ID; - } - - @Override - public String getOpeningStatusCode() { - return "APP"; - } - - @Override - public String getEntryUserId() { - return TEST_USER; - } - - @Override - public LocalDateTime getEntryTimestamp() { - return LocalDateTime.now().minusMonths(1L); - } - - @Override - public LocalDateTime getUpdateTimestamp() { - return LocalDateTime.now().minusDays(3); - } - - @Override - public Long getAdminDistrictNo() { - return ADMIN_DISTRICT_NO; - } - - @Override - public Long getResultsSubmissionId() { - return 22L; - } - - @Override - public LocalDateTime getActionTimestamp() { - return LocalDateTime.now().minusDays(3); - } - }; - } - - private DashboardOpeningSubmissionDto mockDashboardOpeningSubmissionDto(Long id) { - return new DashboardOpeningSubmissionDto() { - - @Override - public Long getResultsSubmissionId() { - return id; - } - - @Override - public String getClientNumber() { - return "0012797"; - } - }; - } - - private DashboardResultsAuditDto mockDashboardResultsAuditDto() { - return new DashboardResultsAuditDto() { - - @Override - public String getResultsAuditActionCode() { - return AUDIT_ACTION_CODE; - } - - @Override - public LocalDateTime getActionDate() { - return LocalDateTime.now().minusDays(1L); - } - - @Override - public LocalDateTime getEntryTimestamp() { - return LocalDateTime.now().minusDays(20L); - } - - @Override - public String getEntryUserid() { - return TEST_USER; - } - - @Override - public Long getOpeningId() { - return OPENING_ID; - } - - @Override - public LocalDateTime getActionTimestamp() { - return LocalDateTime.now().minusDays(1L); - } - }; - } - - private DashboardStockingEventDto mockDashboardStockingEventDto(boolean nullOpening) { - return new DashboardStockingEventDto() { - - @Override - public String getResultsAuditActionCode() { - return AUDIT_ACTION_CODE; - } - - @Override - public String getEntryUserid() { - return TEST_USER; - } - - @Override - public Long getOpeningId() { - return nullOpening ? null : OPENING_ID; - } - - @Override - public LocalDateTime getEntryTimestamp() { - return LocalDateTime.now().minusDays(20L); - } - - @Override - public LocalDateTime getAmendEventTimestamp() { - return LocalDateTime.now().minusDays(10L); - } - - @Override - public LocalDateTime getActionTimestamp() { - return LocalDateTime.now().minusDays(10L); - } - }; - } - - private DashboardOrgUnitDto mockDashboardOrgUnitDto() { - return new DashboardOrgUnitDto() { - - @Override - public Long getOrgUnitNo() { - return ADMIN_DISTRICT_NO; - } - - @Override - public String getOrgUnitCode() { - return "DCR"; - } - - @Override - public String getOrgUnitName() { - return "DCR Name"; - } - }; - } - - private DashboardActionCodeDto mockDashboardActionCodeDto() { - return new DashboardActionCodeDto() { - - @Override - public String getResultsAuditActionCode() { - return AUDIT_ACTION_CODE; - } - - @Override - public String getDescription() { - return AUDIT_ACTION_CODE; - } - }; - } - - @BeforeEach - void setup() { - dashboardInsertionService = - new DashboardInsertionService( - openingsLastYearRepository, openingsActivityRepository, oracleExtractionLogsRepository); - } - - @Test - @DisplayName("Load dashboard data happy path should succeed") - void loadDashboardData_happyPath_shouldSucceed() { - // openings last year - doNothing().when(openingsLastYearRepository).deleteAll(); - doNothing().when(openingsLastYearRepository).flush(); - when(openingsLastYearRepository.saveAllAndFlush(any())).thenReturn(List.of()); - - // opening activities - doNothing().when(openingsActivityRepository).deleteAll(); - doNothing().when(openingsActivityRepository).flush(); - when(openingsActivityRepository.saveAllAndFlush(any())).thenReturn(List.of()); - - LocalDateTime startDateTime = LocalDateTime.now(); - OracleExtractionParamsDto paramsDto = new OracleExtractionParamsDto(24, true, false); - - // Params - DashboardOpeningDto mainOpenings = mockDashboardOpeningDto(); - DashboardOpeningSubmissionDto submissionDto = mockDashboardOpeningSubmissionDto(22L); - DashboardResultsAuditDto resultsAudits = mockDashboardResultsAuditDto(); - DashboardStockingEventDto stockingEvents = mockDashboardStockingEventDto(false); - DashboardOrgUnitDto orgUnits = mockDashboardOrgUnitDto(); - DashboardActionCodeDto actionCodes = mockDashboardActionCodeDto(); - List logMessages = new ArrayList<>(); - logMessages.add(new OracleLogDto("Hey", LocalDateTime.now())); - OracleExtractionDto extractionDto = - new OracleExtractionDto( - List.of(mainOpenings), - List.of(submissionDto), - List.of(resultsAudits), - List.of(stockingEvents), - List.of(orgUnits), - List.of(actionCodes), - logMessages); - - dashboardInsertionService.loadDashboardData(extractionDto, startDateTime, paramsDto); - - Assertions.assertFalse(extractionDto.logMessages().isEmpty()); - } - - @Test - @DisplayName("Load dashboard data null values should succeed") - void loadDashboardData_nullValues_shouldSucceed() { - // openings last year - doNothing().when(openingsLastYearRepository).deleteAll(); - doNothing().when(openingsLastYearRepository).flush(); - when(openingsLastYearRepository.saveAllAndFlush(any())).thenReturn(List.of()); - - // opening activities - doNothing().when(openingsActivityRepository).deleteAll(); - doNothing().when(openingsActivityRepository).flush(); - when(openingsActivityRepository.saveAllAndFlush(any())).thenReturn(List.of()); - - LocalDateTime startDateTime = LocalDateTime.now(); - OracleExtractionParamsDto paramsDto = new OracleExtractionParamsDto(24, true, false); - - // Params - DashboardOpeningDto mainOpenings = mockDashboardOpeningDto(); - DashboardOpeningSubmissionDto submissionDto = mockDashboardOpeningSubmissionDto(23L); - DashboardResultsAuditDto resultsAudits = mockDashboardResultsAuditDto(); - DashboardStockingEventDto stockingEvents = mockDashboardStockingEventDto(true); - DashboardOrgUnitDto orgUnits = mockDashboardOrgUnitDto(); - DashboardActionCodeDto actionCodes = mockDashboardActionCodeDto(); - List logMessages = new ArrayList<>(); - logMessages.add(new OracleLogDto("Hey", LocalDateTime.now())); - OracleExtractionDto extractionDto = - new OracleExtractionDto( - List.of(mainOpenings), - List.of(submissionDto), - List.of(resultsAudits), - List.of(stockingEvents), - List.of(orgUnits), - List.of(actionCodes), - logMessages); - - dashboardInsertionService.loadDashboardData(extractionDto, startDateTime, paramsDto); - - Assertions.assertFalse(extractionDto.logMessages().isEmpty()); - } -}