From 21a7c3ab28874093601667e71e4de05bce9c2039 Mon Sep 17 00:00:00 2001 From: Santhosh Gandhe <1909520+san81@users.noreply.github.com> Date: Fri, 1 Nov 2024 17:56:24 -0700 Subject: [PATCH] one more additional test Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com> --- .../jira/rest/auth/JiraOauthConfig.java | 5 ++-- .../jira/rest/auth/JiraOauthConfigTest.java | 28 +++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/data-prepper-plugins/saas-source-plugins/jira-source/src/main/java/org/opensearch/dataprepper/plugins/source/jira/rest/auth/JiraOauthConfig.java b/data-prepper-plugins/saas-source-plugins/jira-source/src/main/java/org/opensearch/dataprepper/plugins/source/jira/rest/auth/JiraOauthConfig.java index 620db899a3..93cae4afce 100644 --- a/data-prepper-plugins/saas-source-plugins/jira-source/src/main/java/org/opensearch/dataprepper/plugins/source/jira/rest/auth/JiraOauthConfig.java +++ b/data-prepper-plugins/saas-source-plugins/jira-source/src/main/java/org/opensearch/dataprepper/plugins/source/jira/rest/auth/JiraOauthConfig.java @@ -10,6 +10,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.util.StringUtils; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; @@ -131,9 +132,9 @@ public void renewCredentials() { @Override public String getUrl() { - if (url == null || url.isEmpty()) { + if (!StringUtils.hasLength(url)) { synchronized (cloudIdFetchLock) { - if (url == null || url.isEmpty()) { + if (!StringUtils.hasLength(url)) { initCredentials(); } } diff --git a/data-prepper-plugins/saas-source-plugins/jira-source/src/test/java/org/opensearch/dataprepper/plugins/source/jira/rest/auth/JiraOauthConfigTest.java b/data-prepper-plugins/saas-source-plugins/jira-source/src/test/java/org/opensearch/dataprepper/plugins/source/jira/rest/auth/JiraOauthConfigTest.java index 4c470c117c..77c50557df 100644 --- a/data-prepper-plugins/saas-source-plugins/jira-source/src/test/java/org/opensearch/dataprepper/plugins/source/jira/rest/auth/JiraOauthConfigTest.java +++ b/data-prepper-plugins/saas-source-plugins/jira-source/src/test/java/org/opensearch/dataprepper/plugins/source/jira/rest/auth/JiraOauthConfigTest.java @@ -5,6 +5,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.opensearch.dataprepper.plugins.source.jira.JiraSourceConfig; +import org.opensearch.dataprepper.plugins.source.jira.exception.UnAuthorizedException; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; @@ -89,10 +90,12 @@ void testGetJiraAccountCloudId() { jiraOauthConfig.restTemplate = restTemplateMock; ExecutorService executor = Executors.newFixedThreadPool(2); - Future firstCall = executor.submit(jiraOauthConfig::getUrl); - Future secondCall = executor.submit(jiraOauthConfig::getUrl); + Future firstCall = executor.submit(jiraOauthConfig::initCredentials); + Future secondCall = executor.submit(jiraOauthConfig::initCredentials); while (!firstCall.isDone() || !secondCall.isDone()) { // Do nothing. Wait for the calls to complete + System.out.println("First: " + firstCall.isDone()); + System.out.println("Second: " + secondCall.isDone()); } executor.shutdown(); @@ -101,6 +104,27 @@ void testGetJiraAccountCloudId() { jiraOauthConfig.getUrl(); verify(restTemplateMock, times(1)) .exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class)); + } + + @Test + void testGetJiraAccountCloudIdUnauthorizedCase() { + + when(restTemplateMock.exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class))) + .thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED)); + Map mockRenewTokenResponse = Map.of("access_token", "first_mock_access_token", + "refresh_token", "first_mock_refresh_token", + "expires_in", 3600); + when(restTemplateMock.postForEntity(any(String.class), any(HttpEntity.class), any(Class.class))) + .thenReturn(new ResponseEntity<>(mockRenewTokenResponse, HttpStatus.OK)); + JiraOauthConfig jiraOauthConfig = new JiraOauthConfig(jiraSourceConfig); + jiraOauthConfig.restTemplate = restTemplateMock; + + + assertThrows(UnAuthorizedException.class, () -> jiraOauthConfig.initCredentials()); + verify(restTemplateMock, times(6)) + .exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class)); + verify(restTemplateMock, times(1)) + .postForEntity(any(String.class), any(HttpEntity.class), any(Class.class)); }