Skip to content

Commit d3a23fb

Browse files
committed
fix(java): waitForApiKey consistency
1 parent e97a4fc commit d3a23fb

File tree

7 files changed

+407
-39
lines changed

7 files changed

+407
-39
lines changed

scripts/cts/testServer/waitForApiKey.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ function addRoutes(app: Express): void {
2525
maxHitsPerQuery: 20,
2626
createdAt: 1720094400,
2727
});
28+
29+
retryCount.add = -1;
2830
} else {
31+
// eslint-disable-next-line no-console
32+
console.error(`Invalid retry count: ${retryCount.add}`);
2933
res.status(500).json({ message: `Internal Server Error` });
3034
}
3135

@@ -51,7 +55,11 @@ function addRoutes(app: Express): void {
5155
maxHitsPerQuery: 20,
5256
createdAt: 1720094400,
5357
});
58+
59+
retryCount.update = -1;
5460
} else {
61+
// eslint-disable-next-line no-console
62+
console.error(`Invalid retry count: ${retryCount.update}`);
5563
res.status(500).json({ message: `Internal Server Error` });
5664
}
5765

@@ -69,12 +77,18 @@ function addRoutes(app: Express): void {
6977
});
7078
} else if (retryCount.delete === 3) {
7179
res.status(404).json({ message: `API key doesn't exist` });
80+
81+
retryCount.delete = -1;
7282
} else {
83+
// eslint-disable-next-line no-console
84+
console.error(`Invalid retry count: ${retryCount.delete}`);
7385
res.status(500).json({ message: `Internal Server Error` });
7486
}
7587

7688
retryCount.delete += 1;
7789
} else {
90+
// eslint-disable-next-line no-console
91+
console.error(`Invalid API key ${req.params.key}`);
7892
res.status(500).json({ message: `Internal Server Error` });
7993
}
8094
});

