Skip to content

Commit

Permalink
me on cleanup island (cleaning up files and fixing tests)
Browse files Browse the repository at this point in the history
Signed-off-by: Maxwell Brown <[email protected]>
  • Loading branch information
Galactus22625 committed Jan 27, 2025
1 parent 5dfefd3 commit 7efbcae
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin;
import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor;
import org.opensearch.dataprepper.model.buffer.Buffer;
import org.opensearch.dataprepper.model.configuration.PipelineDescription;
import org.opensearch.dataprepper.model.event.Event;
import org.opensearch.dataprepper.model.plugin.PluginFactory;
import org.opensearch.dataprepper.model.record.Record;
Expand Down Expand Up @@ -57,12 +56,10 @@ public JiraSource(final PluginMetrics pluginMetrics,
final AcknowledgementSetManager acknowledgementSetManager,
Crawler crawler,
PluginExecutorServiceProvider executorServiceProvider,
final PipelineDescription pipelineDescription,
final JiraService jiraService,
final JiraRestClient jiraRestClient) {
super(PLUGIN_NAME, pluginMetrics, jiraSourceConfig, pluginFactory, acknowledgementSetManager, crawler, executorServiceProvider);
log.info("Creating Jira Source Plugin");
jiraSourceConfig.setPipelineName(pipelineDescription.getPipelineName());
jiraService.setPluginMetrics(pluginMetrics);
crawler.setPluginMetrics(pluginMetrics);
jiraRestClient.setPluginMetrics(pluginMetrics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import lombok.Getter;
import lombok.Setter;
import org.opensearch.dataprepper.plugins.source.jira.configuration.AuthenticationConfig;
import org.opensearch.dataprepper.plugins.source.jira.configuration.FilterConfig;
import org.opensearch.dataprepper.plugins.source.source_crawler.base.CrawlerSourceConfig;

import java.util.List;

import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.PLUGIN_NAME;

@Getter
public class JiraSourceConfig implements CrawlerSourceConfig {

Expand Down Expand Up @@ -60,15 +57,6 @@ public class JiraSourceConfig implements CrawlerSourceConfig {
@JsonProperty("acknowledgments")
private boolean acknowledgments = false;

/**
* Pipeline name to be used for pipeline metrics
*/
@Setter
@Getter
private String pipelineName;

@Getter
final private String pluginName = PLUGIN_NAME;

public String getAccountUrl() {
return this.getHosts().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.micrometer.core.instrument.Counter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.metrics.PluginMetrics;
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;
Expand Down Expand Up @@ -76,6 +78,12 @@ public class JiraServiceTest {
@Mock
private JiraRestClient jiraRestClient;

@Mock
private PluginMetrics pluginMetrics;

@Mock
private Counter counter;


private static InputStream getResourceAsStream(String resourceName) {
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
Expand Down Expand Up @@ -210,6 +218,9 @@ public void testGetJiraEntities() throws JsonProcessingException {

Instant timestamp = Instant.ofEpochSecond(0);
Queue<ItemInfo> itemInfoQueue = new ConcurrentLinkedQueue<>();

when(pluginMetrics.counter(anyString())).thenReturn(counter);
jiraService.setPluginMetrics(pluginMetrics);
jiraService.getJiraEntities(jiraSourceConfig, timestamp, itemInfoQueue);
assertEquals(mockIssues.size(), itemInfoQueue.size());
}
Expand All @@ -236,6 +247,8 @@ public void buildIssueItemInfoMultipleFutureThreads() throws JsonProcessingExcep

Instant timestamp = Instant.ofEpochSecond(0);
Queue<ItemInfo> itemInfoQueue = new ConcurrentLinkedQueue<>();
when(pluginMetrics.counter(anyString())).thenReturn(counter);
jiraService.setPluginMetrics(pluginMetrics);
jiraService.getJiraEntities(jiraSourceConfig, timestamp, itemInfoQueue);
assertTrue(itemInfoQueue.size() >= 100);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.model.acknowledgements.AcknowledgementSetManager;
import org.opensearch.dataprepper.model.buffer.Buffer;
import org.opensearch.dataprepper.model.configuration.PipelineDescription;
import org.opensearch.dataprepper.model.event.Event;
import org.opensearch.dataprepper.model.plugin.PluginFactory;
import org.opensearch.dataprepper.model.record.Record;
import org.opensearch.dataprepper.model.source.coordinator.enhanced.EnhancedSourceCoordinator;
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.rest.auth.JiraAuthConfig;
import org.opensearch.dataprepper.plugins.source.jira.rest.JiraRestClient;
import org.opensearch.dataprepper.plugins.source.jira.rest.auth.JiraAuthConfig;
import org.opensearch.dataprepper.plugins.source.source_crawler.base.Crawler;
import org.opensearch.dataprepper.plugins.source.source_crawler.base.PluginExecutorServiceProvider;

Expand Down Expand Up @@ -79,9 +78,6 @@ public class JiraSourceTest {
@Mock
BasicConfig basicConfig;

@Mock
private PipelineDescription pipelineDescription;

@Mock
private JiraService jiraService;

Expand All @@ -91,14 +87,14 @@ public class JiraSourceTest {
@Test
void initialization() {
when(executorServiceProvider.get()).thenReturn(executorService);
JiraSource source = new JiraSource(pluginMetrics, jiraSourceConfig, jiraOauthConfig, pluginFactory, acknowledgementSetManager, crawler, executorServiceProvider, pipelineDescription, jiraService, jiraRestClient);
JiraSource source = new JiraSource(pluginMetrics, jiraSourceConfig, jiraOauthConfig, pluginFactory, acknowledgementSetManager, crawler, executorServiceProvider, jiraService, jiraRestClient);
assertNotNull(source);
}

@Test
void testStart() {
when(executorServiceProvider.get()).thenReturn(executorService);
JiraSource source = new JiraSource(pluginMetrics, jiraSourceConfig, jiraOauthConfig, pluginFactory, acknowledgementSetManager, crawler, executorServiceProvider, pipelineDescription, jiraService, jiraRestClient);
JiraSource source = new JiraSource(pluginMetrics, jiraSourceConfig, jiraOauthConfig, pluginFactory, acknowledgementSetManager, crawler, executorServiceProvider, jiraService, jiraRestClient);
when(jiraSourceConfig.getAccountUrl()).thenReturn(ACCESSIBLE_RESOURCES);
when(jiraSourceConfig.getAuthType()).thenReturn(BASIC);
when(jiraSourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
Expand All @@ -114,7 +110,7 @@ void testStart() {
@Test
void testStop() {
when(executorServiceProvider.get()).thenReturn(executorService);
JiraSource source = new JiraSource(pluginMetrics, jiraSourceConfig, jiraOauthConfig, pluginFactory, acknowledgementSetManager, crawler, executorServiceProvider, pipelineDescription, jiraService, jiraRestClient);
JiraSource source = new JiraSource(pluginMetrics, jiraSourceConfig, jiraOauthConfig, pluginFactory, acknowledgementSetManager, crawler, executorServiceProvider, jiraService, jiraRestClient);
when(jiraSourceConfig.getAccountUrl()).thenReturn(ACCESSIBLE_RESOURCES);
when(jiraSourceConfig.getAuthType()).thenReturn(BASIC);
when(jiraSourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
Expand All @@ -131,7 +127,7 @@ void testStop() {
@Test
void testStop_WhenNotStarted() {
when(executorServiceProvider.get()).thenReturn(executorService);
JiraSource source = new JiraSource(pluginMetrics, jiraSourceConfig, jiraOauthConfig, pluginFactory, acknowledgementSetManager, crawler, executorServiceProvider, pipelineDescription, jiraService, jiraRestClient);
JiraSource source = new JiraSource(pluginMetrics, jiraSourceConfig, jiraOauthConfig, pluginFactory, acknowledgementSetManager, crawler, executorServiceProvider, jiraService, jiraRestClient);

source.stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package org.opensearch.dataprepper.plugins.source.jira.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Timer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -19,6 +21,7 @@
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.plugins.source.jira.JiraServiceTest;
import org.opensearch.dataprepper.plugins.source.jira.JiraSourceConfig;
import org.opensearch.dataprepper.plugins.source.jira.exception.BadRequestException;
Expand All @@ -40,6 +43,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -58,6 +62,15 @@ public class JiraRestClientTest {
@Mock
private JiraAuthConfig authConfig;

@Mock
private PluginMetrics pluginMetrics;

@Mock
private Timer timer;

@Mock
private Counter counter;

private static Stream<Arguments> provideHttpStatusCodesWithExceptionClass() {
return Stream.of(
Arguments.of(HttpStatus.FORBIDDEN, UnAuthorizedException.class),
Expand All @@ -75,6 +88,9 @@ public void testFetchingJiraIssue(String configFileName) {
JiraSourceConfig jiraSourceConfig = JiraServiceTest.createJiraConfigurationFromYaml(configFileName);
JiraAuthConfig authConfig = new JiraAuthFactory(jiraSourceConfig).getObject();
JiraRestClient jiraRestClient = new JiraRestClient(restTemplate, authConfig);
when(pluginMetrics.counter(anyString())).thenReturn(counter);
when(pluginMetrics.timer(anyString())).thenReturn(timer);
jiraRestClient.setPluginMetrics(pluginMetrics);
String ticketDetails = jiraRestClient.getIssue("key");
assertEquals(exampleTicketResponse, ticketDetails);
}
Expand All @@ -86,6 +102,9 @@ void testInvokeRestApiTokenExpired(HttpStatus statusCode, Class expectedExceptio
jiraRestClient.setSleepTimeMultiplier(1);
when(authConfig.getUrl()).thenReturn("https://example.com/rest/api/2/issue/key");
when(restTemplate.getForEntity(any(URI.class), any(Class.class))).thenThrow(new HttpClientErrorException(statusCode));
when(pluginMetrics.counter(anyString())).thenReturn(counter);
when(pluginMetrics.timer(anyString())).thenReturn(timer);
jiraRestClient.setPluginMetrics(pluginMetrics);
assertThrows(expectedExceptionType, () -> jiraRestClient.getIssue("key"));
}

Expand All @@ -94,6 +113,9 @@ void testInvokeRestApiTokenExpiredInterruptException() throws InterruptedExcepti
JiraRestClient jiraRestClient = new JiraRestClient(restTemplate, authConfig);
when(authConfig.getUrl()).thenReturn("https://example.com/rest/api/2/issue/key");
when(restTemplate.getForEntity(any(URI.class), any(Class.class))).thenThrow(new HttpClientErrorException(HttpStatus.TOO_MANY_REQUESTS));
when(pluginMetrics.counter(anyString())).thenReturn(counter);
when(pluginMetrics.timer(anyString())).thenReturn(timer);
jiraRestClient.setPluginMetrics(pluginMetrics);
jiraRestClient.setSleepTimeMultiplier(100000);

Thread testThread = new Thread(() -> {
Expand Down Expand Up @@ -121,6 +143,9 @@ public void testGetAllIssuesOauth2() throws JsonProcessingException {
SearchResults mockSearchResults = mock(SearchResults.class);
doReturn("http://mock-service.jira.com/").when(authConfig).getUrl();
doReturn(new ResponseEntity<>(mockSearchResults, HttpStatus.OK)).when(restTemplate).getForEntity(any(URI.class), any(Class.class));
when(pluginMetrics.counter(anyString())).thenReturn(counter);
when(pluginMetrics.timer(anyString())).thenReturn(timer);
jiraRestClient.setPluginMetrics(pluginMetrics);
SearchResults results = jiraRestClient.getAllIssues(jql, 0, jiraSourceConfig);
assertNotNull(results);
}
Expand All @@ -136,6 +161,9 @@ public void testGetAllIssuesBasic() throws JsonProcessingException {
SearchResults mockSearchResults = mock(SearchResults.class);
when(authConfig.getUrl()).thenReturn("https://example.com/");
doReturn(new ResponseEntity<>(mockSearchResults, HttpStatus.OK)).when(restTemplate).getForEntity(any(URI.class), any(Class.class));
when(pluginMetrics.counter(anyString())).thenReturn(counter);
when(pluginMetrics.timer(anyString())).thenReturn(timer);
jiraRestClient.setPluginMetrics(pluginMetrics);
SearchResults results = jiraRestClient.getAllIssues(jql, 0, jiraSourceConfig);
assertNotNull(results);
}
Expand All @@ -144,6 +172,9 @@ public void testGetAllIssuesBasic() throws JsonProcessingException {
public void testRestApiAddressValidation() throws JsonProcessingException {
when(authConfig.getUrl()).thenReturn("https://224.0.0.1/");
JiraRestClient jiraRestClient = new JiraRestClient(restTemplate, authConfig);
when(pluginMetrics.counter(anyString())).thenReturn(counter);
when(pluginMetrics.timer(anyString())).thenReturn(timer);
jiraRestClient.setPluginMetrics(pluginMetrics);
assertThrows(BadRequestException.class, () -> jiraRestClient.getIssue("TEST-1"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class Crawler {
private static final Logger log = LoggerFactory.getLogger(Crawler.class);
private Timer crawlingTimer;
private PluginMetrics pluginMetrics;


private final CrawlerClient client;

public Crawler(CrawlerClient client) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.opensearch.dataprepper.plugins.source.source_crawler.base;

import io.micrometer.core.instrument.Timer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.model.acknowledgements.AcknowledgementSet;
import org.opensearch.dataprepper.model.buffer.Buffer;
import org.opensearch.dataprepper.model.event.Event;
Expand All @@ -24,6 +26,7 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -40,6 +43,12 @@ public class CrawlerTest {
@Mock
private Buffer<Record<Event>> buffer;

@Mock
private PluginMetrics pluginMetrics;

@Mock
private Timer timer;

@Mock
private CrawlerClient client;

Expand All @@ -53,6 +62,8 @@ public class CrawlerTest {
@BeforeEach
public void setup() {
crawler = new Crawler(client);
when(pluginMetrics.timer(anyString())).thenReturn(timer);
crawler.setPluginMetrics(pluginMetrics);
}

@Test
Expand Down

0 comments on commit 7efbcae

Please sign in to comment.