Search in sources :

Example 16 with SnapshotSummaryModel

use of bio.terra.model.SnapshotSummaryModel in project jade-data-repo by DataBiosphere.

the class SnapshotConnectedTest method testMinimal.

@Test
public void testMinimal() throws Exception {
    DatasetSummaryModel datasetMinimalSummary = setupMinimalDataset();
    String datasetName = PDAO_PREFIX + datasetMinimalSummary.getName();
    BigQueryProject bigQueryProject = TestUtils.bigQueryProjectForDatasetName(datasetDao, dataLocationService, datasetMinimalSummary.getName());
    long datasetParticipants = queryForCount(datasetName, "participant", bigQueryProject);
    assertThat("dataset participants loaded properly", datasetParticipants, equalTo(2L));
    long datasetSamples = queryForCount(datasetName, "sample", bigQueryProject);
    assertThat("dataset samples loaded properly", datasetSamples, equalTo(5L));
    SnapshotRequestModel snapshotRequest = makeSnapshotTestRequest(datasetMinimalSummary, "dataset-minimal-snapshot.json");
    MockHttpServletResponse response = performCreateSnapshot(snapshotRequest, "");
    SnapshotSummaryModel summaryModel = validateSnapshotCreated(snapshotRequest, response);
    SnapshotModel snapshotModel = getTestSnapshot(summaryModel.getId(), snapshotRequest, datasetMinimalSummary);
    List<TableModel> tables = snapshotModel.getTables();
    Optional<TableModel> participantTable = tables.stream().filter(t -> t.getName().equals("participant")).findFirst();
    Optional<TableModel> sampleTable = tables.stream().filter(t -> t.getName().equals("sample")).findFirst();
    assertThat("participant table exists", participantTable.isPresent(), equalTo(true));
    assertThat("sample table exists", sampleTable.isPresent(), equalTo(true));
    long snapshotParticipants = queryForCount(summaryModel.getName(), "participant", bigQueryProject);
    assertThat("dataset participants loaded properly", snapshotParticipants, equalTo(1L));
    assertThat("participant row count matches expectation", participantTable.get().getRowCount(), equalTo(1));
    long snapshotSamples = queryForCount(summaryModel.getName(), "sample", bigQueryProject);
    assertThat("dataset samples loaded properly", snapshotSamples, equalTo(2L));
    assertThat("sample row count matches expectation", sampleTable.get().getRowCount(), equalTo(2));
    List<RelationshipModel> relationships = snapshotModel.getRelationships();
    assertThat("a relationship comes back", relationships.size(), equalTo(1));
    RelationshipModel relationshipModel = relationships.get(0);
    assertThat("relationship name is right", relationshipModel.getName(), equalTo("participant_sample"));
    assertThat("from table is right", relationshipModel.getFrom().getTable(), equalTo("participant"));
    assertThat("from column is right", relationshipModel.getFrom().getColumn(), equalTo("id"));
    assertThat("to table is right", relationshipModel.getTo().getTable(), equalTo("sample"));
    assertThat("to column is right", relationshipModel.getTo().getColumn(), equalTo("participant_id"));
}
Also used : BillingProfileModel(bio.terra.model.BillingProfileModel) FieldValue(com.google.cloud.bigquery.FieldValue) CoreMatchers(org.hamcrest.CoreMatchers) SnapshotModel(bio.terra.model.SnapshotModel) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) StorageOptions(com.google.cloud.storage.StorageOptions) StringUtils(org.apache.commons.lang3.StringUtils) BigQuery(com.google.cloud.bigquery.BigQuery) Assert.assertThat(org.junit.Assert.assertThat) RelationshipModel(bio.terra.model.RelationshipModel) FieldValueList(com.google.cloud.bigquery.FieldValueList) After(org.junit.After) DrsId(bio.terra.service.filedata.DrsId) TableResult(com.google.cloud.bigquery.TableResult) SpringRunner(org.springframework.test.context.junit4.SpringRunner) URI(java.net.URI) DataLocationService(bio.terra.service.resourcemanagement.DataLocationService) MockBean(org.springframework.boot.test.mock.mockito.MockBean) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PDAO_PREFIX(bio.terra.common.PdaoConstant.PDAO_PREFIX) MediaType(org.springframework.http.MediaType) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) EnumerateSnapshotModel(bio.terra.model.EnumerateSnapshotModel) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) SnapshotRequestContentsModel(bio.terra.model.SnapshotRequestContentsModel) Collectors(java.util.stream.Collectors) DatasetSummaryModel(bio.terra.model.DatasetSummaryModel) StandardCharsets(java.nio.charset.StandardCharsets) ConfigEnum(bio.terra.service.configuration.ConfigEnum) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) ST(org.stringtemplate.v4.ST) Assert.assertFalse(org.junit.Assert.assertFalse) TestUtils(bio.terra.common.TestUtils) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Storage(com.google.cloud.storage.Storage) ConfigurationService(bio.terra.service.configuration.ConfigurationService) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) GoogleResourceConfiguration(bio.terra.service.resourcemanagement.google.GoogleResourceConfiguration) ConnectedOperations(bio.terra.common.fixtures.ConnectedOperations) Names(bio.terra.common.fixtures.Names) DRSObject(bio.terra.model.DRSObject) RunWith(org.junit.runner.RunWith) BigQueryProject(bio.terra.service.tabulardata.google.BigQueryProject) DrsIdService(bio.terra.service.filedata.DrsIdService) TableModel(bio.terra.model.TableModel) MockMvcResultMatchers.content(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ArrayList(java.util.ArrayList) MockMvc(org.springframework.test.web.servlet.MockMvc) SnapshotRequestModel(bio.terra.model.SnapshotRequestModel) MockMvcRequestBuilders.delete(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) MvcResult(org.springframework.test.web.servlet.MvcResult) JsonLoader(bio.terra.common.fixtures.JsonLoader) ProfileDao(bio.terra.service.resourcemanagement.ProfileDao) ConnectedTestConfiguration(bio.terra.app.configuration.ConnectedTestConfiguration) StringStartsWith.startsWith(org.hamcrest.core.StringStartsWith.startsWith) Before(org.junit.Before) BlobInfo(com.google.cloud.storage.BlobInfo) FileLoadModel(bio.terra.model.FileLoadModel) FileModel(bio.terra.model.FileModel) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Connected(bio.terra.common.category.Connected) Assert.assertTrue(org.junit.Assert.assertTrue) DeleteResponseModel(bio.terra.model.DeleteResponseModel) Test(org.junit.Test) IamProviderInterface(bio.terra.service.iam.IamProviderInterface) Mockito.when(org.mockito.Mockito.when) SnapshotSourceModel(bio.terra.model.SnapshotSourceModel) DatasetDao(bio.terra.service.dataset.DatasetDao) TimeUnit(java.util.concurrent.TimeUnit) ErrorModel(bio.terra.model.ErrorModel) HttpStatus(org.springframework.http.HttpStatus) IngestRequestModel(bio.terra.model.IngestRequestModel) AutoConfigureMockMvc(org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc) Assert.assertNull(org.junit.Assert.assertNull) SnapshotSummaryModel(bio.terra.model.SnapshotSummaryModel) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) Assert.assertEquals(org.junit.Assert.assertEquals) SnapshotSummaryModel(bio.terra.model.SnapshotSummaryModel) RelationshipModel(bio.terra.model.RelationshipModel) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SnapshotRequestModel(bio.terra.model.SnapshotRequestModel) SnapshotModel(bio.terra.model.SnapshotModel) EnumerateSnapshotModel(bio.terra.model.EnumerateSnapshotModel) BigQueryProject(bio.terra.service.tabulardata.google.BigQueryProject) DatasetSummaryModel(bio.terra.model.DatasetSummaryModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) TableModel(bio.terra.model.TableModel) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 17 with SnapshotSummaryModel

