Skip to content

Commit

Permalink
one more additional test
Browse files Browse the repository at this point in the history
Signed-off-by: Santhosh Gandhe <[email protected]>
  • Loading branch information
san81 committed Nov 2, 2024
1 parent 7f1dfae commit 21a7c3a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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<String, Object> 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));

}

Expand Down

0 comments on commit 21a7c3a

Please sign in to comment.