From d23f4e417b04b6d0f27954baafa9c3df8670c88b Mon Sep 17 00:00:00 2001 From: noerog <32459203+noerog@users.noreply.github.com> Date: Thu, 24 Oct 2019 17:05:25 -0400 Subject: [PATCH] Add Healthcare API snippets. (#1612) * 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 --- .../dicom/DicomWebSearchStudies.java | 82 +++++++++++++++++++ .../snippets/healthcare/DicomStoreTests.java | 2 +- .../snippets/healthcare/DicomWebTests.java | 27 +++--- .../snippets/healthcare/FhirStoreTests.java | 7 +- 4 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 healthcare/v1beta1/src/main/java/snippets/healthcare/dicom/DicomWebSearchStudies.java diff --git a/healthcare/v1beta1/src/main/java/snippets/healthcare/dicom/DicomWebSearchStudies.java b/healthcare/v1beta1/src/main/java/snippets/healthcare/dicom/DicomWebSearchStudies.java new file mode 100644 index 00000000000..6d5cef35de0 --- /dev/null +++ b/healthcare/v1beta1/src/main/java/snippets/healthcare/dicom/DicomWebSearchStudies.java @@ -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] diff --git a/healthcare/v1beta1/src/test/java/snippets/healthcare/DicomStoreTests.java b/healthcare/v1beta1/src/test/java/snippets/healthcare/DicomStoreTests.java index 6a57e450239..5fd9a0d5e3b 100644 --- a/healthcare/v1beta1/src/test/java/snippets/healthcare/DicomStoreTests.java +++ b/healthcare/v1beta1/src/test/java/snippets/healthcare/DicomStoreTests.java @@ -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(); diff --git a/healthcare/v1beta1/src/test/java/snippets/healthcare/DicomWebTests.java b/healthcare/v1beta1/src/test/java/snippets/healthcare/DicomWebTests.java index cf34ef1f1b9..db8a842443d 100644 --- a/healthcare/v1beta1/src/test/java/snippets/healthcare/DicomWebTests.java +++ b/healthcare/v1beta1/src/test/java/snippets/healthcare/DicomWebTests.java @@ -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; @@ -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) @@ -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"; @@ -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("-", "_"); @@ -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)); @@ -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:")); @@ -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); @@ -179,4 +181,3 @@ public void test_DicomWebDeleteStudy() throws IOException { assertThat(output, containsString("DICOM study deleted.")); } } - diff --git a/healthcare/v1beta1/src/test/java/snippets/healthcare/FhirStoreTests.java b/healthcare/v1beta1/src/test/java/snippets/healthcare/FhirStoreTests.java index e1b728c49e6..d9d8427fff7 100644 --- a/healthcare/v1beta1/src/test/java/snippets/healthcare/FhirStoreTests.java +++ b/healthcare/v1beta1/src/test/java/snippets/healthcare/FhirStoreTests.java @@ -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; @@ -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:"));