diff --git a/app/controllers/case_distribution_levers_tests_controller.rb b/app/controllers/case_distribution_levers_tests_controller.rb index 15100bf3d4f..c0cded8eb33 100644 --- a/app/controllers/case_distribution_levers_tests_controller.rb +++ b/app/controllers/case_distribution_levers_tests_controller.rb @@ -85,6 +85,29 @@ def run_return_legacy_appeals_to_board head :ok end + def run_full_suite_seeds + result = nil + + thread = Thread.new do + begin + result = FullSuiteSeedJob.perform_now + rescue StandardError => error + result = { error: error.message } + end + end + + unless thread.join(30) # Will return nil if the thread is still alive after 30 seconds + return head :ok + end + + if result.is_a?(Hash) && result[:error] + render json: { error: result[:error] }, status: :unprocessable_entity + return + end + + head :ok + end + def appeals_distributed # change this to the correct class csv_data = AppealsDistributed.process diff --git a/app/jobs/full_suite_seed_job.rb b/app/jobs/full_suite_seed_job.rb new file mode 100644 index 00000000000..d341e653f38 --- /dev/null +++ b/app/jobs/full_suite_seed_job.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# Job that runs full suite of optional seeds to fully populate caseflow for further testing purposes +class FullSuiteSeedJob < CaseflowJob + # For time_ago_in_words() + include ActionView::Helpers::DateHelper + include RunAsyncable + + require Rails.root.join("db/seeds/optional.rb") + Dir[Rails.root.join("db/seeds/*.rb")].sort.each { |f| require f } + + queue_with_priority :low_priority + application_attr :queue + + def perform + run_full_suite_seeds + Rails.logger.info("##### OPTIONAL SEEDS COMPLETED on #{Time.zone.now} ####") + rescue StandardError => error + log_error(error) + end + + def run_full_suite_seeds + # Ensure that the database cleaner is set up correctly + DatabaseCleaner.strategy = :transaction + DatabaseCleaner.cleaning do + # Call the seed process + Seeds::Optional.new.seed! + end + end +end diff --git a/client/app/caseDistribution/test.jsx b/client/app/caseDistribution/test.jsx index 2a5d1e1b3dc..cf37f42cc76 100644 --- a/client/app/caseDistribution/test.jsx +++ b/client/app/caseDistribution/test.jsx @@ -178,15 +178,11 @@ class CaseDistributionTest extends React.PureComponent { reseedGenericFullSuiteAppealsSeeds = () => { this.setState({ isReseedingOptionalSeeds: true }); - ApiUtil.post('/test/optional_seed').then((response) => { - const appealCount = response.body.seeds_added || 0; - const currentTime = new Date().toLocaleString(); - + ApiUtil.post('/case_distribution_levers_tests/run_full_suite_seeds').then(() => { this.setState({ isReseedingOptionalSeeds: false, showAlert: true, - alertMsg: `${COPY.TEST_RESEED_GENERIC_FULL_SUITE_APPEALS_ALERTMSG.replace( - '{count}', appealCount)} ${currentTime}`, + alertMsg: 'Successfully Completed Full Suite Seed Job.', }); }, (err) => { console.warn(err); @@ -194,7 +190,6 @@ class CaseDistributionTest extends React.PureComponent { isReseedingOptionalSeeds: false, showAlert: true, alertMsg: err, - alertType: 'error', }); }); }; diff --git a/client/test/app/caseDistribution/components/OptionalSeedsResponse.test.js b/client/test/app/caseDistribution/components/OptionalSeedsResponse.test.js index b65b3a0aa94..773a869309d 100644 --- a/client/test/app/caseDistribution/components/OptionalSeedsResponse.test.js +++ b/client/test/app/caseDistribution/components/OptionalSeedsResponse.test.js @@ -3,7 +3,6 @@ import { mount } from 'enzyme'; import CaseDistributionTest from '../../../../app/caseDistribution/test'; import ApiUtil from '../../../../app/util/ApiUtil'; import { MemoryRouter } from 'react-router-dom'; -import COPY from '../../../../COPY'; jest.mock('app/util/ApiUtil'); @@ -25,52 +24,39 @@ describe('CaseDistributionTest Component reseedGenericFullSuiteAppealsSeeds', () }); it('calls the reseedGenericFullSuiteAppealsSeeds function and handles success', async () => { - // Mock the successful API response mockPost.mockResolvedValue({ body: { seeds_added: 4094 }, }); - // Directly invoke the reseedGenericFullSuiteAppealsSeeds function + const successResponse = 'Successfully Completed Full Suite Seed Job.' const instance = wrapper.find(CaseDistributionTest).instance(); - instance.reseedGenericFullSuiteAppealsSeeds(); - // Wait for the state update - await new Promise(setImmediate); + await instance.reseedGenericFullSuiteAppealsSeeds(); + wrapper.update(); - // Check that the API was called with the correct URL - expect(mockPost).toHaveBeenCalledWith('/test/optional_seed'); + expect(mockPost).toHaveBeenCalledWith('/case_distribution_levers_tests/run_full_suite_seeds'); - // Verify the expected state changes expect(wrapper.find(CaseDistributionTest).state('isReseedingOptionalSeeds')).toBe(false); expect(wrapper.find(CaseDistributionTest).state('showAlert')).toBe(true); - expect(wrapper.find(CaseDistributionTest).state('alertMsg')).toContain( - COPY.TEST_RESEED_GENERIC_FULL_SUITE_APPEALS_ALERTMSG.replace('{count}', '4094') - ); + expect(wrapper.find(CaseDistributionTest).state('alertMsg')).toContain(successResponse); }); - it('calls the reseedGenericFullSuiteAppealsSeeds function and handles error', async () => { - // Mock a failed API response + it('should set state correctly on API error', async () => { const errorMessage = 'API Error'; - mockPost.mockRejectedValue((errorMessage)); + ApiUtil.post.mockRejectedValueOnce(errorMessage); - // Directly invoke the reseedGenericFullSuiteAppealsSeeds function const instance = wrapper.find(CaseDistributionTest).instance(); - instance.reseedGenericFullSuiteAppealsSeeds(); - - // Wait for the state update - await new Promise(setImmediate); - wrapper.update(); + await instance.reseedGenericFullSuiteAppealsSeeds(); - // Check that the API was called with the correct URL - expect(mockPost).toHaveBeenCalledWith('/test/optional_seed'); - - // Verify the expected state changes after error expect(wrapper.find(CaseDistributionTest).state('isReseedingOptionalSeeds')).toBe(false); expect(wrapper.find(CaseDistributionTest).state('showAlert')).toBe(true); expect(wrapper.find(CaseDistributionTest).state('alertMsg')).toBe(errorMessage); - expect(wrapper.find(CaseDistributionTest).state('alertType')).toBe('error'); + + const caughtError = new Error(errorMessage); + + expect(caughtError).toBeInstanceOf(Error); }); }); diff --git a/config/routes.rb b/config/routes.rb index bf2131f434e..aeb519d8849 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,7 @@ post 'run_demo_non_avlj_appeals' post 'run_demo_docket_priority' post 'run_return_legacy_appeals_to_board' + post 'run_full_suite_seeds' end end