|
| 1 | +/* |
| 2 | + * Copyright 2025 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 genai.batchprediction; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 21 | +import static com.google.genai.types.JobState.Known.JOB_STATE_PENDING; |
| 22 | +import static com.google.genai.types.JobState.Known.JOB_STATE_RUNNING; |
| 23 | +import static com.google.genai.types.JobState.Known.JOB_STATE_SUCCEEDED; |
| 24 | +import static org.mockito.ArgumentMatchers.any; |
| 25 | +import static org.mockito.ArgumentMatchers.anyString; |
| 26 | +import static org.mockito.Mockito.RETURNS_SELF; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.mockStatic; |
| 29 | +import static org.mockito.Mockito.times; |
| 30 | +import static org.mockito.Mockito.verify; |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | +import com.google.genai.Batches; |
| 34 | +import com.google.genai.Client; |
| 35 | +import com.google.genai.types.BatchJob; |
| 36 | +import com.google.genai.types.BatchJobSource; |
| 37 | +import com.google.genai.types.CreateBatchJobConfig; |
| 38 | +import com.google.genai.types.GetBatchJobConfig; |
| 39 | +import com.google.genai.types.JobState; |
| 40 | +import java.io.ByteArrayOutputStream; |
| 41 | +import java.io.PrintStream; |
| 42 | +import java.lang.reflect.Field; |
| 43 | +import java.util.Optional; |
| 44 | +import org.junit.After; |
| 45 | +import org.junit.Before; |
| 46 | +import org.junit.BeforeClass; |
| 47 | +import org.junit.Test; |
| 48 | +import org.junit.runner.RunWith; |
| 49 | +import org.junit.runners.JUnit4; |
| 50 | +import org.mockito.MockedStatic; |
| 51 | + |
| 52 | +@RunWith(JUnit4.class) |
| 53 | +public class BatchPredictionIT { |
| 54 | + |
| 55 | + private static final String GEMINI_FLASH = "gemini-2.5-flash"; |
| 56 | + private static final String EMBEDDING_MODEL = "text-embedding-005"; |
| 57 | + private static String jobName; |
| 58 | + private static String outputGcsUri; |
| 59 | + private ByteArrayOutputStream bout; |
| 60 | + private Batches mockedBatches; |
| 61 | + private MockedStatic<Client> mockedStatic; |
| 62 | + |
| 63 | + // Check if the required environment variables are set. |
| 64 | + public static void requireEnvVar(String envVarName) { |
| 65 | + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) |
| 66 | + .that(System.getenv(envVarName)) |
| 67 | + .isNotEmpty(); |
| 68 | + } |
| 69 | + |
| 70 | + @BeforeClass |
| 71 | + public static void checkRequirements() { |
| 72 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 73 | + jobName = "projects/project_id/locations/us-central1/batchPredictionJobs/job_id"; |
| 74 | + outputGcsUri = "gs://your-bucket/your-prefix"; |
| 75 | + } |
| 76 | + |
| 77 | + @Before |
| 78 | + public void setUp() throws NoSuchFieldException, IllegalAccessException { |
| 79 | + bout = new ByteArrayOutputStream(); |
| 80 | + System.setOut(new PrintStream(bout)); |
| 81 | + |
| 82 | + // Arrange |
| 83 | + Client.Builder mockedBuilder = mock(Client.Builder.class, RETURNS_SELF); |
| 84 | + mockedBatches = mock(Batches.class); |
| 85 | + mockedStatic = mockStatic(Client.class); |
| 86 | + mockedStatic.when(Client::builder).thenReturn(mockedBuilder); |
| 87 | + Client mockedClient = mock(Client.class); |
| 88 | + when(mockedBuilder.build()).thenReturn(mockedClient); |
| 89 | + |
| 90 | + // Using reflection because 'batches' is a final field and cannot be mocked directly. |
| 91 | + // This is brittle but necessary for testing this class structure. |
| 92 | + Field field = Client.class.getDeclaredField("batches"); |
| 93 | + field.setAccessible(true); |
| 94 | + field.set(mockedClient, mockedBatches); |
| 95 | + |
| 96 | + // Mock the sequence of job states to test the polling loop |
| 97 | + BatchJob pendingJob = mock(BatchJob.class); |
| 98 | + when(pendingJob.name()).thenReturn(Optional.of(jobName)); |
| 99 | + when(pendingJob.state()).thenReturn(Optional.of(new JobState(JOB_STATE_PENDING))); |
| 100 | + |
| 101 | + BatchJob runningJob = mock(BatchJob.class); |
| 102 | + when(runningJob.state()).thenReturn(Optional.of(new JobState(JOB_STATE_RUNNING))); |
| 103 | + |
| 104 | + BatchJob succeededJob = mock(BatchJob.class); |
| 105 | + when(succeededJob.state()).thenReturn(Optional.of(new JobState(JOB_STATE_SUCCEEDED))); |
| 106 | + |
| 107 | + when(mockedBatches.create( |
| 108 | + anyString(), any(BatchJobSource.class), any(CreateBatchJobConfig.class))) |
| 109 | + .thenReturn(pendingJob); |
| 110 | + when(mockedBatches.get(anyString(), any(GetBatchJobConfig.class))) |
| 111 | + .thenReturn(runningJob, succeededJob); |
| 112 | + } |
| 113 | + |
| 114 | + @After |
| 115 | + public void tearDown() { |
| 116 | + System.setOut(null); |
| 117 | + bout.reset(); |
| 118 | + mockedStatic.close(); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testBatchPredictionWithGcs() throws InterruptedException { |
| 123 | + // Act |
| 124 | + JobState response = BatchPredictionWithGcs.createBatchJob(GEMINI_FLASH, outputGcsUri); |
| 125 | + |
| 126 | + // Assert |
| 127 | + verify(mockedBatches, times(1)) |
| 128 | + .create(anyString(), any(BatchJobSource.class), any(CreateBatchJobConfig.class)); |
| 129 | + verify(mockedBatches, times(2)).get(anyString(), any(GetBatchJobConfig.class)); |
| 130 | + |
| 131 | + assertThat(response).isNotNull(); |
| 132 | + assertThat(response.knownEnum()).isEqualTo(JOB_STATE_SUCCEEDED); |
| 133 | + |
| 134 | + String output = bout.toString(); |
| 135 | + assertThat(output).contains("Job name: " + jobName); |
| 136 | + assertThat(output).contains("Job state: JOB_STATE_PENDING"); |
| 137 | + assertThat(output).contains("Job state: JOB_STATE_RUNNING"); |
| 138 | + assertThat(output).contains("Job state: JOB_STATE_SUCCEEDED"); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testBatchPredictionEmbeddingsWithGcs() throws InterruptedException { |
| 143 | + // Act |
| 144 | + JobState response = |
| 145 | + BatchPredictionEmbeddingsWithGcs.createBatchJob(EMBEDDING_MODEL, outputGcsUri); |
| 146 | + |
| 147 | + // Assert |
| 148 | + verify(mockedBatches, times(1)) |
| 149 | + .create(anyString(), any(BatchJobSource.class), any(CreateBatchJobConfig.class)); |
| 150 | + verify(mockedBatches, times(2)).get(anyString(), any(GetBatchJobConfig.class)); |
| 151 | + |
| 152 | + assertThat(response).isNotNull(); |
| 153 | + assertThat(response.knownEnum()).isEqualTo(JOB_STATE_SUCCEEDED); |
| 154 | + |
| 155 | + String output = bout.toString(); |
| 156 | + assertThat(output).contains("Job name: " + jobName); |
| 157 | + assertThat(output).contains("Job state: JOB_STATE_PENDING"); |
| 158 | + assertThat(output).contains("Job state: JOB_STATE_RUNNING"); |
| 159 | + assertThat(output).contains("Job state: JOB_STATE_SUCCEEDED"); |
| 160 | + } |
| 161 | +} |
0 commit comments