Skip to content

Commit

Permalink
test(openchallenges): improve organization service exception unit tes…
Browse files Browse the repository at this point in the history
…t coverage to 100% (#2348)

* improve unit test coverage for exception to 100%

* update unit test formatting

* update format

* Update apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/GlobalExceptionHandlerTest.java

Co-authored-by: Thomas Schaffter <[email protected]>

* add changes to condense tests

* fix variable name

* add back unit test

* format

* condense unit tests

* update format

* update tests

* fix bad request

* update test name

* update test name

* update tests

* update test

* update unit test

* format update

* add changes to unit tests

* formatting

* push changes again

* update changes

* use .isNull()

* assert Response Entity for both exception types

* fix test comments and name

* remove whitespace

* update assertions of the exception

* update comments

* update test name

* fix format

* changed message back to detail

---------

Co-authored-by: Thomas Schaffter <[email protected]>
  • Loading branch information
mdsage1 and tschaffter authored Dec 4, 2023
1 parent 9e037ca commit 3355901
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 0 deletions.
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());
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);

// 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);
assertThat(exception.getType()).isEqualTo(ErrorConstants.ENTITY_NOT_FOUND.getType());
assertThat(exception.getStatus()).isEqualTo(ErrorConstants.ENTITY_NOT_FOUND.getStatus());
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);

// 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);
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();

// 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);
}
}

0 comments on commit 3355901

Please sign in to comment.