From 46177f61dc33484d7c34c45da01f5b88d1a340f6 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 21 Aug 2025 09:32:41 +0000 Subject: [PATCH] Bug 39345: (QA follow-up): Add exception handling for unsupported counter release To test this on the UI, you can add the following line of code at the beginning on 'new' in SushiCounter.pm: ->{response}->{Report_Header}->{Release} = 6; This will force the release to be '6'. Then you can perform a harvest using the UI. The background job will fail with the error message saying that counter release 6 is not supported prove t/db_dependent/Koha/BackgroundJobs/ErmSushiHarvester.t Signed-off-by: Eric Phetteplace Signed-off-by: Nick Clemens --- Koha/ERM/EUsage/SushiCounter.pm | 4 +- Koha/ERM/EUsage/UsageDataProvider.pm | 42 +++++--- Koha/Exceptions/ERM/EUsage/CounterFile.pm | 3 +- .../Koha/BackgroundJobs/ErmSushiHarvester.t | 20 +++- .../UnsupportedCOUNTER.json | 101 ++++++++++++++++++ 5 files changed, 150 insertions(+), 20 deletions(-) create mode 100644 t/db_dependent/data/erm/eusage/UnsupportedCOUNTER/UnsupportedCOUNTER.json diff --git a/Koha/ERM/EUsage/SushiCounter.pm b/Koha/ERM/EUsage/SushiCounter.pm index 349245d5f23..78e8b8694d8 100644 --- a/Koha/ERM/EUsage/SushiCounter.pm +++ b/Koha/ERM/EUsage/SushiCounter.pm @@ -49,8 +49,8 @@ sub new { require Koha::ERM::EUsage::COUNTER::5_1; $counter_release = Koha::ERM::EUsage::COUNTER::5_1->new; } else { - - #TODO: Throw an exception stating release not found / not supported! + Koha::Exceptions::ERM::EUsage::CounterFile::UnsupportedRelease->throw( + { counter_release => $params->{response}->{Report_Header}->{Release} } ); } $counter_release->{sushi} = { diff --git a/Koha/ERM/EUsage/UsageDataProvider.pm b/Koha/ERM/EUsage/UsageDataProvider.pm index 876b6dc2f8e..9f7fb5c6c76 100644 --- a/Koha/ERM/EUsage/UsageDataProvider.pm +++ b/Koha/ERM/EUsage/UsageDataProvider.pm @@ -25,6 +25,8 @@ use LWP::UserAgent; use Koha::Exceptions; +use Try::Tiny qw( catch try ); + use base qw(Koha::Object); use Koha; @@ -218,24 +220,34 @@ sub harvest_sushi { return if $self->_sushi_errors($result); # Parse the SUSHI response - my $sushi_counter = Koha::ERM::EUsage::SushiCounter->new( { response => $result } ); - my $counter_file = $sushi_counter->get_COUNTER_from_SUSHI; - - return if $self->_counter_file_size_too_large($counter_file); + try { + my $sushi_counter = Koha::ERM::EUsage::SushiCounter->new( { response => $result } ); + my $counter_file = $sushi_counter->get_COUNTER_from_SUSHI; - $self->counter_files( - [ - { - usage_data_provider_id => $self->erm_usage_data_provider_id, - file_content => $counter_file, - date_uploaded => POSIX::strftime( "%Y%m%d%H%M%S", localtime ), + return if $self->_counter_file_size_too_large($counter_file); - #TODO: add ".csv" to end of filename here - filename => $self->name . "_" . $self->{report_type}, - } - ] - ); + $self->counter_files( + [ + { + usage_data_provider_id => $self->erm_usage_data_provider_id, + file_content => $counter_file, + date_uploaded => POSIX::strftime( "%Y%m%d%H%M%S", localtime ), + #TODO: add ".csv" to end of filename here + filename => $self->name . "_" . $self->{report_type}, + } + ] + ); + } catch { + if ( $_->isa('Koha::Exceptions::ERM::EUsage::CounterFile::UnsupportedRelease') ) { + $self->{job_callbacks}->{add_message_callback}->( + { + type => 'error', + message => 'COUNTER release ' . $_->{message}->{counter_release} . ' not supported', + } + ) if $self->{job_callbacks}; + } + }; } =head3 set_background_job_callbacks diff --git a/Koha/Exceptions/ERM/EUsage/CounterFile.pm b/Koha/Exceptions/ERM/EUsage/CounterFile.pm index 70b00998187..db6eda07b43 100644 --- a/Koha/Exceptions/ERM/EUsage/CounterFile.pm +++ b/Koha/Exceptions/ERM/EUsage/CounterFile.pm @@ -26,7 +26,8 @@ use Exception::Class ( }, 'Koha::Exceptions::ERM::EUsage::CounterFile::UnsupportedRelease' => { isa => 'Koha::Exceptions::ERM::EUsage::CounterFile', - description => 'This COUNTER release is not supported' + description => 'This COUNTER release is not supported', + fields => ['counter_release'], } ); diff --git a/t/db_dependent/Koha/BackgroundJobs/ErmSushiHarvester.t b/t/db_dependent/Koha/BackgroundJobs/ErmSushiHarvester.t index 3bc6a70e113..bcf0d47729a 100755 --- a/t/db_dependent/Koha/BackgroundJobs/ErmSushiHarvester.t +++ b/t/db_dependent/Koha/BackgroundJobs/ErmSushiHarvester.t @@ -18,7 +18,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 4; +use Test::More tests => 6; use Koha::Database; use Koha::BackgroundJobs; @@ -37,7 +37,7 @@ my $builder = t::lib::TestBuilder->new; my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); -my $sushi_response_file_TR_J1 = dirname(__FILE__) . "/../../data/erm/eusage/TR_J1.json"; +my $sushi_response_file_TR_J1 = dirname(__FILE__) . "/../../data/erm/eusage/COUNTER_5/TR_J1.json"; my $sushi_counter_5_response_TR_J1 = read_file($sushi_response_file_TR_J1); my $sushi_counter_report_TR_J1 = Koha::ERM::EUsage::SushiCounter->new( { response => decode_json($sushi_counter_5_response_TR_J1) } ); @@ -306,3 +306,19 @@ subtest 'enqueue_sushi_harvest_jobs_error_handling' => sub { $schema->storage->txn_rollback; }; + +my $unsupported_sushi_response_file = + dirname(__FILE__) . "/../../data/erm/eusage/UnsupportedCOUNTER/UnsupportedCOUNTER.json"; +my $unsupported_sushi_counter_5_response_TR_J1 = read_file($unsupported_sushi_response_file); + +eval { + Koha::ERM::EUsage::SushiCounter->new( { response => decode_json($unsupported_sushi_counter_5_response_TR_J1) } ); +}; +if ($@) { + isa_ok( + $@, 'Koha::Exceptions::ERM::EUsage::CounterFile::UnsupportedRelease', + 'Unsupported release throws UnsupportedRelease exception' + ); + + is( $@->{message}->{counter_release}, 99, 'exception message has counter_release set to 99' ); +} diff --git a/t/db_dependent/data/erm/eusage/UnsupportedCOUNTER/UnsupportedCOUNTER.json b/t/db_dependent/data/erm/eusage/UnsupportedCOUNTER/UnsupportedCOUNTER.json new file mode 100644 index 00000000000..eac8d339f15 --- /dev/null +++ b/t/db_dependent/data/erm/eusage/UnsupportedCOUNTER/UnsupportedCOUNTER.json @@ -0,0 +1,101 @@ +{ + "Report_Header": { + "Release": "99", + "Report_ID": "TR_B2", + "Report_Name": "Book Access Denied", + "Created": "2023-02-15T09:11:12Z", + "Created_By": "Sample Publisher", + "Institution_ID": { + "ISNI": [ + "1234123412341234" + ] + }, + "Institution_Name": "Sample Institution", + "Registry_Record": "https://registry.projectcounter.org/platform/99999999-9999-9999-9999-999999999999", + "Report_Filters": { + "Metric_Type": [ + "Limit_Exceeded", + "No_License" + ], + "Begin_Date": "2022-01-01", + "End_Date": "2022-03-31", + "Data_Type": [ + "Book", + "Reference_Work" + ], + "Access_Method": [ + "Regular" + ] + } + }, + "Report_Items": [ + { + "Title": "Title 1", + "Publisher": "Sample Publisher", + "Publisher_ID": { + "ISNI": [ + "4321432143214321" + ] + }, + "Platform": "Platform 1", + "Item_ID": { + "DOI": "10.9999/xxxxt01", + "Proprietary": "P1:T01", + "ISBN": "979-8-88888-888-8", + "URI": "https://doi.org/10.9999/xxxxt01" + }, + "Attribute_Performance": [ + { + "Data_Type": "Book", + "YOP": "2022", + "Performance": { + "Limit_Exceeded": { + "2022-01": 49, + "2022-02": 90, + "2022-03": 40 + }, + "No_License": { + "2022-01": 50, + "2022-02": 84, + "2022-03": 47 + } + } + } + ] + }, + { + "Title": "Title 7", + "Publisher": "Sample Publisher", + "Publisher_ID": { + "ISNI": [ + "4321432143214321" + ] + }, + "Platform": "Platform 1", + "Item_ID": { + "DOI": "10.9999/xxxxt07", + "Proprietary": "P1:T07", + "ISBN": "979-8-88888-888-9", + "URI": "https://doi.org/10.9999/xxxxt07" + }, + "Attribute_Performance": [ + { + "Data_Type": "Reference_Work", + "YOP": "2019", + "Performance": { + "Limit_Exceeded": { + "2022-01": 84, + "2022-02": 84, + "2022-03": 49 + }, + "No_License": { + "2022-01": 70, + "2022-02": 74, + "2022-03": 54 + } + } + } + ] + } + ] +} \ No newline at end of file -- 2.39.5