Skip to content

Commit a990dad

Browse files
SK-2258 fixed utils test and add tokens in response
1 parent 1e92923 commit a990dad

File tree

3 files changed

+168
-34
lines changed

3 files changed

+168
-34
lines changed

v3/src/main/java/com/skyflow/utils/Utils.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import com.skyflow.generated.rest.types.RecordResponseObject;
99
import com.skyflow.vault.data.ErrorRecord;
1010
import com.skyflow.vault.data.Success;
11+
import com.skyflow.vault.data.Token;
1112

1213
import java.util.ArrayList;
14+
import java.util.HashMap;
1315
import java.util.List;
1416
import java.util.Map;
1517

@@ -101,17 +103,29 @@ public static com.skyflow.vault.data.InsertResponse formatResponse(InsertRespons
101103
ErrorRecord errorRecord = new ErrorRecord(indexNumber, record.get(index).getError().get(), record.get(index).getHttpCode().get());
102104
errorRecords.add(errorRecord);
103105
} else {
104-
Success success = new Success(index, record.get(index).getSkyflowId().get(), null, null);
105-
// if (record.get(index).getTokens().isPresent()) {
106-
// List<Token> tokens = null;
107-
// Map<String, Object> tok = record.get(index).getTokens().get();
108-
// for (int i = 0; i < tok.size(); i++) {
109-
// Object obj = tok.get(i);
110-
//// Token token = new Token(obj.toString());
111-
// }
112-
// }
113-
// success.setTokens(record.get(index).getTokens().get());
106+
Map<String, List<Token>> tokensMap = null;
114107

108+
if (record.get(index).getTokens().isPresent()) {
109+
tokensMap = new HashMap<>();
110+
Map<String, Object> tok = record.get(index).getTokens().get();
111+
for (Map.Entry<String, Object> entry : tok.entrySet()) {
112+
String key = entry.getKey();
113+
Object value = entry.getValue();
114+
List<Token> tokenList = new ArrayList<>();
115+
if (value instanceof List) {
116+
List<?> valueList = (List<?>) value;
117+
for (Object item : valueList) {
118+
if(item instanceof Map) {
119+
Map<String, Object> tokenMap = (Map<String, Object>) item;
120+
Token token = new Token((String) tokenMap.get("token"), (String) tokenMap.get("tokenGroupName"));
121+
tokenList.add(token);
122+
}
123+
}
124+
}
125+
tokensMap.put(key, tokenList);
126+
}
127+
}
128+
Success success = new Success(index, record.get(index).getSkyflowId().get(), tokensMap, null);
115129
successRecords.add(success);
116130
}
117131
indexNumber++;

