Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorgantaylor committed Sep 24, 2024
1 parent d24bef3 commit dcda1f3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package bio.terra.pipelines.service;

import static bio.terra.pipelines.testutils.TestUtils.createTestPipelineWithId;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import bio.terra.common.exception.InternalServerErrorException;
import bio.terra.pipelines.db.entities.Pipeline;
import bio.terra.pipelines.db.entities.PipelineOutput;
import bio.terra.pipelines.db.entities.PipelineOutputDefinition;
import bio.terra.pipelines.db.entities.PipelineRun;
Expand All @@ -20,6 +22,7 @@
import bio.terra.rawls.model.Entity;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand All @@ -40,6 +43,35 @@ class PipelineInputsOutputsServiceTest extends BaseEmbeddedDbTest {

private final UUID testJobId = TestUtils.TEST_NEW_UUID;

@Test
void prepareFileInputs() throws MalformedURLException {
Pipeline testPipelineWithId = createTestPipelineWithId();
String fileInputKeyName = "testRequiredVcfInput";
String fileInputValue = "fake/file.vcf.gz";
Map<String, Object> userPipelineInputs =
new HashMap<>(Map.of(fileInputKeyName, fileInputValue));

URL fakeUrl = new URL("https://storage.googleapis.com/signed-url-stuff");

when(mockGcsService.generatePutObjectSignedUrl(
eq(testPipelineWithId.getWorkspaceGoogleProject()),
eq(testPipelineWithId.getWorkspaceStorageContainerName()),
anyString()))
.thenReturn(fakeUrl);

Map<String, Map<String, String>> formattedPipelineFileInputs =
pipelineInputsOutputsService.prepareFileInputs(
testPipelineWithId, testJobId, userPipelineInputs);

assertEquals(userPipelineInputs.size(), formattedPipelineFileInputs.size());
assertEquals(
fakeUrl.toString(), formattedPipelineFileInputs.get(fileInputKeyName).get("signedUrl"));
assertEquals(
"curl -X PUT -H 'Content-Type: application/octet-stream' --upload-file %s '%s'"
.formatted(fileInputValue, fakeUrl.toString()),
formattedPipelineFileInputs.get(fileInputKeyName).get("curlCommand"));
}

@Test
void extractPipelineOutputsFromEntity() {
// test that the method correctly extracts the outputs from the entity
Expand Down Expand Up @@ -112,4 +144,11 @@ void formatPipelineRunOutputs() throws MalformedURLException {

assertEquals(fakeUrl.toString(), apiPipelineRunOutputs.get("testFileOutputKey"));
}

@Test
void stringToMapBadString() {
assertThrows(
InternalServerErrorException.class,
() -> pipelineInputsOutputsService.stringToMap("i am not a map"));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package bio.terra.pipelines.service;

import static bio.terra.pipelines.testutils.TestUtils.createNewPipelineRunWithJobId;
import static bio.terra.pipelines.testutils.TestUtils.createNewPipelineRunWithJobIdAndUser;
import static bio.terra.pipelines.testutils.TestUtils.createTestPipelineWithId;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -72,28 +75,6 @@ class PipelineRunsServiceTest extends BaseEmbeddedDbTest {

private SimpleMeterRegistry meterRegistry;

private Pipeline createTestPipelineWithId() {
Pipeline pipeline =
new Pipeline(
TestUtils.TEST_PIPELINE_1.getName(),
TestUtils.TEST_PIPELINE_1.getVersion(),
TestUtils.TEST_PIPELINE_1.getDisplayName(),
TestUtils.TEST_PIPELINE_1.getDescription(),
TestUtils.TEST_PIPELINE_1.getPipelineType(),
TestUtils.TEST_PIPELINE_1.getWdlUrl(),
TestUtils.TEST_PIPELINE_1.getWdlMethodName(),
TestUtils.TEST_PIPELINE_1.getWdlMethodVersion(),
TestUtils.TEST_PIPELINE_1.getWorkspaceId(),
TestUtils.TEST_PIPELINE_1.getWorkspaceBillingProject(),
TestUtils.TEST_PIPELINE_1.getWorkspaceName(),
TestUtils.TEST_PIPELINE_1.getWorkspaceStorageContainerName(),
TestUtils.TEST_PIPELINE_1.getWorkspaceGoogleProject(),
TestUtils.TEST_PIPELINE_1.getPipelineInputDefinitions(),
TestUtils.TEST_PIPELINE_1.getPipelineOutputDefinitions());
pipeline.setId(3L);
return pipeline;
}

@BeforeEach
void initMocks() {
// stairway submit method returns a good flightId
Expand Down Expand Up @@ -163,13 +144,13 @@ void writeNewRunToDbOk() {
void writeRunToDbDuplicateRun() {
// try to save a run with the same job id two times, the second time it should throw duplicate
// exception error
PipelineRun newPipelineRun = TestUtils.createNewPipelineRunWithJobId(testJobId);
PipelineRun newPipelineRun = createNewPipelineRunWithJobId(testJobId);

PipelineRun savedJobFirst =
pipelineRunsService.writePipelineRunToDbThrowsDuplicateException(newPipelineRun);
assertNotNull(savedJobFirst);

PipelineRun newPipelineRunSameId = TestUtils.createNewPipelineRunWithJobId(testJobId);
PipelineRun newPipelineRunSameId = createNewPipelineRunWithJobId(testJobId);
assertThrows(
DuplicateObjectException.class,
() ->
Expand All @@ -183,7 +164,7 @@ void testGetCorrectNumberOfRows() {
assertEquals(1, pipelineRuns.size());

// insert another row and verify that it shows up
PipelineRun newPipelineRun = TestUtils.createNewPipelineRunWithJobId(testJobId);
PipelineRun newPipelineRun = createNewPipelineRunWithJobId(testJobId);

pipelineRunsRepository.save(newPipelineRun);
pipelineRuns = pipelineRunsRepository.findAllByUserId(testUserId);
Expand All @@ -199,7 +180,7 @@ void testCorrectUserIsolation() {
// insert row for second user and verify that it shows up
String testUserId2 = TestUtils.TEST_USER_ID_2;
PipelineRun newPipelineRun =
TestUtils.createNewPipelineRunWithJobIdAndUser(UUID.randomUUID(), testUserId2);
createNewPipelineRunWithJobIdAndUser(UUID.randomUUID(), testUserId2);
pipelineRunsRepository.save(newPipelineRun);

// Verify that the old userid still show only 1 record
Expand Down Expand Up @@ -534,7 +515,7 @@ void createImputationRunStairwayError() {

@Test
void markPipelineRunSuccess() {
PipelineRun pipelineRun = TestUtils.createNewPipelineRunWithJobId(testJobId);
PipelineRun pipelineRun = createNewPipelineRunWithJobId(testJobId);
pipelineRunsRepository.save(pipelineRun);

PipelineRun updatedPipelineRun =
Expand Down Expand Up @@ -576,11 +557,11 @@ void findPipelineRunsPaginatedNoOtherPages() {
@Test
void findPageResultsResultsUseNextPage() {
// add 3 new jobs so 4 total exist in database
PipelineRun pipelineRun = TestUtils.createNewPipelineRunWithJobId(testJobId);
PipelineRun pipelineRun = createNewPipelineRunWithJobId(testJobId);
pipelineRunsRepository.save(pipelineRun);
pipelineRun = TestUtils.createNewPipelineRunWithJobId(UUID.randomUUID());
pipelineRun = createNewPipelineRunWithJobId(UUID.randomUUID());
pipelineRunsRepository.save(pipelineRun);
pipelineRun = TestUtils.createNewPipelineRunWithJobId(UUID.randomUUID());
pipelineRun = createNewPipelineRunWithJobId(UUID.randomUUID());
pipelineRunsRepository.save(pipelineRun);

// query for first (default) page with page size 2 so there is a next page token that exists
Expand Down
22 changes: 22 additions & 0 deletions service/src/test/java/bio/terra/pipelines/testutils/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ public class TestUtils {
TEST_PIPELINE_INPUTS_DEFINITION_LIST,
TEST_PIPELINE_OUTPUTS_DEFINITION_LIST);

public static Pipeline createTestPipelineWithId() {
Pipeline pipeline =
new Pipeline(
TestUtils.TEST_PIPELINE_1.getName(),
TestUtils.TEST_PIPELINE_1.getVersion(),
TestUtils.TEST_PIPELINE_1.getDisplayName(),
TestUtils.TEST_PIPELINE_1.getDescription(),
TestUtils.TEST_PIPELINE_1.getPipelineType(),
TestUtils.TEST_PIPELINE_1.getWdlUrl(),
TestUtils.TEST_PIPELINE_1.getWdlMethodName(),
TestUtils.TEST_PIPELINE_1.getWdlMethodVersion(),
TestUtils.TEST_PIPELINE_1.getWorkspaceId(),
TestUtils.TEST_PIPELINE_1.getWorkspaceBillingProject(),
TestUtils.TEST_PIPELINE_1.getWorkspaceName(),
TestUtils.TEST_PIPELINE_1.getWorkspaceStorageContainerName(),
TestUtils.TEST_PIPELINE_1.getWorkspaceGoogleProject(),
TestUtils.TEST_PIPELINE_1.getPipelineInputDefinitions(),
TestUtils.TEST_PIPELINE_1.getPipelineOutputDefinitions());
pipeline.setId(3L);
return pipeline;
}

public static final String TEST_USER_ID_1 =
"testUser"; // this matches the job pre-populated in the db for tests
public static final String TEST_USER_ID_2 = "testUser2";
Expand Down

0 comments on commit dcda1f3

Please sign in to comment.