From 58ba5ce455987e3b608870a29d2e057a21e4dc66 Mon Sep 17 00:00:00 2001 From: Saman Ehsan Date: Mon, 8 Jul 2024 10:55:46 -0400 Subject: [PATCH] Fix parsing accessUrl from response (#115) --- .../drshub/tracking/TrackingInterceptor.java | 10 +++---- .../tracking/TrackingInterceptorTest.java | 28 +++++++++++++++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/service/src/main/java/bio/terra/drshub/tracking/TrackingInterceptor.java b/service/src/main/java/bio/terra/drshub/tracking/TrackingInterceptor.java index 14505bc7..5e6ec1e7 100644 --- a/service/src/main/java/bio/terra/drshub/tracking/TrackingInterceptor.java +++ b/service/src/main/java/bio/terra/drshub/tracking/TrackingInterceptor.java @@ -119,13 +119,13 @@ private void addToPropertiesIfPresentInHeader( public void addResolvedCloudToProperties( HttpServletResponse response, Map properties) { Map responseBody = readResponseBody(response); - if (responseBody.containsKey("accessUrl")) { - Map accessUrlResponse = (Map) responseBody.get("accessUrl"); - String accessUrl = accessUrlResponse.get("url"); + if (responseBody.get("accessUrl") != null) { + Map accessURLResponse = (Map) responseBody.get("accessUrl"); + String url = accessURLResponse.get("url"); String resolvedCloud = ""; - if (accessUrl.contains("windows.net")) { + if (url.contains("windows.net")) { resolvedCloud = "azure"; - } else if (accessUrl.contains("googleapis.com")) { + } else if (url.contains("googleapis.com")) { resolvedCloud = "gcp"; } properties.put("resolvedCloud", resolvedCloud); diff --git a/service/src/test/java/bio/terra/drshub/tracking/TrackingInterceptorTest.java b/service/src/test/java/bio/terra/drshub/tracking/TrackingInterceptorTest.java index 8457a051..02eb8cb4 100644 --- a/service/src/test/java/bio/terra/drshub/tracking/TrackingInterceptorTest.java +++ b/service/src/test/java/bio/terra/drshub/tracking/TrackingInterceptorTest.java @@ -122,6 +122,27 @@ void testHappyPathEmittingToBardWithResolvedCloudAzure() throws Exception { TEST_BEARER_TOKEN, EVENT_NAME, expectedBardProperties(List.of("accessUrl"), "azure")); } + @Test + void testEmittingToBardOn401Error() throws Exception { + mockBardEmissionsEnabled(); + mockResolveDrsResponse(null); + postRequest( + REQUEST_URL, + objectMapper.writeValueAsString( + Map.of( + "url", + DRS_URI, + "cloudPlatform", + CloudPlatformEnum.GS, + "fields", + List.of("accessUrl")))) + .andExpect(status().isOk()); + + verify(trackingService) + .logEvent( + TEST_BEARER_TOKEN, EVENT_NAME, expectedBardProperties(List.of("accessUrl"), null)); + } + @Test void testDoesNotLogWhenBardEmissionsDisabled() throws Exception { postRequest( @@ -181,8 +202,11 @@ private void mockBardEmissionsEnabled() { } private void mockResolveDrsResponse(String accessUrl) { - DrsMetadata drsMetadata = - new DrsMetadata.Builder().accessUrl(new AccessURL().url(accessUrl)).build(); + AccessURL metadataAccessUrl = null; + if (accessUrl != null) { + metadataAccessUrl = new AccessURL().url(accessUrl); + } + DrsMetadata drsMetadata = new DrsMetadata.Builder().accessUrl(metadataAccessUrl).build(); AnnotatedResourceMetadata metadata = AnnotatedResourceMetadata.builder() .requestedFields(List.of("accessUrl"))