use of bio.terra.model.SnapshotSummaryModel in project jade-data-repo by DataBiosphere.

the class SnapshotConnectedTest method testArrayStruct.

@Test
public void testArrayStruct() throws Exception {
    DatasetSummaryModel datasetArraySummary = setupArrayStructDataset();
    String datasetName = PDAO_PREFIX + datasetArraySummary.getName();
    BigQueryProject bigQueryProject = TestUtils.bigQueryProjectForDatasetName(datasetDao, dataLocationService, datasetArraySummary.getName());
    long datasetParticipants = queryForCount(datasetName, "participant", bigQueryProject);
    assertThat("dataset participants loaded properly", datasetParticipants, equalTo(2L));
    long datasetSamples = queryForCount(datasetName, "sample", bigQueryProject);
    assertThat("dataset samples loaded properly", datasetSamples, equalTo(5L));
    SnapshotRequestModel snapshotRequest = makeSnapshotTestRequest(datasetArraySummary, "snapshot-array-struct.json");
    MockHttpServletResponse response = performCreateSnapshot(snapshotRequest, "");
    SnapshotSummaryModel summaryModel = validateSnapshotCreated(snapshotRequest, response);
    getTestSnapshot(summaryModel.getId(), snapshotRequest, datasetArraySummary);
    long snapshotParticipants = queryForCount(summaryModel.getName(), "participant", bigQueryProject);
    assertThat("dataset participants loaded properly", snapshotParticipants, equalTo(2L));
    long snapshotSamples = queryForCount(summaryModel.getName(), "sample", bigQueryProject);
    assertThat("dataset samples loaded properly", snapshotSamples, equalTo(3L));
}
Also used : SnapshotSummaryModel(bio.terra.model.SnapshotSummaryModel) BigQueryProject(bio.terra.service.tabulardata.google.BigQueryProject) DatasetSummaryModel(bio.terra.model.DatasetSummaryModel) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SnapshotRequestModel(bio.terra.model.SnapshotRequestModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 18 with SnapshotSummaryModel

use of bio.terra.model.SnapshotSummaryModel in project jade-data-repo by DataBiosphere.

the class SnapshotConnectedTest method validateSnapshotCreated.

private SnapshotSummaryModel validateSnapshotCreated(SnapshotRequestModel snapshotRequest, MockHttpServletResponse response) throws Exception {
    SnapshotSummaryModel summaryModel = connectedOperations.handleCreateSnapshotSuccessCase(response);
    assertThat(summaryModel.getDescription(), equalTo(snapshotRequest.getDescription()));
    assertThat(summaryModel.getName(), equalTo(snapshotRequest.getName()));
    // Reset the name in the snapshot request
    snapshotRequest.setName(snapshotOriginalName);
    return summaryModel;
}
Also used : SnapshotSummaryModel(bio.terra.model.SnapshotSummaryModel)

Example 19 with SnapshotSummaryModel

use of bio.terra.model.SnapshotSummaryModel in project jade-data-repo by DataBiosphere.

the class SnapshotConnectedTest method testDuplicateName.

@Test
public void testDuplicateName() throws Exception {
    // create a snapshot
    SnapshotRequestModel snapshotRequest = makeSnapshotTestRequest(datasetSummary, "snapshot-test-snapshot.json");
    MockHttpServletResponse response = performCreateSnapshot(snapshotRequest, "_dup_");
    SnapshotSummaryModel summaryModel = validateSnapshotCreated(snapshotRequest, response);
    // fetch the snapshot and confirm the metadata matches the request
    SnapshotModel snapshotModel = getTestSnapshot(summaryModel.getId(), snapshotRequest, datasetSummary);
    assertNotNull("fetched snapshot successfully after creation", snapshotModel);
    // check that the snapshot metadata row is unlocked
    String exclusiveLock = snapshotDao.getExclusiveLockState(UUID.fromString(snapshotModel.getId()));
    assertNull("snapshot row is unlocked", exclusiveLock);
    // try to create the same snapshot again and check that it fails
    snapshotRequest.setName(snapshotModel.getName());
    response = performCreateSnapshot(snapshotRequest, null);
    ErrorModel errorModel = handleCreateSnapshotFailureCase(response);
    assertThat(response.getStatus(), equalTo(HttpStatus.BAD_REQUEST.value()));
    assertThat("error message includes name conflict", errorModel.getMessage(), containsString("Snapshot name already exists"));
    // delete and confirm deleted
    connectedOperations.deleteTestSnapshot(snapshotModel.getId());
    connectedOperations.getSnapshotExpectError(snapshotModel.getId(), HttpStatus.NOT_FOUND);
}
Also used : SnapshotSummaryModel(bio.terra.model.SnapshotSummaryModel) ErrorModel(bio.terra.model.ErrorModel) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SnapshotRequestModel(bio.terra.model.SnapshotRequestModel) SnapshotModel(bio.terra.model.SnapshotModel) EnumerateSnapshotModel(bio.terra.model.EnumerateSnapshotModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 20 with SnapshotSummaryModel

use of bio.terra.model.SnapshotSummaryModel in project jade-data-repo by DataBiosphere.

the class AccessTest method checkShared.

@Test
public void checkShared() throws Exception {
    makeIngestTestDataset();
    IngestRequestModel request = dataRepoFixtures.buildSimpleIngest("participant", "ingest-test/ingest-test-participant.json");
    dataRepoFixtures.ingestJsonData(steward(), datasetId, request);
    request = dataRepoFixtures.buildSimpleIngest("sample", "ingest-test/ingest-test-sample.json");
    dataRepoFixtures.ingestJsonData(steward(), datasetId, request);
    DatasetModel dataset = dataRepoFixtures.getDataset(steward(), datasetId);
    String datasetBqSnapshotName = "datarepo_" + dataset.getName();
    BigQuery custodianBigQuery = BigQueryFixtures.getBigQuery(dataset.getDataProject(), custodianToken);
    try {
        BigQueryFixtures.datasetExists(custodianBigQuery, dataset.getDataProject(), datasetBqSnapshotName);
        fail("custodian shouldn't be able to access bq dataset before it is shared with them");
    } catch (IllegalStateException e) {
        assertThat("checking message for pdao exception error", e.getMessage(), equalTo("existence check failed for " + datasetBqSnapshotName));
    }
    dataRepoFixtures.addDatasetPolicyMember(steward(), datasetId, IamRole.CUSTODIAN, custodian().getEmail());
    DataRepoResponse<EnumerateDatasetModel> enumDatasets = dataRepoFixtures.enumerateDatasetsRaw(custodian());
    assertThat("Custodian is authorized to enumerate datasets", enumDatasets.getStatusCode(), equalTo(HttpStatus.OK));
    boolean custodianHasAccess = BigQueryFixtures.hasAccess(custodianBigQuery, dataset.getDataProject(), datasetBqSnapshotName);
    assertThat("custodian can access the bq snapshot after it has been shared", custodianHasAccess, equalTo(true));
    SnapshotSummaryModel snapshotSummaryModel = dataRepoFixtures.createSnapshot(custodian(), datasetSummaryModel, "ingest-test-snapshot.json");
    SnapshotModel snapshotModel = dataRepoFixtures.getSnapshot(custodian(), snapshotSummaryModel.getId());
    BigQuery bigQuery = BigQueryFixtures.getBigQuery(snapshotModel.getDataProject(), readerToken);
    try {
        BigQueryFixtures.datasetExists(bigQuery, snapshotModel.getDataProject(), snapshotModel.getName());
        fail("reader shouldn't be able to access bq dataset before it is shared with them");
    } catch (IllegalStateException e) {
        assertThat("checking message for exception error", e.getMessage(), equalTo("existence check failed for ".concat(snapshotSummaryModel.getName())));
    }
    dataRepoFixtures.addSnapshotPolicyMember(custodian(), snapshotSummaryModel.getId(), IamRole.READER, reader().getEmail());
    AuthenticatedUserRequest authenticatedReaderRequest = new AuthenticatedUserRequest().email(reader().getEmail()).token(Optional.of(readerToken));
    assertThat("correctly added reader", iamService.isAuthorized(authenticatedReaderRequest, IamResourceType.DATASNAPSHOT, snapshotSummaryModel.getId(), IamAction.READ_DATA), equalTo(true));
    boolean readerHasAccess = BigQueryFixtures.hasAccess(bigQuery, snapshotModel.getDataProject(), snapshotModel.getName());
    assertThat("reader can access the snapshot after it has been shared", readerHasAccess, equalTo(true));
}
Also used : BigQuery(com.google.cloud.bigquery.BigQuery) SnapshotSummaryModel(bio.terra.model.SnapshotSummaryModel) IngestRequestModel(bio.terra.model.IngestRequestModel) EnumerateDatasetModel(bio.terra.model.EnumerateDatasetModel) DatasetModel(bio.terra.model.DatasetModel) SnapshotModel(bio.terra.model.SnapshotModel) EnumerateDatasetModel(bio.terra.model.EnumerateDatasetModel) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

SnapshotSummaryModel (bio.terra.model.SnapshotSummaryModel)28 Test (org.junit.Test)20 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)20 SnapshotModel (bio.terra.model.SnapshotModel)17 IngestRequestModel (bio.terra.model.IngestRequestModel)13 DatasetSummaryModel (bio.terra.model.DatasetSummaryModel)12 EnumerateSnapshotModel (bio.terra.model.EnumerateSnapshotModel)10 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)10 SnapshotRequestModel (bio.terra.model.SnapshotRequestModel)9 BigQuery (com.google.cloud.bigquery.BigQuery)7 BlobInfo (com.google.cloud.storage.BlobInfo)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 MvcResult (org.springframework.test.web.servlet.MvcResult)6 DRSObject (bio.terra.model.DRSObject)5 DatasetModel (bio.terra.model.DatasetModel)5 DeleteResponseModel (bio.terra.model.DeleteResponseModel)5 ErrorModel (bio.terra.model.ErrorModel)5 FileModel (bio.terra.model.FileModel)5 BillingProfileModel (bio.terra.model.BillingProfileModel)4 DrsId (bio.terra.service.filedata.DrsId)4