/*
 * 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.examples.cookbook;

import org.apache.beam.sdk.io.gcp.bigquery.BigQueryOptions;
import org.apache.beam.sdk.io.gcp.testing.BigqueryMatcher;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.testing.TestPipeline;
import org.apache.beam.sdk.testing.TestPipelineOptions;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * An end-to-end test for {@link org.apache.beam.examples.cookbook.BigQueryTornadoes}.
 *
 * <p>This test reads the public samples of weather data from BigQuery, counts the number of
 * tornadoes that occur in each month, and writes the results to BigQuery. It requires
 * "BigQueryTornadoesIT" BigQuery dataset to be created before running.
 *
 * <p>Running instructions:
 *
 * <pre>
 *  ./gradlew integrationTest -p examples/java/ -DintegrationTestPipelineOptions='[
 *  "--tempLocation=gs://your-location/"]'
 *  --tests org.apache.beam.examples.cookbook.BigQueryTornadoesIT
 *  -DintegrationTestRunner=direct
 * </pre>
 *
 * <p>Check {@link org.apache.beam.examples.cookbook.BigQueryTornadoes} form more configuration
 * options via PipelineOptions.
 */
@RunWith(JUnit4.class)
public class BigQueryTornadoesIT {

  private static final String DEFAULT_OUTPUT_CHECKSUM = "1ab4c7ec460b94bbb3c3885b178bf0e6bed56e1f";

  /** Options for the BigQueryTornadoes Integration Test. */
  public interface BigQueryTornadoesITOptions
      extends TestPipelineOptions, BigQueryTornadoes.Options, BigQueryOptions {}

  @BeforeClass
  public static void setUp() {
    PipelineOptionsFactory.register(BigQueryTornadoesITOptions.class);
  }

  @Test
  public void testE2EBigQueryTornadoes() throws Exception {
    BigQueryTornadoesITOptions options =
        TestPipeline.testingPipelineOptions().as(BigQueryTornadoesITOptions.class);
    options.setOutput(
        String.format(
            "%s.%s", "BigQueryTornadoesIT", "monthly_tornadoes_" + System.currentTimeMillis()));

    String query = String.format("SELECT month, tornado_count FROM [%s]", options.getOutput());
    options.setOnSuccessMatcher(
        new BigqueryMatcher(
            options.getAppName(), options.getProject(), query, DEFAULT_OUTPUT_CHECKSUM));

    BigQueryTornadoes.runBigQueryTornadoes(options);
  }
}

@@ -843,16 +843,88 @@
 kbook;%0A%0A
+import org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TypedRead.Method;%0A
 import o
@@ -2551,16 +2551,421 @@
 );%0A  %7D%0A%0A
+  private void runE2EBigQueryTornadoesTest(BigQueryTornadoesITOptions options) throws Exception %7B%0A    String query = String.format(%22SELECT month, tornado_count FROM %5B%25s%5D%22, options.getOutput());%0A    options.setOnSuccessMatcher(%0A        new BigqueryMatcher(%0A            options.getAppName(), options.getProject(), query, DEFAULT_OUTPUT_CHECKSUM));%0A%0A    BigQueryTornadoes.runBigQueryTornadoes(options);%0A  %7D%0A%0A
   @Test%0A
@@ -3002,16 +3002,26 @@
 ornadoes
+WithExport
 () throw
@@ -3159,16 +3159,58 @@
 class);%0A
+    options.setReadMethod(Method.EXPORT);%0A
     opti
@@ -3353,248 +3353,464 @@
     
-String query = String.format(%22SELECT month, tornado_count FROM %5B%25s%5D%22, options.getOutput());%0A    options.setOnSuccessMatcher(%0A        new BigqueryMatcher(%0A            options.getAppName(), options.getProject(), query, DEFAULT_OUTPUT_CHECKSUM
+runE2EBigQueryTornadoesTest(options);%0A  %7D%0A%0A  @Test%0A  public void testE2eBigQueryTornadoesWithStorageApi() throws Exception %7B%0A    BigQueryTornadoesITOptions options =%0A        TestPipeline.testingPipelineOptions().as(BigQueryTornadoesITOptions.class);%0A    options.setReadMethod(Method.DIRECT_READ);%0A    options.setOutput(%0A        String.format(%0A            %22%25s.%25s%22,%0A            %22BigQueryTornadoesIT%22, %22monthly_tornadoes_storage_%22 + System.currentTimeMillis()
 ));%0A
@@ -3806,32 +3806,38 @@
 illis()));%0A%0A    
+runE2E
 BigQueryTornadoe
@@ -3837,37 +3837,20 @@
 ornadoes
-.runBigQueryTornado
+T
 es
+t
 (options

Add BQ Storage API case to BigQueryTornadoesIT.