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

test(openchallenges): improve organization service exception unit test coverage to 100% #2348

Merged
merged 34 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a928002
improve unit test coverage for exception to 100%
mdsage1 Nov 9, 2023
c5512ed
update unit test formatting
mdsage1 Nov 9, 2023
6664e37
update format
mdsage1 Nov 9, 2023
fab8628
Update apps/openchallenges/organization-service/src/test/java/org/sag…
mdsage1 Nov 10, 2023
6e267df
add changes to condense tests
mdsage1 Nov 10, 2023
15f89fd
fix variable name
mdsage1 Nov 10, 2023
c954c9f
add back unit test
mdsage1 Nov 10, 2023
6130fbf
format
mdsage1 Nov 10, 2023
1dd4fa8
condense unit tests
mdsage1 Nov 10, 2023
0a2d8a8
update format
mdsage1 Nov 10, 2023
5b24f09
update tests
mdsage1 Nov 12, 2023
e2cd511
fix bad request
mdsage1 Nov 12, 2023
dfee1d3
update test name
mdsage1 Nov 15, 2023
d2bcde2
update test name
mdsage1 Nov 15, 2023
fac9bae
Merge branch 'Sage-Bionetworks:main' into org_srv_unit_tst_excptns
mdsage1 Nov 15, 2023
8c43158
update tests
mdsage1 Nov 17, 2023
4f10231
update test
mdsage1 Nov 17, 2023
ff35879
update unit test
mdsage1 Nov 18, 2023
cf4a474
format update
mdsage1 Nov 18, 2023
885ff67
add changes to unit tests
mdsage1 Nov 21, 2023
e0e0f22
formatting
mdsage1 Nov 21, 2023
c1b70e2
push changes again
mdsage1 Nov 21, 2023
ef1fe76
update changes
mdsage1 Nov 21, 2023
7b1da9a
use .isNull()
mdsage1 Nov 21, 2023
c524182
assert Response Entity for both exception types
mdsage1 Nov 21, 2023
218f9a1
fix test comments and name
mdsage1 Nov 21, 2023
8b7417c
remove whitespace
mdsage1 Nov 21, 2023
038485c
update assertions of the exception
mdsage1 Nov 21, 2023
15d12f2
update comments
mdsage1 Nov 21, 2023
5e57be8
update test name
mdsage1 Nov 21, 2023
016bc79
fix format
mdsage1 Nov 21, 2023
58a1c36
Merge branch 'Sage-Bionetworks:main' into org_srv_unit_tst_excptns
mdsage1 Nov 21, 2023
0c09ab5
changed message back to detail
mdsage1 Nov 29, 2023
7fae8ab
Merge branch 'Sage-Bionetworks:main' into org_srv_unit_tst_excptns
mdsage1 Nov 29, 2023
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
@@ -0,0 +1,23 @@
package org.sagebionetworks.openchallenges.organization.service.exception;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class BadRequestExceptionTest {

@Test
public void BadRequestException_ShouldReturnExceptionConstructors_WhenKeyIsPassed() {
// Set up the input detail
String detail = "Errordetail";

// Create an instance of BadRequestException
BadRequestException exception = new BadRequestException(detail);

// Verify the properties of the exception
assertThat(exception.getStatus()).isEqualTo(ErrorConstants.BAD_REQUEST.getStatus());
assertThat(exception.getDetail()).isEqualTo(detail);
assertThat(exception.getType()).isEqualTo(ErrorConstants.BAD_REQUEST.getType());
mdsage1 marked this conversation as resolved.
Show resolved Hide resolved
assertThat(exception.getTitle()).isEqualTo(ErrorConstants.BAD_REQUEST.getTitle());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.sagebionetworks.openchallenges.organization.service.exception;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Locale;
import org.junit.jupiter.api.Test;
import org.sagebionetworks.openchallenges.organization.service.model.dto.BasicErrorDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public class GlobalExceptionHandlerTest {

@Test
public void
GlobalExceptionHandler_ShouldReturnInternalServerErrorStatusCode_WhenExceptionAndLocalePassed() {

// Create a sample Exception
Exception exception = new Exception("An exception occurred");
Locale locale = Locale.getDefault();

// Create the GlobalExceptionHandler instance
GlobalExceptionHandler exceptionHandler = new GlobalExceptionHandler();

// Call handleException
ResponseEntity<BasicErrorDto> responseEntity =
exceptionHandler.handleException(exception, locale);

// Verify the Response Entity matches the Internal Server Error Code
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}

@Test
public void
GlobalExceptionHandler_ShouldReturnStatusCodeOfSimpleChallengeGlobalExceptionResponseEntityObject_WhenArgsPassedToSimpleChallengeGlobalException() {

// Create a sample Exception
Locale locale = Locale.getDefault();

// Define the exception details
String type = "ExceptionType";
String title = "Exception Title";
HttpStatus status = HttpStatus.BAD_REQUEST;
String detail = "Exception detail message";

// Create newSimpleChallengeGlobalException instance
SimpleChallengeGlobalException exception =
new SimpleChallengeGlobalException(type, title, status, detail);

// Create the GlobalExceptionHandler instance
GlobalExceptionHandler exceptionHandler = new GlobalExceptionHandler();

// Call handleGlobalException
ResponseEntity<BasicErrorDto> responseEntity =
exceptionHandler.handleGlobalException(exception, locale);
mdsage1 marked this conversation as resolved.
Show resolved Hide resolved

// confirm that the status code that was set was retrieved and applied to the Response Entity
// object
assertThat(responseEntity.getStatusCode()).isEqualTo(exception.getStatus());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.sagebionetworks.openchallenges.organization.service.exception;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class InvalidOrganizationExceptionTest {

@Test
public void InvalidOrganizationException_ShouldReturnExceptionObject_WhenDetailKeyIsPassed() {
// Set up the input detail
String detail = "Errordetail";

// Create an instance of InvalidOrganizationException
InvalidOrganizationException exception = new InvalidOrganizationException(detail);

// Verify the properties of the exception
assertThat(exception.getDetail()).isEqualTo(detail);
mdsage1 marked this conversation as resolved.
Show resolved Hide resolved
assertThat(exception.getType()).isEqualTo(ErrorConstants.ENTITY_NOT_FOUND.getType());
assertThat(exception.getStatus()).isEqualTo(ErrorConstants.ENTITY_NOT_FOUND.getStatus());
mdsage1 marked this conversation as resolved.
Show resolved Hide resolved
assertThat(exception.getTitle()).isEqualTo(ErrorConstants.ENTITY_NOT_FOUND.getTitle());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.sagebionetworks.openchallenges.organization.service.exception;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class OrganizationAlreadyExistsExceptionTest {

@Test
public void OrganizationAlreadyExistsException_ShouldReturnDetail_WhenDetailKeyIsPassed() {
// Set up the input detail
String detail = "Errordetail";

// Create an instance of OrganizationAlreadyExistsException
OrganizationAlreadyExistsException exception = new OrganizationAlreadyExistsException(detail);
tschaffter marked this conversation as resolved.
Show resolved Hide resolved

// Verify the properties of the exception
assertThat(exception.getDetail()).isEqualTo(detail);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.sagebionetworks.openchallenges.organization.service.exception;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class OrganizationNotFoundExceptionTest {

@Test
public void OrganizationNotFoundException_ShouldReturnDetail_WhenDetailKeyIsPassed() {
// Set up the input detail
String detail = "Errordetail";

// Create an instance of OrganizationNotFoundException
OrganizationNotFoundException exception = new OrganizationNotFoundException(detail);

// Verify the properties of the exception
assertThat(exception.getDetail()).isEqualTo(detail);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.sagebionetworks.openchallenges.organization.service.exception;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;

public class SimpleChallengeGlobalExceptionTest {

@Test
public void SimpleChallengeGlobalException_ShouldReturnMessage_WhenMessageKeyIsPassed() {
// Create an instance of SimpleChallengeGlobalException using the constructor with details
String message = "Something went wrong";
SimpleChallengeGlobalException exception = new SimpleChallengeGlobalException(message);

// Verify the exception details
assertThat(exception.getMessage()).isEqualTo(message);
}

@Test
public void SimpleChallengeGlobalException_ShouldReturnStatusTypeTitleDetail_WhenArgsPassed() {
// Define the exception details
String type = "ExceptionType";
String title = "Exception Title";
HttpStatus status = HttpStatus.BAD_REQUEST;
String detail = "Exception detail message";

// Create an instance of SimpleChallengeGlobalException using the all-args constructor
SimpleChallengeGlobalException exception =
new SimpleChallengeGlobalException(type, title, status, detail);

// Verify the exception details
assertThat(exception.getStatus()).isEqualTo(status);
assertThat(exception.getTitle()).isEqualTo(title);
assertThat(exception.getType()).isEqualTo(type);
mdsage1 marked this conversation as resolved.
Show resolved Hide resolved
assertThat(exception.getDetail()).isEqualTo(detail);
}

@Test
public void
SimpleChallengeGlobalException_ShouldReturnNullArgs_WhenCalledUsingNoArgsConstructor() {

// Create an instance of SimpleChallengeGlobalException using the no-args constructor
SimpleChallengeGlobalException exception = new SimpleChallengeGlobalException();
mdsage1 marked this conversation as resolved.
Show resolved Hide resolved

// confirm that not passing args leads to null arguments
assertThat(exception.getTitle()).isNull();
assertThat(exception.getType()).isNull();
assertThat(exception.getStatus()).isNull();
assertThat(exception.getDetail()).isNull();
}

@Test
public void
SimpleChallengeGlobalException_ShouldSetArgs_WhenNoArgsConstructorExceptionPassedArgsKeys() {
// Define the exception details
String type = "ExceptionType";
String title = "Exception Title";
HttpStatus status = HttpStatus.BAD_REQUEST;
String detail = "Exception detail message";

// Create an instance of SimpleChallengeGlobalException using the no-args constructor
SimpleChallengeGlobalException exception = new SimpleChallengeGlobalException();

// Create args for the exception
exception.setType(type);
exception.setTitle(title);
exception.setStatus(status);
exception.setDetail(detail);

// confirm that the args were passed
assertThat(exception.getType()).isEqualTo(type);
assertThat(exception.getTitle()).isEqualTo(title);
assertThat(exception.getStatus()).isEqualTo(status);
assertThat(exception.getDetail()).isEqualTo(detail);
}
}