templates/java/api_helpers.mustache

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ this.waitForAppTask(taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIM
112112
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
113113
*/
114114
public GetApiKeyResponse waitForApiKey(
115-
ApiKeyOperation operation,
116115
String key,
116+
ApiKeyOperation operation,
117117
ApiKey apiKey,
118118
int maxRetries,
119119
IntUnaryOperator timeout,
@@ -146,28 +146,26 @@ public GetApiKeyResponse waitForApiKey(
146146
);
147147
}
148148

149-
// bypass lambda restriction to modify final object
150-
final GetApiKeyResponse[] addedKey = new GetApiKeyResponse[] { null };
151-
152-
// check the status of the getApiKey method
153-
TaskUtils.retryUntil(
149+
return TaskUtils.retryUntil(
154150
() -> {
155151
try {
156-
addedKey[0] = this.getApiKey(key, requestOptions);
157-
// magic number to signify we found the key
158-
return -2;
152+
return this.getApiKey(key, requestOptions);
159153
} catch (AlgoliaApiException e) {
160-
return e.getStatusCode();
154+
if (e.getStatusCode() == 404) {
155+
return null;
156+
}
157+
158+
throw e;
161159
}
162160
},
163-
(Integer status) -> {
161+
(GetApiKeyResponse response) -> {
164162
switch (operation) {
165163
case ADD:
166-
// stop either when the key is created or when we don't receive 404
167-
return status == -2 || status != 404;
164+
// stop when we don't receive 404 meaning the key is created
165+
return response != null;
168166
case DELETE:
169167
// stop when the key is not found
170-
return status == 404;
168+
return response == null;
171169
default:
172170
// continue
173171
return false;
@@ -176,84 +174,82 @@ public GetApiKeyResponse waitForApiKey(
176174
maxRetries,
177175
timeout
178176
);
179-
180-
return addedKey[0];
181177
}
182178
183179
/**
184180
* Helper: Wait for an API key to be added or deleted based on a given `operation`.
185181
*
186-
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
187182
* @param key The `key` that has been added or deleted.
183+
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
188184
* @param maxRetries The maximum number of retry. 50 by default. (optional)
189185
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
190186
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
191187
*/
192-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) {
193-
return this.waitForApiKey(operation, key, null, maxRetries, timeout, requestOptions);
188+
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) {
189+
return this.waitForApiKey(key, operation, null, maxRetries, timeout, requestOptions);
194190
}
195191
/**
196192
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
197193
*
198-
* @param operation The `operation` that was done on a `key`.
199194
* @param key The `key` that has been added, deleted or updated.
195+
* @param operation The `operation` that was done on a `key`.
200196
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
201197
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
202198
*/
203-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey, RequestOptions requestOptions) {
204-
return this.waitForApiKey(operation, key, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
199+
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, ApiKey apiKey, RequestOptions requestOptions) {
200+
return this.waitForApiKey(key, operation, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
205201
}
206202
/**
207203
* Helper: Wait for an API key to be added or deleted based on a given `operation`.
208204
*
209-
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
210205
* @param key The `key` that has been added or deleted.
206+
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
211207
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
212208
*/
213-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, RequestOptions requestOptions) {
214-
return this.waitForApiKey(operation, key, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
209+
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, RequestOptions requestOptions) {
210+
return this.waitForApiKey(key, operation, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
215211
}
216212
/**
217213
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
218214
*
219-
* @param operation The `operation` that was done on a `key`.
220215
* @param key The `key` that has been added, deleted or updated.
216+
* @param operation The `operation` that was done on a `key`.
221217
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
222218
* @param maxRetries The maximum number of retry. 50 by default. (optional)
223219
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
224220
*/
225-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey, int maxRetries, IntUnaryOperator timeout) {
226-
return this.waitForApiKey(operation, key, apiKey, maxRetries, timeout, null);
221+
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, ApiKey apiKey, int maxRetries, IntUnaryOperator timeout) {
222+
return this.waitForApiKey(key, operation, apiKey, maxRetries, timeout, null);
227223
}
228224
/**
229225
* Helper: Wait for an API key to be added or deleted based on a given `operation`.
230226
*
231-
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
232227
* @param key The `key` that has been added or deleted.
228+
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
233229
* @param maxRetries The maximum number of retry. 50 by default. (optional)
234230
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
235231
*/
236-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, int maxRetries, IntUnaryOperator timeout) {
237-
return this.waitForApiKey(operation, key, null, maxRetries, timeout, null);
232+
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, int maxRetries, IntUnaryOperator timeout) {
233+
return this.waitForApiKey(key, operation, null, maxRetries, timeout, null);
238234
}
239235
/**
240236
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
241237
*
242-
* @param operation The `operation` that was done on a `key`.
243238
* @param key The `key` that has been added, deleted or updated.
239+
* @param operation The `operation` that was done on a `key`.
244240
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
245241
*/
246-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey) {
247-
return this.waitForApiKey(operation, key, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
242+
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation, ApiKey apiKey) {
243+
return this.waitForApiKey(key, operation, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
248244
}
249245
/**
250246
* Helper: Wait for an API key to be added or deleted based on a given `operation`.
251247
*
252-
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
253248
* @param key The `key` that has been added or deleted.
249+
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
254250
*/
255-
public GetApiKeyResponse waitForApiKey(ApiKeyOperation operation, String key) {
256-
return this.waitForApiKey(operation, key, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
251+
public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation) {
252+
return this.waitForApiKey(key, operation, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
257253
}
258254
259255
/**

templates/java/tests/client/suite.mustache

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.junit.jupiter.api.BeforeAll;
1616
import org.junit.jupiter.api.DisplayName;
1717
import org.junit.jupiter.api.Test;
1818
import org.junit.jupiter.api.TestInstance;
19+
import com.fasterxml.jackson.annotation.JsonInclude;
1920
import com.fasterxml.jackson.databind.DeserializationFeature;
2021
import com.fasterxml.jackson.databind.json.JsonMapper;
2122
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -29,7 +30,11 @@ class {{client}}ClientTests {
2930
3031
@BeforeAll
3132
void init() {
32-
this.json = JsonMapper.builder().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).build();
33+
this.json = JsonMapper.
34+
builder().
35+
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).
36+
serializationInclusion(JsonInclude.Include.NON_NULL).
37+
build();
3338
}
3439

3540
{{client}} createClient() {
@@ -82,7 +87,12 @@ class {{client}}ClientTests {
8287
assertDoesNotThrow(() -> JSONAssert.assertEquals("{{#lambda.escapeQuotes}}{{{match.parameters}}}{{/lambda.escapeQuotes}}", json.writeValueAsString(res), JSONCompareMode.STRICT));
8388
{{/matchIsJSON}}
8489
{{^matchIsJSON}}
85-
assertEquals("{{{match}}}", res);
90+
{{#matchIsNull}}
91+
assertEquals(null, res);
92+
{{/matchIsNull}}
93+
{{^matchIsNull}}
94+
assertEquals("{{{match}}}", res);
95+
{{/matchIsNull}}
8696
{{/matchIsJSON}}
8797
{{/testResponse}}
8898
{{/match}}
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)