|
1 | 1 | package com.getindata.connectors.http.internal.table.lookup; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.net.URI; |
4 | 5 | import java.net.http.HttpClient; |
5 | 6 | import java.net.http.HttpRequest; |
@@ -305,4 +306,187 @@ public void shouldHandleEmptyCollectionInRowDataCollector() throws Configuration |
305 | 306 | assertThat(result).isEmpty(); |
306 | 307 | } |
307 | 308 |
|
| 309 | + @Test |
| 310 | + public void shouldDeserializeArrayWithValidObjects() throws Exception { |
| 311 | + // GIVEN |
| 312 | + DeserializationSchema<RowData> mockDecoder = new DeserializationSchema<RowData>() { |
| 313 | + @Override |
| 314 | + public RowData deserialize(byte[] message) throws IOException { |
| 315 | + return null; |
| 316 | + } |
| 317 | + |
| 318 | + @Override |
| 319 | + public void deserialize(byte[] message, Collector<RowData> out) throws IOException { |
| 320 | + String msg = new String(message); |
| 321 | + if (msg.contains("value1")) { |
| 322 | + out.collect(GenericRowData.of(StringData.fromString("row1"))); |
| 323 | + } else if (msg.contains("value2")) { |
| 324 | + out.collect(GenericRowData.of(StringData.fromString("row2"))); |
| 325 | + } |
| 326 | + out.close(); |
| 327 | + } |
| 328 | + |
| 329 | + @Override |
| 330 | + public boolean isEndOfStream(RowData nextElement) { |
| 331 | + return false; |
| 332 | + } |
| 333 | + |
| 334 | + @Override |
| 335 | + public org.apache.flink.api.common.typeinfo.TypeInformation<RowData> getProducedType() { |
| 336 | + return null; |
| 337 | + } |
| 338 | + }; |
| 339 | + |
| 340 | + Properties properties = new Properties(); |
| 341 | + properties.setProperty(HttpConnectorConfigConstants.RESULT_TYPE, "array"); |
| 342 | + |
| 343 | + HttpLookupConfig lookupConfig = HttpLookupConfig.builder() |
| 344 | + .url(BASE_URL) |
| 345 | + .properties(properties) |
| 346 | + .build(); |
| 347 | + |
| 348 | + JavaNetHttpPollingClient client = new JavaNetHttpPollingClient( |
| 349 | + httpClient, |
| 350 | + mockDecoder, |
| 351 | + lookupConfig, |
| 352 | + new GetRequestFactory( |
| 353 | + new GenericGetQueryCreator(lookupRow), |
| 354 | + headerPreprocessor, |
| 355 | + lookupConfig |
| 356 | + ) |
| 357 | + ); |
| 358 | + |
| 359 | + // WHEN |
| 360 | + java.lang.reflect.Method method = JavaNetHttpPollingClient.class |
| 361 | + .getDeclaredMethod("deserializeArray", byte[].class); |
| 362 | + method.setAccessible(true); |
| 363 | + |
| 364 | + String jsonArray = "[{\"key\":\"value1\"},{\"key\":\"value2\"}]"; |
| 365 | + List<RowData> result = (List<RowData>) method.invoke(client, jsonArray.getBytes()); |
| 366 | + |
| 367 | + // THEN |
| 368 | + assertThat(result).isNotNull(); |
| 369 | + assertThat(result).hasSize(2); |
| 370 | + } |
| 371 | + |
| 372 | + @Test |
| 373 | + public void shouldHandleNullNodesInArray() throws Exception { |
| 374 | + // GIVEN |
| 375 | + DeserializationSchema<RowData> mockDecoder = new DeserializationSchema<RowData>() { |
| 376 | + @Override |
| 377 | + public RowData deserialize(byte[] message) throws IOException { |
| 378 | + return null; |
| 379 | + } |
| 380 | + |
| 381 | + @Override |
| 382 | + public void deserialize(byte[] message, Collector<RowData> out) throws IOException { |
| 383 | + out.collect(GenericRowData.of(StringData.fromString("valid"))); |
| 384 | + out.close(); |
| 385 | + } |
| 386 | + |
| 387 | + @Override |
| 388 | + public boolean isEndOfStream(RowData nextElement) { |
| 389 | + return false; |
| 390 | + } |
| 391 | + |
| 392 | + @Override |
| 393 | + public org.apache.flink.api.common.typeinfo.TypeInformation<RowData> getProducedType() { |
| 394 | + return null; |
| 395 | + } |
| 396 | + }; |
| 397 | + |
| 398 | + Properties properties = new Properties(); |
| 399 | + properties.setProperty(HttpConnectorConfigConstants.RESULT_TYPE, "array"); |
| 400 | + |
| 401 | + HttpLookupConfig lookupConfig = HttpLookupConfig.builder() |
| 402 | + .url(BASE_URL) |
| 403 | + .properties(properties) |
| 404 | + .build(); |
| 405 | + |
| 406 | + JavaNetHttpPollingClient client = new JavaNetHttpPollingClient( |
| 407 | + httpClient, |
| 408 | + mockDecoder, |
| 409 | + lookupConfig, |
| 410 | + new GetRequestFactory( |
| 411 | + new GenericGetQueryCreator(lookupRow), |
| 412 | + headerPreprocessor, |
| 413 | + lookupConfig |
| 414 | + ) |
| 415 | + ); |
| 416 | + |
| 417 | + // WHEN |
| 418 | + java.lang.reflect.Method method = JavaNetHttpPollingClient.class |
| 419 | + .getDeclaredMethod("deserializeArray", byte[].class); |
| 420 | + method.setAccessible(true); |
| 421 | + |
| 422 | + String jsonArray = "[{\"key\":\"value1\"},null,{\"key\":\"value2\"}]"; |
| 423 | + List<RowData> result = (List<RowData>) method.invoke(client, jsonArray.getBytes()); |
| 424 | + |
| 425 | + // THEN - null nodes should be skipped |
| 426 | + assertThat(result).isNotNull(); |
| 427 | + assertThat(result).hasSize(2); |
| 428 | + } |
| 429 | + |
| 430 | + @Test |
| 431 | + public void shouldHandleEmptyDeserializationInArray() throws Exception { |
| 432 | + // GIVEN |
| 433 | + DeserializationSchema<RowData> mockDecoder = new DeserializationSchema<RowData>() { |
| 434 | + @Override |
| 435 | + public RowData deserialize(byte[] message) throws IOException { |
| 436 | + return null; |
| 437 | + } |
| 438 | + |
| 439 | + @Override |
| 440 | + public void deserialize(byte[] message, Collector<RowData> out) throws IOException { |
| 441 | + String msg = new String(message); |
| 442 | + // Only collect for specific messages, return empty for others |
| 443 | + if (msg.contains("\"status\":\"valid\"")) { |
| 444 | + out.collect(GenericRowData.of(StringData.fromString("data"))); |
| 445 | + } |
| 446 | + // Don't collect anything for other messages |
| 447 | + out.close(); |
| 448 | + } |
| 449 | + |
| 450 | + @Override |
| 451 | + public boolean isEndOfStream(RowData nextElement) { |
| 452 | + return false; |
| 453 | + } |
| 454 | + |
| 455 | + @Override |
| 456 | + public org.apache.flink.api.common.typeinfo.TypeInformation<RowData> getProducedType() { |
| 457 | + return null; |
| 458 | + } |
| 459 | + }; |
| 460 | + |
| 461 | + Properties properties = new Properties(); |
| 462 | + properties.setProperty(HttpConnectorConfigConstants.RESULT_TYPE, "array"); |
| 463 | + |
| 464 | + HttpLookupConfig lookupConfig = HttpLookupConfig.builder() |
| 465 | + .url(BASE_URL) |
| 466 | + .properties(properties) |
| 467 | + .build(); |
| 468 | + |
| 469 | + JavaNetHttpPollingClient client = new JavaNetHttpPollingClient( |
| 470 | + httpClient, |
| 471 | + mockDecoder, |
| 472 | + lookupConfig, |
| 473 | + new GetRequestFactory( |
| 474 | + new GenericGetQueryCreator(lookupRow), |
| 475 | + headerPreprocessor, |
| 476 | + lookupConfig |
| 477 | + ) |
| 478 | + ); |
| 479 | + |
| 480 | + // WHEN |
| 481 | + java.lang.reflect.Method method = JavaNetHttpPollingClient.class |
| 482 | + .getDeclaredMethod("deserializeArray", byte[].class); |
| 483 | + method.setAccessible(true); |
| 484 | + |
| 485 | + String jsonArray = "[{\"status\":\"invalid\"},{\"status\":\"valid\"},{\"status\":\"invalid\"}]"; |
| 486 | + List<RowData> result = (List<RowData>) method.invoke(client, jsonArray.getBytes()); |
| 487 | + |
| 488 | + // THEN - only valid deserialization should be included |
| 489 | + assertThat(result).isNotNull(); |
| 490 | + assertThat(result).hasSize(1); |
| 491 | + } |
308 | 492 | } |
0 commit comments