Repository navigation
Expand file tree
/
Copy pathFileSearchStoresTest.java
More file actions
238 lines (209 loc) · 9.11 KB
/
Copy pathFileSearchStoresTest.java
File metadata and controls
238 lines (209 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright 2026 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
*
* https://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.google.genai;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.genai.types.ChunkingConfig;
import com.google.genai.types.ClientOptions;
import com.google.genai.types.CustomMetadata;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.UploadToFileSearchStoreConfig;
import com.google.genai.types.UploadToFileSearchStoreOperation;
import com.google.genai.types.WhiteSpaceConfig;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.ResponseBody;
import org.junit.jupiter.api.Test;
public class FileSearchStoresTest {
private static final String STORE_NAME = "fileSearchStores/store-1";
private static final byte[] CONTENT = "hello".getBytes(StandardCharsets.UTF_8);
/** Records the start request of a resumable upload and fakes both upload steps. */
private static class RecordingApiClient extends ApiClient {
String startPath;
String startRequestJson;
Optional<HttpOptions> startHttpOptions;
RecordingApiClient() {
super(
Optional.of("fake-api-key"),
Optional.<HttpOptions>empty(),
Optional.<ClientOptions>empty());
}
@Override
public ApiResponse request(
String httpMethod, String path, String requestJson, Optional<HttpOptions> httpOptions) {
startPath = path;
startRequestJson = requestJson;
startHttpOptions = httpOptions;
return new FakeApiResponse(
Headers.of("x-goog-upload-url", "https://upload.example.com/session-1"),
ResponseBody.create("", MediaType.get("text/plain")));
}
@Override
public ApiResponse request(
String httpMethod, String path, byte[] requestBytes, Optional<HttpOptions> httpOptions) {
return new FakeApiResponse(
Headers.of("X-Goog-Upload-Status", "final"),
ResponseBody.create(
"{\"name\":\"fileSearchStores/store-1/upload/operations/op-1\"}",
MediaType.get("application/json")));
}
@Override
public CompletableFuture<ApiResponse> asyncRequest(
String httpMethod, String path, String requestJson, Optional<HttpOptions> httpOptions) {
return CompletableFuture.completedFuture(request(httpMethod, path, requestJson, httpOptions));
}
@Override
public CompletableFuture<ApiResponse> asyncRequest(
String httpMethod, String path, byte[] requestBytes, Optional<HttpOptions> httpOptions) {
return CompletableFuture.completedFuture(
request(httpMethod, path, requestBytes, httpOptions));
}
JsonNode startBody() throws Exception {
return new ObjectMapper().readTree(startRequestJson);
}
}
private static UploadToFileSearchStoreConfig fullConfig() {
return UploadToFileSearchStoreConfig.builder()
.mimeType("text/plain")
.displayName("my-doc")
.customMetadata(
Arrays.asList(CustomMetadata.builder().key("author").stringValue("alice").build()))
.chunkingConfig(
ChunkingConfig.builder()
.whiteSpaceConfig(
WhiteSpaceConfig.builder().maxTokensPerChunk(200).maxOverlapTokens(20).build())
.build())
.build();
}
private static void assertFullConfigInBody(JsonNode body) {
assertEquals("my-doc", body.path("displayName").asText(null));
assertEquals("author", body.path("customMetadata").path(0).path("key").asText(null));
assertEquals("alice", body.path("customMetadata").path(0).path("stringValue").asText(null));
assertEquals(
200,
body.path("chunkingConfig").path("whiteSpaceConfig").path("maxTokensPerChunk").asInt());
assertEquals(
20, body.path("chunkingConfig").path("whiteSpaceConfig").path("maxOverlapTokens").asInt());
}
/** The start request body carries the display name, metadata and chunking config. */
@Test
public void uploadToFileSearchStore_bytes_forwardsConfigFieldsInStartRequest() throws Exception {
// Arrange
RecordingApiClient apiClient = new RecordingApiClient();
FileSearchStores fileSearchStores = new FileSearchStores(apiClient);
// Act
UploadToFileSearchStoreOperation operation =
fileSearchStores.uploadToFileSearchStore(STORE_NAME, CONTENT, fullConfig());
// Assert
assertFullConfigInBody(apiClient.startBody());
assertEquals(
"upload/{api_version}/fileSearchStores/store-1:uploadToFileSearchStore",
apiClient.startPath);
assertTrue(operation.name().isPresent());
}
/** The async client forwards the same config fields in its start request. */
@Test
public void asyncUploadToFileSearchStore_bytes_forwardsConfigFieldsInStartRequest()
throws Exception {
// Arrange
RecordingApiClient apiClient = new RecordingApiClient();
AsyncFileSearchStores fileSearchStores = new AsyncFileSearchStores(apiClient);
// Act
UploadToFileSearchStoreOperation operation =
fileSearchStores.uploadToFileSearchStore(STORE_NAME, CONTENT, fullConfig()).get();
// Assert
assertFullConfigInBody(apiClient.startBody());
assertEquals(
"upload/{api_version}/fileSearchStores/store-1:uploadToFileSearchStore",
apiClient.startPath);
assertTrue(operation.name().isPresent());
}
/** The InputStream overload also sends the user's config in the start request. */
@Test
public void uploadToFileSearchStore_inputStream_forwardsDisplayName() throws Exception {
// Arrange
RecordingApiClient apiClient = new RecordingApiClient();
FileSearchStores fileSearchStores = new FileSearchStores(apiClient);
UploadToFileSearchStoreConfig config =
UploadToFileSearchStoreConfig.builder()
.mimeType("text/plain")
.displayName("my-doc")
.build();
// Act
UploadToFileSearchStoreOperation operation =
fileSearchStores.uploadToFileSearchStore(
STORE_NAME, new ByteArrayInputStream(CONTENT), CONTENT.length, config);
// Assert
assertTrue(operation.name().isPresent());
assertEquals("my-doc", apiClient.startBody().path("displayName").asText(null));
}
/** Fields the caller did not set are not sent in the start request. */
@Test
public void uploadToFileSearchStore_unsetFieldsAreOmittedFromStartRequest() throws Exception {
// Arrange
RecordingApiClient apiClient = new RecordingApiClient();
FileSearchStores fileSearchStores = new FileSearchStores(apiClient);
UploadToFileSearchStoreConfig config =
UploadToFileSearchStoreConfig.builder().mimeType("text/plain").build();
// Act
UploadToFileSearchStoreOperation operation =
fileSearchStores.uploadToFileSearchStore(STORE_NAME, CONTENT, config);
// Assert
assertTrue(operation.name().isPresent());
JsonNode body = apiClient.startBody();
assertFalse(body.has("displayName"));
assertFalse(body.has("customMetadata"));
assertFalse(body.has("chunkingConfig"));
}
/**
* The start request always reads the upload URL from the response headers and uses the
* resumable-upload headers, whatever the caller set for those two fields.
*/
@Test
public void uploadToFileSearchStore_overridesShouldReturnHttpResponseAndHttpOptions()
throws Exception {
// Arrange
RecordingApiClient apiClient = new RecordingApiClient();
FileSearchStores fileSearchStores = new FileSearchStores(apiClient);
UploadToFileSearchStoreConfig config =
UploadToFileSearchStoreConfig.builder()
.mimeType("text/plain")
.displayName("my-doc")
.shouldReturnHttpResponse(false)
.httpOptions(
HttpOptions.builder().headers(Collections.singletonMap("X-Custom", "1")).build())
.build();
// Act
UploadToFileSearchStoreOperation operation =
fileSearchStores.uploadToFileSearchStore(STORE_NAME, CONTENT, config);
// Assert
assertTrue(operation.name().isPresent());
Map<String, String> headers = apiClient.startHttpOptions.get().headers().get();
assertEquals("start", headers.get("X-Goog-Upload-Command"));
assertEquals("resumable", headers.get("X-Goog-Upload-Protocol"));
assertEquals("my-doc", apiClient.startBody().path("displayName").asText(null));
}
}