forked from data-integrations/google-cloud
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
226 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/io/cdap/plugin/gcp/bigquery/exception/BigQueryJobExecutionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, Inc. | ||
* | ||
* 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 io.cdap.plugin.gcp.bigquery.exception; | ||
|
||
/** | ||
* Custom exception class for handling errors related to BigQuery job execution. | ||
* This exception should be thrown when an issue occurs during the execution of a BigQuery job, | ||
* and the calling code should consider retrying the operation. | ||
*/ | ||
public class BigQueryJobExecutionException extends Exception { | ||
/** | ||
* Constructs a new BigQueryJobExecutionException with the specified detail message. | ||
* | ||
* @param message The detail message that describes the exception. | ||
*/ | ||
public BigQueryJobExecutionException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
113 changes: 113 additions & 0 deletions
113
src/test/java/io/cdap/plugin/gcp/bigquery/action/BigQueryExecuteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, Inc. | ||
* | ||
* 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 io.cdap.plugin.gcp.bigquery.action; | ||
|
||
import com.google.cloud.bigquery.BigQuery; | ||
import com.google.cloud.bigquery.BigQueryError; | ||
import com.google.cloud.bigquery.Job; | ||
import com.google.cloud.bigquery.JobId; | ||
import com.google.cloud.bigquery.JobInfo; | ||
import com.google.cloud.bigquery.JobStatistics; | ||
import com.google.cloud.bigquery.JobStatus; | ||
import com.google.cloud.bigquery.QueryJobConfiguration; | ||
import com.google.cloud.bigquery.TableResult; | ||
import io.cdap.cdap.api.metrics.Metrics; | ||
import io.cdap.cdap.etl.api.StageMetrics; | ||
import io.cdap.cdap.etl.api.action.ActionContext; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
public class BigQueryExecuteTest { | ||
@Mock | ||
BigQuery bigQuery; | ||
@Mock | ||
Job queryJob; | ||
@Mock | ||
JobStatus jobStatus; | ||
@Mock | ||
BigQueryError bigQueryError; | ||
@Mock | ||
TableResult queryResults; | ||
@Mock | ||
JobStatistics.QueryStatistics queryStatistics; | ||
@Mock | ||
ActionContext context; | ||
@Mock | ||
StageMetrics stageMetrics; | ||
@Mock | ||
Metrics metrics; | ||
QueryJobConfiguration queryJobConfiguration; | ||
BigQueryExecute.Config config; | ||
JobInfo jobInfo; | ||
JobId jobId; | ||
BigQueryExecute bq; | ||
|
||
@Before | ||
public void setUp() throws InterruptedException, NoSuchMethodException { | ||
MockitoAnnotations.initMocks(this); | ||
|
||
queryJobConfiguration = QueryJobConfiguration.newBuilder("select * from test").build(); | ||
config = BigQueryExecute.Config.builder() | ||
.setLocation("US").setProject("testProject").setRowAsArguments("false").build(); | ||
jobId = JobId.newBuilder().setRandomJob().setLocation(config.getLocation()).build(); | ||
jobInfo = JobInfo.newBuilder(queryJobConfiguration).setJobId(jobId).build(); | ||
bq = new BigQueryExecute(config); | ||
|
||
// Mock Job Creation | ||
Mockito.when(bigQuery.create((JobInfo) Mockito.any())).thenReturn(queryJob); | ||
Mockito.when(queryJob.waitFor()).thenReturn(queryJob); | ||
Mockito.when(queryJob.getStatus()).thenReturn(jobStatus); | ||
Mockito.when(jobStatus.getError()).thenReturn(bigQueryError); | ||
|
||
// Mock Successful Query | ||
Mockito.when(queryJob.getQueryResults()).thenReturn(queryResults); | ||
Mockito.when(queryResults.getTotalRows()).thenReturn(1L); | ||
Mockito.when(queryJob.getStatistics()).thenReturn(queryStatistics); | ||
Mockito.when(queryStatistics.getTotalBytesProcessed()).thenReturn(1L); | ||
|
||
// Mock context | ||
Mockito.when(context.getMetrics()).thenReturn(stageMetrics); | ||
Mockito.doNothing().when(stageMetrics).gauge(Mockito.anyString(), Mockito.anyLong()); | ||
Mockito.when(stageMetrics.child(Mockito.any())).thenReturn(metrics); | ||
Mockito.doNothing().when(metrics).countLong(Mockito.anyString(), Mockito.anyLong()); | ||
|
||
} | ||
|
||
@Test(expected = java.lang.RuntimeException.class) | ||
public void testExecuteQueryWithExponentialBackoffFailsWithNonRetryError() { | ||
Mockito.when(bigQueryError.getReason()).thenReturn("accessDenied"); | ||
bq.executeQueryWithExponentialBackoff(bigQuery, queryJobConfiguration, context); | ||
} | ||
|
||
@Test(expected = java.lang.RuntimeException.class) | ||
public void testExecuteQueryWithExponentialBackoffFailsRetryError() { | ||
Mockito.when(bigQueryError.getReason()).thenReturn("jobBackendError"); | ||
bq.executeQueryWithExponentialBackoff(bigQuery, queryJobConfiguration, context); | ||
} | ||
|
||
@Test | ||
public void testExecuteQueryWithExponentialBackoffSuccess() throws InterruptedException { | ||
Mockito.when(jobStatus.getError()).thenReturn(null); | ||
Mockito.when(queryJob.getQueryResults()).thenReturn(queryResults); | ||
bq.executeQueryWithExponentialBackoff(bigQuery, queryJobConfiguration, context); | ||
} | ||
} | ||
|