Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jira tests #9

Merged
merged 22 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public JiraClient(JiraService service,
this.configuration = sourceConfig;
}


@Override
public Iterator<ItemInfo> listItems() {
jiraIterator.initialize(lastPollTime);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package org.opensearch.dataprepper.plugins.source.jira;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.buffer.Buffer;
import org.opensearch.dataprepper.model.event.Event;
import org.opensearch.dataprepper.model.record.Record;
import org.opensearch.dataprepper.plugins.source.source_crawler.base.CrawlerSourceConfig;
import org.opensearch.dataprepper.plugins.source.source_crawler.base.PluginExecutorServiceProvider;
import org.opensearch.dataprepper.plugins.source.source_crawler.coordination.state.SaasWorkerProgressState;

import java.time.Instant;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class JiraClientTest {

@Mock
private Buffer<Record<Event>> buffer;

@Mock
private SaasWorkerProgressState saasWorkerProgressState;

@Mock
private CrawlerSourceConfig crawlerSourceConfig;

@Mock
private JiraSourceConfig jiraSourceConfig;

@Mock
ObjectMapper objectMapper;

@Mock
private JiraService jiraService;

@Mock
private JiraIterator jiraIterator;

private PluginExecutorServiceProvider executorServiceProvider = new PluginExecutorServiceProvider();

@Test
void testConstructor() {
JiraClient jiraClient;
jiraClient = new JiraClient(jiraService, jiraIterator, executorServiceProvider, jiraSourceConfig);
Galactus22625 marked this conversation as resolved.
Show resolved Hide resolved
assertNotNull(jiraClient);
}

@Test
void testListItems() {
JiraClient jiraClient;
jiraClient = new JiraClient(jiraService, jiraIterator, executorServiceProvider, jiraSourceConfig);
assertNotNull(jiraClient.listItems());
}

@Test
void testSetLastPollTime() throws NoSuchFieldException, IllegalAccessException {
Galactus22625 marked this conversation as resolved.
Show resolved Hide resolved
JiraClient jiraClient;
jiraClient = new JiraClient(jiraService, jiraIterator, executorServiceProvider, jiraSourceConfig);
jiraClient.setLastPollTime(Instant.ofEpochSecond(1234L));

}


@Test
void testExecutePartition() throws Exception {
JiraClient jiraClient;
jiraClient = new JiraClient(jiraService, jiraIterator, executorServiceProvider, jiraSourceConfig);
Galactus22625 marked this conversation as resolved.
Show resolved Hide resolved
Map<String, Object> keyAttributes = new HashMap<>();
keyAttributes.put("project", "test");
when(saasWorkerProgressState.getKeyAttributes()).thenReturn(keyAttributes);
List<String> itemIds = List.of("ID1", "ID2", "ID3", "ID4");
when(saasWorkerProgressState.getItemIds()).thenReturn(itemIds);
Instant exportStartTime = Instant.now();
when(saasWorkerProgressState.getExportStartTime()).thenReturn(Instant.ofEpochSecond(exportStartTime.toEpochMilli()));

when(jiraService.getIssue(anyString())).thenReturn("{\"id\":\"ID1\",\"key\":\"TEST-1\"}");

ArgumentCaptor<Collection<Record<Event>>> recordsCaptor = ArgumentCaptor.forClass((Class) Collection.class);

jiraClient.executePartition(saasWorkerProgressState, buffer, crawlerSourceConfig);

verify(buffer).writeAll(recordsCaptor.capture(), anyInt());
Collection<Record<Event>> capturedRecords = recordsCaptor.getValue();
assertFalse(capturedRecords.isEmpty());
for (Record<Event> record : capturedRecords) {
assertNotNull(record.getData());
}
}

@Test
void bufferWriteRuntimeTest() throws Exception {
JiraClient jiraClient;
jiraClient = new JiraClient(jiraService, jiraIterator, executorServiceProvider, jiraSourceConfig);
Map<String, Object> keyAttributes = new HashMap<>();
keyAttributes.put("project", "test");
when(saasWorkerProgressState.getKeyAttributes()).thenReturn(keyAttributes);
List<String> itemIds = List.of("ID1", "ID2", "ID3", "ID4");
when(saasWorkerProgressState.getItemIds()).thenReturn(itemIds);
Instant exportStartTime = Instant.now();
when(saasWorkerProgressState.getExportStartTime()).thenReturn(Instant.ofEpochSecond(exportStartTime.toEpochMilli()));

when(jiraService.getIssue(anyString())).thenReturn("{\"id\":\"ID1\",\"key\":\"TEST-1\"}");

ArgumentCaptor<Collection<Record<Event>>> recordsCaptor = ArgumentCaptor.forClass((Class) Collection.class);

doThrow(new RuntimeException()).when(buffer).writeAll(recordsCaptor.capture(), anyInt());
assertThrows(RuntimeException.class, () -> jiraClient.executePartition(saasWorkerProgressState, buffer, crawlerSourceConfig));
}
}
Loading
Loading