11import json
2+ from os .path import join as path_join
23from typing import Dict , List , Optional
34from unittest import TestCase
45from unittest .mock import ANY , patch
89
910from hoss .adapter import HossAdapter
1011from hoss .bbox_utilities import BBox
12+ from hoss .exceptions import CustomError , StagingFailed
1113from tests .utilities import Granule , create_stac , spy_on
1214
1315
@@ -33,6 +35,7 @@ def setUpClass(cls):
3335 cls .africa_stac = create_stac (
3436 [Granule (cls .africa_granule_url , None , ['opendap' , 'data' ])]
3537 )
38+ cls .staging_location = 's3://example-bucket'
3639
3740 def setUp (self ):
3841 self .config = config (validate = False )
@@ -559,3 +562,72 @@ def test_missing_variables(
559562 location = 's3://example-bucket/' ,
560563 logger = hoss .logger ,
561564 )
565+
566+ def test_hoss_stage (self , mock_stage , mock_subset_granule , mock_get_mimetype ):
567+ """Ensure hoss_stage successfully calls hoss_stage and returns the URL"""
568+ mock_subset_granule .return_value = '/path/to/output.nc'
569+ expected_output_basename = '/path/to/output.nc'
570+ mock_get_mimetype .return_value = ('application/x-netcdf4' , None )
571+ expected_staged_url = path_join (self .staging_location , expected_output_basename )
572+ mock_stage .return_value = expected_staged_url
573+
574+ collection_short_name = 'harmony_example_l2'
575+
576+ message = self .create_message (
577+ 'C1233860183-EEDTEST' , collection_short_name , [], 'jlovell'
578+ )
579+
580+ hoss = HossAdapter (message , config = self .config , catalog = self .africa_stac )
581+
582+ with patch .object (HossAdapter , 'process_item' , self .process_item_spy ):
583+ hoss .invoke ()
584+
585+ mock_subset_granule .assert_called_once_with (
586+ self .africa_granule_url ,
587+ message .sources [0 ],
588+ ANY ,
589+ hoss .message ,
590+ hoss .config ,
591+ )
592+
593+ mock_get_mimetype .assert_called_once_with ('/path/to/output.nc' )
594+
595+ result = hoss .hoss_stage (
596+ expected_output_basename ,
597+ 'africa.nc4' ,
598+ 'application/x-netcdf4' ,
599+ )
600+
601+ assert result == expected_staged_url
602+
603+ def test_hoss_stage_raises_exception (
604+ self , mock_stage , mock_subset_granule , mock_get_mimetype
605+ ):
606+ """Ensure hoss_stage raises an StagingFailed exception when
607+ the Harmony stage function throws an exception.
608+
609+ """
610+ mock_stage .side_effect = Exception ("Connection timeout" )
611+ mock_subset_granule .return_value = '/path/to/output.nc'
612+ mock_get_mimetype .return_value = ('application/x-netcdf4' , None )
613+
614+ collection_short_name = 'harmony_example_l2'
615+
616+ message = self .create_message (
617+ 'C1233860183-EEDTEST' , collection_short_name , [], 'jlovell'
618+ )
619+
620+ hoss = HossAdapter (message , config = self .config , catalog = self .africa_stac )
621+
622+ with self .assertRaises (StagingFailed ) as context_manager :
623+ hoss .hoss_stage (
624+ '/path/to/output.nc' ,
625+ 'africa.nc4' ,
626+ 'application/x-netcdf4' ,
627+ )
628+
629+ self .assertIsInstance (context_manager .exception , CustomError )
630+ self .assertEqual (
631+ str (context_manager .exception ),
632+ 'Staging failed, Connection timeout' ,
633+ )
0 commit comments