diff --git a/cumulus_lambda_functions/uds_api/dapa/granules_dapa_query_es.py b/cumulus_lambda_functions/uds_api/dapa/granules_dapa_query_es.py index ba785603..345674cd 100644 --- a/cumulus_lambda_functions/uds_api/dapa/granules_dapa_query_es.py +++ b/cumulus_lambda_functions/uds_api/dapa/granules_dapa_query_es.py @@ -172,7 +172,8 @@ def get_single_granule(self, granule_id): 'size': 1, 'sort': [{'id': {'order': 'asc'}}], 'query': {'bool': {'must': [{ - 'term': {'id': granule_id} + 'term': {'id': granule_id}}, { + 'term': {'collection': self.__collection_id}, }]}} } LOGGER.debug(f'granules_query_dsl: {granules_query_dsl}') @@ -182,7 +183,7 @@ def get_single_granule(self, granule_id): granules_query_dsl) LOGGER.debug(f'granules_query_result: {granules_query_result}') if len(granules_query_result['hits']['hits']) < 1: - raise ValueError(f'cannot find granule for : {granule_id}') + return None each_granules_query_result_stripped = granules_query_result['hits']['hits'][0]['_source'] self_link = Link(rel='self', target=f'{self.__base_url}/{WebServiceConstants.COLLECTIONS}/{self.__collection_id}/items/{each_granules_query_result_stripped["id"]}', media_type='application/json', title=each_granules_query_result_stripped["id"]).to_dict(False) diff --git a/cumulus_lambda_functions/uds_api/granules_api.py b/cumulus_lambda_functions/uds_api/granules_api.py index b17a14ca..0e74887f 100644 --- a/cumulus_lambda_functions/uds_api/granules_api.py +++ b/cumulus_lambda_functions/uds_api/granules_api.py @@ -136,6 +136,8 @@ async def get_single_granule_dapa(request: Request, collection_id: str, granule_ except Exception as e: LOGGER.exception('failed during get_granules_dapa') raise HTTPException(status_code=500, detail=str(e)) + if granules_result is None: + raise HTTPException(status_code=404, detail={'message': f'no granule with id: {granule_id} in collection: {collection_id}'}) return granules_result @router.delete("/{collection_id}/items/{granule_id}") diff --git a/tests/integration_tests/test_uds_api.py b/tests/integration_tests/test_uds_api.py index 480127cd..21d26637 100644 --- a/tests/integration_tests/test_uds_api.py +++ b/tests/integration_tests/test_uds_api.py @@ -233,6 +233,26 @@ def test_single_granule_get(self): return + + def test_single_granule_get_wrong_collection(self): + # post_url = f'{self.uds_url}collections/urn:nasa:unity:uds_local_test:DEV1:CHRP_16_DAY_REBIN___10/items/urn:nasa:unity:uds_local_test:DEV1:CHRP_16_DAY_REBIN___10:SNDR.SS1330.CHIRP.20230101T0000.m06.g001.L1_J1.std.v02_48.G.200101070318_REBIN' + post_url = f'{self.uds_url}collections/URN:NASA:UNITY:UDS_LOCAL_TEST_3:DEV:DDD-02___001/items/URN:NASA:UNITY:UDS_LOCAL_TEST_3:DEV:DDD-01___001:test_file10' + headers = { + 'Authorization': f'Bearer {self.bearer_token}', + } + print(post_url) + query_result = requests.get(url=post_url, headers=headers,) + self.assertEqual(query_result.status_code, 404, f'wrong status code. {query_result.text}') + response_json = json.loads(query_result.text) + print(json.dumps(response_json)) + + post_url = f'{self.uds_url}collections/URN:NASA:UNITY:UDS_LOCAL_TEST_3:DEV:DDD-01___001/items/URN:NASA:UNITY:UDS_LOCAL_TEST_3:DEV:DDD-01___001:test_file10' + query_result = requests.get(url=post_url, headers=headers,) + self.assertEqual(query_result.status_code, 200, f'wrong status code. {query_result.text}') + response_json = json.loads(query_result.text) + print(json.dumps(response_json)) + return + def test_create_new_collection(self): post_url = f'{self.uds_url}collections/' # MCP Dev headers = {