Skip to content

Commit fbc1ae2

Browse files
noerogkurtisvg
authored andcommitted
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
1 parent 8752293 commit fbc1ae2

File tree

4 files changed

+100
-18
lines changed

4 files changed

+100
-18
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package snippets.healthcare.dicom;
18+
19+
// [START healthcare_dicomweb_search_studies]
20+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
21+
import com.google.api.client.http.HttpRequestInitializer;
22+
import com.google.api.client.http.HttpResponse;
23+
import com.google.api.client.http.javanet.NetHttpTransport;
24+
import com.google.api.client.json.JsonFactory;
25+
import com.google.api.client.json.jackson2.JacksonFactory;
26+
import com.google.api.services.healthcare.v1beta1.CloudHealthcare;
27+
import com.google.api.services.healthcare.v1beta1.CloudHealthcare.Projects.Locations.Datasets.DicomStores;
28+
import com.google.api.services.healthcare.v1beta1.CloudHealthcareScopes;
29+
import java.io.IOException;
30+
import java.util.Collections;
31+
32+
public class DicomWebSearchStudies {
33+
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
34+
private static final NetHttpTransport HTTP_TRANSPORT = new NetHttpTransport();
35+
36+
public static void dicomWebSearchStudies(String dicomStoreName) throws IOException {
37+
// String dicomStoreName =
38+
// String.format(
39+
// DICOM_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-dicom-id");
40+
41+
// Initialize the client, which will be used to interact with the service.
42+
CloudHealthcare client = createClient();
43+
44+
DicomStores.SearchForStudies request =
45+
client
46+
.projects()
47+
.locations()
48+
.datasets()
49+
.dicomStores()
50+
.searchForStudies(dicomStoreName, "studies")
51+
// Refine your search by appending DICOM tags to the
52+
// request in the form of query parameters. This sample
53+
// searches for studies containing a patient's name.
54+
.set("PatientName", "Sally Zhang");
55+
56+
// Execute the request and process the results.
57+
HttpResponse response = request.executeUnparsed();
58+
System.out.println("Studies found: \n" + response.toString());
59+
}
60+
61+
private static CloudHealthcare createClient() throws IOException {
62+
// Use Application Default Credentials (ADC) to authenticate the requests
63+
// For more information see https://cloud.google.com/docs/authentication/production
64+
GoogleCredential credential =
65+
GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY)
66+
.createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM));
67+
68+
// Create a HttpRequestInitializer, which will provide a baseline configuration to all requests.
69+
HttpRequestInitializer requestInitializer =
70+
request -> {
71+
credential.initialize(request);
72+
request.setConnectTimeout(60000); // 1 minute connect timeout
73+
request.setReadTimeout(60000); // 1 minute read timeout
74+
};
75+
76+
// Build the client for interacting with the service.
77+
return new CloudHealthcare.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer)
78+
.setApplicationName("your-application-name")
79+
.build();
80+
}
81+
}
82+
// [END healthcare_dicomweb_search_studies]

