Skip to content

Commit

Permalink
Merge pull request #8 from Galactus22625/Jira-Unit-Tests
Browse files Browse the repository at this point in the history
Jira unit tests
  • Loading branch information
Galactus22625 authored Oct 24, 2024
2 parents 54f8388 + 2ebe912 commit e00fd65
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public static InetAddress getInetAddress(String url) {
*/
public static void validateInetAddress(@NonNull final InetAddress address) {
if (address.isMulticastAddress() || address.isAnyLocalAddress() || address.isLinkLocalAddress()
|| address.isSiteLocalAddress() || address.isLoopbackAddress()) {
|| address.isSiteLocalAddress() || address.isLoopbackAddress())
{
throw new BadRequestException(INVALID_URL);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.opensearch.dataprepper.plugins.source.saas.jira.exception;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;

public class BadRequestExceptionTest {
private String message;
private Throwable throwable;

@BeforeEach
void setUp() {
message = "Bad Request";
throwable = mock(Throwable.class);
}

@Nested
class MessageOnlyConstructor {
private BadRequestException createObjectUnderTest() {
return new BadRequestException(message);
}

@Test
void getMessage_returns_message() {
assertEquals(createObjectUnderTest().getMessage(), message);
}

@Test
void getCause_returns_null() {
assertNull(createObjectUnderTest().getCause());
}
}

@Nested
class MessageThrowableConstructor {
private BadRequestException createObjectUnderTest() {
return new BadRequestException(message, throwable);
}

@Test
void getMessage_returns_message() {
assertEquals(createObjectUnderTest().getMessage(), message);
}

@Test
void getCause_returns_throwable() {
assertEquals(createObjectUnderTest().getCause(), throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.opensearch.dataprepper.plugins.source.saas.jira.exception;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;

public class UnAuthorizedExceptionTest {
private String message;
private Throwable throwable;

@BeforeEach
void setUp() {
message = "UnAuthorized Exception";
throwable = mock(Throwable.class);
}

@Nested
class MessageOnlyConstructor {
private UnAuthorizedException createObjectUnderTest() {
return new UnAuthorizedException(message);
}

@Test
void getMessage_returns_message() {
assertEquals(createObjectUnderTest().getMessage(), message);
}

@Test
void getCause_returns_null() {
assertNull(createObjectUnderTest().getCause());
}
}

@Nested
class MessageThrowableConstructor {
private UnAuthorizedException createObjectUnderTest() {
return new UnAuthorizedException(message, throwable);
}

@Test
void getMessage_returns_message() {
assertEquals(createObjectUnderTest().getMessage(), message);
}

@Test
void getCause_returns_throwable() {
assertEquals(createObjectUnderTest().getCause(), throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.opensearch.dataprepper.plugins.source.saas.jira.rest.auth;

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.plugins.source.saas.jira.JiraSourceConfig;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.mockito.Mockito.when;
import static org.opensearch.dataprepper.plugins.source.saas.jira.utils.Constants.OAUTH2;

@ExtendWith(MockitoExtension.class)
public class JiraAuthFactoryTest {

@Mock
private JiraSourceConfig sourceConfig;

private JiraAuthFactory jiraAuthFactory;

@BeforeEach
void setUp() {
jiraAuthFactory = new JiraAuthFactory(sourceConfig);
}

@Test
void testGetObjectOauth2(){
when(sourceConfig.getAuthType()).thenReturn(OAUTH2);
assertInstanceOf(JiraOauthConfig.class, jiraAuthFactory.getObject());
}

@Test
void testGetObjectBasicAuth(){
assertInstanceOf(JiraBasicAuthConfig.class, jiraAuthFactory.getObject());
}

@Test
void testGetObjectType(){
assertEquals(JiraAuthConfig.class, jiraAuthFactory.getObjectType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.opensearch.dataprepper.plugins.source.saas.jira.utils;

import org.junit.jupiter.api.Test;


import org.opensearch.dataprepper.plugins.source.saas.jira.exception.BadRequestException;

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class AddressValidationTest {

@Test
void testInitialization () {
AddressValidation addressValidation = new AddressValidation();
assertNotNull(addressValidation);
}

@Test
void testGetInetAddress(){
String testUrl = "https://www.amazon.com";
System.out.print("Test output");
System.out.print(AddressValidation.getInetAddress(testUrl));
}

@Test
void testGetInetAddressWithMalformedUrl(){
String testUrl = "XXXXXXXXXXXXXXXXXXXXXX";
assertThrows(BadRequestException.class, () -> AddressValidation.getInetAddress(testUrl));
}

@Test
void testGetInetAddressWithUnknownHost(){
String testUrl = "https://www.thisurldoesntexist1384276t5917278481073.com";
assertThrows(BadRequestException.class, () -> AddressValidation.getInetAddress(testUrl));
}

@Test
void testGetInetAddressWithNullUrl(){
String testUrl = null;
assertThrows(BadRequestException.class, () -> AddressValidation.getInetAddress(testUrl));
}


@Test
void testValidateInetAddressAnyLocalAddress() throws UnknownHostException {
InetAddress wildcardAddress = InetAddress.getByName("0.0.0.0");
assertThrows(BadRequestException.class, () -> AddressValidation.validateInetAddress(wildcardAddress));
}

@Test
void testValidateInetAddressMulticastAddress() throws UnknownHostException {
InetAddress multicastAddress = InetAddress.getByName("224.0.0.1");
assertThrows(BadRequestException.class, () -> AddressValidation.validateInetAddress(multicastAddress));
}

@Test
void testValidateInetAddressLinkLocalAddress() throws UnknownHostException {
InetAddress linkLocalAddress = InetAddress.getByName("169.254.1.1");
assertThrows(BadRequestException.class, () -> AddressValidation.validateInetAddress(linkLocalAddress));
}

@Test
void testValidateInetAddressSiteLocalAddress() throws UnknownHostException {
InetAddress siteLocalAddress = InetAddress.getByName("10.0.0.1");
assertThrows(BadRequestException.class, () -> AddressValidation.validateInetAddress(siteLocalAddress));
}

@Test
void testValidateInetAddressLoopbackAddress() throws UnknownHostException {
InetAddress loopbackAddress = InetAddress.getByName("127.0.0.1");
assertThrows(BadRequestException.class, () -> AddressValidation.validateInetAddress(loopbackAddress));
}

@Test
void testValidateInetAddressValidAddress() throws UnknownHostException, MalformedURLException {
InetAddress validAddress = InetAddress.getByName(new URL("https://www.amazon.com").getHost());
assertDoesNotThrow(() -> AddressValidation.validateInetAddress(validAddress));
}

@Test
void testValidateInetAddressNullAddress() throws UnknownHostException {
InetAddress nullAddress = null;
assertThrows(NullPointerException.class, () -> AddressValidation.validateInetAddress(nullAddress));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.opensearch.dataprepper.plugins.source.saas.jira.utils;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ConstantsTest {
private Constants constants;

@Test
public void testInitialization() {
constants = new Constants();
assertNotNull(Constants.SOLUTION_FOR_JIRA_ISSUE_TYPE_FILTER);
assertNotNull(constants);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.opensearch.dataprepper.plugins.source.saas.jira.utils;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ExceptionUtilTest {

String errorCode;
String errorMessage;

private ExceptionUtil exceptionUtil;

@BeforeEach
void setUp() {
errorCode = "123";
errorMessage = "Test Error Message";
}

@Test
void testInitialization() {
exceptionUtil = new ExceptionUtil();
assertNotNull(exceptionUtil);
}

@Test
void errorCodeEnumHandlingTest(){
assertTrue(ExceptionUtil.getErrorMessage(ErrorCodeEnum.FIELD_SIZE_OVER_MAX_LIMIT).contains("Field Size is more than max limit"));
assertTrue(ExceptionUtil.getErrorMessage(ErrorCodeEnum.ERROR_JIRA_PROJECT_KEY_FILTER).contains("IRA Project Key Filter list size is too large"));
}

@Test
void errorMessageHandlingTest(){
assertTrue(ExceptionUtil.getErrorMessage(errorCode, errorMessage).contains(errorCode));
assertTrue(ExceptionUtil.getErrorMessage(errorCode, errorMessage).contains(errorMessage));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.opensearch.dataprepper.plugins.source.saas.jira.utils;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class JiraContentTypeTest {
@Test
void testEnumConstants() {
assertNotNull(JiraContentType.PROJECT);
assertNotNull(JiraContentType.ISSUE);
assertNotNull(JiraContentType.COMMENT);
assertNotNull(JiraContentType.ATTACHMENT);
assertNotNull(JiraContentType.WORKLOG);
}

@Test
void testTypeValues() {
assertEquals("PROJECT", JiraContentType.PROJECT.getType());
assertEquals("ISSUE", JiraContentType.ISSUE.getType());
assertEquals("COMMENT", JiraContentType.COMMENT.getType());
assertEquals("ATTACHMENT", JiraContentType.ATTACHMENT.getType());
assertEquals("WORKLOG", JiraContentType.WORKLOG.getType());
}
}

0 comments on commit e00fd65

Please sign in to comment.