/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.beam.sdk.io.gcp.healthcare;

import static org.apache.beam.sdk.io.gcp.healthcare.FhirIOTestUtil.DEFAULT_TEMP_BUCKET;
import static org.apache.beam.sdk.io.gcp.healthcare.HL7v2IOTestUtil.HEALTHCARE_DATASET_TEMPLATE;

import com.google.api.services.healthcare.v1beta1.model.DeidentifyConfig;
import com.google.api.services.healthcare.v1beta1.model.FhirStore;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.List;
import org.apache.beam.sdk.testing.TestPipeline;
import org.apache.beam.sdk.values.PCollection;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class FhirIOLROIT {
  @Rule public final transient TestPipeline pipeline = TestPipeline.create();

  private transient HealthcareApiClient client;
  private final String project;
  private String healthcareDataset;
  private final String fhirStoreId;
  private String version;

  public FhirIOLROIT() {
    long testTime = System.currentTimeMillis();
    this.fhirStoreId = "FHIR_store_" + testTime + "_" + (new SecureRandom().nextInt(32));
    this.version = "STU3";
    this.project =
        TestPipeline.testingPipelineOptions()
            .as(HealthcareStoreTestPipelineOptions.class)
            .getStoreProjectId();
  }

  @Before
  public void setup() throws Exception {
    healthcareDataset = String.format(HEALTHCARE_DATASET_TEMPLATE, project);
    if (client == null) {
      this.client = new HttpHealthcareApiClient();
    }
    client.createFhirStore(healthcareDataset, fhirStoreId, version, null);

    // Execute bundles to populate some data.
    FhirIOTestUtil.executeFhirBundles(
        client,
        healthcareDataset + "/fhirStores/" + fhirStoreId,
        FhirIOTestUtil.BUNDLES.get(version));
  }

  @After
  public void deleteAllFhirStores() throws IOException {
    HealthcareApiClient client = new HttpHealthcareApiClient();
    List<FhirStore> fhirStores = client.listAllFhirStores(healthcareDataset);
    for (FhirStore fs : fhirStores) {
      client.deleteFhirStore(fs.getName());
    }
  }

  @AfterClass
  public static void teardownBucket() throws IOException {
    FhirIOTestUtil.tearDownTempBucket();
  }

  @Test
  public void test_FhirIO_exportFhirResourcesGcs() {
    String fhirStoreName = healthcareDataset + "/fhirStores/" + fhirStoreId;
    String exportGcsUriPrefix =
        "gs://" + DEFAULT_TEMP_BUCKET + "/export/" + (new SecureRandom().nextInt(32));
    PCollection<String> resources =
        pipeline.apply(FhirIO.exportResourcesToGcs(fhirStoreName, exportGcsUriPrefix));
    pipeline.run();
  }

  @Test
  public void test_FhirIO_deidentify() throws IOException {
    String fhirStoreName = healthcareDataset + "/fhirStores/" + fhirStoreId;
    String destinationFhirStoreId = fhirStoreId + "_deid";
    client.createFhirStore(healthcareDataset, destinationFhirStoreId, version, null);
    String destinationFhirStoreName = healthcareDataset + "/fhirStores/" + destinationFhirStoreId;
    DeidentifyConfig deidConfig = new DeidentifyConfig(); // use default DeidentifyConfig
    pipeline.apply(FhirIO.deidentify(fhirStoreName, destinationFhirStoreName, deidConfig));
    pipeline.run();
  }
}

@@ -1111,75 +1111,8 @@
 ig;%0A
-import com.google.api.services.healthcare.v1beta1.model.FhirStore;%0A
 impo
@@ -1131,24 +1131,24 @@
 OException;%0A
+
 import java.
@@ -1174,31 +1174,8 @@
 om;%0A
-import java.util.List;%0A
 impo
@@ -1738,16 +1738,56 @@
 toreId;%0A
+  private final String deidFhirStoreId;%0A
   privat
@@ -1964,24 +1964,118 @@
 xtInt(32));%0A
+    this.deidFhirStoreId = %22FHIR_store_%22 + testTime + %22_%22 + (new SecureRandom().nextInt(32));%0A
     this.ver
@@ -2536,16 +2536,95 @@
 , null);
+%0A    client.createFhirStore(healthcareDataset, deidFhirStoreId, version, null);
 %0A%0A    //
@@ -2889,24 +2889,36 @@
 Exception %7B%0A
+    try %7B%0A  
     Healthca
@@ -2977,44 +2977,17 @@
     
-List%3CFhirStore%3E fhirStores =
+ 
  client.
 list
@@ -2986,15 +2986,14 @@
 ent.
-listAll
+delete
 Fhir
@@ -2997,17 +2997,16 @@
 hirStore
-s
 (healthc
@@ -3019,29 +3019,22 @@
 aset
-);%0A    for (F
+ + %22/f
 hirStore
  fs 
@@ -3033,13 +3033,13 @@
 tore
- fs :
+s/%22 +
  fhi
@@ -3048,12 +3048,12 @@
 tore
-s) %7B
+Id);
 %0A   
@@ -3082,22 +3082,113 @@
 ore(
-fs.getName());
+healthcareDataset + %22/fhirStores/%22 + deidFhirStoreId);%0A    %7D catch (IOException e) %7B%0A      // Do nothing.
 %0A   
@@ -3869,153 +3869,8 @@
 Id;%0A
-    String destinationFhirStoreId = fhirStoreId + %22_deid%22;%0A    client.createFhirStore(healthcareDataset, destinationFhirStoreId, version, null);%0A
     
@@ -3942,25 +3942,18 @@
 s/%22 + de
-stination
+id
 FhirStor

Fix FhirLROIT tests (#12902)