diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java b/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java index f06244b57324e733e376f8a3b6cf0328092cb5a7..0f17696c60e75888c09792c01857702229e4fc65 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java @@ -57,15 +57,20 @@ public class CredentialHeadersProvider implements FactoryBean<DpsHeaders> { case POST: case PUT: case PATCH: { - Map<String, String> attributes = this.pubsubRequestBodyExtractor.extractAttributesFromRequestBody(); - try { - //extract headers from pubsub message - String dataPartitionId = attributes.get(DpsHeaders.DATA_PARTITION_ID); - String authToken = this.serviceAccountJwtClient.getIdToken(dataPartitionId); - attributes.put(DpsHeaders.AUTHORIZATION, authToken); - return DpsHeaders.createFromMap(attributes); - } catch (AppException e) { - throw new Exception("Failed to generate headers for register service."); + if(this.pubsubRequestBodyExtractor.isHandshakeRequest()) { + // The headers are not needed for the handshake requests. + return new DpsHeaders(); + } else { + Map<String, String> attributes = this.pubsubRequestBodyExtractor.extractAttributesFromRequestBody(); + try { + //extract headers from pubsub message + String dataPartitionId = attributes.get(DpsHeaders.DATA_PARTITION_ID); + String authToken = this.serviceAccountJwtClient.getIdToken(dataPartitionId); + attributes.put(DpsHeaders.AUTHORIZATION, authToken); + return DpsHeaders.createFromMap(attributes); + } catch (AppException e) { + throw new Exception("Failed to generate headers for register service."); + } } } diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/di/CredentialHeadersProviderTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/di/CredentialHeadersProviderTest.java index 3d18ffc2082ec80b618f8a41339ea7400c152be1..c660635f98e083dfa198f4f39353da3a39a8b5eb 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/di/CredentialHeadersProviderTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/di/CredentialHeadersProviderTest.java @@ -5,6 +5,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor; import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.web.bind.annotation.RequestMethod; @@ -18,6 +20,9 @@ public class CredentialHeadersProviderTest { @Mock private HttpServletRequest httpRequest; + @Mock + private IPubsubRequestBodyExtractor pubsubRequestBodyExtractor; + @InjectMocks private CredentialHeadersProvider headersProvider; @@ -26,4 +31,19 @@ public class CredentialHeadersProviderTest { when(httpRequest.getMethod()).thenReturn(RequestMethod.GET.toString()); assertNotNull(headersProvider.getObject()); } + + @Test + public void testHandshake() throws Exception { + // set up + when(httpRequest.getMethod()).thenReturn(RequestMethod.GET.toString()); + when(pubsubRequestBodyExtractor.isHandshakeRequest()).thenReturn(true); + + // Act + DpsHeaders headers = headersProvider.getObject(); + + // Assert + assertNotNull(headers); + assertNull(headers.getCorrelationId()); + assertNull(headers.getPartitionId()); + } } \ No newline at end of file