|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package aiplatform; |
| 18 | + |
| 19 | +import static junit.framework.TestCase.assertNotNull; |
| 20 | +import static org.hamcrest.CoreMatchers.containsString; |
| 21 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | + |
| 23 | +import aiplatform.batchpredict.CreateBatchPredictionGeminiBigqueryJobSample; |
| 24 | +import aiplatform.batchpredict.CreateBatchPredictionGeminiJobSample; |
| 25 | +import com.google.cloud.aiplatform.v1.BatchPredictionJob; |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.PrintStream; |
| 29 | +import java.time.Instant; |
| 30 | +import java.util.concurrent.ExecutionException; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | +import java.util.concurrent.TimeoutException; |
| 33 | +import org.junit.AfterClass; |
| 34 | +import org.junit.BeforeClass; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.junit.runners.JUnit4; |
| 38 | + |
| 39 | +@RunWith(JUnit4.class) |
| 40 | +public class CreateBatchPredictionGeminiJobSampleTest { |
| 41 | + |
| 42 | + private static final String PROJECT = System.getenv("UCAIP_PROJECT_ID"); |
| 43 | + private static final String GCS_OUTPUT_URI = "gs://ucaip-samples-test-output/"; |
| 44 | + private static final String now = String.valueOf(Instant.now().getEpochSecond()); |
| 45 | + private static final String BIGQUERY_DESTINATION_OUTPUT_URI_PREFIX = |
| 46 | + String.format("bq://%s.gen_ai_batch_prediction.predictions_%s", PROJECT, now); |
| 47 | + |
| 48 | + private static ByteArrayOutputStream bout; |
| 49 | + private static PrintStream originalPrintStream; |
| 50 | + private static String batchPredictionGcsJobId; |
| 51 | + private static String batchPredictionBqJobId; |
| 52 | + |
| 53 | + private static void requireEnvVar(String varName) { |
| 54 | + String errorMessage = |
| 55 | + String.format("Environment variable '%s' is required to perform these tests.", varName); |
| 56 | + assertNotNull(errorMessage, System.getenv(varName)); |
| 57 | + } |
| 58 | + |
| 59 | + @BeforeClass |
| 60 | + public static void checkRequirements() { |
| 61 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 62 | + requireEnvVar("UCAIP_PROJECT_ID"); |
| 63 | + } |
| 64 | + |
| 65 | + @AfterClass |
| 66 | + public static void tearDown() |
| 67 | + throws InterruptedException, ExecutionException, IOException, TimeoutException { |
| 68 | + // Set up |
| 69 | + bout = new ByteArrayOutputStream(); |
| 70 | + PrintStream out = new PrintStream(bout); |
| 71 | + originalPrintStream = System.out; |
| 72 | + System.setOut(out); |
| 73 | + |
| 74 | + // Cloud Storage job |
| 75 | + CancelBatchPredictionJobSample.cancelBatchPredictionJobSample(PROJECT, batchPredictionGcsJobId); |
| 76 | + |
| 77 | + // Assert |
| 78 | + String cancelResponse = bout.toString(); |
| 79 | + assertThat(cancelResponse, containsString("Cancelled the Batch Prediction Job")); |
| 80 | + TimeUnit.MINUTES.sleep(2); |
| 81 | + |
| 82 | + // Delete the Batch Prediction Job |
| 83 | + DeleteBatchPredictionJobSample.deleteBatchPredictionJobSample(PROJECT, batchPredictionGcsJobId); |
| 84 | + |
| 85 | + // Assert |
| 86 | + String deleteResponse = bout.toString(); |
| 87 | + assertThat(deleteResponse, containsString("Deleted Batch")); |
| 88 | + |
| 89 | + // BigQuery job |
| 90 | + CancelBatchPredictionJobSample.cancelBatchPredictionJobSample(PROJECT, batchPredictionBqJobId); |
| 91 | + |
| 92 | + // Assert |
| 93 | + cancelResponse = bout.toString(); |
| 94 | + assertThat(cancelResponse, containsString("Cancelled the Batch Prediction Job")); |
| 95 | + TimeUnit.MINUTES.sleep(2); |
| 96 | + |
| 97 | + // Delete the Batch Prediction Job |
| 98 | + DeleteBatchPredictionJobSample.deleteBatchPredictionJobSample(PROJECT, batchPredictionBqJobId); |
| 99 | + |
| 100 | + // Assert |
| 101 | + deleteResponse = bout.toString(); |
| 102 | + assertThat(deleteResponse, containsString("Deleted Batch")); |
| 103 | + |
| 104 | + System.out.flush(); |
| 105 | + System.setOut(originalPrintStream); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testCreateBatchPredictionGeminiJobSampleTest() throws IOException { |
| 110 | + // Cloud Storage job |
| 111 | + // Act |
| 112 | + BatchPredictionJob job = |
| 113 | + CreateBatchPredictionGeminiJobSample.createBatchPredictionGeminiJobSample( |
| 114 | + PROJECT, GCS_OUTPUT_URI); |
| 115 | + |
| 116 | + // Assert |
| 117 | + assertThat(job.getName(), containsString("batchPredictionJobs")); |
| 118 | + |
| 119 | + String[] id = job.getName().split("/"); |
| 120 | + batchPredictionGcsJobId = id[id.length - 1]; |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testCreateBatchPredictionGeminiBigqueryJobSampleTest() throws IOException { |
| 125 | + // BigQuery job |
| 126 | + // Act |
| 127 | + BatchPredictionJob job = |
| 128 | + CreateBatchPredictionGeminiBigqueryJobSample.createBatchPredictionGeminiBigqueryJobSample( |
| 129 | + PROJECT, BIGQUERY_DESTINATION_OUTPUT_URI_PREFIX); |
| 130 | + |
| 131 | + // Assert |
| 132 | + assertThat(job.getName(), containsString("batchPredictionJobs")); |
| 133 | + |
| 134 | + String[] id = job.getName().split("/"); |
| 135 | + batchPredictionBqJobId = id[id.length - 1]; |
| 136 | + } |
| 137 | +} |
0 commit comments