/*
 * 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;

import static junit.framework.TestCase.assertNotNull;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;

import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import snippets.healthcare.datasets.DatasetCreate;
import snippets.healthcare.datasets.DatasetDeIdentify;
import snippets.healthcare.datasets.DatasetDelete;
import snippets.healthcare.datasets.DatasetGet;
import snippets.healthcare.datasets.DatasetGetIamPolicy;
import snippets.healthcare.datasets.DatasetList;
import snippets.healthcare.datasets.DatasetPatch;
import snippets.healthcare.datasets.DatasetSetIamPolicy;

@RunWith(JUnit4.class)
public class DatasetTests {
  private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
  private static final String REGION_ID = "us-central1";

  private static String datasetName;

  private final PrintStream originalOut = System.out;
  private ByteArrayOutputStream bout;

  private static void requireEnvVar(String varName) {
    assertNotNull(
        System.getenv(varName),
        String.format("Environment variable '%s' is required to perform these tests.", varName));
  }

  @BeforeClass
  public static void checkRequirements() {
    requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
    requireEnvVar("GOOGLE_CLOUD_PROJECT");
  }

  @Before
  public void beforeTest() throws IOException {
    bout = new ByteArrayOutputStream();
    System.setOut(new PrintStream(bout));

    String datasetId = "dataset-" + UUID.randomUUID().toString().replaceAll("-", "_");
    datasetName =
        String.format("projects/%s/locations/%s/datasets/%s", PROJECT_ID, REGION_ID, datasetId);

    DatasetCreate.datasetCreate(PROJECT_ID, REGION_ID, datasetId);

    bout = new ByteArrayOutputStream();
    System.setOut(new PrintStream(bout));
  }

  @After
  public void tearDown() {
    System.setOut(originalOut);
    bout.reset();
  }

  @Test
  public void test_DatasetCreate() throws IOException {
    String newDatasetName =
        String.format("projects/%s/locations/%s/datasets/new-dataset", PROJECT_ID, REGION_ID);
    try {
      DatasetDelete.datasetDelete(newDatasetName);
    } catch (GoogleJsonResponseException gjre) {
      // Expected if new-dataset does not exist.
    }
    DatasetCreate.datasetCreate(PROJECT_ID, REGION_ID, "new-dataset");

    String output = bout.toString(StandardCharsets.UTF_8);
    assertThat(output, containsString("Dataset created."));
  }

  @Test
  public void test_DatasetGet() throws IOException {
    DatasetGet.datasetGet(datasetName);

    String output = bout.toString();
    assertThat(output, containsString("Dataset retrieved:"));
  }

  @Test
  public void test_DatasetList() throws IOException {
    DatasetList.datasetList(PROJECT_ID, REGION_ID);

    String output = bout.toString();
    assertThat(output, containsString("Retrieved"));
  }

  @Test
  public void test_DataSetPatch() throws IOException {
    DatasetPatch.datasetPatch(datasetName);

    String output = bout.toString();
    assertThat(output, containsString("Dataset patched:"));
  }

  @Test
  public void test_DatasetDeidentify() throws IOException {
    DatasetDeIdentify.datasetDeIdentify(datasetName, datasetName + "-died");

    String output = bout.toString();
    assertThat(output, containsString("De-identified Dataset created."));
  }

  @Test
  public void test_DatasetGetIamPolicy() throws IOException {
    DatasetGetIamPolicy.datasetGetIamPolicy(datasetName);

    String output = bout.toString();
    assertThat(output, containsString("Dataset IAMPolicy retrieved:"));
  }

  @Test
  public void test_DatasetSetIamPolicy() throws IOException {
    DatasetSetIamPolicy.datasetSetIamPolicy(datasetName);

    String output = bout.toString();
    assertThat(output, containsString("Dataset policy has been updated: "));
  }

  @Test
  public void test_DatasetDelete() throws IOException {
    DatasetDelete.datasetDelete(datasetName);

    String output = bout.toString();
    assertThat(output, containsString("Dataset deleted."));
  }
}

@@ -2513,29 +2513,27 @@
     
-datasetName =%0A       
+String parentName =
  Str
@@ -2572,20 +2572,8 @@
 s/%25s
-/datasets/%25s
 %22, P
@@ -2584,32 +2584,95 @@
 CT_ID, REGION_ID
+);%0A    datasetName = String.format(%22%25s/datasets/%25s%22, parentName
 , datasetId);%0A%0A 
@@ -2858,16 +2858,35 @@
 arDown()
+ throws IOException
  %7B%0A    S
@@ -2912,16 +2912,171 @@
 alOut);%0A
+    try %7B%0A      DatasetDelete.datasetDelete(datasetName);%0A    %7D catch (GoogleJsonResponseException ex) %7B%0A      // Dataset already deleted, continue.%0A    %7D%0A
     bout
@@ -3130,16 +3130,22 @@
 etCreate
+Delete
 () throw
@@ -3174,23 +3174,56 @@
 ring new
-Dataset
+Name = %22new-dataset%22;%0A    String newFull
 Name =%0A 
@@ -3278,27 +3278,18 @@
 atasets/
-new-dataset
+%25s
 %22, PROJE
@@ -3300,24 +3300,33 @@
 D, REGION_ID
+, newName
 );%0A    try %7B
@@ -3363,23 +3363,20 @@
 lete(new
-Dataset
+Full
 Name);%0A 
@@ -3533,29 +3533,23 @@
 ION_ID, 
-%22
 new
--dataset%22
+Name
 );%0A%0A    
@@ -3655,32 +3655,188 @@
 et created.%22));%0A
+    bout.reset();%0A%0A    DatasetDelete.datasetDelete(newFullName);%0A%0A    output = bout.toString();%0A    assertThat(output, containsString(%22Dataset deleted.%22));%0A
   %7D%0A%0A  @Test%0A  p
@@ -3917,33 +3917,32 @@
 t(datasetName);%0A
-%0A
     String outpu
@@ -5204,220 +5204,7 @@
 %0A  %7D
-%0A%0A  @Test%0A  public void test_DatasetDelete() throws IOException %7B%0A    DatasetDelete.datasetDelete(datasetName);%0A%0A    String output = bout.toString();%0A    assertThat(output, containsString(%22Dataset deleted.%22));%0A  %7D
 %0A%7D%0A

Delete dataset after each test in DatasetTests. (#3767)