diff --git a/automl/snippets/pom.xml b/automl/snippets/pom.xml index 6e3a84a34ad..d4ac5632ef4 100644 --- a/automl/snippets/pom.xml +++ b/automl/snippets/pom.xml @@ -42,6 +42,14 @@ com.google.cloud google-cloud-automl + + com.google.cloud + google-cloud-bigquery + + + com.google.cloud + google-cloud-storage + net.sourceforge.argparse4j diff --git a/automl/snippets/src/main/java/com/beta/automl/BatchPredict.java b/automl/snippets/src/main/java/com/beta/automl/BatchPredict.java new file mode 100644 index 00000000000..5b88a03139c --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/BatchPredict.java @@ -0,0 +1,82 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_batch_predict_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.BatchPredictInputConfig; +import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig; +import com.google.cloud.automl.v1beta1.BatchPredictRequest; +import com.google.cloud.automl.v1beta1.BatchPredictResult; +import com.google.cloud.automl.v1beta1.GcsDestination; +import com.google.cloud.automl.v1beta1.GcsSource; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.PredictionServiceClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class BatchPredict { + + static void batchPredict() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl"; + String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/"; + batchPredict(projectId, modelId, inputUri, outputUri); + } + + static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (PredictionServiceClient client = PredictionServiceClient.create()) { + // Get the full path of the model. + ModelName name = ModelName.of(projectId, "us-central1", modelId); + + // Configure the source of the file from a GCS bucket + GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build(); + BatchPredictInputConfig inputConfig = + BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build(); + + // Configure where to store the output in a GCS bucket + GcsDestination gcsDestination = + GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build(); + BatchPredictOutputConfig outputConfig = + BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); + + // Build the request that will be sent to the API + BatchPredictRequest request = + BatchPredictRequest.newBuilder() + .setName(name.toString()) + .setInputConfig(inputConfig) + .setOutputConfig(outputConfig) + .build(); + + // Start an asynchronous request + OperationFuture future = + client.batchPredictAsync(request); + + System.out.println("Waiting for operation to complete..."); + BatchPredictResult response = future.get(); + System.out.println("Batch Prediction results saved to specified Cloud Storage bucket."); + } + } +} +// [END automl_batch_predict_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/CancelOperation.java b/automl/snippets/src/main/java/com/beta/automl/CancelOperation.java new file mode 100644 index 00000000000..557e5fb8787 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/CancelOperation.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_cancel_operation_beta] + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import io.grpc.StatusRuntimeException; +import java.io.IOException; + +class CancelOperation { + + static void cancelOperation() throws IOException, InterruptedException, StatusRuntimeException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String location = "us-central1"; + String operationId = "YOUR_OPERATION_ID"; + String operationFullId = + String.format("projects/%s/locations/%s/operations/%s", projectId, location, operationId); + cancelOperation(operationFullId); + } + + static void cancelOperation(String operationFullId) + throws IOException, InterruptedException, StatusRuntimeException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + client.getOperationsClient().cancelOperation(operationFullId); + System.out.println("Operation cancelled"); + } + } +} +// [END automl_cancel_operation_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/DeleteDataset.java b/automl/snippets/src/main/java/com/beta/automl/DeleteDataset.java new file mode 100644 index 00000000000..1c902df2cfe --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/DeleteDataset.java @@ -0,0 +1,49 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_delete_dataset_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DatasetName; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class DeleteDataset { + + static void deleteDataset() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + deleteDataset(projectId, datasetId); + } + + // Delete a dataset + static void deleteDataset(String projectId, String datasetId) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); + Empty response = client.deleteDatasetAsync(datasetFullId).get(); + System.out.format("Dataset deleted. %s%n", response); + } + } +} +// [END automl_delete_dataset_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/DeleteModel.java b/automl/snippets/src/main/java/com/beta/automl/DeleteModel.java new file mode 100644 index 00000000000..cd1660ba80b --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/DeleteModel.java @@ -0,0 +1,54 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_delete_model_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class DeleteModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + deleteModel(projectId, modelId); + } + + // Delete a model + static void deleteModel(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); + + // Delete a model. + Empty response = client.deleteModelAsync(modelFullId).get(); + + System.out.println("Model deletion started..."); + System.out.println(String.format("Model deleted. %s", response)); + } + } +} +// [END automl_delete_model_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/DeployModel.java b/automl/snippets/src/main/java/com/beta/automl/DeployModel.java new file mode 100644 index 00000000000..0f9c0d38116 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/DeployModel.java @@ -0,0 +1,57 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_deploy_model_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DeployModelRequest; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class DeployModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + deployModel(projectId, modelId); + } + + // Deploy a model for prediction + static void deployModel(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); + DeployModelRequest request = + DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + OperationFuture future = client.deployModelAsync(request); + + future.get(); + System.out.println("Model deployment finished"); + } + } +} +// [END automl_deploy_model_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/GetModel.java b/automl/snippets/src/main/java/com/beta/automl/GetModel.java new file mode 100644 index 00000000000..330bc003b59 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/GetModel.java @@ -0,0 +1,62 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_get_model_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Model; +import com.google.cloud.automl.v1beta1.ModelName; +import io.grpc.StatusRuntimeException; +import java.io.IOException; + +class GetModel { + + static void getModel() throws IOException, StatusRuntimeException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + getModel(projectId, modelId); + } + + // Get a model + static void getModel(String projectId, String modelId) + throws IOException, StatusRuntimeException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); + Model model = client.getModel(modelFullId); + + // Display the model information. + System.out.format("Model name: %s%n", model.getName()); + // To get the model id, you have to parse it out of the `name` field. As models Ids are + // required for other methods. + // Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}` + String[] names = model.getName().split("/"); + String retrievedModelId = names[names.length - 1]; + System.out.format("Model id: %s%n", retrievedModelId); + System.out.format("Model display name: %s%n", model.getDisplayName()); + System.out.println("Model create time:"); + System.out.format("\tseconds: %s%n", model.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s%n", model.getCreateTime().getNanos()); + System.out.format("Model deployment state: %s%n", model.getDeploymentState()); + } + } +} +// [END automl_get_model_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/GetModelEvaluation.java b/automl/snippets/src/main/java/com/beta/automl/GetModelEvaluation.java new file mode 100644 index 00000000000..13393c71431 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/GetModelEvaluation.java @@ -0,0 +1,73 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_video_classification_get_model_evaluation_beta] +// [START automl_video_object_tracking_get_model_evaluation_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ModelEvaluation; +import com.google.cloud.automl.v1beta1.ModelEvaluationName; +import java.io.IOException; + +class GetModelEvaluation { + + static void getModelEvaluation() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String modelEvaluationId = "YOUR_MODEL_EVALUATION_ID"; + getModelEvaluation(projectId, modelId, modelEvaluationId); + } + + // Get a model evaluation + static void getModelEvaluation(String projectId, String modelId, String modelEvaluationId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the model evaluation. + ModelEvaluationName modelEvaluationFullId = + ModelEvaluationName.of(projectId, "us-central1", modelId, modelEvaluationId); + + // Get complete detail of the model evaluation. + ModelEvaluation modelEvaluation = client.getModelEvaluation(modelEvaluationFullId); + + System.out.format("Model Evaluation Name: %s%n", modelEvaluation.getName()); + System.out.format("Model Annotation Spec Id: %s", modelEvaluation.getAnnotationSpecId()); + System.out.println("Create Time:"); + System.out.format("\tseconds: %s%n", modelEvaluation.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s", modelEvaluation.getCreateTime().getNanos() / 1e9); + System.out.format( + "Evalution Example Count: %d%n", modelEvaluation.getEvaluatedExampleCount()); + + // [END automl_video_object_tracking_get_model_evaluation_beta] + System.out.format( + "Classification Model Evaluation Metrics: %s%n", + modelEvaluation.getClassificationEvaluationMetrics()); + // [END automl_video_classification_get_model_evaluation_beta] + + // [START automl_video_object_tracking_get_model_evaluation_beta] + System.out.format( + "Video Object Tracking Evaluation Metrics: %s%n", + modelEvaluation.getVideoObjectTrackingEvaluationMetrics()); + // [START automl_video_classification_get_model_evaluation_beta] + } + } +} +// [END automl_video_classification_get_model_evaluation_beta] +// [END automl_video_object_tracking_get_model_evaluation_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/GetOperationStatus.java b/automl/snippets/src/main/java/com/beta/automl/GetOperationStatus.java new file mode 100644 index 00000000000..711c91e6b26 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/GetOperationStatus.java @@ -0,0 +1,57 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_get_operation_status_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.longrunning.Operation; +import java.io.IOException; + +class GetOperationStatus { + + static void getOperationStatus() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String operationFullId = "projects/[projectId]/locations/us-central1/operations/[operationId]"; + getOperationStatus(operationFullId); + } + + // Get the status of an operation + static void getOperationStatus(String operationFullId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the latest state of a long-running operation. + Operation operation = client.getOperationsClient().getOperation(operationFullId); + + // Display operation details. + System.out.println("Operation details:"); + System.out.format("\tName: %s%n", operation.getName()); + System.out.format("\tMetadata Type Url: %s%n", operation.getMetadata().getTypeUrl()); + System.out.format("\tDone: %s%n", operation.getDone()); + if (operation.hasResponse()) { + System.out.format("\tResponse Type Url: %s%n", operation.getResponse().getTypeUrl()); + } + if (operation.hasError()) { + System.out.println("\tResponse:"); + System.out.format("\t\tError code: %s%n", operation.getError().getCode()); + System.out.format("\t\tError message: %s%n", operation.getError().getMessage()); + } + } + } +} +// [END automl_get_operation_status_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/ImportDataset.java b/automl/snippets/src/main/java/com/beta/automl/ImportDataset.java new file mode 100644 index 00000000000..890ac50eb1a --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/ImportDataset.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_import_dataset_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.api.gax.retrying.RetrySettings; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.AutoMlSettings; +import com.google.cloud.automl.v1beta1.DatasetName; +import com.google.cloud.automl.v1beta1.GcsSource; +import com.google.cloud.automl.v1beta1.InputConfig; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.threeten.bp.Duration; + +class ImportDataset { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String path = "gs://BUCKET_ID/path_to_training_data.csv"; + importDataset(projectId, datasetId, path); + } + + // Import a dataset + static void importDataset(String projectId, String datasetId, String path) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + Duration totalTimeout = Duration.ofMinutes(45); + RetrySettings retrySettings = RetrySettings.newBuilder().setTotalTimeout(totalTimeout).build(); + AutoMlSettings.Builder builder = AutoMlSettings.newBuilder(); + builder.importDataSettings().setRetrySettings(retrySettings).build(); + AutoMlSettings settings = builder.build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create(settings)) { + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); + + // Get multiple Google Cloud Storage URIs to import data from + GcsSource gcsSource = + GcsSource.newBuilder().addAllInputUris(Arrays.asList(path.split(","))).build(); + + // Import data from the input URI + InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); + System.out.println("Processing import..."); + + // Start the import job + OperationFuture operation = client + .importDataAsync(datasetFullId, inputConfig); + + System.out.format("Operation name: %s%n", operation.getName()); + + // If you want to wait for the operation to finish, adjust the timeout appropriately. The + // operation will still run if you choose not to wait for it to complete. You can check the + // status of your operation using the operation's name. + Empty response = operation.get(45, TimeUnit.MINUTES); + System.out.format("Dataset imported. %s%n", response); + } catch (TimeoutException e) { + System.out.println("The operation's polling period was not long enough."); + System.out.println("You can use the Operation's name to get the current status."); + System.out.println("The import job is still running and will complete as expected."); + throw e; + } + } +} +// [END automl_import_dataset_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/ListDatasets.java b/automl/snippets/src/main/java/com/beta/automl/ListDatasets.java new file mode 100644 index 00000000000..cc36b61db3d --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/ListDatasets.java @@ -0,0 +1,102 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_video_classification_list_datasets_beta] +// [START automl_video_object_tracking_list_datasets_beta] +// [START automl_tables_list_datasets_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.AutoMlSettings; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.ListDatasetsRequest; +import com.google.cloud.automl.v1beta1.LocationName; +import java.io.IOException; +import org.threeten.bp.Duration; + +class ListDatasets { + + static void listDatasets() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + listDatasets(projectId); + } + + // List the datasets + static void listDatasets(String projectId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + AutoMlSettings.Builder autoMlSettingsBuilder = AutoMlSettings.newBuilder(); + + autoMlSettingsBuilder + .listDatasetsSettings() + .setRetrySettings( + autoMlSettingsBuilder + .listDatasetsSettings() + .getRetrySettings() + .toBuilder() + .setTotalTimeout(Duration.ofSeconds(15)) + .build()); + AutoMlSettings autoMlSettings = autoMlSettingsBuilder.build(); + + try (AutoMlClient client = AutoMlClient.create(autoMlSettings)) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + ListDatasetsRequest request = + ListDatasetsRequest.newBuilder().setParent(projectLocation.toString()).build(); + + // List all the datasets available in the region by applying filter. + System.out.println("List of datasets:"); + for (Dataset dataset : client.listDatasets(request).iterateAll()) { + // Display the dataset information + System.out.format("%nDataset name: %s%n", dataset.getName()); + // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are + // required for other methods. + // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}` + String[] names = dataset.getName().split("/"); + String retrievedDatasetId = names[names.length - 1]; + System.out.format("Dataset id: %s%n", retrievedDatasetId); + System.out.format("Dataset display name: %s%n", dataset.getDisplayName()); + System.out.println("Dataset create time:"); + System.out.format("\tseconds: %s%n", dataset.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s%n", dataset.getCreateTime().getNanos()); + + // [END automl_video_object_tracking_list_datasets_beta] + // [END automl_tables_list_datasets_beta] + System.out.format( + "Video classification dataset metadata: %s%n", + dataset.getVideoClassificationDatasetMetadata()); + // [END automl_video_classification_list_datasets_beta] + + // [START automl_video_object_tracking_list_datasets_beta] + System.out.format( + "Video object tracking dataset metadata: %s%n", + dataset.getVideoObjectTrackingDatasetMetadata()); + // [END automl_video_object_tracking_list_datasets_beta] + + // [START automl_tables_list_datasets_beta] + System.out.format("Tables dataset metadata: %s%n", dataset.getTablesDatasetMetadata()); + + // [START automl_video_classification_list_datasets_beta] + // [START automl_video_object_tracking_list_datasets_beta] + } + } + } +} +// [END automl_video_classification_list_datasets_beta] +// [END automl_video_object_tracking_list_datasets_beta] +// [END automl_tables_list_datasets_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/ListModelEvaluations.java b/automl/snippets/src/main/java/com/beta/automl/ListModelEvaluations.java new file mode 100644 index 00000000000..e165ec39353 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/ListModelEvaluations.java @@ -0,0 +1,66 @@ +/* + * 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 com.beta.automl; + +// [START automl_tables_list_model_evaluations_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ListModelEvaluationsRequest; +import com.google.cloud.automl.v1beta1.ModelEvaluation; +import com.google.cloud.automl.v1beta1.ModelName; +import java.io.IOException; + +class ListModelEvaluations { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + listModelEvaluations(projectId, modelId); + } + + // List model evaluations + static void listModelEvaluations(String projectId, String modelId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); + ListModelEvaluationsRequest modelEvaluationsrequest = + ListModelEvaluationsRequest.newBuilder().setParent(modelFullId.toString()).build(); + + // List all the model evaluations in the model by applying filter. + System.out.println("List of model evaluations:"); + for (ModelEvaluation modelEvaluation : + client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { + + System.out.format("Model Evaluation Name: %s%n", modelEvaluation.getName()); + System.out.format("Model Annotation Spec Id: %s", modelEvaluation.getAnnotationSpecId()); + System.out.println("Create Time:"); + System.out.format("\tseconds: %s%n", modelEvaluation.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s", modelEvaluation.getCreateTime().getNanos() / 1e9); + System.out.format( + "Evalution Example Count: %d%n", modelEvaluation.getEvaluatedExampleCount()); + + System.out.format( + "Tables Model Evaluation Metrics: %s%n", + modelEvaluation.getClassificationEvaluationMetrics()); + } + } + } +} +// [END automl_tables_list_model_evaluations_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/ListModels.java b/automl/snippets/src/main/java/com/beta/automl/ListModels.java new file mode 100644 index 00000000000..a96472b7204 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/ListModels.java @@ -0,0 +1,86 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_list_models_beta] + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.AutoMlSettings; +import com.google.cloud.automl.v1beta1.ListModelsRequest; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.Model; +import java.io.IOException; +import org.threeten.bp.Duration; + +class ListModels { + + static void listModels() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + listModels(projectId); + } + + // List the models available in the specified location + static void listModels(String projectId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + AutoMlSettings.Builder autoMlSettingsBuilder = AutoMlSettings.newBuilder(); + + autoMlSettingsBuilder + .listModelsSettings() + .setRetrySettings( + autoMlSettingsBuilder + .listModelsSettings() + .getRetrySettings() + .toBuilder() + .setTotalTimeout(Duration.ofSeconds(30)) + .build()); + AutoMlSettings autoMlSettings = autoMlSettingsBuilder.build(); + + try (AutoMlClient client = AutoMlClient.create(autoMlSettings)) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + + // Create list models request. + ListModelsRequest listModelsRequest = + ListModelsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter("") + .build(); + + // List all the models available in the region by applying filter. + System.out.println("List of models:"); + for (Model model : client.listModels(listModelsRequest).iterateAll()) { + // Display the model information. + System.out.format("Model name: %s%n", model.getName()); + // To get the model id, you have to parse it out of the `name` field. As models Ids are + // required for other methods. + // Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}` + String[] names = model.getName().split("/"); + String retrievedModelId = names[names.length - 1]; + System.out.format("Model id: %s%n", retrievedModelId); + System.out.format("Model display name: %s%n", model.getDisplayName()); + System.out.println("Model create time:"); + System.out.format("\tseconds: %s%n", model.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s%n", model.getCreateTime().getNanos()); + System.out.format("Model deployment state: %s%n", model.getDeploymentState()); + } + } + } +} +// [END automl_list_models_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/SetEndpoint.java b/automl/snippets/src/main/java/com/beta/automl/SetEndpoint.java new file mode 100644 index 00000000000..e28e3d55848 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/SetEndpoint.java @@ -0,0 +1,55 @@ +/* + * 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 com.beta.automl; + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.AutoMlSettings; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.ListDatasetsRequest; +import com.google.cloud.automl.v1beta1.LocationName; +import java.io.IOException; + +class SetEndpoint { + + // Change your endpoint + static void setEndpoint(String projectId) throws IOException { + // [START automl_set_endpoint] + AutoMlSettings settings = + AutoMlSettings.newBuilder().setEndpoint("eu-automl.googleapis.com:443").build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + AutoMlClient client = AutoMlClient.create(settings); + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "eu"); + // [END automl_set_endpoint] + + ListDatasetsRequest request = + ListDatasetsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter("translation_dataset_metadata:*") + .build(); + // List all the datasets available + System.out.println("List of datasets:"); + for (Dataset dataset : client.listDatasets(request).iterateAll()) { + System.out.println(dataset); + } + client.close(); + } +} diff --git a/automl/snippets/src/main/java/com/beta/automl/TablesBatchPredictBigQuery.java b/automl/snippets/src/main/java/com/beta/automl/TablesBatchPredictBigQuery.java new file mode 100644 index 00000000000..9863beafd42 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/TablesBatchPredictBigQuery.java @@ -0,0 +1,82 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_tables_batch_predict_bigquery_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.BatchPredictInputConfig; +import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig; +import com.google.cloud.automl.v1beta1.BatchPredictRequest; +import com.google.cloud.automl.v1beta1.BatchPredictResult; +import com.google.cloud.automl.v1beta1.BigQueryDestination; +import com.google.cloud.automl.v1beta1.BigQuerySource; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.PredictionServiceClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class TablesBatchPredictBigQuery { + + static void batchPredict() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String inputUri = "bq://YOUR_PROJECT_ID.bqDatasetID.bqTableId"; + String outputUri = "bq://YOUR_PROJECT_ID"; + batchPredict(projectId, modelId, inputUri, outputUri); + } + + static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (PredictionServiceClient client = PredictionServiceClient.create()) { + // Get the full path of the model. + ModelName name = ModelName.of(projectId, "us-central1", modelId); + + // Configure the source of the file from BigQuery + BigQuerySource bigQuerySource = BigQuerySource.newBuilder().setInputUri(inputUri).build(); + BatchPredictInputConfig inputConfig = + BatchPredictInputConfig.newBuilder().setBigquerySource(bigQuerySource).build(); + + // Configure where to store the output in BigQuery + BigQueryDestination bigQueryDestination = + BigQueryDestination.newBuilder().setOutputUri(outputUri).build(); + BatchPredictOutputConfig outputConfig = + BatchPredictOutputConfig.newBuilder().setBigqueryDestination(bigQueryDestination).build(); + + // Build the request that will be sent to the API + BatchPredictRequest request = + BatchPredictRequest.newBuilder() + .setName(name.toString()) + .setInputConfig(inputConfig) + .setOutputConfig(outputConfig) + .build(); + + // Start an asynchronous request + OperationFuture future = + client.batchPredictAsync(request); + + System.out.println("Waiting for operation to complete..."); + BatchPredictResult response = future.get(); + System.out.println("Batch Prediction results saved to BigQuery."); + } + } +} +// [END automl_tables_batch_predict_bigquery_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/TablesCreateDataset.java b/automl/snippets/src/main/java/com/beta/automl/TablesCreateDataset.java new file mode 100644 index 00000000000..e48e4ab710d --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/TablesCreateDataset.java @@ -0,0 +1,63 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_tables_create_dataset_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.TablesDatasetMetadata; +import java.io.IOException; + +class TablesCreateDataset { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + TablesDatasetMetadata metadata = TablesDatasetMetadata.newBuilder().build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setTablesDatasetMetadata(metadata) + .build(); + + Dataset createdDataset = client.createDataset(projectLocation, dataset); + + // Display the dataset information. + System.out.format("Dataset name: %s%n", createdDataset.getName()); + // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are + // required for other methods. + // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}` + String[] names = createdDataset.getName().split("/"); + String datasetId = names[names.length - 1]; + System.out.format("Dataset id: %s%n", datasetId); + } + } +} +// [END automl_tables_create_dataset_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/TablesCreateModel.java b/automl/snippets/src/main/java/com/beta/automl/TablesCreateModel.java new file mode 100644 index 00000000000..34cbca52bd9 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/TablesCreateModel.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_tables_create_model_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ColumnSpec; +import com.google.cloud.automl.v1beta1.ColumnSpecName; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.Model; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.TablesModelMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class TablesCreateModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String tableSpecId = "YOUR_TABLE_SPEC_ID"; + String columnSpecId = "YOUR_COLUMN_SPEC_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, tableSpecId, columnSpecId, displayName); + } + + // Create a model + static void createModel( + String projectId, + String datasetId, + String tableSpecId, + String columnSpecId, + String displayName) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + + // Get the complete path of the column. + ColumnSpecName columnSpecName = + ColumnSpecName.of(projectId, "us-central1", datasetId, tableSpecId, columnSpecId); + + // Build the get column spec. + ColumnSpec targetColumnSpec = + ColumnSpec.newBuilder().setName(columnSpecName.toString()).build(); + + // Set model metadata. + TablesModelMetadata metadata = + TablesModelMetadata.newBuilder() + .setTargetColumnSpec(targetColumnSpec) + .setTrainBudgetMilliNodeHours(24000) + .build(); + + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setTablesModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s%n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_tables_create_model_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/TablesGetModel.java b/automl/snippets/src/main/java/com/beta/automl/TablesGetModel.java new file mode 100644 index 00000000000..ea0cdd8c8eb --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/TablesGetModel.java @@ -0,0 +1,83 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_tables_get_model] + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Model; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.TablesModelColumnInfo; +import io.grpc.StatusRuntimeException; +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +public class TablesGetModel { + + public static void main(String[] args) throws IOException, StatusRuntimeException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String region = "YOUR_REGION"; + String modelId = "YOUR_MODEL_ID"; + getModel(projectId, region, modelId); + } + + // Demonstrates using the AutoML client to get model details. + public static void getModel(String projectId, String computeRegion, String modelId) + throws IOException, StatusRuntimeException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); + + // Get complete detail of the model. + Model model = client.getModel(modelFullId); + + // Display the model information. + System.out.format("Model name: %s%n", model.getName()); + System.out.format( + "Model Id: %s\n", model.getName().split("/")[model.getName().split("/").length - 1]); + System.out.format("Model display name: %s%n", model.getDisplayName()); + System.out.format("Dataset Id: %s%n", model.getDatasetId()); + System.out.println("Tables Model Metadata: "); + System.out.format( + "\tTraining budget: %s%n", model.getTablesModelMetadata().getTrainBudgetMilliNodeHours()); + System.out.format( + "\tTraining cost: %s%n", model.getTablesModelMetadata().getTrainBudgetMilliNodeHours()); + + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + String createTime = + dateFormat.format(new java.util.Date(model.getCreateTime().getSeconds() * 1000)); + System.out.format("Model create time: %s%n", createTime); + + System.out.format("Model deployment state: %s%n", model.getDeploymentState()); + + // Get features of top importance + for (TablesModelColumnInfo info : + model.getTablesModelMetadata().getTablesModelColumnInfoList()) { + System.out.format( + "Column: %s - Importance: %.2f%n", + info.getColumnDisplayName(), info.getFeatureImportance()); + } + } + } +} +// [END automl_tables_get_model] diff --git a/automl/snippets/src/main/java/com/beta/automl/TablesImportDataset.java b/automl/snippets/src/main/java/com/beta/automl/TablesImportDataset.java new file mode 100644 index 00000000000..ee2d9098168 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/TablesImportDataset.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_tables_import_dataset_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.BigQuerySource; +import com.google.cloud.automl.v1beta1.DatasetName; +import com.google.cloud.automl.v1beta1.GcsSource; +import com.google.cloud.automl.v1beta1.InputConfig; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; + +class TablesImportDataset { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String path = "gs://BUCKET_ID/path/to//data.csv or bq://project_id.dataset_id.table_id"; + importDataset(projectId, datasetId, path); + } + + // Import a dataset via BigQuery or Google Cloud Storage + static void importDataset(String projectId, String datasetId, String path) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); + + InputConfig.Builder inputConfigBuilder = InputConfig.newBuilder(); + + // Determine which source type was used for the input path (BigQuery or GCS) + if (path.startsWith("bq")) { + // Get training data file to be imported from a BigQuery source. + BigQuerySource.Builder bigQuerySource = BigQuerySource.newBuilder(); + bigQuerySource.setInputUri(path); + inputConfigBuilder.setBigquerySource(bigQuerySource); + } else { + // Get multiple Google Cloud Storage URIs to import data from + GcsSource gcsSource = + GcsSource.newBuilder().addAllInputUris(Arrays.asList(path.split(","))).build(); + inputConfigBuilder.setGcsSource(gcsSource); + } + + // Import data from the input URI + System.out.println("Processing import..."); + + Empty response = client.importDataAsync(datasetFullId, inputConfigBuilder.build()).get(); + System.out.format("Dataset imported. %s%n", response); + } + } +} +// [END automl_tables_import_dataset_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/TablesPredict.java b/automl/snippets/src/main/java/com/beta/automl/TablesPredict.java new file mode 100644 index 00000000000..8e4b24f3919 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/TablesPredict.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_tables_predict_beta] +import com.google.cloud.automl.v1beta1.AnnotationPayload; +import com.google.cloud.automl.v1beta1.ExamplePayload; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.PredictRequest; +import com.google.cloud.automl.v1beta1.PredictResponse; +import com.google.cloud.automl.v1beta1.PredictionServiceClient; +import com.google.cloud.automl.v1beta1.Row; +import com.google.cloud.automl.v1beta1.TablesAnnotation; +import com.google.protobuf.Value; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +class TablesPredict { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + // Values should match the input expected by your model. + List values = new ArrayList<>(); + // values.add(Value.newBuilder().setBoolValue(true).build()); + // values.add(Value.newBuilder().setNumberValue(10).build()); + // values.add(Value.newBuilder().setStringValue("YOUR_STRING").build()); + predict(projectId, modelId, values); + } + + static void predict(String projectId, String modelId, List values) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (PredictionServiceClient client = PredictionServiceClient.create()) { + // Get the full path of the model. + ModelName name = ModelName.of(projectId, "us-central1", modelId); + Row row = Row.newBuilder().addAllValues(values).build(); + ExamplePayload payload = ExamplePayload.newBuilder().setRow(row).build(); + + // Feature importance gives you visibility into how the features in a specific prediction + // request informed the resulting prediction. For more info, see: + // https://cloud.google.com/automl-tables/docs/features#local + PredictRequest request = + PredictRequest.newBuilder() + .setName(name.toString()) + .setPayload(payload) + .putParams("feature_importance", "true") + .build(); + + PredictResponse response = client.predict(request); + + System.out.println("Prediction results:"); + for (AnnotationPayload annotationPayload : response.getPayloadList()) { + TablesAnnotation tablesAnnotation = annotationPayload.getTables(); + System.out.format( + "Classification label: %s%n", tablesAnnotation.getValue().getStringValue()); + System.out.format("Classification score: %.3f%n", tablesAnnotation.getScore()); + // Get features of top importance + tablesAnnotation + .getTablesModelColumnInfoList() + .forEach( + info -> + System.out.format( + "\tColumn: %s - Importance: %.2f%n", + info.getColumnDisplayName(), info.getFeatureImportance())); + } + } + } +} +// [END automl_tables_predict_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/UndeployModel.java b/automl/snippets/src/main/java/com/beta/automl/UndeployModel.java new file mode 100644 index 00000000000..29722fe44f6 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/UndeployModel.java @@ -0,0 +1,56 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_undeploy_model_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.UndeployModelRequest; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class UndeployModel { + + static void undeployModel() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + undeployModel(projectId, modelId); + } + + // Undeploy a model from prediction + static void undeployModel(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); + UndeployModelRequest request = + UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + OperationFuture future = client.undeployModelAsync(request); + + future.get(); + System.out.println("Model undeployment finished"); + } + } +} +// [END automl_undeploy_model_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/VideoClassificationCreateDataset.java b/automl/snippets/src/main/java/com/beta/automl/VideoClassificationCreateDataset.java new file mode 100644 index 00000000000..ab0064625f6 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/VideoClassificationCreateDataset.java @@ -0,0 +1,64 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_video_classification_create_dataset_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.VideoClassificationDatasetMetadata; +import java.io.IOException; + +class VideoClassificationCreateDataset { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + VideoClassificationDatasetMetadata metadata = + VideoClassificationDatasetMetadata.newBuilder().build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setVideoClassificationDatasetMetadata(metadata) + .build(); + + Dataset createdDataset = client.createDataset(projectLocation, dataset); + + // Display the dataset information. + System.out.format("Dataset name: %s%n", createdDataset.getName()); + // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are + // required for other methods. + // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}` + String[] names = createdDataset.getName().split("/"); + String datasetId = names[names.length - 1]; + System.out.format("Dataset id: %s%n", datasetId); + } + } +} +// [END automl_video_classification_create_dataset_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/VideoClassificationCreateModel.java b/automl/snippets/src/main/java/com/beta/automl/VideoClassificationCreateModel.java new file mode 100644 index 00000000000..892bbd6deed --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/VideoClassificationCreateModel.java @@ -0,0 +1,70 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_video_classification_create_model_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.Model; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.VideoClassificationModelMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class VideoClassificationCreateModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, displayName); + } + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + // Set model metadata. + VideoClassificationModelMetadata metadata = + VideoClassificationModelMetadata.newBuilder().build(); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setVideoClassificationModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s%n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_video_classification_create_model_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/VideoObjectTrackingCreateDataset.java b/automl/snippets/src/main/java/com/beta/automl/VideoObjectTrackingCreateDataset.java new file mode 100644 index 00000000000..f92e6581419 --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/VideoObjectTrackingCreateDataset.java @@ -0,0 +1,64 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_video_object_tracking_create_dataset_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.VideoObjectTrackingDatasetMetadata; +import java.io.IOException; + +class VideoObjectTrackingCreateDataset { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + VideoObjectTrackingDatasetMetadata metadata = + VideoObjectTrackingDatasetMetadata.newBuilder().build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setVideoObjectTrackingDatasetMetadata(metadata) + .build(); + + Dataset createdDataset = client.createDataset(projectLocation, dataset); + + // Display the dataset information. + System.out.format("Dataset name: %s%n", createdDataset.getName()); + // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are + // required for other methods. + // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}` + String[] names = createdDataset.getName().split("/"); + String datasetId = names[names.length - 1]; + System.out.format("Dataset id: %s%n", datasetId); + } + } +} +// [END automl_video_object_tracking_create_dataset_beta] diff --git a/automl/snippets/src/main/java/com/beta/automl/VideoObjectTrackingCreateModel.java b/automl/snippets/src/main/java/com/beta/automl/VideoObjectTrackingCreateModel.java new file mode 100644 index 00000000000..ec2315144bc --- /dev/null +++ b/automl/snippets/src/main/java/com/beta/automl/VideoObjectTrackingCreateModel.java @@ -0,0 +1,70 @@ +/* + * Copyright 2020 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.beta.automl; + +// [START automl_video_object_tracking_create_model_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.Model; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.VideoObjectTrackingModelMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class VideoObjectTrackingCreateModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, displayName); + } + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + // Set model metadata. + VideoObjectTrackingModelMetadata metadata = + VideoObjectTrackingModelMetadata.newBuilder().build(); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setVideoObjectTrackingModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s%n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_video_object_tracking_create_model_beta] diff --git a/automl/snippets/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.java b/automl/snippets/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.java index e45021c65ba..04af77592e5 100644 --- a/automl/snippets/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.java +++ b/automl/snippets/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.java @@ -46,6 +46,7 @@ static void createModel(String projectId, String datasetId, String displayName) // A resource that represents Google Cloud Platform location. LocationName projectLocation = LocationName.of(projectId, "us-central1"); // Set model metadata. + System.out.println(datasetId); TextSentimentModelMetadata metadata = TextSentimentModelMetadata.newBuilder().build(); Model model = Model.newBuilder() diff --git a/automl/snippets/src/test/java/com/beta/automl/BatchPredictTest.java b/automl/snippets/src/test/java/com/beta/automl/BatchPredictTest.java new file mode 100644 index 00000000000..f9d3c840708 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/BatchPredictTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class BatchPredictTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String MODEL_ID = "VCN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testBatchPredict() { + // As batch prediction can take a long time. Try to batch predict on a model and confirm that + // the model was not found, but other elements of the request were valid. + try { + String inputUri = String.format("gs://%s/entity-extraction/input.csv", BUCKET_ID); + String outputUri = String.format("gs://%s/TEST_BATCH_PREDICT/", BUCKET_ID); + BatchPredict.batchPredict(PROJECT_ID, MODEL_ID, inputUri, outputUri); + String got = bout.toString(); + assertThat(got).contains("does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("does not exist"); + } + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/CancelOperationTest.java b/automl/snippets/src/test/java/com/beta/automl/CancelOperationTest.java new file mode 100644 index 00000000000..a44366aea16 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/CancelOperationTest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import io.grpc.StatusRuntimeException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +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; + +@RunWith(JUnit4.class) +public class CancelOperationTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testCancelOperation() throws IOException { + String operationFullPathId = + String.format( + "projects/%s/locations/%s/operations/%s", PROJECT_ID, "us-central1", + "TCN0000000000"); + // Any cancelled operation on models or datasets will be hidden once the operations are flagged + // as failed operations + // which makes them hard to delete in the teardown. + try { + CancelOperation.cancelOperation(operationFullPathId); + String got = bout.toString(); + assertThat(got).contains("not found"); + } catch (NotFoundException | StatusRuntimeException | InterruptedException e) { + assertThat(e.getMessage()).contains("not found"); + } + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/DeleteDatasetTest.java b/automl/snippets/src/test/java/com/beta/automl/DeleteDatasetTest.java new file mode 100644 index 00000000000..8f446bc1aca --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/DeleteDatasetTest.java @@ -0,0 +1,102 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.TextExtractionDatasetMetadata; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DeleteDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() throws IOException { + // Create a fake dataset to be deleted + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + try (AutoMlClient client = AutoMlClient.create()) { + LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1"); + TextExtractionDatasetMetadata metadata = TextExtractionDatasetMetadata.newBuilder().build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(datasetName) + .setTextExtractionDatasetMetadata(metadata) + .build(); + Dataset createdDataset = client.createDataset(projectLocation, dataset); + String[] names = createdDataset.getName().split("/"); + datasetId = names[names.length - 1]; + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeleteDataset() throws IOException, ExecutionException, InterruptedException { + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + String got = bout.toString(); + assertThat(got).contains("Dataset deleted."); + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/DeleteModelTest.java b/automl/snippets/src/test/java/com/beta/automl/DeleteModelTest.java new file mode 100644 index 00000000000..a56d9822992 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/DeleteModelTest.java @@ -0,0 +1,83 @@ +/* + * 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 com.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +public class DeleteModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeleteModel() { + // As model creation can take many hours, instead try to delete a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + DeleteModel.deleteModel(PROJECT_ID, "TRL0000000000000000000"); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/DeployModelTest.java b/automl/snippets/src/test/java/com/beta/automl/DeployModelTest.java new file mode 100644 index 00000000000..5295c02395e --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/DeployModelTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +public class DeployModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = "TEN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeployModel() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + DeployModel.deployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/GetModelEvaluationTest.java b/automl/snippets/src/test/java/com/beta/automl/GetModelEvaluationTest.java new file mode 100644 index 00000000000..57b81be75ba --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/GetModelEvaluationTest.java @@ -0,0 +1,97 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.ListModelEvaluationsRequest; +import com.google.cloud.automl.v1.ModelEvaluation; +import com.google.cloud.automl.v1.ModelName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +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; + +@RunWith(JUnit4.class) +public class GetModelEvaluationTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); + private String modelEvaluationId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() throws IOException { + // Get a model evaluation ID from the List request first to be used in the Get call + try (AutoMlClient client = AutoMlClient.create()) { + ModelName modelFullId = ModelName.of(PROJECT_ID, "us-central1", MODEL_ID); + ListModelEvaluationsRequest modelEvaluationsrequest = + ListModelEvaluationsRequest.newBuilder().setParent(modelFullId.toString()).build(); + ModelEvaluation modelEvaluation = + client + .listModelEvaluations(modelEvaluationsrequest) + .getPage() + .getValues() + .iterator() + .next(); + modelEvaluationId = modelEvaluation.getName().split("/modelEvaluations/")[1]; + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetModelEvaluation() throws IOException { + GetModelEvaluation.getModelEvaluation(PROJECT_ID, MODEL_ID, modelEvaluationId); + String got = bout.toString(); + assertThat(got).contains("Model Evaluation Name:"); + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/GetModelTest.java b/automl/snippets/src/test/java/com/beta/automl/GetModelTest.java new file mode 100644 index 00000000000..1ed269c3fa5 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/GetModelTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +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; + +@RunWith(JUnit4.class) +public class GetModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetModel() throws IOException { + GetModel.getModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model id: " + MODEL_ID); + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/GetOperationStatusTest.java b/automl/snippets/src/test/java/com/beta/automl/GetOperationStatusTest.java new file mode 100644 index 00000000000..508bab09fae --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/GetOperationStatusTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.longrunning.ListOperationsRequest; +import com.google.longrunning.Operation; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +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; + +@RunWith(JUnit4.class) +public class GetOperationStatusTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private String operationId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() throws IOException { + // Use list operations to get a single operation id for the get call. + try (AutoMlClient client = AutoMlClient.create()) { + LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1"); + ListOperationsRequest request = + ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build(); + Operation operation = + client.getOperationsClient().listOperations(request).iterateAll().iterator().next(); + operationId = operation.getName(); + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetOperationStatus() throws IOException { + GetOperationStatus.getOperationStatus(operationId); + String got = bout.toString(); + assertThat(got).contains("Operation details:"); + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/ImportDatasetTest.java b/automl/snippets/src/test/java/com/beta/automl/ImportDatasetTest.java new file mode 100644 index 00000000000..f608b7c2a64 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/ImportDatasetTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ImportDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String BUCKET = "gs://" + BUCKET_ID; + private String datasetId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testImportDataset() + throws TimeoutException { + try { + ImportDataset.importDataset( + PROJECT_ID, "TCN0000000000", BUCKET + "/entity-extraction/dataset.csv"); + String got = bout.toString(); + assertThat(got).contains("doesn't exist"); + } catch (NotFoundException | IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("doesn't exist"); + } + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/ListDatasetsTest.java b/automl/snippets/src/test/java/com/beta/automl/ListDatasetsTest.java new file mode 100644 index 00000000000..00daf10d656 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/ListDatasetsTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ListDatasetsTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testListDataset() throws IOException { + ListDatasets.listDatasets(PROJECT_ID); + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/ListModelEvaluationsTest.java b/automl/snippets/src/test/java/com/beta/automl/ListModelEvaluationsTest.java new file mode 100644 index 00000000000..13541ffe81d --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/ListModelEvaluationsTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +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; + +@RunWith(JUnit4.class) +public class ListModelEvaluationsTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testListModelEvaluations() throws IOException { + ListModelEvaluations.listModelEvaluations(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model Evaluation Name:"); + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/ListModelsTest.java b/automl/snippets/src/test/java/com/beta/automl/ListModelsTest.java new file mode 100644 index 00000000000..b995fcb9c2a --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/ListModelsTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListModelsTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + // Skipping this test until backend is cleaned up. + // https://github.com/googleapis/java-automl/issues/291 + @Test + @Ignore + public void testListModels() throws IOException { + ListModels.listModels(PROJECT_ID); + String got = bout.toString(); + assertThat(got).contains("Model id:"); + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/SetEndpointIT.java b/automl/snippets/src/test/java/com/beta/automl/SetEndpointIT.java new file mode 100644 index 00000000000..4ad7601c01e --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/SetEndpointIT.java @@ -0,0 +1,82 @@ +/* + * 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 com.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +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; + +/** + * Tests for Automl Set Endpoint + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class SetEndpointIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testSetEndpoint() throws IOException { + // Act + SetEndpoint.setEndpoint(PROJECT_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains("display_name: \"do_not_delete_eu\""); + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/TablesBatchPredictBigQueryTest.java b/automl/snippets/src/test/java/com/beta/automl/TablesBatchPredictBigQueryTest.java new file mode 100644 index 00000000000..c99e9bef065 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/TablesBatchPredictBigQueryTest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TablesBatchPredictBigQueryTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "TBL0000000000000000000"; + private static final String INPUT_URI = + String.format( + "bq://%s.automl_do_not_delete_predict_test.automl_predict_test_table", + PROJECT_ID); + private static final String OUTPUT_URI = "bq://" + PROJECT_ID; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testTablesBigQueryBatchPredict() { + // As batch prediction can take a long time. Try to batch predict on a model and confirm that + // the model was not found, but other elements of the request were valid. + try { + TablesBatchPredictBigQuery.batchPredict(PROJECT_ID, MODEL_ID, INPUT_URI, OUTPUT_URI); + String got = bout.toString(); + assertThat(got).contains("does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("does not exist"); + } + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/TablesCreateDatasetTest.java b/automl/snippets/src/test/java/com/beta/automl/TablesCreateDatasetTest.java new file mode 100644 index 00000000000..5afacee402c --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/TablesCreateDatasetTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TablesCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + System.setOut(originalPrintStream); + } + + @Test + public void testTablesCreateDataset() + throws IOException, ExecutionException, InterruptedException { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + TablesCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/TablesCreateModelTest.java b/automl/snippets/src/test/java/com/beta/automl/TablesCreateModelTest.java new file mode 100644 index 00000000000..6d514a4172b --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/TablesCreateModelTest.java @@ -0,0 +1,93 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TablesCreateModelTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String DATASET_ID = "TBL00000000000000000000"; + private static final String TABLE_SPEC_ID = "3172574831249981440"; + private static final String COLUMN_SPEC_ID = "3224682886313541632"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String operationId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testTablesCreateModel() throws IOException, ExecutionException, InterruptedException { + try { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String modelName = + String.format("test_%s", + UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + TablesCreateModel + .createModel(PROJECT_ID, DATASET_ID, TABLE_SPEC_ID, COLUMN_SPEC_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/TablesGetModelTest.java b/automl/snippets/src/test/java/com/beta/automl/TablesGetModelTest.java new file mode 100644 index 00000000000..c1055e23759 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/TablesGetModelTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +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; + +@RunWith(JUnit4.class) +public class TablesGetModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = "TBL7473655411900416000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("TABLE_MODEL_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetModel() throws IOException { + GetModel.getModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model id: " + MODEL_ID); + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/TablesImportDatasetTest.java b/automl/snippets/src/test/java/com/beta/automl/TablesImportDatasetTest.java new file mode 100644 index 00000000000..ac96c7a17a5 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/TablesImportDatasetTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TablesImportDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testTablesImportDataset() { + try { + TablesImportDataset.importDataset( + PROJECT_ID, "TEN0000000000000000000", "gs://cloud-ml-tables-data/bank-marketing.csv"); + String got = bout.toString(); + assertThat(got).contains("The Dataset doesn't exist or is inaccessible for use with AutoMl."); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()) + .contains("The Dataset doesn't exist or is inaccessible for use with AutoMl."); + } + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/TablesPredictTest.java b/automl/snippets/src/test/java/com/beta/automl/TablesPredictTest.java new file mode 100644 index 00000000000..91075b1f4e2 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/TablesPredictTest.java @@ -0,0 +1,116 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.Model; +import com.google.cloud.automl.v1.ModelName; +import com.google.protobuf.Value; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TablesPredictTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "TBL7972827093840953344"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() throws IOException, ExecutionException, InterruptedException { + // Verify that the model is deployed for prediction + try (AutoMlClient client = AutoMlClient.create()) { + ModelName modelFullId = ModelName.of(PROJECT_ID, "us-central1", MODEL_ID); + Model model = client.getModel(modelFullId); + if (model.getDeploymentState() == Model.DeploymentState.UNDEPLOYED) { + // Deploy the model if not deployed + DeployModelRequest request = + DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + client.deployModelAsync(request).get(); + } + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testTablesPredict() throws IOException { + List values = new ArrayList<>(); + values.add(Value.newBuilder().setNumberValue(39).build()); // Age + values.add(Value.newBuilder().setStringValue("technician").build()); // Job + values.add(Value.newBuilder().setStringValue("married").build()); // MaritalStatus + values.add(Value.newBuilder().setStringValue("secondary").build()); // Education + values.add(Value.newBuilder().setStringValue("no").build()); // Default + values.add(Value.newBuilder().setNumberValue(52).build()); // Balance + values.add(Value.newBuilder().setStringValue("no").build()); // Housing + values.add(Value.newBuilder().setStringValue("no").build()); // Loan + values.add(Value.newBuilder().setStringValue("cellular").build()); // Contact + values.add(Value.newBuilder().setNumberValue(12).build()); // Day + values.add(Value.newBuilder().setStringValue("aug").build()); // Month + values.add(Value.newBuilder().setNumberValue(96).build()); // Duration + values.add(Value.newBuilder().setNumberValue(2).build()); // Campaign + values.add(Value.newBuilder().setNumberValue(-1).build()); // PDays + values.add(Value.newBuilder().setNumberValue(0).build()); // Previous + values.add(Value.newBuilder().setStringValue("unknown").build()); // POutcome + + TablesPredict.predict(PROJECT_ID, MODEL_ID, values); + + String got = bout.toString(); + assertThat(got).contains("Prediction results:"); + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/UndeployModelTest.java b/automl/snippets/src/test/java/com/beta/automl/UndeployModelTest.java new file mode 100644 index 00000000000..21ecb2266b4 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/UndeployModelTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +public class UndeployModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = "TEN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testUndeployModel() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + UndeployModel.undeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/VideoClassificationCreateDatasetTest.java b/automl/snippets/src/test/java/com/beta/automl/VideoClassificationCreateDatasetTest.java new file mode 100644 index 00000000000..72e7cb963e8 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/VideoClassificationCreateDatasetTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DatasetName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VideoClassificationCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the dataset. + DatasetName datasetFullId = DatasetName.of(PROJECT_ID, "us-central1", datasetId); + client.deleteDatasetAsync(datasetFullId).get(); + } + System.setOut(originalPrintStream); + } + + @Test + public void testCreateDataset() throws IOException, ExecutionException, InterruptedException { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + VideoClassificationCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/VideoClassificationCreateModelTest.java b/automl/snippets/src/test/java/com/beta/automl/VideoClassificationCreateModelTest.java new file mode 100644 index 00000000000..b5382a2d117 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/VideoClassificationCreateModelTest.java @@ -0,0 +1,91 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VideoClassificationCreateModelTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String DATASET_ID = "VCN00000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String operationId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testVisionClassificationCreateModel() + throws IOException, ExecutionException, InterruptedException { + try { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String modelName = + String.format("test_%s", + UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + VideoClassificationCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/VideoObjectTrackingCreateDatasetTest.java b/automl/snippets/src/test/java/com/beta/automl/VideoObjectTrackingCreateDatasetTest.java new file mode 100644 index 00000000000..05a0cc45895 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/VideoObjectTrackingCreateDatasetTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DatasetName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VideoObjectTrackingCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the dataset. + DatasetName datasetFullId = DatasetName.of(PROJECT_ID, "us-central1", datasetId); + client.deleteDatasetAsync(datasetFullId).get(); + } + System.setOut(originalPrintStream); + } + + @Test + public void testCreateDataset() throws IOException, ExecutionException, InterruptedException { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + VideoObjectTrackingCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/snippets/src/test/java/com/beta/automl/VideoObjectTrackingCreateModelTest.java b/automl/snippets/src/test/java/com/beta/automl/VideoObjectTrackingCreateModelTest.java new file mode 100644 index 00000000000..9c47bc83322 --- /dev/null +++ b/automl/snippets/src/test/java/com/beta/automl/VideoObjectTrackingCreateModelTest.java @@ -0,0 +1,93 @@ +/* + * Copyright 2020 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.beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +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; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VideoObjectTrackingCreateModelTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String DATASET_ID = "VOT0000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String operationId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testVisionClassificationCreateModel() + throws IOException, ExecutionException, InterruptedException { + + try { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String modelName = + String.format("test_%s", + UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + VideoObjectTrackingCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/snippets/src/test/java/com/example/automl/BatchPredictTest.java b/automl/snippets/src/test/java/com/example/automl/BatchPredictTest.java index d16c69363f7..29dfa3305d6 100644 --- a/automl/snippets/src/test/java/com/example/automl/BatchPredictTest.java +++ b/automl/snippets/src/test/java/com/example/automl/BatchPredictTest.java @@ -38,6 +38,7 @@ public class BatchPredictTest { private static final String MODEL_ID = "TEN0000000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -56,12 +57,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/DeleteDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/DeleteDatasetTest.java index 75777ae7500..fd4f96171dd 100644 --- a/automl/snippets/src/test/java/com/example/automl/DeleteDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/DeleteDatasetTest.java @@ -38,6 +38,7 @@ public class DeleteDatasetTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private String datasetId; private static void requireEnvVar(String varName) { @@ -56,6 +57,7 @@ public static void checkRequirements() { public void setUp() throws InterruptedException, ExecutionException, IOException { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); // Create a fake dataset to be deleted @@ -70,12 +72,15 @@ public void setUp() throws InterruptedException, ExecutionException, IOException bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/DeleteModelTest.java b/automl/snippets/src/test/java/com/example/automl/DeleteModelTest.java index a0799721cfe..96b5c59fff3 100644 --- a/automl/snippets/src/test/java/com/example/automl/DeleteModelTest.java +++ b/automl/snippets/src/test/java/com/example/automl/DeleteModelTest.java @@ -35,6 +35,7 @@ public class DeleteModelTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -52,12 +53,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/DeployModelTest.java b/automl/snippets/src/test/java/com/example/automl/DeployModelTest.java index 1fa9e163c6d..5ad7f560778 100644 --- a/automl/snippets/src/test/java/com/example/automl/DeployModelTest.java +++ b/automl/snippets/src/test/java/com/example/automl/DeployModelTest.java @@ -36,6 +36,7 @@ public class DeployModelTest { private static final String MODEL_ID = "TEN0000000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -53,12 +54,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/ExportDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/ExportDatasetTest.java index afd53f5a57e..6dc3a7ed65e 100644 --- a/automl/snippets/src/test/java/com/example/automl/ExportDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/ExportDatasetTest.java @@ -40,6 +40,7 @@ public class ExportDatasetTest { private static final String BUCKET = "gs://" + BUCKET_ID; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -58,12 +59,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/GetDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/GetDatasetTest.java index d2e8974891e..50629da4f48 100644 --- a/automl/snippets/src/test/java/com/example/automl/GetDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/GetDatasetTest.java @@ -37,6 +37,7 @@ public class GetDatasetTest { private static final String DATASET_ID = System.getenv("ENTITY_EXTRACTION_DATASET_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -55,12 +56,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/GetModelEvaluationTest.java b/automl/snippets/src/test/java/com/example/automl/GetModelEvaluationTest.java index 60025612a6e..74e4f16781d 100644 --- a/automl/snippets/src/test/java/com/example/automl/GetModelEvaluationTest.java +++ b/automl/snippets/src/test/java/com/example/automl/GetModelEvaluationTest.java @@ -36,6 +36,7 @@ public class GetModelEvaluationTest { private String modelEvaluationId; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -54,6 +55,7 @@ public static void checkRequirements() { public void setUp() throws IOException { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); // Get a model evaluation ID from the List request first to be used in the Get call @@ -64,12 +66,15 @@ public void setUp() throws IOException { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/GetModelTest.java b/automl/snippets/src/test/java/com/example/automl/GetModelTest.java index de216b19047..dc375d88ad3 100644 --- a/automl/snippets/src/test/java/com/example/automl/GetModelTest.java +++ b/automl/snippets/src/test/java/com/example/automl/GetModelTest.java @@ -35,6 +35,7 @@ public class GetModelTest { private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -53,12 +54,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/GetOperationStatusTest.java b/automl/snippets/src/test/java/com/example/automl/GetOperationStatusTest.java index 561aca51e85..bc233a9a602 100644 --- a/automl/snippets/src/test/java/com/example/automl/GetOperationStatusTest.java +++ b/automl/snippets/src/test/java/com/example/automl/GetOperationStatusTest.java @@ -35,6 +35,7 @@ public class GetOperationStatusTest { private String operationId; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -52,6 +53,7 @@ public static void checkRequirements() { public void setUp() throws IOException { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); ListOperationStatus.listOperationStatus(PROJECT_ID); @@ -61,12 +63,15 @@ public void setUp() throws IOException { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/ImportDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/ImportDatasetTest.java index e516523117b..727df922dd7 100644 --- a/automl/snippets/src/test/java/com/example/automl/ImportDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/ImportDatasetTest.java @@ -19,20 +19,12 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; -import com.google.api.core.ApiFuture; -import com.google.cloud.automl.v1beta1.AutoMlClient; -import com.google.cloud.automl.v1beta1.CreateDatasetRequest; -import com.google.cloud.automl.v1beta1.Dataset; -import com.google.cloud.automl.v1beta1.LocationName; -import com.google.cloud.automl.v1beta1.TextExtractionDatasetMetadata; -import com.google.longrunning.Operation; +import com.google.api.gax.rpc.NotFoundException; +import io.grpc.StatusRuntimeException; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; -import java.util.UUID; -import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; @@ -46,11 +38,12 @@ public class ImportDatasetTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String DATASET_ID = "TEN0000000000000000000"; private static final String BUCKET_ID = PROJECT_ID + "-lcm"; private static final String BUCKET = "gs://" + BUCKET_ID; - private String datasetId; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -65,66 +58,28 @@ public static void checkRequirements() { } @Before - public void setUp() - throws IOException, InterruptedException, ExecutionException, TimeoutException { - // Create a fake dataset to be deleted - // Create a random dataset name with a length of 32 characters (max allowed by AutoML) - // To prevent name collisions when running tests in multiple java versions at once. - // AutoML doesn't allow "-", but accepts "_" - String datasetName = - String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); - try (AutoMlClient client = AutoMlClient.create()) { - - LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1"); - TextExtractionDatasetMetadata metadata = TextExtractionDatasetMetadata.newBuilder().build(); - Dataset dataset = - Dataset.newBuilder() - .setDisplayName(datasetName) - .setTextExtractionDatasetMetadata(metadata) - .build(); - - CreateDatasetRequest request = - CreateDatasetRequest.newBuilder() - .setParent(projectLocation.toString()) - .setDataset(dataset) - .build(); - ApiFuture future = client.createDatasetCallable().futureCall(request); - Dataset createdDataset = future.get(5, TimeUnit.MINUTES); - String[] names = createdDataset.getName().split("/"); - datasetId = names[names.length - 1]; - } - + public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After - public void tearDown() throws InterruptedException, ExecutionException, IOException { - // Delete the created dataset - DeleteDataset.deleteDataset(PROJECT_ID, datasetId); - System.setOut(null); + public void tearDown() { + System.setOut(originalPrintStream); } @Test - public void testImportDataset() - throws InterruptedException, ExecutionException, TimeoutException, IOException { + public void testImportDataset() throws InterruptedException, TimeoutException, IOException { try { - ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv"); - } catch (CancellationException ex) { - // capture operation ID from output and wait for that operation to be finished. - String fullOperationId = ex.getMessage().split("Operation name: ")[1].trim(); - AutoMlClient client = AutoMlClient.create(); - Operation importDatasetLro = client.getOperationsClient().getOperation(fullOperationId); - while (!importDatasetLro.getDone()) { - Thread.sleep(3000); - } - // retry the import. - ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv"); + ImportDataset.importDataset( + PROJECT_ID, DATASET_ID, BUCKET + "/entity-extraction/dataset.csv"); + String got = bout.toString(); + assertThat(got).contains("The Dataset doesn't exist "); + } catch (NotFoundException | ExecutionException | StatusRuntimeException ex) { + assertThat(ex.getMessage()).contains("The Dataset doesn't exist"); } - String got = bout.toString(); - - assertThat(got).contains("Dataset imported."); } } diff --git a/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionCreateDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionCreateDatasetTest.java index e3045478e47..decbfeb36d0 100644 --- a/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionCreateDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionCreateDatasetTest.java @@ -38,6 +38,7 @@ public class LanguageEntityExtractionCreateDatasetTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private String datasetId; private static void requireEnvVar(String varName) { @@ -56,6 +57,7 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @@ -63,7 +65,7 @@ public void setUp() { public void tearDown() throws InterruptedException, ExecutionException, IOException { // Delete the created dataset DeleteDataset.deleteDataset(PROJECT_ID, datasetId); - System.setOut(null); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionCreateModelTest.java b/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionCreateModelTest.java index 724dd451e2d..21baebcc927 100644 --- a/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionCreateModelTest.java +++ b/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionCreateModelTest.java @@ -39,6 +39,7 @@ public class LanguageEntityExtractionCreateModelTest { private static final String DATASET_ID = "TEN0000000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -56,12 +57,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionPredictTest.java b/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionPredictTest.java index 939bf5a2afa..bf553f4b3d5 100644 --- a/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionPredictTest.java +++ b/automl/snippets/src/test/java/com/example/automl/LanguageEntityExtractionPredictTest.java @@ -41,6 +41,7 @@ public class LanguageEntityExtractionPredictTest { private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -72,12 +73,15 @@ public void setUp() throws IOException, ExecutionException, InterruptedException bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateDatasetTest.java index a5dbc5ce43a..7e065772946 100644 --- a/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateDatasetTest.java @@ -38,6 +38,7 @@ public class LanguageSentimentAnalysisCreateDatasetTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private String datasetId; private static void requireEnvVar(String varName) { @@ -56,6 +57,7 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @@ -63,7 +65,7 @@ public void setUp() { public void tearDown() throws InterruptedException, ExecutionException, IOException { // Delete the created dataset DeleteDataset.deleteDataset(PROJECT_ID, datasetId); - System.setOut(null); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateModelTest.java b/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateModelTest.java index c91fbf9d86e..8d28cf81076 100644 --- a/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateModelTest.java +++ b/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateModelTest.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; -import com.google.cloud.automl.v1.AutoMlClient; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -37,10 +36,10 @@ public class LanguageSentimentAnalysisCreateModelTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); - private static final String DATASET_ID = System.getenv("SENTIMENT_ANALYSIS_DATASET_ID"); + private static final String DATASET_ID = "TST00000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; - private String operationId; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -52,39 +51,35 @@ private static void requireEnvVar(String varName) { public static void checkRequirements() { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("AUTOML_PROJECT_ID"); - requireEnvVar("SENTIMENT_ANALYSIS_DATASET_ID"); } @Before public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After - public void tearDown() throws IOException { - // Cancel the operation - try (AutoMlClient client = AutoMlClient.create()) { - client.getOperationsClient().cancelOperation(operationId); - } - - System.setOut(null); + public void tearDown() { + System.setOut(originalPrintStream); } @Test - public void testLanguageSentimentAnalysisCreateModel() - throws IOException, ExecutionException, InterruptedException { - // Create a random dataset name with a length of 32 characters (max allowed by AutoML) - // To prevent name collisions when running tests in multiple java versions at once. - // AutoML doesn't allow "-", but accepts "_" - String modelName = - String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); - LanguageSentimentAnalysisCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); - - String got = bout.toString(); - assertThat(got).contains("Training started"); - - operationId = got.split("Training operation name: ")[1].split("\n")[0]; + public void testLanguageSentimentAnalysisCreateModel() { + // Create a model from a nonexistent dataset. + try { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + LanguageSentimentAnalysisCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } } } diff --git a/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisPredictTest.java b/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisPredictTest.java index b3f2eba9928..665712719ce 100644 --- a/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisPredictTest.java +++ b/automl/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisPredictTest.java @@ -41,6 +41,7 @@ public class LanguageSentimentAnalysisPredictTest { private static final String MODEL_ID = System.getenv("SENTIMENT_ANALYSIS_MODEL_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -71,12 +72,15 @@ public void setUp() throws IOException, ExecutionException, InterruptedException bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationCreateDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationCreateDatasetTest.java index 74a39a75085..b4c43c51956 100644 --- a/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationCreateDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationCreateDatasetTest.java @@ -38,6 +38,7 @@ public class LanguageTextClassificationCreateDatasetTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private String datasetId; private static void requireEnvVar(String varName) { @@ -56,6 +57,7 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @@ -63,7 +65,7 @@ public void setUp() { public void tearDown() throws InterruptedException, ExecutionException, IOException { // Delete the created dataset DeleteDataset.deleteDataset(PROJECT_ID, datasetId); - System.setOut(null); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationCreateModelTest.java b/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationCreateModelTest.java index 913d125e776..14bd5098d4c 100644 --- a/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationCreateModelTest.java +++ b/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationCreateModelTest.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; -import com.google.cloud.automl.v1.AutoMlClient; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -37,10 +36,10 @@ public class LanguageTextClassificationCreateModelTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); - private static final String DATASET_ID = System.getenv("TEXT_CLASSIFICATION_DATASET_ID"); + private static final String DATASET_ID = "TCN00000000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; - private String operationId; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -52,39 +51,35 @@ private static void requireEnvVar(String varName) { public static void checkRequirements() { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("AUTOML_PROJECT_ID"); - requireEnvVar("TEXT_CLASSIFICATION_DATASET_ID"); } @Before public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After - public void tearDown() throws IOException { - // Cancel the operation - try (AutoMlClient client = AutoMlClient.create()) { - client.getOperationsClient().cancelOperation(operationId); - } - - System.setOut(null); + public void tearDown() { + System.setOut(originalPrintStream); } @Test - public void testLanguageTextClassificationCreateModel() - throws IOException, ExecutionException, InterruptedException { - // Create a random dataset name with a length of 32 characters (max allowed by AutoML) - // To prevent name collisions when running tests in multiple java versions at once. - // AutoML doesn't allow "-", but accepts "_" - String modelName = - String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); - LanguageTextClassificationCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); - - String got = bout.toString(); - assertThat(got).contains("Training started"); - - operationId = got.split("Training operation name: ")[1].split("\n")[0]; + public void testLanguageTextClassificationCreateModel() { + // Create a model from a nonexistent dataset. + try { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + LanguageTextClassificationCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } } } diff --git a/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationPredictTest.java b/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationPredictTest.java index c45111d7081..f27234e74dc 100644 --- a/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationPredictTest.java +++ b/automl/snippets/src/test/java/com/example/automl/LanguageTextClassificationPredictTest.java @@ -41,6 +41,7 @@ public class LanguageTextClassificationPredictTest { private static final String MODEL_ID = System.getenv("TEXT_CLASSIFICATION_MODEL_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -71,12 +72,15 @@ public void setUp() throws IOException, ExecutionException, InterruptedException bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/ListDatasetsTest.java b/automl/snippets/src/test/java/com/example/automl/ListDatasetsTest.java index 412e63c77b6..df2a91892f4 100644 --- a/automl/snippets/src/test/java/com/example/automl/ListDatasetsTest.java +++ b/automl/snippets/src/test/java/com/example/automl/ListDatasetsTest.java @@ -36,6 +36,7 @@ public class ListDatasetsTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -53,12 +54,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/ListModelEvaluationsTest.java b/automl/snippets/src/test/java/com/example/automl/ListModelEvaluationsTest.java index 521c0da497b..7d7d08e31dd 100644 --- a/automl/snippets/src/test/java/com/example/automl/ListModelEvaluationsTest.java +++ b/automl/snippets/src/test/java/com/example/automl/ListModelEvaluationsTest.java @@ -35,6 +35,7 @@ public class ListModelEvaluationsTest { private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -53,12 +54,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/ListModelsTest.java b/automl/snippets/src/test/java/com/example/automl/ListModelsTest.java index 40510556067..d6eadeb9e54 100644 --- a/automl/snippets/src/test/java/com/example/automl/ListModelsTest.java +++ b/automl/snippets/src/test/java/com/example/automl/ListModelsTest.java @@ -25,6 +25,7 @@ import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -34,6 +35,7 @@ public class ListModelsTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -51,15 +53,21 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } + // Skipping this test until backend is cleaned up. + // https://github.com/googleapis/java-automl/issues/291 @Test + @Ignore public void testListModels() throws IOException { ListModels.listModels(PROJECT_ID); String got = bout.toString(); diff --git a/automl/snippets/src/test/java/com/example/automl/ListOperationStatusTest.java b/automl/snippets/src/test/java/com/example/automl/ListOperationStatusTest.java index 169a97112b4..2b60834aa61 100644 --- a/automl/snippets/src/test/java/com/example/automl/ListOperationStatusTest.java +++ b/automl/snippets/src/test/java/com/example/automl/ListOperationStatusTest.java @@ -34,6 +34,7 @@ public class ListOperationStatusTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -51,12 +52,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/TranslateCreateDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/TranslateCreateDatasetTest.java index 1d94cb6369b..ad2081a06ad 100644 --- a/automl/snippets/src/test/java/com/example/automl/TranslateCreateDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/TranslateCreateDatasetTest.java @@ -38,6 +38,7 @@ public class TranslateCreateDatasetTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private String datasetId; private static void requireEnvVar(String varName) { @@ -56,6 +57,7 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @@ -63,7 +65,7 @@ public void setUp() { public void tearDown() throws InterruptedException, ExecutionException, IOException { // Delete the created dataset DeleteDataset.deleteDataset(PROJECT_ID, datasetId); - System.setOut(null); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/TranslateCreateModelTest.java b/automl/snippets/src/test/java/com/example/automl/TranslateCreateModelTest.java index 5c3a97b3dac..71a3b7b0dee 100644 --- a/automl/snippets/src/test/java/com/example/automl/TranslateCreateModelTest.java +++ b/automl/snippets/src/test/java/com/example/automl/TranslateCreateModelTest.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; -import com.google.cloud.automl.v1.AutoMlClient; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -36,10 +35,10 @@ @RunWith(JUnit4.class) public class TranslateCreateModelTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); - private static final String DATASET_ID = System.getenv("TRANSLATION_DATASET_ID"); + private static final String DATASET_ID = "TRL00000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; - private String operationId; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -51,39 +50,35 @@ private static void requireEnvVar(String varName) { public static void checkRequirements() { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("AUTOML_PROJECT_ID"); - requireEnvVar("TRANSLATION_DATASET_ID"); } @Before public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After - public void tearDown() throws IOException { - // Cancel the operation - try (AutoMlClient client = AutoMlClient.create()) { - client.getOperationsClient().cancelOperation(operationId); - } - - System.setOut(null); + public void tearDown() { + System.setOut(originalPrintStream); } @Test - public void testTranslateCreateModel() - throws IOException, ExecutionException, InterruptedException { - // Create a random dataset name with a length of 32 characters (max allowed by AutoML) - // To prevent name collisions when running tests in multiple java versions at once. - // AutoML doesn't allow "-", but accepts "_" - String modelName = - String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); - TranslateCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); - - String got = bout.toString(); - assertThat(got).contains("Training started"); - - operationId = got.split("Training operation name: ")[1].split("\n")[0]; + public void testTranslateCreateModel() { + // Create a model from a nonexistent dataset. + try { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + TranslateCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } } } diff --git a/automl/snippets/src/test/java/com/example/automl/TranslatePredictTest.java b/automl/snippets/src/test/java/com/example/automl/TranslatePredictTest.java index 2d041ee4976..8713ab99351 100644 --- a/automl/snippets/src/test/java/com/example/automl/TranslatePredictTest.java +++ b/automl/snippets/src/test/java/com/example/automl/TranslatePredictTest.java @@ -38,6 +38,7 @@ public class TranslatePredictTest { private static final String filePath = "./resources/input.txt"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -56,12 +57,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/UndeployModelTest.java b/automl/snippets/src/test/java/com/example/automl/UndeployModelTest.java index e70d048262c..02d480a673a 100644 --- a/automl/snippets/src/test/java/com/example/automl/UndeployModelTest.java +++ b/automl/snippets/src/test/java/com/example/automl/UndeployModelTest.java @@ -36,6 +36,7 @@ public class UndeployModelTest { private static final String MODEL_ID = "TEN0000000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -53,12 +54,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/VisionClassificationCreateDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/VisionClassificationCreateDatasetTest.java index 6c764156df5..bf8b759cb2b 100644 --- a/automl/snippets/src/test/java/com/example/automl/VisionClassificationCreateDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/VisionClassificationCreateDatasetTest.java @@ -38,6 +38,7 @@ public class VisionClassificationCreateDatasetTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private String datasetId; private static void requireEnvVar(String varName) { @@ -56,6 +57,7 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @@ -63,7 +65,7 @@ public void setUp() { public void tearDown() throws InterruptedException, ExecutionException, IOException { // Delete the created dataset DeleteDataset.deleteDataset(PROJECT_ID, datasetId); - System.setOut(null); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/VisionClassificationCreateModelTest.java b/automl/snippets/src/test/java/com/example/automl/VisionClassificationCreateModelTest.java index f874b2596d5..1fac2cccb66 100644 --- a/automl/snippets/src/test/java/com/example/automl/VisionClassificationCreateModelTest.java +++ b/automl/snippets/src/test/java/com/example/automl/VisionClassificationCreateModelTest.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; -import com.google.cloud.automl.v1.AutoMlClient; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -37,9 +36,10 @@ public class VisionClassificationCreateModelTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); - private static final String DATASET_ID = System.getenv("VISION_CLASSIFICATION_DATASET_ID"); + private static final String DATASET_ID = "ICN000000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private String operationId; private static void requireEnvVar(String varName) { @@ -52,39 +52,35 @@ private static void requireEnvVar(String varName) { public static void checkRequirements() { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("AUTOML_PROJECT_ID"); - requireEnvVar("VISION_CLASSIFICATION_DATASET_ID"); } @Before public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After - public void tearDown() throws IOException { - // Cancel the operation - try (AutoMlClient client = AutoMlClient.create()) { - client.getOperationsClient().cancelOperation(operationId); - } - - System.setOut(null); + public void tearDown() { + System.setOut(originalPrintStream); } @Test - public void testVisionClassificationCreateModel() - throws IOException, ExecutionException, InterruptedException { - // Create a random dataset name with a length of 32 characters (max allowed by AutoML) - // To prevent name collisions when running tests in multiple java versions at once. - // AutoML doesn't allow "-", but accepts "_" - String modelName = - String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); - VisionClassificationCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); - - String got = bout.toString(); - assertThat(got).contains("Training started"); - - operationId = got.split("Training operation name: ")[1].split("\n")[0]; + public void testVisionClassificationCreateModel() { + // Create a model from a nonexistent dataset. + try { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + VisionClassificationCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } } } diff --git a/automl/snippets/src/test/java/com/example/automl/VisionClassificationDeployModelNodeCountTest.java b/automl/snippets/src/test/java/com/example/automl/VisionClassificationDeployModelNodeCountTest.java index 3bca11b95c2..2414c00a51b 100644 --- a/automl/snippets/src/test/java/com/example/automl/VisionClassificationDeployModelNodeCountTest.java +++ b/automl/snippets/src/test/java/com/example/automl/VisionClassificationDeployModelNodeCountTest.java @@ -36,6 +36,7 @@ public class VisionClassificationDeployModelNodeCountTest { private static final String MODEL_ID = "ICN0000000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -53,12 +54,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/VisionClassificationPredictTest.java b/automl/snippets/src/test/java/com/example/automl/VisionClassificationPredictTest.java index 1855294886b..e4c3af69a4f 100644 --- a/automl/snippets/src/test/java/com/example/automl/VisionClassificationPredictTest.java +++ b/automl/snippets/src/test/java/com/example/automl/VisionClassificationPredictTest.java @@ -41,6 +41,7 @@ public class VisionClassificationPredictTest { private static final String MODEL_ID = System.getenv("VISION_CLASSIFICATION_MODEL_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -71,12 +72,15 @@ public void setUp() throws IOException, ExecutionException, InterruptedException bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionCreateDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionCreateDatasetTest.java index f1933e4923f..20e6e1c5a47 100644 --- a/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionCreateDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionCreateDatasetTest.java @@ -38,6 +38,7 @@ public class VisionObjectDetectionCreateDatasetTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private String datasetId; private static void requireEnvVar(String varName) { @@ -56,6 +57,7 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @@ -63,7 +65,7 @@ public void setUp() { public void tearDown() throws InterruptedException, ExecutionException, IOException { // Delete the created dataset DeleteDataset.deleteDataset(PROJECT_ID, datasetId); - System.setOut(null); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionCreateModelTest.java b/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionCreateModelTest.java index b1b70b76401..3bd4228f8f8 100644 --- a/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionCreateModelTest.java +++ b/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionCreateModelTest.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; -import com.google.cloud.automl.v1.AutoMlClient; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -37,10 +36,10 @@ public class VisionObjectDetectionCreateModelTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); - private static final String DATASET_ID = System.getenv("OBJECT_DETECTION_DATASET_ID"); + private static final String DATASET_ID = "IOD0000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; - private String operationId; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -52,39 +51,35 @@ private static void requireEnvVar(String varName) { public static void checkRequirements() { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("AUTOML_PROJECT_ID"); - requireEnvVar("OBJECT_DETECTION_DATASET_ID"); } @Before public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After - public void tearDown() throws IOException { - // Cancel the operation - try (AutoMlClient client = AutoMlClient.create()) { - client.getOperationsClient().cancelOperation(operationId); - } - - System.setOut(null); + public void tearDown() { + System.setOut(originalPrintStream); } @Test - public void testVisionObjectDetectionCreateModel() - throws IOException, ExecutionException, InterruptedException { - // Create a random dataset name with a length of 32 characters (max allowed by AutoML) - // To prevent name collisions when running tests in multiple java versions at once. - // AutoML doesn't allow "-", but accepts "_" - String modelName = - String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); - VisionObjectDetectionCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); - - String got = bout.toString(); - assertThat(got).contains("Training started"); - - operationId = got.split("Training operation name: ")[1].split("\n")[0]; + public void testVisionObjectDetectionCreateModel() { + // Create a model from a nonexistent dataset. + try { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + VisionObjectDetectionCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } } } diff --git a/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionDeployModelNodeCountTest.java b/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionDeployModelNodeCountTest.java index c3bc13f0c1e..b218d2e59fb 100644 --- a/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionDeployModelNodeCountTest.java +++ b/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionDeployModelNodeCountTest.java @@ -36,6 +36,7 @@ public class VisionObjectDetectionDeployModelNodeCountTest { private static final String MODEL_ID = "0000000000000000000000"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -53,12 +54,15 @@ public static void checkRequirements() { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionPredictTest.java b/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionPredictTest.java index 59dfb120c96..1580c94b39b 100644 --- a/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionPredictTest.java +++ b/automl/snippets/src/test/java/com/example/automl/VisionObjectDetectionPredictTest.java @@ -41,6 +41,7 @@ public class VisionObjectDetectionPredictTest { private static final String MODEL_ID = System.getenv("OBJECT_DETECTION_MODEL_ID"); private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private static void requireEnvVar(String varName) { assertNotNull( @@ -71,12 +72,15 @@ public void setUp() throws IOException, ExecutionException, InterruptedException bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java b/automl/snippets/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java index 16370a99244..b6dd3035b75 100644 --- a/automl/snippets/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java +++ b/automl/snippets/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java @@ -39,6 +39,7 @@ public class DatasetApiIT { private static final String COMPUTE_REGION = "us-central1"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private DatasetApi app; private String datasetId; private String getdatasetId = "TRL3946265060617537378"; @@ -47,12 +48,15 @@ public class DatasetApiIT { public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java b/automl/snippets/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java index 82c185ef9eb..ba8c293419a 100644 --- a/automl/snippets/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java +++ b/automl/snippets/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java @@ -34,6 +34,7 @@ public class ModelApiIT { private static final String COMPUTE_REGION = "us-central1"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; private ModelApi app; private String modelId; private String modelEvaluationId; @@ -43,12 +44,15 @@ public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test diff --git a/automl/snippets/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java b/automl/snippets/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java index 950ccd2d963..7aa34b54de1 100644 --- a/automl/snippets/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java +++ b/automl/snippets/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java @@ -36,17 +36,21 @@ public class PredictionApiIT { private static final String filePath = "./resources/input.txt"; private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalPrintStream; @Before public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); + originalPrintStream = System.out; System.setOut(out); } @After public void tearDown() { - System.setOut(null); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); } @Test