/*
 * Copyright 2018 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 com.example.asset;

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.ServiceOptions;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetId;
import com.google.cloud.bigquery.DatasetInfo;
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobListOption;
import com.google.cloud.storage.StorageOptions;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for quickstart sample. */
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class QuickStartIT {
  private static final String bucketName = UUID.randomUUID().toString();
  private static final String datasetName = RemoteBigQueryHelper.generateDatasetName();
  private ByteArrayOutputStream bout;
  private PrintStream out;
  private BigQuery bigquery;

  private static final void deleteBucket(String bucketName) {
    Storage storage = StorageOptions.getDefaultInstance().getService();
    for (BlobInfo info : storage.list(bucketName, BlobListOption.versions(true)).getValues()) {
      storage.delete(info.getBlobId());
    }
    storage.delete(bucketName);
  }

  private static final void createBucket(String bucketName) {
    Storage storage = StorageOptions.getDefaultInstance().getService();
    if (storage.get(bucketName) != null) {
      // Bucket exists.
      return;
    }
    Bucket bucket = storage.create(BucketInfo.of(bucketName));
    assertThat(bucket).isNotNull();
  }

  @Before
  public void setUp() {
    createBucket(bucketName);
    bigquery = BigQueryOptions.getDefaultInstance().getService();
    if (bigquery.getDataset(datasetName) == null) {
      Dataset dataset = bigquery.create(DatasetInfo.newBuilder(datasetName).build());
    }
    bout = new ByteArrayOutputStream();
    out = new PrintStream(bout);
    System.setOut(out);
  }

  @After
  public void tearDown() {
    String consoleOutput = bout.toString();
    System.setOut(null);
    deleteBucket(bucketName);
    DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), datasetName);
    bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
  }

  @Test
  public void testExportAssetExample() throws Exception {
    String assetDumpPath = String.format("gs://%s/my-assets-dump.txt", bucketName);
    ExportAssetsExample.main(assetDumpPath);
    String got = bout.toString();
    assertThat(got).contains(String.format("uri: \"%s\"", assetDumpPath));
  }

  @Test
  public void testExportAssetBigqueryExample() throws Exception {
    String dataset =
        String.format("projects/%s/datasets/%s", ServiceOptions.getDefaultProjectId(), datasetName);
    String table = "java_test";
    ExportAssetsBigqueryExample.exportBigQuery(dataset, table);
    String got = bout.toString();
    assertThat(got).contains(String.format("dataset: \"%s\"", dataset));
  }

  @Test
  public void testBatchGetAssetsHistory() throws Exception {
    // Wait 10 seconds to let bucket creation event go to CAI
    Thread.sleep(10000);
    String bucketAssetName = String.format("//storage.googleapis.com/%s", bucketName);
    BatchGetAssetsHistoryExample.main(bucketAssetName);
    String got = bout.toString();
    if (!got.isEmpty()) {
      assertThat(got).contains(bucketAssetName);
    }
  }
}

@@ -1106,92 +1106,8 @@
 fo;%0A
-import com.google.cloud.storage.Bucket;%0Aimport com.google.cloud.storage.BucketInfo;%0A
 impo
@@ -1661,16 +1661,82 @@
 etName =
+ %22java-docs-samples-testing%22;%0A  private static final String path =
  UUID.ra
@@ -1979,32 +1979,16 @@
 lete
-Bucket(String bucketName
+Objects(
 ) %7B%0A
@@ -2087,482 +2087,294 @@
 fo :
- storage.list(bucketName, BlobListOption.versions(true)).getValues()) %7B%0A      storage.delete(info.getBlobId());%0A
+%0A        storage%0A            .list(%0A                bucketName,%0A                BlobListOption.versions(true),%0A       
     
-%7D%0A
     
-storage.delete(bucketName);%0A  %7D%0A%0A  private static final void createBucket(String bucketName) %7B%0A    Storage storage = StorageOptions.getDefaultInstance().getService();%0A    if (storage.get(bucketName) != null) %7B%0A      // Bucket exists.%0A      return;%0A    %7D%0A    Bucket bucket = storage.create(BucketInfo.of(bucketName));%0A    assertThat(bucket).isNotNull();
+ BlobListOption.currentDirectory(),%0A                BlobListOption.prefix(path + %22/%22))%0A            .getValues()) %7B%0A      storage.delete(info.getBlobId());%0A    %7D
 %0A  %7D
@@ -2413,38 +2413,8 @@
 ) %7B%0A
-    createBucket(bucketName);%0A
     
@@ -2840,25 +2840,16 @@
 lete
-Bucket(bucketName
+Objects(
 );%0A 
@@ -3129,16 +3129,19 @@
 gs://%25s/
+%25s/
 my-asset
@@ -3159,24 +3159,30 @@
 , bucketName
+, path
 );%0A    Expor

Update QuickStartIT.java (#2716)