v3/src/main/java/com/skyflow/vault/data/InsertResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public List<ErrorRecord> getErrors() {
5353
public ArrayList<HashMap<String, Object>> getRecordsToRetry() {
5454
if (recordsToRetry == null) {
5555
recordsToRetry = new ArrayList<>();
56-
return errors.stream()
56+
recordsToRetry = errors.stream()
5757
.filter(error -> (error.getCode() >= 500 && error.getCode() <= 599) && error.getCode() != 529)
5858
.map(errorRecord -> originalPayload.get(errorRecord.getIndex()))
5959
.collect(Collectors.toCollection(ArrayList::new));
6060
}
61-
return this.recordsToRetry;
61+
return recordsToRetry;
6262
}
6363

6464
@Override

v3/test/java/com/skyflow/utils/UtilsTests.java

Lines changed: 142 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.JsonObject;
44
import com.skyflow.config.Credentials;
5+
import com.skyflow.enums.Env;
56
import com.skyflow.errors.ErrorCode;
67
import com.skyflow.errors.ErrorMessage;
78
import com.skyflow.errors.SkyflowException;
@@ -10,6 +11,8 @@
1011
import com.skyflow.generated.rest.types.InsertResponse;
1112
import com.skyflow.generated.rest.types.RecordResponseObject;
1213
import com.skyflow.vault.data.ErrorRecord;
14+
import com.skyflow.vault.data.Success;
15+
import com.skyflow.vault.data.Token;
1316
import org.junit.Assert;
1417
import org.junit.BeforeClass;
1518
import org.junit.Test;
@@ -18,7 +21,7 @@
1821

1922
public class UtilsTests {
2023
private static final String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception";
21-
private static final String EXCEPTION_NOT_THROWN = "Should have thrown an exception";
24+
private static final String EXCEPTIONNOTTHROWN = "Should have thrown an exception";
2225
private static String filePath = null;
2326
private static String credentialsString = null;
2427
private static String token = null;
@@ -30,13 +33,43 @@ public static void setup() {
3033
filePath = "invalid/file/path/credentials.json";
3134
credentialsString = "invalid credentials string";
3235
token = "invalid-token";
33-
context = "test_context";
36+
context = "testcontext";
3437
roles = new ArrayList<>();
35-
String role = "test_role";
38+
String role = "testrole";
3639
roles.add(role);
3740
SdkVersion.setSdkPrefix(Constants.SDK_PREFIX);
3841
}
3942

43+
@Test
44+
public void testGetVaultURL() {
45+
// Test with production environment
46+
String prodUrl = Utils.getVaultURL("abc123", Env.PROD);
47+
Assert.assertEquals(
48+
"https://abc123.skyvault.skyflowapis.com",
49+
prodUrl
50+
);
51+
52+
// Test with development environment
53+
String devUrl = Utils.getVaultURL("xyz789", Env.DEV);
54+
Assert.assertEquals(
55+
"https://xyz789.skyvault.skyflowapis.dev",
56+
devUrl
57+
);
58+
}
59+
@Test(expected = NullPointerException.class)
60+
public void testGetVaultURLWithNullEnv() {
61+
Utils.getVaultURL("abc123", null);
62+
}
63+
64+
@Test
65+
public void testGetVaultURLWithEmptyClusterId() {
66+
String url = Utils.getVaultURL("", Env.PROD);
67+
Assert.assertEquals(
68+
"https://.skyvault.skyflowapis.com",
69+
url
70+
);
71+
}
72+
4073
@Test
4174
public void testGenerateBearerTokenWithCredentialsFile() {
4275
try {
@@ -45,7 +78,7 @@ public void testGenerateBearerTokenWithCredentialsFile() {
4578
credentials.setContext(context);
4679
credentials.setRoles(roles);
4780
Utils.generateBearerToken(credentials);
48-
Assert.fail(EXCEPTION_NOT_THROWN);
81+
Assert.fail(EXCEPTIONNOTTHROWN);
4982
} catch (SkyflowException e) {
5083
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
5184
Assert.assertEquals(
@@ -63,7 +96,7 @@ public void testGenerateBearerTokenWithCredentialsString() {
6396
credentials.setContext(context);
6497
credentials.setRoles(roles);
6598
Utils.generateBearerToken(credentials);
66-
Assert.fail(EXCEPTION_NOT_THROWN);
99+
Assert.fail(EXCEPTIONNOTTHROWN);
67100
} catch (SkyflowException e) {
68101
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
69102
Assert.assertEquals(ErrorMessage.CredentialsStringInvalidJson.getMessage(), e.getMessage());
@@ -116,7 +149,7 @@ public void testGetMetricsWithException() {
116149
}
117150

118151
@Test
119-
public void testCreateBatches_MultipleBatches() {
152+
public void testCreateBatchesMultipleBatches() {
120153
List<InsertRecordData> records = new ArrayList<>();
121154
for (int i = 0; i < 125; i++) {
122155
records.add(InsertRecordData.builder().build());
@@ -130,14 +163,14 @@ public void testCreateBatches_MultipleBatches() {
130163
}
131164

132165
@Test
133-
public void testCreateBatches_WithEmptyList() {
166+
public void testCreateBatchesWithEmptyList() {
134167
List<InsertRecordData> records = new ArrayList<>();
135168
List<List<InsertRecordData>> batches = Utils.createBatches(records, 50);
136169
Assert.assertTrue("Batches should be empty for empty input", batches.isEmpty());
137170
}
138171

139172
@Test
140-
public void testCreateBatches_WithSmallerSizeThanBatch() {
173+
public void testCreateBatchesWithSmallerSizeThanBatch() {
141174
List<InsertRecordData> records = new ArrayList<>();
142175
for (int i = 0; i < 25; i++) {
143176
records.add(InsertRecordData.builder().build());
@@ -149,7 +182,7 @@ public void testCreateBatches_WithSmallerSizeThanBatch() {
149182
}
150183

151184
@Test
152-
public void testCreateBatches_WithExactBatchSize() {
185+
public void testCreateBatchesWithExactBatchSize() {
153186
List<InsertRecordData> records = new ArrayList<>();
154187
for (int i = 0; i < 50; i++) {
155188
records.add(InsertRecordData.builder().build());
@@ -161,7 +194,7 @@ public void testCreateBatches_WithExactBatchSize() {
161194
}
162195

163196
@Test
164-
public void testCreateBatches_PreservesOrder() {
197+
public void testCreateBatchesPreservesOrder() {
165198
List<InsertRecordData> records = new ArrayList<>();
166199
for (int i = 0; i < 75; i++) {
167200
InsertRecordData record = InsertRecordData.builder()
@@ -180,7 +213,7 @@ public void testCreateBatches_PreservesOrder() {
180213
}
181214

182215
@Test(expected = NullPointerException.class)
183-
public void testCreateBatches_WithNullList() {
216+
public void testCreateBatchesWithNullList() {
184217
Utils.createBatches(null, 50);
185218
}
186219

@@ -199,7 +232,7 @@ public void testCreateErrorRecord() {
199232
}
200233

201234
@Test
202-
public void testHandleBatchException_ApiClientExceptionWithSingleError() {
235+
public void testHandleBatchExceptionApiClientExceptionWithSingleError() {
203236
List<InsertRecordData> batch = Arrays.asList(InsertRecordData.builder().build(), InsertRecordData.builder().build());
204237
List<List<InsertRecordData>> batches = Collections.singletonList(batch);
205238

@@ -223,7 +256,7 @@ public void testHandleBatchException_ApiClientExceptionWithSingleError() {
223256
}
224257

225258
@Test
226-
public void testHandleBatchException_WithNonApiClientException() {
259+
public void testHandleBatchExceptionWithNonApiClientException() {
227260
List<InsertRecordData> batch = Arrays.asList(InsertRecordData.builder().build(), InsertRecordData.builder().build());
228261
List<List<InsertRecordData>> batches = Collections.singletonList(batch);
229262

@@ -239,7 +272,7 @@ public void testHandleBatchException_WithNonApiClientException() {
239272
}
240273

241274
@Test
242-
public void testHandleBatchException_WithNonZeroBatchNumber() {
275+
public void testHandleBatchExceptionWithNonZeroBatchNumber() {
243276
List<InsertRecordData> batch = Arrays.asList(InsertRecordData.builder().build(), InsertRecordData.builder().build());
244277
List<List<InsertRecordData>> batches = Arrays.asList(new ArrayList<>(), batch);
245278

@@ -253,7 +286,7 @@ public void testHandleBatchException_WithNonZeroBatchNumber() {
253286
}
254287

255288
@Test
256-
public void testHandleBatchException_WithNullResponseBody() {
289+
public void testHandleBatchExceptionWithNullResponseBody() {
257290
List<InsertRecordData> batch = Arrays.asList(InsertRecordData.builder().build(), InsertRecordData.builder().build());
258291
List<List<InsertRecordData>> batches = Collections.singletonList(batch);
259292

@@ -266,7 +299,7 @@ public void testHandleBatchException_WithNullResponseBody() {
266299
}
267300

268301
@Test
269-
public void testFormatResponse_WithSuccessAndErrorRecords() {
302+
public void testFormatResponseWithSuccessAndErrorRecords() {
270303
RecordResponseObject successRecord = RecordResponseObject.builder()
271304
.skyflowId(Optional.of("testId1"))
272305
.error(Optional.empty())
@@ -293,13 +326,13 @@ public void testFormatResponse_WithSuccessAndErrorRecords() {
293326
}
294327

295328
@Test
296-
public void testFormatResponse_WithNullResponse() {
329+
public void testFormatResponseWithNullResponse() {
297330
com.skyflow.vault.data.InsertResponse result = Utils.formatResponse(null, 0, 50);
298331
Assert.assertNull(result);
299332
}
300333

301334
@Test
302-
public void testFormatResponse_WithSuccessRecordsOnly() {
335+
public void testFormatResponseWithSuccessRecordsOnly() {
303336
RecordResponseObject successRecord1 = RecordResponseObject.builder()
304337
.skyflowId(Optional.of("id1"))
305338
.error(Optional.empty())
@@ -323,7 +356,7 @@ public void testFormatResponse_WithSuccessRecordsOnly() {
323356
}
324357

325358
@Test
326-
public void testFormatResponse_WithErrorRecordsOnly() {
359+
public void testFormatResponseWithErrorRecordsOnly() {
327360
RecordResponseObject errorRecord1 = RecordResponseObject.builder()
328361
.error(Optional.of("Error 1"))
329362
.httpCode(Optional.of(400))
@@ -349,7 +382,7 @@ public void testFormatResponse_WithErrorRecordsOnly() {
349382
}
350383

351384
@Test
352-
public void testFormatResponse_WithBatchOffset() {
385+
public void testFormatResponseWithBatchOffset() {
353386
RecordResponseObject successRecord = RecordResponseObject.builder()
354387
.skyflowId(Optional.of("id1"))
355388
.error(Optional.empty())
@@ -370,7 +403,7 @@ public void testFormatResponse_WithBatchOffset() {
370403
}
371404

372405
@Test
373-
public void testFormatResponse_WithEmptyRecords() {
406+
public void testFormatResponseWithEmptyRecords() {
374407
InsertResponse response = InsertResponse.builder()
375408
.records(Optional.of(new ArrayList<>()))
376409
.build();
@@ -383,7 +416,7 @@ public void testFormatResponse_WithEmptyRecords() {
383416
}
384417

385418
@Test
386-
public void testFormatResponse_WithTokens() {
419+
public void testFormatResponseWithTokens() {
387420
Map<String, Object> tokens = new HashMap<>();
388421
tokens.put("field1", "token1");
389422
tokens.put("field2", "token2");
@@ -404,4 +437,91 @@ public void testFormatResponse_WithTokens() {
404437
Assert.assertEquals("Should have one success record", 1, result.getSuccess().size());
405438
Assert.assertEquals("Skyflow ID should match", "id1", result.getSuccess().get(0).getSkyflowId());
406439
}
440+
@Test
441+
public void testFormatResponseWithTokenListMapping() {
442+
// Prepare test data
443+
Map<String, Object> tokenData = new HashMap<>();
444+
List<Map<String, String>> tokenList = new ArrayList<>();
445+
Map<String, String> tokenMap = new HashMap<>();
446+
tokenMap.put("token", "token123");
447+
tokenMap.put("tokenGroupName", "group1");
448+
tokenList.add(tokenMap);
449+
tokenData.put("field1", tokenList);
450+
451+
// Create success record with tokens
452+
RecordResponseObject successRecord = RecordResponseObject.builder()
453+
.skyflowId(Optional.of("id1"))
454+
.tokens(Optional.of(tokenData))
455+
.error(Optional.empty())
456+
.build();
457+
458+
// Create response object
459+
InsertResponse response = InsertResponse.builder()
460+
.records(Optional.of(Collections.singletonList(successRecord)))
461+
.build();
462+
463+
// Format response
464+
com.skyflow.vault.data.InsertResponse result = Utils.formatResponse(response, 0, 50);
465+
466+
// Assertions
467+
Assert.assertNotNull("Response should not be null", result);
468+
Assert.assertEquals("Should have one success record", 1, result.getSuccess().size());
469+
470+
Success successResult = result.getSuccess().get(0);
471+
Assert.assertEquals("Skyflow ID should match", "id1", successResult.getSkyflowId());
472+
473+
Map<String, List<Token>> tokens = successResult.getTokens();
474+
Assert.assertNotNull("Tokens map should not be null", tokens);
475+
Assert.assertTrue("Should contain field1", tokens.containsKey("field1"));
476+
477+
List<Token> tokensList = tokens.get("field1");
478+
Assert.assertEquals("Should have one token", 1, tokensList.size());
479+
Assert.assertEquals("Token value should match", "token123", tokensList.get(0).getToken());
480+
Assert.assertEquals("Token group name should match", "group1", tokensList.get(0).getTokenGroupName());
481+
}
482+
@Test
483+
public void testHandleBatchExceptionWithRecordsInResponseBody() {
484+
// Prepare test data
485+
List<InsertRecordData> batch = Arrays.asList(
486+
InsertRecordData.builder().build(),
487+
InsertRecordData.builder().build()
488+
);
489+
List<List<InsertRecordData>> batches = Collections.singletonList(batch);
490+
491+
// Create nested records with errors
492+
List<Map<String, Object>> recordsList = new ArrayList<>();
493+
Map<String, Object> record1 = new HashMap<>();
494+
record1.put("error", "Error 1");
495+
record1.put("http_code", 400);
496+
Map<String, Object> record2 = new HashMap<>();
497+
record2.put("error", "Error 2");
498+
record2.put("http_code", 401);
499+
recordsList.add(record1);
500+
recordsList.add(record2);
501+
502+
// Create response body
503+
Map<String, Object> responseBody = new HashMap<>();
504+
responseBody.put("records", recordsList);
505+
506+
// Create API exception
507+
ApiClientApiException apiException = new ApiClientApiException("Bad Request", 400, responseBody);
508+
Exception exception = new Exception("Test exception", apiException);
509+
510+
// Test the method
511+
List<ErrorRecord> errors = Utils.handleBatchException(exception, batch, 0, batches);
512+
513+
// Assertions
514+
Assert.assertNotNull("Errors list should not be null", errors);
515+
Assert.assertEquals("Should have two error records", 2, errors.size());
516+
517+
// Verify first error
518+
Assert.assertEquals("First error message should match", "Error 1", errors.get(0).getError());
519+
Assert.assertEquals("First error code should match", 400, errors.get(0).getCode());
520+
Assert.assertEquals("First error index should be 0", 0, errors.get(0).getIndex());
521+
522+
// Verify second error
523+
Assert.assertEquals("Second error message should match", "Error 2", errors.get(1).getError());
524+
Assert.assertEquals("Second error code should match", 401, errors.get(1).getCode());
525+
Assert.assertEquals("Second error index should be 1", 1, errors.get(1).getIndex());
526+
}
407527
}

0 commit comments

Comments
 (0)