-
Notifications
You must be signed in to change notification settings - Fork 0
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
TSPS-150 add tests for JobApiUtils #51
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
251 changes: 251 additions & 0 deletions
251
service/src/test/java/bio/terra/pipelines/controller/JobApiUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
package bio.terra.pipelines.controller; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import bio.terra.common.exception.ErrorReportException; | ||
import bio.terra.pipelines.app.controller.JobApiUtils; | ||
import bio.terra.pipelines.dependencies.stairway.JobMapKeys; | ||
import bio.terra.pipelines.dependencies.stairway.exception.InternalStairwayException; | ||
import bio.terra.pipelines.dependencies.stairway.exception.InvalidResultStateException; | ||
import bio.terra.pipelines.dependencies.stairway.model.EnumeratedJobs; | ||
import bio.terra.pipelines.generated.model.ApiErrorReport; | ||
import bio.terra.pipelines.generated.model.ApiGetJobsResponse; | ||
import bio.terra.pipelines.generated.model.ApiJobReport; | ||
import bio.terra.pipelines.testutils.StairwayTestUtils; | ||
import bio.terra.pipelines.testutils.TestUtils; | ||
import bio.terra.stairway.FlightMap; | ||
import bio.terra.stairway.FlightState; | ||
import bio.terra.stairway.FlightStatus; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpStatus; | ||
|
||
class JobApiUtilsTest { | ||
|
||
@Test | ||
void testMapEnumeratedJobsToApi() { | ||
EnumeratedJobs bothJobs = StairwayTestUtils.ENUMERATED_JOBS; | ||
|
||
ApiGetJobsResponse mappedResponse = JobApiUtils.mapEnumeratedJobsToApi(bothJobs); | ||
|
||
assertEquals(bothJobs.getTotalResults(), mappedResponse.getTotalResults()); | ||
assertEquals(bothJobs.getPageToken(), mappedResponse.getPageToken()); | ||
assertEquals(bothJobs.getResults().size(), mappedResponse.getResults().size()); | ||
assertEquals( | ||
bothJobs.getResults().get(0).getFlightState().getFlightId(), | ||
mappedResponse.getResults().get(0).getId()); | ||
assertEquals( | ||
bothJobs.getResults().get(1).getFlightState().getFlightId(), | ||
mappedResponse.getResults().get(1).getId()); | ||
} | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportSucceeded() { | ||
FlightState flightState = StairwayTestUtils.FLIGHT_STATE_DONE_SUCCESS_1; | ||
|
||
ApiJobReport apiJobReport = JobApiUtils.mapFlightStateToApiJobReport(flightState); | ||
|
||
assertEquals(TestUtils.TEST_NEW_UUID.toString(), apiJobReport.getId()); | ||
assertEquals(StairwayTestUtils.TEST_DESCRIPTION, apiJobReport.getDescription()); | ||
assertEquals( | ||
"SUCCEEDED", apiJobReport.getStatus().name()); // "SUCCESS" gets mapped to "SUCCEEDED" | ||
assertEquals(StairwayTestUtils.TIME_SUBMITTED_1.toString(), apiJobReport.getSubmitted()); | ||
assertEquals(StairwayTestUtils.TIME_COMPLETED_1.toString(), apiJobReport.getCompleted()); | ||
assertEquals("", apiJobReport.getResultURL()); | ||
// if there is no status code in the working map, we assume it's a success/200 | ||
assertEquals(200, apiJobReport.getStatusCode()); | ||
} | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportSucceededNoCompletedTime() { | ||
FlightState flightState = | ||
StairwayTestUtils.constructFlightStateWithStatusAndId( | ||
FlightStatus.SUCCESS, | ||
TestUtils.TEST_NEW_UUID, | ||
StairwayTestUtils.CREATE_JOB_INPUT_PARAMS, | ||
StairwayTestUtils.EMPTY_WORKING_MAP, | ||
StairwayTestUtils.TIME_SUBMITTED_1, | ||
null); | ||
|
||
// a completed job should have a completed time, otherwise it's an error | ||
assertThrows( | ||
InvalidResultStateException.class, | ||
() -> JobApiUtils.mapFlightStateToApiJobReport(flightState)); | ||
} | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportSucceededWithStatusCode() { | ||
// Ensure the custom status code in the working map gets extracted and used | ||
HttpStatus httpStatus = HttpStatus.I_AM_A_TEAPOT; // status code 418 | ||
FlightMap flightMapWithStatusCode = new FlightMap(); | ||
flightMapWithStatusCode.put(JobMapKeys.STATUS_CODE.getKeyName(), httpStatus); | ||
|
||
FlightState flightState = | ||
StairwayTestUtils.constructFlightStateWithStatusAndId( | ||
FlightStatus.SUCCESS, | ||
TestUtils.TEST_NEW_UUID, | ||
StairwayTestUtils.CREATE_JOB_INPUT_PARAMS, | ||
flightMapWithStatusCode, | ||
StairwayTestUtils.TIME_SUBMITTED_1, | ||
StairwayTestUtils.TIME_COMPLETED_1); | ||
|
||
ApiJobReport apiJobReport = JobApiUtils.mapFlightStateToApiJobReport(flightState); | ||
|
||
assertEquals( | ||
"SUCCEEDED", apiJobReport.getStatus().name()); // "SUCCESS" gets mapped to "SUCCEEDED" | ||
assertEquals(418, apiJobReport.getStatusCode()); | ||
} | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportFailed() { | ||
FlightState flightState = | ||
StairwayTestUtils.constructFlightStateWithStatusAndId( | ||
FlightStatus.ERROR, TestUtils.TEST_NEW_UUID); | ||
flightState.setException(new InternalStairwayException("some message")); | ||
|
||
ApiJobReport apiJobReport = JobApiUtils.mapFlightStateToApiJobReport(flightState); | ||
|
||
assertEquals(TestUtils.TEST_NEW_UUID.toString(), apiJobReport.getId()); | ||
assertEquals("FAILED", apiJobReport.getStatus().name()); // ERROR gets mapped to FAILED | ||
// InternalServerError is a 500 | ||
assertEquals(500, apiJobReport.getStatusCode()); | ||
} | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportFailedMissingException() { | ||
FlightState flightState = | ||
StairwayTestUtils.constructFlightStateWithStatusAndId( | ||
FlightStatus.ERROR, | ||
TestUtils.TEST_NEW_UUID, | ||
StairwayTestUtils.CREATE_JOB_INPUT_PARAMS, | ||
StairwayTestUtils.EMPTY_WORKING_MAP, | ||
StairwayTestUtils.TIME_SUBMITTED_1, | ||
StairwayTestUtils.TIME_COMPLETED_1); | ||
|
||
assertThrows( | ||
InvalidResultStateException.class, | ||
() -> JobApiUtils.mapFlightStateToApiJobReport(flightState)); | ||
} | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportRunning() { | ||
FlightState flightState = | ||
StairwayTestUtils.constructFlightStateWithStatusAndId( | ||
FlightStatus.RUNNING, TestUtils.TEST_NEW_UUID); | ||
|
||
ApiJobReport apiJobReport = JobApiUtils.mapFlightStateToApiJobReport(flightState); | ||
|
||
assertEquals(TestUtils.TEST_NEW_UUID.toString(), apiJobReport.getId()); | ||
assertEquals(StairwayTestUtils.TEST_DESCRIPTION, apiJobReport.getDescription()); | ||
assertEquals("RUNNING", apiJobReport.getStatus().name()); | ||
assertNull(apiJobReport.getCompleted()); | ||
assertEquals("", apiJobReport.getResultURL()); | ||
assertEquals(202, apiJobReport.getStatusCode()); | ||
} | ||
|
||
// the following tests are effectively tests of mapFlightStatusToApi(), which is private | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportRunningQueued() { | ||
FlightState flightState = | ||
StairwayTestUtils.constructFlightStateWithStatusAndId( | ||
FlightStatus.QUEUED, TestUtils.TEST_NEW_UUID); | ||
|
||
ApiJobReport apiJobReport = JobApiUtils.mapFlightStateToApiJobReport(flightState); | ||
|
||
assertEquals("RUNNING", apiJobReport.getStatus().name()); | ||
assertNull(apiJobReport.getCompleted()); | ||
assertEquals(202, apiJobReport.getStatusCode()); | ||
} | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportRunningWaiting() { | ||
FlightState flightState = | ||
StairwayTestUtils.constructFlightStateWithStatusAndId( | ||
FlightStatus.WAITING, TestUtils.TEST_NEW_UUID); | ||
|
||
ApiJobReport apiJobReport = JobApiUtils.mapFlightStateToApiJobReport(flightState); | ||
|
||
assertEquals("RUNNING", apiJobReport.getStatus().name()); | ||
assertNull(apiJobReport.getCompleted()); | ||
assertEquals(202, apiJobReport.getStatusCode()); | ||
} | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportRunningReady() { | ||
FlightState flightState = | ||
StairwayTestUtils.constructFlightStateWithStatusAndId( | ||
FlightStatus.READY, TestUtils.TEST_NEW_UUID); | ||
|
||
ApiJobReport apiJobReport = JobApiUtils.mapFlightStateToApiJobReport(flightState); | ||
|
||
assertEquals("RUNNING", apiJobReport.getStatus().name()); | ||
assertNull(apiJobReport.getCompleted()); | ||
assertEquals(202, apiJobReport.getStatusCode()); | ||
} | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportRunningReadyToRestart() { | ||
FlightState flightState = | ||
StairwayTestUtils.constructFlightStateWithStatusAndId( | ||
FlightStatus.READY_TO_RESTART, TestUtils.TEST_NEW_UUID); | ||
|
||
ApiJobReport apiJobReport = JobApiUtils.mapFlightStateToApiJobReport(flightState); | ||
|
||
assertEquals("RUNNING", apiJobReport.getStatus().name()); | ||
assertNull(apiJobReport.getCompleted()); | ||
assertEquals(202, apiJobReport.getStatusCode()); | ||
} | ||
|
||
@Test | ||
void testMapFlightStateToApiJobReportFailedFatal() { | ||
FlightState flightState = | ||
StairwayTestUtils.constructFlightStateWithStatusAndId( | ||
FlightStatus.FATAL, TestUtils.TEST_NEW_UUID); | ||
flightState.setException(new InternalStairwayException("some message")); | ||
|
||
ApiJobReport apiJobReport = JobApiUtils.mapFlightStateToApiJobReport(flightState); | ||
|
||
assertEquals("FAILED", apiJobReport.getStatus().name()); | ||
assertEquals(500, apiJobReport.getStatusCode()); | ||
} | ||
|
||
@Test | ||
void testBuildApiErrorReportErrorReportExceptionCustomStatus() { | ||
String errorMessage = "some message"; | ||
ErrorReportException exception = | ||
new ErrorReportException(errorMessage, null, HttpStatus.I_AM_A_TEAPOT) {}; | ||
|
||
ApiErrorReport apiErrorReport = JobApiUtils.buildApiErrorReport(exception); | ||
|
||
assertEquals(errorMessage, apiErrorReport.getMessage()); | ||
assertEquals(418, apiErrorReport.getStatusCode()); | ||
assertTrue(apiErrorReport.getCauses().isEmpty()); | ||
} | ||
|
||
@Test | ||
void testBuildApiErrorReportErrorReportExceptionCustomStatusWithCauses() { | ||
String errorMessage = "some message"; | ||
List<String> causes = List.of("cause 1", "cause 2"); | ||
ErrorReportException exception = | ||
new ErrorReportException(errorMessage, causes, HttpStatus.I_AM_A_TEAPOT) {}; | ||
|
||
ApiErrorReport apiErrorReport = JobApiUtils.buildApiErrorReport(exception); | ||
|
||
assertEquals(errorMessage, apiErrorReport.getMessage()); | ||
assertEquals(418, apiErrorReport.getStatusCode()); | ||
assertEquals(causes, apiErrorReport.getCauses()); | ||
} | ||
|
||
@Test | ||
void testBuildApiErrorReport() { | ||
String errorMessage = "some message"; | ||
InternalStairwayException exception = new InternalStairwayException(errorMessage); | ||
|
||
ApiErrorReport apiErrorReport = JobApiUtils.buildApiErrorReport(exception); | ||
|
||
assertEquals(errorMessage, apiErrorReport.getMessage()); | ||
assertEquals(500, apiErrorReport.getStatusCode()); | ||
assertTrue(apiErrorReport.getCauses().isEmpty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huh im kind of surprised the ApiJobReport doesnt report the exception message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was too. the spec defines the JobReport as the response for "get job status", and you have to call "get job result" to get the ErrorReport (that contains the error message and causes). we could consider adding the error message to JobReport, or we could create the "get job result" endpoint such that it can be used for polling, and if you use that one you'll get the ErrorReport. I'd lean toward the latter but we can discuss and make another ticket if needed.