Skip to content

Commit

Permalink
addition tests
Browse files Browse the repository at this point in the history
Signed-off-by: Santhosh Gandhe <[email protected]>
  • Loading branch information
san81 committed Oct 31, 2024
1 parent f6dd991 commit 3a615e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.springframework.web.client.RestTemplate;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -61,7 +60,7 @@ public JiraOauthConfig(JiraSourceConfig jiraSourceConfig) {
this.clientSecret = jiraSourceConfig.getClientSecret();
}

private String getJiraAccountCloudId(JiraSourceConfig config) {
String getJiraAccountCloudId() {
log.info("Getting Jira Account Cloud ID");
if (this.cloudId != null) {
return this.cloudId;
Expand All @@ -73,7 +72,7 @@ private String getJiraAccountCloudId(JiraSourceConfig config) {
}

HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(config.getAccessToken());
headers.setBearerAuth(jiraSourceConfig.getAccessToken());
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<Void> entity = new HttpEntity<>(headers);
int retryCount = 0;
Expand All @@ -82,8 +81,8 @@ private String getJiraAccountCloudId(JiraSourceConfig config) {
try {
ResponseEntity<Object> exchangeResponse =
restTemplate.exchange(ACCESSIBLE_RESOURCES, HttpMethod.GET, entity, Object.class);
List listResponse = (ArrayList) exchangeResponse.getBody();
Map<String, Object> response = (Map<String, Object>) listResponse.get(0);
List<Map<String, Object>> listResponse = (List<Map<String, Object>>) exchangeResponse.getBody();
Map<String, Object> response = listResponse.get(0);
return (String) response.get("id");
} catch (HttpClientErrorException e) {
if (e.getStatusCode().value() == TOKEN_EXPIRED) {
Expand Down Expand Up @@ -118,7 +117,7 @@ public void renewCredentials() {
"client_id", clientId,
"client_secret", clientSecret,
"refresh_token", refreshToken);
String payload = null;
String payload;
try {
payload = objectMapper.writeValueAsString(payloadMap);
} catch (JsonProcessingException e) {
Expand Down Expand Up @@ -162,7 +161,7 @@ public void renewCredentials() {
@Override
public String getUrl() {
if (url == null || url.isEmpty()) {
synchronized (this) {
synchronized (cloudIdFetchLock) {
if (url == null || url.isEmpty()) {
initCredentials();
}
Expand All @@ -177,7 +176,7 @@ public String getUrl() {
@Override
public void initCredentials() {
//For OAuth based flow, we use a different Jira url
this.cloudId = getJiraAccountCloudId(jiraSourceConfig);
this.cloudId = getJiraAccountCloudId();
this.url = OAuth2_URL + this.cloudId + SLASH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
@Slf4j
public class AddressValidation {

public AddressValidation() {
}

/**
* Method for getInetAddress.
*
Expand All @@ -30,10 +27,7 @@ public AddressValidation() {
public static InetAddress getInetAddress(String url) {
try {
return InetAddress.getByName(new URL(url).getHost());
} catch (UnknownHostException e) {
log.error(INVALID_URL, e);
throw new BadRequestException(e.getMessage(), e);
} catch (MalformedURLException e) {
} catch (UnknownHostException | MalformedURLException e) {
log.error(INVALID_URL, e);
throw new BadRequestException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.plugins.source.jira.JiraSourceConfig;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -40,7 +43,6 @@ void testRenewToken() {
Map<String, Object> firstMockResponseMap = Map.of("access_token", "first_mock_access_token",
"refresh_token", "first_mock_refresh_token",
"expires_in", 3600);

JiraOauthConfig jiraOauthConfig = new JiraOauthConfig(jiraSourceConfig);
when(restTemplateMock.postForEntity(any(String.class), any(HttpEntity.class), any(Class.class)))
.thenReturn(new ResponseEntity<>(firstMockResponseMap, HttpStatus.OK));
Expand All @@ -53,7 +55,6 @@ void testRenewToken() {
}
executor.shutdown();
assertNotNull(jiraOauthConfig.getAccessToken());
assertNotNull(jiraOauthConfig.getExpiresInSeconds());
assertNotNull(jiraOauthConfig.getExpireTime());
assertEquals(jiraOauthConfig.getRefreshToken(), "first_mock_refresh_token");
assertEquals(jiraOauthConfig.getExpiresInSeconds(), 3600);
Expand Down Expand Up @@ -89,4 +90,20 @@ void testEmptyRefreshToken() {
jiraOauthConfig.restTemplate = restTemplateMock;
assertThrows(RuntimeException.class, jiraOauthConfig::renewCredentials);
}

@Test
void testGetJiraAccountCloudId() {
Map<String, Object> mockGetCallResponse = new HashMap<>();
mockGetCallResponse.put("id", "test_cloud_id");
when(restTemplateMock.exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class)))
.thenReturn(new ResponseEntity<>(List.of(mockGetCallResponse), HttpStatus.OK));
JiraOauthConfig jiraOauthConfig = new JiraOauthConfig(jiraSourceConfig);
jiraOauthConfig.restTemplate = restTemplateMock;
assertEquals(jiraOauthConfig.getJiraAccountCloudId(), "test_cloud_id");

assertEquals("https://api.atlassian.com/ex/jira/test_cloud_id/", jiraOauthConfig.getUrl());


}

}

0 comments on commit 3a615e6

Please sign in to comment.