Search in sources :

Example 1 with BigQueryProject

use of bio.terra.service.tabulardata.google.BigQueryProject in project jade-data-repo by DataBiosphere.

the class TestUtils method selectFromBigQueryDataset.

/**
 * Execute a SELECT query on BigQuery dataset.
 * @param bigQueryPdao pass in from the calling test class
 * @param datasetDao pass in from the calling test class
 * @param dataLocationService pass in from the calling test class
 * @param datasetName the name of the Data Repo dataset
 * @param tableName the name of Data Repo table
 * @param columns a comma-separated string of the columns to select (e.g. "name", "name, fileref")
 * @return the BigQuery TableResult
 */
public static TableResult selectFromBigQueryDataset(BigQueryPdao bigQueryPdao, DatasetDao datasetDao, DataLocationService dataLocationService, String datasetName, String tableName, String columns) throws Exception {
    String bqDatasetName = bigQueryPdao.prefixName(datasetName);
    BigQueryProject bigQueryProject = bigQueryProjectForDatasetName(datasetDao, dataLocationService, datasetName);
    String bigQueryProjectId = bigQueryProject.getProjectId();
    BigQuery bigQuery = bigQueryProject.getBigQuery();
    ST sqlTemplate = new ST(selectFromBigQueryDatasetTemplate);
    sqlTemplate.add("columns", columns);
    sqlTemplate.add("project", bigQueryProjectId);
    sqlTemplate.add("dataset", bqDatasetName);
    sqlTemplate.add("table", tableName);
    QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(sqlTemplate.render()).build();
    return bigQuery.query(queryConfig);
}
Also used : ST(org.stringtemplate.v4.ST) BigQuery(com.google.cloud.bigquery.BigQuery) BigQueryProject(bio.terra.service.tabulardata.google.BigQueryProject) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration)

Example 2 with BigQueryProject

use of bio.terra.service.tabulardata.google.BigQueryProject in project jade-data-repo by DataBiosphere.

the class EncodeFileTest method getFileRefIdFromSnapshot.

private String getFileRefIdFromSnapshot(SnapshotSummaryModel snapshotSummary) throws InterruptedException {
    Snapshot snapshot = snapshotDao.retrieveSnapshotByName(snapshotSummary.getName());
    SnapshotDataProject dataProject = dataLocationService.getOrCreateProject(snapshot);
    BigQueryProject bigQueryProject = BigQueryProject.get(dataProject.getGoogleProjectId());
    StringBuilder builder = new StringBuilder().append("SELECT file_ref FROM `").append(dataProject.getGoogleProjectId()).append('.').append(snapshot.getName()).append(".file` AS T").append(" WHERE T.file_ref IS NOT NULL LIMIT 1");
    String sql = builder.toString();
    try {
        QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(sql).build();
        TableResult result = bigQueryProject.getBigQuery().query(queryConfig);
        FieldValueList row = result.iterateAll().iterator().next();
        FieldValue idValue = row.get(0);
        String drsUri = idValue.getStringValue();
        return drsUri;
    } catch (InterruptedException ie) {
        throw new PdaoException("get file ref id from snapshot unexpectedly interrupted", ie);
    }
}
Also used : Snapshot(bio.terra.service.snapshot.Snapshot) TableResult(com.google.cloud.bigquery.TableResult) BigQueryProject(bio.terra.service.tabulardata.google.BigQueryProject) PdaoException(bio.terra.common.exception.PdaoException) FieldValueList(com.google.cloud.bigquery.FieldValueList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) FieldValue(com.google.cloud.bigquery.FieldValue) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) SnapshotDataProject(bio.terra.service.snapshot.SnapshotDataProject)

Example 3 with BigQueryProject

use of bio.terra.service.tabulardata.google.BigQueryProject in project jade-data-repo by DataBiosphere.

the class SnapshotConnectedTest method getFileRefIdFromSnapshot.

// Technically a helper method, but so specific to testExcludeLockedFromSnapshotFileLookups, likely not re-useable
private String getFileRefIdFromSnapshot(SnapshotSummaryModel snapshotSummary) throws InterruptedException {
    Snapshot snapshot = snapshotDao.retrieveSnapshotByName(snapshotSummary.getName());
    SnapshotDataProject dataProject = dataLocationService.getOrCreateProject(snapshot);
    BigQueryProject bigQueryProject = BigQueryProject.get(dataProject.getGoogleProjectId());
    BigQuery bigQuery = bigQueryProject.getBigQuery();
    ST sqlTemplate = new ST(queryForRefIdTemplate);
    sqlTemplate.add("project", dataProject.getGoogleProjectId());
    sqlTemplate.add("snapshot", snapshot.getName());
    sqlTemplate.add("table", "tableA");
    QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(sqlTemplate.render()).build();
    TableResult result = bigQuery.query(queryConfig);
    FieldValueList row = result.iterateAll().iterator().next();
    FieldValue idValue = row.get(0);
    return idValue.getStringValue();
}
Also used : ST(org.stringtemplate.v4.ST) BigQuery(com.google.cloud.bigquery.BigQuery) TableResult(com.google.cloud.bigquery.TableResult) BigQueryProject(bio.terra.service.tabulardata.google.BigQueryProject) FieldValueList(com.google.cloud.bigquery.FieldValueList) FieldValue(com.google.cloud.bigquery.FieldValue) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration)

Example 4 with BigQueryProject

use of bio.terra.service.tabulardata.google.BigQueryProject 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 5 with BigQueryProject

use of bio.terra.service.tabulardata.google.BigQueryProject 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)

Aggregations

BigQueryProject (bio.terra.service.tabulardata.google.BigQueryProject)5 BigQuery (com.google.cloud.bigquery.BigQuery)3 FieldValue (com.google.cloud.bigquery.FieldValue)3 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 DatasetSummaryModel (bio.terra.model.DatasetSummaryModel)2 SnapshotRequestModel (bio.terra.model.SnapshotRequestModel)2 SnapshotSummaryModel (bio.terra.model.SnapshotSummaryModel)2 FieldValueList (com.google.cloud.bigquery.FieldValueList)2 TableResult (com.google.cloud.bigquery.TableResult)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 ST (org.stringtemplate.v4.ST)2 ConnectedTestConfiguration (bio.terra.app.configuration.ConnectedTestConfiguration)1 PDAO_PREFIX (bio.terra.common.PdaoConstant.PDAO_PREFIX)1 TestUtils (bio.terra.common.TestUtils)1 Connected (bio.terra.common.category.Connected)1 PdaoException (bio.terra.common.exception.PdaoException)1 ConnectedOperations (bio.terra.common.fixtures.ConnectedOperations)1