healthcare/v1beta1/src/test/java/snippets/healthcare/DicomStoreTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void test_DicomStoreExport() throws IOException {
165165
@Test
166166
public void test_DicomStoreImport() throws IOException {
167167
String gcsPath =
168-
String.format("gs://%s/%s", GCLOUD_BUCKET_NAME, "IM-0002-0001-JPEG-BASELINE.dcm");
168+
String.format("gs://%s/%s/%s", GCLOUD_BUCKET_NAME, "healthcare-api", "000009.dcm");
169169
DicomStoreImport.dicomStoreImport(dicomStoreName, gcsPath);
170170

171171
String output = bout.toString();

healthcare/v1beta1/src/test/java/snippets/healthcare/DicomWebTests.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.io.PrintStream;
2828
import java.net.URISyntaxException;
2929
import java.util.UUID;
30-
3130
import org.junit.After;
3231
import org.junit.AfterClass;
3332
import org.junit.Before;
@@ -43,6 +42,7 @@
4342
import snippets.healthcare.dicom.DicomWebRetrieveRendered;
4443
import snippets.healthcare.dicom.DicomWebRetrieveStudy;
4544
import snippets.healthcare.dicom.DicomWebSearchForInstances;
45+
import snippets.healthcare.dicom.DicomWebSearchStudies;
4646
import snippets.healthcare.dicom.DicomWebStoreInstance;
4747

4848
@RunWith(JUnit4.class)
@@ -57,8 +57,8 @@ public class DicomWebTests {
5757
private static String studyId = "2.25.330012077234033941963257891139480825153";
5858
private static String seriesId = "2.25.143186483950719304925806365081717734297";
5959
private static String instanceId = "2.25.195151962645072062560826889007364152748";
60-
private static String dicomWebInstancePath = String.format(
61-
"studies/%s/series/%s/instances/%s", studyId, seriesId, instanceId);
60+
private static String dicomWebInstancePath =
61+
String.format("studies/%s/series/%s/instances/%s", studyId, seriesId, instanceId);
6262
private static String dicomWebRenderedPath = dicomWebInstancePath + "/rendered";
6363

6464
private static String instanceOutput = "instance.dcm";
@@ -85,11 +85,8 @@ public static void checkRequirements() {
8585
@BeforeClass
8686
public static void setUp() throws IOException {
8787
String datasetId = "dataset-" + UUID.randomUUID().toString().replaceAll("-", "_");
88-
datasetName = String.format(
89-
"projects/%s/locations/%s/datasets/%s",
90-
PROJECT_ID,
91-
REGION_ID,
92-
datasetId);
88+
datasetName =
89+
String.format("projects/%s/locations/%s/datasets/%s", PROJECT_ID, REGION_ID, datasetId);
9390
DatasetCreate.datasetCreate(PROJECT_ID, REGION_ID, datasetId);
9491

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

111108
// Store before each test so it is always available.
112-
DicomWebStoreInstance.dicomWebStoreInstance(
113-
dicomStoreName, "src/test/resources/jpeg_text.dcm");
109+
DicomWebStoreInstance.dicomWebStoreInstance(dicomStoreName, "src/test/resources/jpeg_text.dcm");
114110

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

125121
@Test
126122
public void test_DicomWebStoreInstance() throws Exception {
127-
DicomWebStoreInstance.dicomWebStoreInstance(
128-
dicomStoreName, "src/test/resources/jpeg_text.dcm");
123+
DicomWebStoreInstance.dicomWebStoreInstance(dicomStoreName, "src/test/resources/jpeg_text.dcm");
129124

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

136+
@Test
137+
public void test_DicomWebSearchStudies() throws Exception {
138+
DicomWebSearchStudies.dicomWebSearchStudies(dicomStoreName);
139+
String output = bout.toString();
140+
assertThat(output, containsString("Studies found:"));
141+
}
142+
141143
@Test
142144
public void test_DicomWebRetrieveStudy() throws Exception {
143145
DicomWebRetrieveStudy.dicomWebRetrieveStudy(dicomStoreName, studyId);
@@ -179,4 +181,3 @@ public void test_DicomWebDeleteStudy() throws IOException {
179181
assertThat(output, containsString("DICOM study deleted."));
180182
}
181183
}
182-

healthcare/v1beta1/src/test/java/snippets/healthcare/FhirStoreTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ public class FhirStoreTests {
5353
private static final String GCLOUD_BUCKET_NAME = "java-docs-samples-testing";
5454
private static final String GCLOUD_PUBSUB_TOPIC = System.getenv("GCLOUD_PUBSUB_TOPIC");
5555

56-
private static String storageFileName = "IM-0002-0001-JPEG-BASELINE.dcm";
57-
private static String gcsFileName = GCLOUD_BUCKET_NAME + "/" + storageFileName;
58-
5956
private static String datasetName;
6057

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

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

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

0 commit comments

Comments
 (0)