Skip to content

Commit

Permalink
Add Healthcare API snippets. (#1612)
Browse files Browse the repository at this point in the history
* Add Healthcare API DICOMweb search studies by tag

* Use client library.

* Fix FhirStoreTests failures

* Remove unnecessary .dcm file and variables.

* Fix DicomStoreTests failures

* Make bucket name in tests an environment variable.

* Make bucket name in tests an environment variable.

* Fix lint, make Storage bucket hard-coded again
  • Loading branch information
noerog authored and bradmiro committed Nov 15, 2019
1 parent e2aa152 commit d23f4e4
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package snippets.healthcare.dicom;

// [START healthcare_dicomweb_search_studies]
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.healthcare.v1beta1.CloudHealthcare;
import com.google.api.services.healthcare.v1beta1.CloudHealthcare.Projects.Locations.Datasets.DicomStores;
import com.google.api.services.healthcare.v1beta1.CloudHealthcareScopes;
import java.io.IOException;
import java.util.Collections;

public class DicomWebSearchStudies {
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final NetHttpTransport HTTP_TRANSPORT = new NetHttpTransport();

public static void dicomWebSearchStudies(String dicomStoreName) throws IOException {
// String dicomStoreName =
// String.format(
// DICOM_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-dicom-id");

// Initialize the client, which will be used to interact with the service.
CloudHealthcare client = createClient();

DicomStores.SearchForStudies request =
client
.projects()
.locations()
.datasets()
.dicomStores()
.searchForStudies(dicomStoreName, "studies")
// Refine your search by appending DICOM tags to the
// request in the form of query parameters. This sample
// searches for studies containing a patient's name.
.set("PatientName", "Sally Zhang");

// Execute the request and process the results.
HttpResponse response = request.executeUnparsed();
System.out.println("Studies found: \n" + response.toString());
}

private static CloudHealthcare createClient() throws IOException {
// Use Application Default Credentials (ADC) to authenticate the requests
// For more information see https://cloud.google.com/docs/authentication/production
GoogleCredential credential =
GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY)
.createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM));

// Create a HttpRequestInitializer, which will provide a baseline configuration to all requests.
HttpRequestInitializer requestInitializer =
request -> {
credential.initialize(request);
request.setConnectTimeout(60000); // 1 minute connect timeout
request.setReadTimeout(60000); // 1 minute read timeout
};

// Build the client for interacting with the service.
return new CloudHealthcare.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer)
.setApplicationName("your-application-name")
.build();
}
}
// [END healthcare_dicomweb_search_studies]
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void test_DicomStoreExport() throws IOException {
@Test
public void test_DicomStoreImport() throws IOException {
String gcsPath =
String.format("gs://%s/%s", GCLOUD_BUCKET_NAME, "IM-0002-0001-JPEG-BASELINE.dcm");
String.format("gs://%s/%s/%s", GCLOUD_BUCKET_NAME, "healthcare-api", "000009.dcm");
DicomStoreImport.dicomStoreImport(dicomStoreName, gcsPath);

String output = bout.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.util.UUID;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
Expand All @@ -43,6 +42,7 @@
import snippets.healthcare.dicom.DicomWebRetrieveRendered;
import snippets.healthcare.dicom.DicomWebRetrieveStudy;
import snippets.healthcare.dicom.DicomWebSearchForInstances;
import snippets.healthcare.dicom.DicomWebSearchStudies;
import snippets.healthcare.dicom.DicomWebStoreInstance;

@RunWith(JUnit4.class)
Expand All @@ -57,8 +57,8 @@ public class DicomWebTests {
private static String studyId = "2.25.330012077234033941963257891139480825153";
private static String seriesId = "2.25.143186483950719304925806365081717734297";
private static String instanceId = "2.25.195151962645072062560826889007364152748";
private static String dicomWebInstancePath = String.format(
"studies/%s/series/%s/instances/%s", studyId, seriesId, instanceId);
private static String dicomWebInstancePath =
String.format("studies/%s/series/%s/instances/%s", studyId, seriesId, instanceId);
private static String dicomWebRenderedPath = dicomWebInstancePath + "/rendered";

private static String instanceOutput = "instance.dcm";
Expand All @@ -85,11 +85,8 @@ public static void checkRequirements() {
@BeforeClass
public static void setUp() throws IOException {
String datasetId = "dataset-" + UUID.randomUUID().toString().replaceAll("-", "_");
datasetName = String.format(
"projects/%s/locations/%s/datasets/%s",
PROJECT_ID,
REGION_ID,
datasetId);
datasetName =
String.format("projects/%s/locations/%s/datasets/%s", PROJECT_ID, REGION_ID, datasetId);
DatasetCreate.datasetCreate(PROJECT_ID, REGION_ID, datasetId);

String dicomStoreId = "dicom-" + UUID.randomUUID().toString().replaceAll("-", "_");
Expand All @@ -109,8 +106,7 @@ public void beforeTest() throws IOException, URISyntaxException {
System.setOut(new PrintStream(bout));

// Store before each test so it is always available.
DicomWebStoreInstance.dicomWebStoreInstance(
dicomStoreName, "src/test/resources/jpeg_text.dcm");
DicomWebStoreInstance.dicomWebStoreInstance(dicomStoreName, "src/test/resources/jpeg_text.dcm");

bout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bout));
Expand All @@ -124,8 +120,7 @@ public void tearDown() {

@Test
public void test_DicomWebStoreInstance() throws Exception {
DicomWebStoreInstance.dicomWebStoreInstance(
dicomStoreName, "src/test/resources/jpeg_text.dcm");
DicomWebStoreInstance.dicomWebStoreInstance(dicomStoreName, "src/test/resources/jpeg_text.dcm");

String output = bout.toString();
assertThat(output, containsString("DICOM instance stored:"));
Expand All @@ -138,6 +133,13 @@ public void test_DicomWebSearchInstances() throws Exception {
assertThat(output, containsString("Dicom store instances found:"));
}

@Test
public void test_DicomWebSearchStudies() throws Exception {
DicomWebSearchStudies.dicomWebSearchStudies(dicomStoreName);
String output = bout.toString();
assertThat(output, containsString("Studies found:"));
}

@Test
public void test_DicomWebRetrieveStudy() throws Exception {
DicomWebRetrieveStudy.dicomWebRetrieveStudy(dicomStoreName, studyId);
Expand Down Expand Up @@ -179,4 +181,3 @@ public void test_DicomWebDeleteStudy() throws IOException {
assertThat(output, containsString("DICOM study deleted."));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public class FhirStoreTests {
private static final String GCLOUD_BUCKET_NAME = "java-docs-samples-testing";
private static final String GCLOUD_PUBSUB_TOPIC = System.getenv("GCLOUD_PUBSUB_TOPIC");

private static String storageFileName = "IM-0002-0001-JPEG-BASELINE.dcm";
private static String gcsFileName = GCLOUD_BUCKET_NAME + "/" + storageFileName;

private static String datasetName;

private static String fhirStoreName;
Expand Down Expand Up @@ -177,7 +174,9 @@ public void test_FhirStoreExport() throws Exception {

@Test
public void test_FhirStoreImport() throws Exception {
FhirStoreImport.fhirStoreImport(fhirStoreName, "gs://" + gcsFileName);
FhirStoreImport.fhirStoreImport(
fhirStoreName,
"gs://" + GCLOUD_BUCKET_NAME + "/healthcare-api/Patient.json");

String output = bout.toString();
assertThat(output, containsString("FHIR store import complete:"));
Expand Down

0 comments on commit d23f4e4

Please sign in to comment.