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 all 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
Expand Up @@ -11,7 +11,6 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -64,7 +63,7 @@ public boolean hasNext() {

private boolean isCrawlerRunning() {
boolean isRunning = Boolean.FALSE;
if (Objects.nonNull(futureList)) {
if (!futureList.isEmpty()) {
for (Future<Boolean> future : futureList) {
if (!future.isDone()) {
isRunning = Boolean.TRUE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.opensearch.dataprepper.plugins.source.jira;

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
private JiraService jiraService;

@Mock
private JiraIterator jiraIterator;

private PluginExecutorServiceProvider executorServiceProvider = new PluginExecutorServiceProvider();

@Test
void testConstructor() {
JiraClient jiraClient = new JiraClient(jiraService, jiraIterator, executorServiceProvider, jiraSourceConfig);
jiraClient.setLastPollTime(Instant.ofEpochSecond(1234L));
assertNotNull(jiraClient);
}

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


@Test
void testExecutePartition() throws Exception {
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);

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 = 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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.opensearch.dataprepper.plugins.source.jira;

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.plugins.source.jira.utils.JiraConfigHelper;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.BASIC;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.OAUTH2;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JiraConfigHelper.ISSUE_STATUS_FILTER;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JiraConfigHelper.ISSUE_TYPE_FILTER;

@ExtendWith(MockitoExtension.class)
public class JiraConfigHelperTest {

@Mock
JiraSourceConfig jiraSourceConfig;

@Test
void testInitialization() {
JiraConfigHelper jiraConfigHelper = new JiraConfigHelper();
assertNotNull(jiraConfigHelper);
}

@Test
void testIssueTypeFilter() {
testGetIssue(ISSUE_TYPE_FILTER);
}

@Test
void testIssueStatusFilter() {
testGetIssue(ISSUE_STATUS_FILTER);
}

private void testGetIssue(String filter) {
List<String> issueTypeFilter = List.of("Bug", "Task");
when(jiraSourceConfig.getAdditionalProperties()).thenReturn(
Map.of(filter, issueTypeFilter)
);
List<String> result = null;
if (filter.equals(ISSUE_TYPE_FILTER)) {
result = JiraConfigHelper.getIssueTypeFilter(jiraSourceConfig);
} else if (filter.equals(ISSUE_STATUS_FILTER)) {
result = JiraConfigHelper.getIssueStatusFilter(jiraSourceConfig);
}
assertEquals(issueTypeFilter, result);
}


@Test
void testGetProjectKeyFilter() {
assertTrue(JiraConfigHelper.getProjectKeyFilter(jiraSourceConfig).isEmpty());
List<String> projectKeyFilter = List.of("TEST", "TEST2");
when(jiraSourceConfig.getProject()).thenReturn(projectKeyFilter);
assertEquals(projectKeyFilter, JiraConfigHelper.getProjectKeyFilter(jiraSourceConfig));
}

@Test
void testValidateConfig() {
assertThrows(RuntimeException.class, () -> JiraConfigHelper.validateConfig(jiraSourceConfig));

when(jiraSourceConfig.getAccountUrl()).thenReturn("https://test.com");
assertThrows(RuntimeException.class, () -> JiraConfigHelper.validateConfig(jiraSourceConfig));

when(jiraSourceConfig.getAuthType()).thenReturn("fakeType");
assertThrows(RuntimeException.class, () -> JiraConfigHelper.validateConfig(jiraSourceConfig));
}

@Test
void testValidateConfigBasic() {
when(jiraSourceConfig.getAccountUrl()).thenReturn("https://test.com");
when(jiraSourceConfig.getAuthType()).thenReturn(BASIC);
assertThrows(RuntimeException.class, () -> JiraConfigHelper.validateConfig(jiraSourceConfig));

when(jiraSourceConfig.getJiraId()).thenReturn("id");
assertThrows(RuntimeException.class, () -> JiraConfigHelper.validateConfig(jiraSourceConfig));

when(jiraSourceConfig.getJiraCredential()).thenReturn("credential");
when(jiraSourceConfig.getJiraId()).thenReturn(null);
assertThrows(RuntimeException.class, () -> JiraConfigHelper.validateConfig(jiraSourceConfig));

when(jiraSourceConfig.getJiraId()).thenReturn("id");
assertDoesNotThrow(() -> JiraConfigHelper.validateConfig(jiraSourceConfig));
}

@Test
void testValidateConfigOauth2() {
when(jiraSourceConfig.getAccountUrl()).thenReturn("https://test.com");
when(jiraSourceConfig.getAuthType()).thenReturn(OAUTH2);
assertThrows(RuntimeException.class, () -> JiraConfigHelper.validateConfig(jiraSourceConfig));

when(jiraSourceConfig.getAccessToken()).thenReturn("id");
assertThrows(RuntimeException.class, () -> JiraConfigHelper.validateConfig(jiraSourceConfig));

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

when(jiraSourceConfig.getAccessToken()).thenReturn("id");
assertDoesNotThrow(() -> JiraConfigHelper.validateConfig(jiraSourceConfig));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.opensearch.dataprepper.plugins.source.jira;

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 java.time.Instant;
import java.util.Map;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import static org.opensearch.dataprepper.plugins.source.jira.JiraItemInfo.PROJECT;

@ExtendWith(MockitoExtension.class)
public class JiraItemInfoTest {
private String project;
private String issueType;
private String id;
private String itemId;
private Instant eventTime;

@Mock
private Map<String, Object> metadata;

@Mock
private Map<String, Object> newMetadata;

@Mock
private JiraItemInfo jiraItemInfo;

@BeforeEach
void setUP() {
issueType = "TestIssue";
id = UUID.randomUUID().toString();
project = "TestProject";
itemId = UUID.randomUUID().toString();
eventTime = Instant.ofEpochSecond(0);
jiraItemInfo = new JiraItemInfo(id, itemId, project, issueType, metadata, eventTime);
}

@Test
void testGetters() {
assertEquals(jiraItemInfo.getItemId(), itemId);
assertEquals(jiraItemInfo.getId(), id);
assertEquals(jiraItemInfo.getProject(), project);
assertEquals(jiraItemInfo.getIssueType(), issueType);
assertEquals(jiraItemInfo.getMetadata(), metadata);
assertEquals(jiraItemInfo.getEventTime(), eventTime);
}

@Test
void testSetter() {
jiraItemInfo.setEventTime(Instant.now());
assertNotEquals(jiraItemInfo.getEventTime(), eventTime);
jiraItemInfo.setItemId("newItemID");
assertNotEquals(jiraItemInfo.getItemId(), itemId);
jiraItemInfo.setId("newID");
assertNotEquals(jiraItemInfo.getId(), id);
jiraItemInfo.setProject("newProject");
assertNotEquals(jiraItemInfo.getProject(), project);
jiraItemInfo.setMetadata(newMetadata);
assertNotEquals(jiraItemInfo.getMetadata(), metadata);
jiraItemInfo.setIssueType("newIssueType");
assertNotEquals(jiraItemInfo.getIssueType(), issueType);

}

@Test
void testGetPartitionKey() {
String partitionKey = jiraItemInfo.getPartitionKey();
assertTrue(partitionKey.contains(project));
assertTrue(partitionKey.contains(issueType));
}

@Test
void testGetKeyAttributes() {
assertNotNull(jiraItemInfo.getKeyAttributes().get(PROJECT));
}

@Test
void testGetLastModifiedAt() {
when(metadata.getOrDefault("updated", "0")).thenReturn("5");
when(metadata.getOrDefault("created", "0")).thenReturn("0");
assertEquals(Instant.ofEpochMilli(5), jiraItemInfo.getLastModifiedAt());

when(metadata.getOrDefault("updated", "0")).thenReturn("5");
when(metadata.getOrDefault("created", "0")).thenReturn("7");
assertEquals(Instant.ofEpochMilli(7), jiraItemInfo.getLastModifiedAt());
}

}
Loading
Loading