Skip to content

Commit a68272e

Browse files
authored
CMR-10693: Fixing Bug when granules do not have a DataGranule element an exception was thrown and when the location doesn't include the search context (#2277)
* CMR-10693: Fixing bug when DataGranule element does not exist in the metadata and if the location is missing the search context. * CMR-10693: Moving print statement to debug log. * CMR-10693: updating function names and variables from PR requests.
1 parent f89de0c commit a68272e

2 files changed

Lines changed: 55 additions & 13 deletions

File tree

subscription/src/search.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,26 @@ def get_concept(self, concept_id, revision_id):
144144
else:
145145
# Request failed
146146
logger.warning(f"Subscription Worker getting search concept using URL {url} failed with status code: {response.status_code}")
147-
147+
148148
def get_producer_granule_id(self, metadata):
149149
"""Get the granule producer id from the metadata."""
150-
identifiers = metadata.get('DataGranule').get('Identifiers')
151-
pgi = None
152-
if identifiers:
153-
for identifier in identifiers:
154-
if identifier.get('IdentifierType') == 'ProducerGranuleId':
155-
pgi = identifier.get('Identifier')
156-
break
157-
if pgi:
158-
return pgi
159-
else:
160-
return None
150+
data_granule = metadata.get('DataGranule', {})
151+
identifiers = data_granule.get('Identifiers', [])
152+
153+
for identifier in identifiers:
154+
if identifier.get('IdentifierType') == 'ProducerGranuleId':
155+
return identifier.get('Identifier')
156+
return None
157+
158+
def add_search_context_to_location_url(self, location_url):
159+
"""This function adds the search context to the location URL."""
160+
161+
if location_url:
162+
if "search" in location_url:
163+
return location_url
164+
else:
165+
parts = location_url.split('concepts')
166+
return parts[0] + "search/concepts" + parts[1]
161167
else:
162168
return None
163169

@@ -177,10 +183,12 @@ def process_message(self, message):
177183
\"location\": \"http://localhost:3003/concepts/G1200484356-ERICH_PROV/39\"}
178184
"""
179185
# Get the granule concept-id from the message.
180-
print(f"Search process_message message type {type(message)}")
186+
logger.debug(f"Search process_message message type {type(message)}")
181187
message_dict = json.loads(message)
182188
concept_id = message_dict["concept-id"]
183189
revision_id = message_dict.get("revision-id", None)
190+
location = message_dict.get("location")
191+
location_url = self.add_search_context_to_location_url(location)
184192
# Get the concept from search
185193
result = self.get_concept(concept_id, revision_id)
186194

@@ -193,4 +201,6 @@ def process_message(self, message):
193201
del message_dict['revision-id']
194202
if pgi:
195203
message_dict.update({"producer-granule-id": pgi})
204+
if location_url:
205+
message_dict.update({"location": location_url})
196206
return message_dict

subscription/test/search_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,38 @@ def test_process_message(self, mock_get_public_search_url, mock_get_concept):
103103
self.assertEqual(result, expected_output)
104104
mock_get_concept.assert_called_once_with("G1200484356-ERICH_PROV", '1')
105105

106+
@patch('search.Search.get_concept')
107+
@patch('search.Search.get_public_search_url')
108+
def test_process_message_no_data_granule(self, mock_get_public_search_url, mock_get_concept):
109+
# This tests some error cases where no DataGranule element exists in the metadata and
110+
# if the location string is missing the search context.
111+
112+
# Setup
113+
search = Search()
114+
115+
# Mock the get_public_search_url method
116+
mock_get_public_search_url.return_value = "https://cmr.earthdata.nasa.gov/search/"
117+
118+
# Mock the get_concept method
119+
mock_get_concept.return_value = """
120+
{
121+
"GranuleUR": "SWOT_L2_HR_PIXC_578_020_221L_20230710T223456_20230710T223506_PIA1_01"
122+
}
123+
"""
124+
125+
# Input message
126+
input_message = '{"concept-id": "G1200484356-ERICH_PROV", "revision-id": "1", "granule-ur": "SWOT_L2_HR_PIXC_578_020_221L_20230710T223456_20230710T223506_PIA1_01", "location": "https://cmr.earthdata.nasa.gov/concepts/G1200484356-ERICH_PROV/1"}'
127+
128+
# Expected output
129+
expected_output = {"concept-id": "G1200484356-ERICH_PROV", "granule-ur": "SWOT_L2_HR_PIXC_578_020_221L_20230710T223456_20230710T223506_PIA1_01", "location": "https://cmr.earthdata.nasa.gov/search/concepts/G1200484356-ERICH_PROV/1"}
130+
131+
# Call the method
132+
result = search.process_message(input_message)
133+
134+
# Assert
135+
self.assertEqual(result, expected_output)
136+
mock_get_concept.assert_called_once_with("G1200484356-ERICH_PROV", '1')
137+
106138
if __name__ == '__main__':
107139
unittest.main()
108140

0 commit comments

Comments
 (0)