Skip to content

Commit bf1ae95

Browse files
authored
automl: fix old beta snippet tests (#1994)
1 parent a4ae988 commit bf1ae95

File tree

6 files changed

+56
-42
lines changed

6 files changed

+56
-42
lines changed

vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
class ClassificationDeployModel {
3131

3232
// Deploy a model
33-
static void classificationDeployModel(String projectId, String modelId) {
33+
static void classificationDeployModel(String projectId, String modelId)
34+
throws IOException, ExecutionException, InterruptedException {
3435
// String projectId = "YOUR_PROJECT_ID";
3536
// String modelId = "YOUR_MODEL_ID";
3637

@@ -54,8 +55,6 @@ static void classificationDeployModel(String projectId, String modelId) {
5455

5556
// Display the deployment details of model.
5657
System.out.println("Model deployment finished");
57-
} catch (IOException | InterruptedException | ExecutionException e) {
58-
e.printStackTrace();
5958
}
6059
}
6160
}

vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
class ClassificationDeployModelNodeCount {
3232

3333
// Deploy a model with a specified node count
34-
static void classificationDeployModelNodeCount(String projectId, String modelId) {
34+
static void classificationDeployModelNodeCount(String projectId, String modelId)
35+
throws IOException, ExecutionException, InterruptedException {
3536
// String projectId = "YOUR_PROJECT_ID";
3637
// String modelId = "YOUR_MODEL_ID";
3738

@@ -54,8 +55,6 @@ static void classificationDeployModelNodeCount(String projectId, String modelId)
5455
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
5556
future.get();
5657
System.out.println("Model deployment on 2 nodes finished");
57-
} catch (IOException | InterruptedException | ExecutionException e) {
58-
e.printStackTrace();
5958
}
6059
}
6160
}

vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationUndeployModel.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
class ClassificationUndeployModel {
3131

3232
// Deploy a model
33-
static void classificationUndeployModel(String projectId, String modelId) {
33+
static void classificationUndeployModel(String projectId, String modelId)
34+
throws IOException, ExecutionException, InterruptedException {
3435
// String projectId = "YOUR_PROJECT_ID";
3536
// String modelId = "YOUR_MODEL_ID";
3637

@@ -54,8 +55,6 @@ static void classificationUndeployModel(String projectId, String modelId) {
5455

5556
// Display the deployment details of model.
5657
System.out.println("Model undeploy finished");
57-
} catch (IOException | InterruptedException | ExecutionException e) {
58-
e.printStackTrace();
5958
}
6059
}
6160
}

vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030

3131
class ObjectDetectionDeployModelNodeCount {
3232

33-
static void objectDetectionDeployModelNodeCount(String projectId, String modelId) {
33+
static void objectDetectionDeployModelNodeCount(String projectId, String modelId)
34+
throws IOException, ExecutionException, InterruptedException {
3435
// String projectId = "YOUR_PROJECT_ID";
3536
// String modelId = "YOUR_MODEL_ID";
3637

@@ -53,8 +54,6 @@ static void objectDetectionDeployModelNodeCount(String projectId, String modelId
5354
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
5455
future.get();
5556
System.out.println("Model deployment on 2 nodes finished");
56-
} catch (IOException | InterruptedException | ExecutionException e) {
57-
e.printStackTrace();
5857
}
5958
}
6059
}

vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java

+36-17
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020

2121
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
2223
import java.io.PrintStream;
24+
import java.util.concurrent.ExecutionException;
2325

2426
import org.junit.After;
2527
import org.junit.Before;
2628
import org.junit.Test;
2729

2830
public class ClassificationDeployModelIT {
2931
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
30-
private static final String MODEL_ID = "ICN3125115511348658176";
32+
private static final String MODEL_ID = "ICN0000000000000000000";
3133
private ByteArrayOutputStream bout;
3234
private PrintStream out;
3335

@@ -45,27 +47,44 @@ public void tearDown() {
4547

4648
@Test
4749
public void testClassificationDeployModelApi() {
48-
ClassificationDeployModel.classificationDeployModel(PROJECT_ID, MODEL_ID);
49-
50-
String got = bout.toString();
51-
assertThat(got).contains("Model deployment finished");
50+
// As model deployment can take a long time, instead try to deploy a
51+
// nonexistent model and confirm that the model was not found, but other
52+
// elements of the request were valid.
53+
try {
54+
ClassificationDeployModel.classificationDeployModel(PROJECT_ID, MODEL_ID);
55+
String got = bout.toString();
56+
assertThat(got).contains("The model does not exist");
57+
} catch (IOException | ExecutionException | InterruptedException e) {
58+
assertThat(e.getMessage()).contains("The model does not exist");
59+
}
60+
}
5261

53-
ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID);
62+
@Test
63+
public void testClassificationUndeployModelApi() {
64+
// As model deployment can take a long time, instead try to deploy a
65+
// nonexistent model and confirm that the model was not found, but other
66+
// elements of the request were valid.
67+
try {
68+
ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID);
69+
String got = bout.toString();
70+
assertThat(got).contains("The model does not exist");
71+
} catch (IOException | ExecutionException | InterruptedException e) {
72+
assertThat(e.getMessage()).contains("The model does not exist");
73+
}
5474

55-
got = bout.toString();
56-
assertThat(got).contains("Model undeploy finished");
5775
}
5876

5977
@Test
6078
public void testClassificationDeployModelNodeCountApi() {
61-
ClassificationDeployModelNodeCount.classificationDeployModelNodeCount(PROJECT_ID, MODEL_ID);
62-
63-
String got = bout.toString();
64-
assertThat(got).contains("Model deployment on 2 nodes finished");
65-
66-
ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID);
67-
68-
got = bout.toString();
69-
assertThat(got).contains("Model undeploy finished");
79+
// As model deployment can take a long time, instead try to deploy a
80+
// nonexistent model and confirm that the model was not found, but other
81+
// elements of the request were valid.
82+
try {
83+
ClassificationDeployModelNodeCount.classificationDeployModelNodeCount(PROJECT_ID, MODEL_ID);
84+
String got = bout.toString();
85+
assertThat(got).contains("The model does not exist");
86+
} catch (IOException | ExecutionException | InterruptedException e) {
87+
assertThat(e.getMessage()).contains("The model does not exist");
88+
}
7089
}
7190
}

vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java

+12-13
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
@SuppressWarnings("checkstyle:abbreviationaswordinname")
4141
public class ObjectDetectionDeployModelNodeCountIT {
4242
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
43-
private static final String MODEL_ID = "IOD1854128448151224320";
43+
private static final String MODEL_ID = "0000000000000000000000";
4444
private ByteArrayOutputStream bout;
4545
private PrintStream out;
4646

@@ -52,22 +52,21 @@ public void setUp() {
5252
}
5353

5454
@After
55-
public void tearDown() throws IOException, InterruptedException, ExecutionException {
55+
public void tearDown() {
5656
System.setOut(null);
57-
58-
try (AutoMlClient client = AutoMlClient.create()) {
59-
OperationFuture<Empty, OperationMetadata> future = client.undeployModelAsync(
60-
ModelName.of(PROJECT_ID, "us-central1", MODEL_ID).toString());
61-
62-
future.get();
63-
}
6457
}
6558

6659
@Test
6760
public void testObjectDetectionDeployModelNodeCountApi() {
68-
ObjectDetectionDeployModelNodeCount.objectDetectionDeployModelNodeCount(PROJECT_ID, MODEL_ID);
69-
70-
String got = bout.toString();
71-
assertThat(got).contains("Model deployment on 2 nodes finished");
61+
// As model deployment can take a long time, instead try to deploy a
62+
// nonexistent model and confirm that the model was not found, but other
63+
// elements of the request were valid.
64+
try {
65+
ObjectDetectionDeployModelNodeCount.objectDetectionDeployModelNodeCount(PROJECT_ID, MODEL_ID);
66+
String got = bout.toString();
67+
assertThat(got).contains("The model does not exist");
68+
} catch (IOException | ExecutionException | InterruptedException e) {
69+
assertThat(e.getMessage()).contains("The model does not exist");
70+
}
7271
}
7372
}

0 commit comments

Comments
 (0)