Skip to content

Commit

Permalink
fixing the test cases based on the new PluginConfigVariable attribute…
Browse files Browse the repository at this point in the history
… used for refreshToken

Signed-off-by: Santhosh Gandhe <[email protected]>
  • Loading branch information
san81 committed Jan 13, 2025
1 parent 2f640df commit 64bf7bc
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public String getIssue(String issueKey) {
return invokeRestApi(uri, String.class).getBody();
}

private <T> ResponseEntity<T> invokeRestApi(URI uri, Class<T> responseType) throws BadRequestException{
private <T> ResponseEntity<T> invokeRestApi(URI uri, Class<T> responseType) throws BadRequestException {
AddressValidation.validateInetAddress(AddressValidation.getInetAddress(uri.toString()));
int retryCount = 0;
while (retryCount < RETRY_ATTEMPT) {
Expand All @@ -119,7 +119,7 @@ private <T> ResponseEntity<T> invokeRestApi(URI uri, Class<T> responseType) thro
} catch (HttpClientErrorException ex) {
HttpStatus statusCode = ex.getStatusCode();
String statusMessage = ex.getMessage();
log.error("An exception has occurred while getting response from Jira search API {}", ex.getMessage(), ex);
log.error("An exception has occurred while getting response from Jira search API {}", ex.getMessage());
if (statusCode == HttpStatus.FORBIDDEN) {
throw new UnAuthorizedException(statusMessage);
} else if (statusCode == HttpStatus.UNAUTHORIZED) {
Expand All @@ -136,7 +136,7 @@ private <T> ResponseEntity<T> invokeRestApi(URI uri, Class<T> responseType) thro
}
retryCount++;
}
String errorMessage = String.format("Exceeded max retry attempts. Failed to execute the Rest API call %s", uri.toString());
String errorMessage = String.format("Exceeded max retry attempts. Failed to execute the Rest API call %s", uri);
log.error(errorMessage);
throw new RuntimeException(errorMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.plugin.PluginConfigVariable;
import org.opensearch.dataprepper.plugins.source.jira.configuration.AuthenticationConfig;
import org.opensearch.dataprepper.plugins.source.jira.configuration.BasicConfig;
import org.opensearch.dataprepper.plugins.source.jira.configuration.FilterConfig;
Expand Down Expand Up @@ -51,10 +52,10 @@ public class JiraConfigHelperTest {
IssueTypeConfig issueTypeConfig;

@Mock
ProjectConfig projectConfig;
ProjectConfig projectConfig;

@Mock
NameConfig nameConfig;
NameConfig nameConfig;

@Mock
AuthenticationConfig authenticationConfig;
Expand All @@ -63,7 +64,10 @@ public class JiraConfigHelperTest {
BasicConfig basicConfig;

@Mock
Oauth2Config oauth2Config;
Oauth2Config oauth2Config;

@Mock
PluginConfigVariable pluginConfigVariable;

@Test
void testInitialization() {
Expand Down Expand Up @@ -156,7 +160,7 @@ void testValidateConfigOauth2() {
when(oauth2Config.getAccessToken()).thenReturn("id");
assertThrows(RuntimeException.class, () -> JiraConfigHelper.validateConfig(jiraSourceConfig));

when(oauth2Config.getRefreshToken()).thenReturn("credential");
when(authenticationConfig.getOauth2Config().getRefreshToken()).thenReturn(pluginConfigVariable);
when(oauth2Config.getAccessToken()).thenReturn(null);
assertThrows(RuntimeException.class, () -> JiraConfigHelper.validateConfig(jiraSourceConfig));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.plugin.PluginConfigVariable;
import org.opensearch.dataprepper.plugins.source.jira.configuration.Oauth2Config;
import org.opensearch.dataprepper.plugins.source.jira.exception.BadRequestException;
import org.opensearch.dataprepper.plugins.source.jira.models.IssueBean;
import org.opensearch.dataprepper.plugins.source.jira.models.SearchResults;
import org.opensearch.dataprepper.plugins.source.jira.rest.JiraRestClient;
import org.opensearch.dataprepper.plugins.source.jira.utils.MockPluginConfigVariableImpl;
import org.opensearch.dataprepper.plugins.source.jira.utils.TestUtilForPrivateFields;
import org.opensearch.dataprepper.plugins.source.source_crawler.base.PluginExecutorServiceProvider;
import org.opensearch.dataprepper.plugins.source.source_crawler.model.ItemInfo;
import org.slf4j.Logger;
Expand Down Expand Up @@ -58,6 +62,7 @@
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.OAUTH2;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.PROJECT;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.UPDATED;
import static org.opensearch.dataprepper.plugins.source.jira.utils.TestUtilForPrivateFields.setPrivateField;


/**
Expand All @@ -84,9 +89,16 @@ private static InputStream getResourceAsStream(String resourceName) {
public static JiraSourceConfig createJiraConfigurationFromYaml(String fileName) {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
try (InputStream inputStream = getResourceAsStream(fileName)) {
return objectMapper.readValue(inputStream, JiraSourceConfig.class);
JiraSourceConfig jiraSourceConfig = objectMapper.readValue(inputStream, JiraSourceConfig.class);
Oauth2Config oauth2Config = jiraSourceConfig.getAuthenticationConfig().getOauth2Config();
if (oauth2Config != null) {
setPrivateField(oauth2Config, "refreshToken", new MockPluginConfigVariableImpl("mockRefreshToken"));
}
return jiraSourceConfig;
} catch (IOException ex) {
log.error("Failed to parse pipeline Yaml", ex);
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
Expand All @@ -95,6 +107,7 @@ public static JiraSourceConfig createJiraConfiguration(String auth_type,
List<String> issueType,
List<String> issueStatus,
List<String> projectKey) throws JsonProcessingException {
PluginConfigVariable pcv = null;
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> authenticationMap = new HashMap<>();
Map<String, String> basicMap = new HashMap<>();
Expand All @@ -103,11 +116,11 @@ public static JiraSourceConfig createJiraConfiguration(String auth_type,
basicMap.put("username", "test_username");
basicMap.put("password", "test_password");
authenticationMap.put("basic", basicMap);
} else if (auth_type.equals(OAUTH2)) {
} else if (auth_type.equals(OAUTH2)) {
oauth2Map.put("client_id", "test-client-id");
oauth2Map.put("client_secret", "test-client-secret");
oauth2Map.put("access_token", "test-access-token");
oauth2Map.put("refresh_token", "test-refresh-token");
pcv = new MockPluginConfigVariableImpl("test-refresh-token");
authenticationMap.put("oauth2", oauth2Map);
}

Expand Down Expand Up @@ -137,7 +150,16 @@ public static JiraSourceConfig createJiraConfiguration(String auth_type,
jiraSourceConfigMap.put("filter", filterMap);

String jiraSourceConfigJsonString = objectMapper.writeValueAsString(jiraSourceConfigMap);
return objectMapper.readValue(jiraSourceConfigJsonString, JiraSourceConfig.class);
JiraSourceConfig jiraSourceConfig = objectMapper.readValue(jiraSourceConfigJsonString, JiraSourceConfig.class);
if (jiraSourceConfig.getAuthenticationConfig().getOauth2Config() != null && pcv != null) {
try {
TestUtilForPrivateFields.setPrivateField(
jiraSourceConfig.getAuthenticationConfig().getOauth2Config(), "refreshToken", pcv);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return jiraSourceConfig;
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

package org.opensearch.dataprepper.plugins.source.jira;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.opensearch.dataprepper.model.plugin.PluginConfigVariable;
import org.opensearch.dataprepper.plugins.source.jira.utils.MockPluginConfigVariableImpl;
import org.opensearch.dataprepper.plugins.source.jira.utils.TestUtilForPrivateFields;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -28,18 +30,19 @@

public class JiraSourceConfigTest {
private final String accessToken = "access token test";
private final String refreshToken = "refresh token test";
private final PluginConfigVariable refreshToken = new MockPluginConfigVariableImpl("refresh token test");
private final String clientId = "client id test";
private final String clientSecret = "client secret test";
private final String password = "test Jira Credential";
private final String username = "test Jira Id";
private final String accountUrl = "https://example.atlassian.net";
private List<String> projectList = new ArrayList<>();
private List<String> issueTypeList = new ArrayList<>();
private List<String> statusList = new ArrayList<>();
private final List<String> projectList = new ArrayList<>();
private final List<String> issueTypeList = new ArrayList<>();
private final List<String> statusList = new ArrayList<>();
private JiraSourceConfig jiraSourceConfig;

private JiraSourceConfig createJiraSourceConfig(String authtype, boolean hasToken) throws JsonProcessingException {
private JiraSourceConfig createJiraSourceConfig(String authtype, boolean hasToken) throws Exception {
PluginConfigVariable pcv = null;
Map<String, Object> configMap = new HashMap<>();
List<String> hosts = new ArrayList<>();
hosts.add(accountUrl);
Expand All @@ -48,18 +51,17 @@ private JiraSourceConfig createJiraSourceConfig(String authtype, boolean hasToke

Map<String, Object> authenticationMap = new HashMap<>();
Map<String, String> basicMap = new HashMap<>();
Map<String, String> oauth2Map = new HashMap<>();
Map<String, Object> oauth2Map = new HashMap<>();
if (authtype.equals(BASIC)) {
basicMap.put("username", username);
basicMap.put("password", password);
authenticationMap.put("basic", basicMap);
} else if (authtype.equals(OAUTH2)) {
if (hasToken) {
oauth2Map.put("access_token", accessToken);
oauth2Map.put("refresh_token", refreshToken);
pcv = refreshToken;
} else {
oauth2Map.put("access_token", null);
oauth2Map.put("refresh_token", null);
}
oauth2Map.put("client_id", clientId);
oauth2Map.put("client_secret", clientSecret);
Expand Down Expand Up @@ -98,11 +100,14 @@ private JiraSourceConfig createJiraSourceConfig(String authtype, boolean hasToke
ObjectMapper objectMapper = new ObjectMapper();
String jsonConfig = objectMapper.writeValueAsString(configMap);
JiraSourceConfig config = objectMapper.readValue(jsonConfig, JiraSourceConfig.class);
if (config.getAuthenticationConfig().getOauth2Config() != null && pcv != null) {
TestUtilForPrivateFields.setPrivateField(config.getAuthenticationConfig().getOauth2Config(), "refreshToken", pcv);
}
return config;
}

@Test
void testGetters() throws JsonProcessingException {
void testGetters() throws Exception {
jiraSourceConfig = createJiraSourceConfig(BASIC, false);
assertEquals(jiraSourceConfig.getFilterConfig().getIssueTypeConfig().getInclude(), issueTypeList);
assertEquals(jiraSourceConfig.getNumWorkers(), DEFAULT_NUMBER_OF_WORKERS);
Expand All @@ -115,18 +120,17 @@ void testGetters() throws JsonProcessingException {
}

@Test
void testFetchGivenOauthAttributeWrongAuthType() throws JsonProcessingException {
void testFetchGivenOauthAttributeWrongAuthType() throws Exception {
jiraSourceConfig = createJiraSourceConfig(BASIC, true);
assertThrows(RuntimeException.class, () -> jiraSourceConfig.getAuthenticationConfig().getOauth2Config().getAccessToken());
}

@Test
void testFetchGivenOauthAtrribute() throws JsonProcessingException {
void testFetchGivenOauthAtrribute() throws Exception {
jiraSourceConfig = createJiraSourceConfig(OAUTH2, true);
assertEquals(accessToken, jiraSourceConfig.getAuthenticationConfig().getOauth2Config().getAccessToken());
assertEquals(refreshToken, jiraSourceConfig.getAuthenticationConfig().getOauth2Config().getRefreshToken());
assertEquals(clientId, jiraSourceConfig.getAuthenticationConfig().getOauth2Config().getClientId());
assertEquals(clientSecret, jiraSourceConfig.getAuthenticationConfig().getOauth2Config().getClientSecret());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.plugin.PluginConfigVariable;
import org.opensearch.dataprepper.plugins.source.jira.JiraSourceConfig;
import org.opensearch.dataprepper.plugins.source.jira.configuration.AuthenticationConfig;
import org.opensearch.dataprepper.plugins.source.jira.configuration.BasicConfig;
Expand Down Expand Up @@ -49,11 +50,14 @@ class CustomRestTemplateConfigTest {
private JiraAuthConfig mockAuthConfig;

@Mock
private BasicConfig mockBasicConfig;
private BasicConfig mockBasicConfig;

@Mock
private Oauth2Config mockOauth2Config;

@Mock
private PluginConfigVariable pluginConfigVariable;

@Mock
private AuthenticationConfig mockAuthenticationConfig;

Expand All @@ -78,7 +82,8 @@ void testBasicAuthRestTemplateWithOAuth2(String authType, Class interceptorClass
lenient().when(mockSourceConfig.getAuthenticationConfig()).thenReturn(mockAuthenticationConfig);
lenient().when(mockAuthenticationConfig.getOauth2Config()).thenReturn(mockOauth2Config);
lenient().when(mockOauth2Config.getAccessToken()).thenReturn("accessToken");
lenient().when(mockOauth2Config.getRefreshToken()).thenReturn("refreshToken");
lenient().when(mockOauth2Config.getRefreshToken()).thenReturn(pluginConfigVariable);
lenient().when(pluginConfigVariable.getValue()).thenReturn("refreshToken");
lenient().when(mockOauth2Config.getClientId()).thenReturn("clientId");
lenient().when(mockOauth2Config.getClientSecret()).thenReturn("clientSecret");
lenient().when(mockAuthenticationConfig.getBasicConfig()).thenReturn(mockBasicConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.plugin.PluginConfigVariable;
import org.opensearch.dataprepper.plugins.source.jira.JiraSourceConfig;
import org.opensearch.dataprepper.plugins.source.jira.configuration.AuthenticationConfig;
import org.opensearch.dataprepper.plugins.source.jira.configuration.Oauth2Config;
Expand All @@ -31,11 +32,14 @@ public class JiraAuthFactoryTest {
private JiraSourceConfig sourceConfig;

@Mock
private AuthenticationConfig authenticationConfig;
private AuthenticationConfig authenticationConfig;

@Mock
private Oauth2Config oauth2Config;

@Mock
private PluginConfigVariable pluginConfigVariable;

private JiraAuthFactory jiraAuthFactory;

@BeforeEach
Expand All @@ -48,6 +52,8 @@ void testGetObjectOauth2() {
when(sourceConfig.getAuthType()).thenReturn(OAUTH2);
when(sourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
when(authenticationConfig.getOauth2Config()).thenReturn(oauth2Config);
when(oauth2Config.getRefreshToken()).thenReturn(pluginConfigVariable);
when(pluginConfigVariable.getValue()).thenReturn("mockRefreshToken");
assertInstanceOf(JiraOauthConfig.class, jiraAuthFactory.getObject());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
*/

package org.opensearch.dataprepper.plugins.source.jira.utils;

import org.opensearch.dataprepper.model.plugin.PluginConfigVariable;

/**
* Mock implementation of PluginConfigVariable interface used only for Unit Testing.
*/
public class MockPluginConfigVariableImpl implements PluginConfigVariable {

private Object defaultValue;

public MockPluginConfigVariableImpl(Object defaultValue) {
this.defaultValue = defaultValue;
}

@Override
public Object getValue() {
return null;
}

@Override
public void setValue(Object someValue) {
this.defaultValue = someValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
*/
package org.opensearch.dataprepper.plugins.source.jira.utils;

import java.lang.reflect.Field;

/**
* Utility class to help test private fields
*/
public class TestUtilForPrivateFields {

// Helper method to set private fields via reflection
public static void setPrivateField(Object targetObject, String fieldName, Object value)
throws Exception {
Field field = targetObject.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(targetObject, value);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
hosts: ["https://jira.com/"]
hosts: [ "https://jira.com/" ]
authentication:
oauth2:
client_id: "client_id"
client_secret: "client_secret"
access_token: "access_token"
refresh_token: "refresh_token"

0 comments on commit 64bf7bc

Please sign in to comment.