|
15 | 15 | import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.stringKey; |
16 | 16 | import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.valueKey; |
17 | 17 | import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static org.assertj.core.api.Assertions.entry; |
18 | 19 |
|
19 | 20 | import com.google.common.collect.ImmutableMap; |
20 | 21 | import io.opentelemetry.api.common.AttributeKey; |
@@ -390,66 +391,175 @@ private static ExtendedAttributeType getType(Object value) { |
390 | 391 |
|
391 | 392 | @Test |
392 | 393 | void complexValueStoredAsString() { |
| 394 | + // When putting a VALUE attribute with a string Value, it should be stored as STRING type |
393 | 395 | ExtendedAttributes attributes = |
394 | 396 | ExtendedAttributes.builder().put(valueKey("key"), Value.of("test")).build(); |
| 397 | + |
| 398 | + // Should be stored as STRING type internally |
395 | 399 | assertThat(attributes.get(stringKey("key"))).isEqualTo("test"); |
| 400 | + assertThat(attributes.get(valueKey("key"))).isEqualTo(Value.of("test")); |
| 401 | + |
| 402 | + // forEach should show STRING type |
| 403 | + Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>(); |
| 404 | + attributes.forEach(entriesSeen::put); |
| 405 | + assertThat(entriesSeen).containsExactly(entry(stringKey("key"), "test")); |
| 406 | + |
| 407 | + // asMap should show STRING type |
| 408 | + assertThat(attributes.asMap()).containsExactly(entry(stringKey("key"), "test")); |
396 | 409 | } |
397 | 410 |
|
398 | 411 | @Test |
399 | 412 | void complexValueStoredAsLong() { |
| 413 | + // When putting a VALUE attribute with a long Value, it should be stored as LONG type |
400 | 414 | ExtendedAttributes attributes = |
401 | 415 | ExtendedAttributes.builder().put(valueKey("key"), Value.of(123L)).build(); |
| 416 | + |
| 417 | + // Should be stored as LONG type internally |
402 | 418 | assertThat(attributes.get(longKey("key"))).isEqualTo(123L); |
| 419 | + assertThat(attributes.get(valueKey("key"))).isEqualTo(Value.of(123L)); |
| 420 | + |
| 421 | + // forEach should show LONG type |
| 422 | + Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>(); |
| 423 | + attributes.forEach(entriesSeen::put); |
| 424 | + assertThat(entriesSeen).containsExactly(entry(longKey("key"), 123L)); |
| 425 | + |
| 426 | + // asMap should show LONG type |
| 427 | + assertThat(attributes.asMap()).containsExactly(entry(longKey("key"), 123L)); |
403 | 428 | } |
404 | 429 |
|
405 | 430 | @Test |
406 | 431 | void complexValueStoredAsDouble() { |
| 432 | + // When putting a VALUE attribute with a double Value, it should be stored as DOUBLE type |
407 | 433 | ExtendedAttributes attributes = |
408 | 434 | ExtendedAttributes.builder().put(valueKey("key"), Value.of(1.23)).build(); |
| 435 | + |
| 436 | + // Should be stored as DOUBLE type internally |
409 | 437 | assertThat(attributes.get(doubleKey("key"))).isEqualTo(1.23); |
| 438 | + assertThat(attributes.get(valueKey("key"))).isEqualTo(Value.of(1.23)); |
| 439 | + |
| 440 | + // forEach should show DOUBLE type |
| 441 | + Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>(); |
| 442 | + attributes.forEach(entriesSeen::put); |
| 443 | + assertThat(entriesSeen).containsExactly(entry(doubleKey("key"), 1.23)); |
| 444 | + |
| 445 | + // asMap should show DOUBLE type |
| 446 | + assertThat(attributes.asMap()).containsExactly(entry(doubleKey("key"), 1.23)); |
410 | 447 | } |
411 | 448 |
|
412 | 449 | @Test |
413 | 450 | void complexValueStoredAsBoolean() { |
| 451 | + // When putting a VALUE attribute with a boolean Value, it should be stored as BOOLEAN type |
414 | 452 | ExtendedAttributes attributes = |
415 | 453 | ExtendedAttributes.builder().put(valueKey("key"), Value.of(true)).build(); |
| 454 | + |
| 455 | + // Should be stored as BOOLEAN type internally |
416 | 456 | assertThat(attributes.get(booleanKey("key"))).isEqualTo(true); |
| 457 | + assertThat(attributes.get(valueKey("key"))).isEqualTo(Value.of(true)); |
| 458 | + |
| 459 | + // forEach should show BOOLEAN type |
| 460 | + Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>(); |
| 461 | + attributes.forEach(entriesSeen::put); |
| 462 | + assertThat(entriesSeen).containsExactly(entry(booleanKey("key"), true)); |
| 463 | + |
| 464 | + // asMap should show BOOLEAN type |
| 465 | + assertThat(attributes.asMap()).containsExactly(entry(booleanKey("key"), true)); |
417 | 466 | } |
418 | 467 |
|
419 | 468 | @Test |
420 | 469 | void complexValueStoredAsStringArray() { |
| 470 | + // When putting a VALUE attribute with a homogeneous string array, it should be stored as |
| 471 | + // STRING_ARRAY type |
421 | 472 | ExtendedAttributes attributes = |
422 | 473 | ExtendedAttributes.builder() |
423 | 474 | .put(valueKey("key"), Value.of(Arrays.asList(Value.of("a"), Value.of("b")))) |
424 | 475 | .build(); |
| 476 | + |
| 477 | + // Should be stored as STRING_ARRAY type internally |
425 | 478 | assertThat(attributes.get(stringArrayKey("key"))).containsExactly("a", "b"); |
| 479 | + assertThat(attributes.get(valueKey("key"))) |
| 480 | + .isEqualTo(Value.of(Arrays.asList(Value.of("a"), Value.of("b")))); |
| 481 | + |
| 482 | + // forEach should show STRING_ARRAY type |
| 483 | + Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>(); |
| 484 | + attributes.forEach(entriesSeen::put); |
| 485 | + assertThat(entriesSeen).containsExactly(entry(stringArrayKey("key"), Arrays.asList("a", "b"))); |
| 486 | + |
| 487 | + // asMap should show STRING_ARRAY type |
| 488 | + assertThat(attributes.asMap()) |
| 489 | + .containsExactly(entry(stringArrayKey("key"), Arrays.asList("a", "b"))); |
426 | 490 | } |
427 | 491 |
|
428 | 492 | @Test |
429 | 493 | void complexValueStoredAsLongArray() { |
| 494 | + // When putting a VALUE attribute with a homogeneous long array, it should be stored as |
| 495 | + // LONG_ARRAY type |
430 | 496 | ExtendedAttributes attributes = |
431 | 497 | ExtendedAttributes.builder() |
432 | 498 | .put(valueKey("key"), Value.of(Arrays.asList(Value.of(1L), Value.of(2L)))) |
433 | 499 | .build(); |
| 500 | + |
| 501 | + // Should be stored as LONG_ARRAY type internally |
434 | 502 | assertThat(attributes.get(longArrayKey("key"))).containsExactly(1L, 2L); |
| 503 | + assertThat(attributes.get(valueKey("key"))) |
| 504 | + .isEqualTo(Value.of(Arrays.asList(Value.of(1L), Value.of(2L)))); |
| 505 | + |
| 506 | + // forEach should show LONG_ARRAY type |
| 507 | + Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>(); |
| 508 | + attributes.forEach(entriesSeen::put); |
| 509 | + assertThat(entriesSeen).containsExactly(entry(longArrayKey("key"), Arrays.asList(1L, 2L))); |
| 510 | + |
| 511 | + // asMap should show LONG_ARRAY type |
| 512 | + assertThat(attributes.asMap()) |
| 513 | + .containsExactly(entry(longArrayKey("key"), Arrays.asList(1L, 2L))); |
435 | 514 | } |
436 | 515 |
|
437 | 516 | @Test |
438 | 517 | void complexValueStoredAsDoubleArray() { |
| 518 | + // When putting a VALUE attribute with a homogeneous double array, it should be stored as |
| 519 | + // DOUBLE_ARRAY type |
439 | 520 | ExtendedAttributes attributes = |
440 | 521 | ExtendedAttributes.builder() |
441 | 522 | .put(valueKey("key"), Value.of(Arrays.asList(Value.of(1.1), Value.of(2.2)))) |
442 | 523 | .build(); |
| 524 | + |
| 525 | + // Should be stored as DOUBLE_ARRAY type internally |
443 | 526 | assertThat(attributes.get(doubleArrayKey("key"))).containsExactly(1.1, 2.2); |
| 527 | + assertThat(attributes.get(valueKey("key"))) |
| 528 | + .isEqualTo(Value.of(Arrays.asList(Value.of(1.1), Value.of(2.2)))); |
| 529 | + |
| 530 | + // forEach should show DOUBLE_ARRAY type |
| 531 | + Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>(); |
| 532 | + attributes.forEach(entriesSeen::put); |
| 533 | + assertThat(entriesSeen).containsExactly(entry(doubleArrayKey("key"), Arrays.asList(1.1, 2.2))); |
| 534 | + |
| 535 | + // asMap should show DOUBLE_ARRAY type |
| 536 | + assertThat(attributes.asMap()) |
| 537 | + .containsExactly(entry(doubleArrayKey("key"), Arrays.asList(1.1, 2.2))); |
444 | 538 | } |
445 | 539 |
|
446 | 540 | @Test |
447 | 541 | void complexValueStoredAsBooleanArray() { |
| 542 | + // When putting a VALUE attribute with a homogeneous boolean array, it should be stored as |
| 543 | + // BOOLEAN_ARRAY type |
448 | 544 | ExtendedAttributes attributes = |
449 | 545 | ExtendedAttributes.builder() |
450 | 546 | .put(valueKey("key"), Value.of(Arrays.asList(Value.of(true), Value.of(false)))) |
451 | 547 | .build(); |
| 548 | + |
| 549 | + // Should be stored as BOOLEAN_ARRAY type internally |
452 | 550 | assertThat(attributes.get(booleanArrayKey("key"))).containsExactly(true, false); |
| 551 | + assertThat(attributes.get(valueKey("key"))) |
| 552 | + .isEqualTo(Value.of(Arrays.asList(Value.of(true), Value.of(false)))); |
| 553 | + |
| 554 | + // forEach should show BOOLEAN_ARRAY type |
| 555 | + Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>(); |
| 556 | + attributes.forEach(entriesSeen::put); |
| 557 | + assertThat(entriesSeen) |
| 558 | + .containsExactly(entry(booleanArrayKey("key"), Arrays.asList(true, false))); |
| 559 | + |
| 560 | + // asMap should show BOOLEAN_ARRAY type |
| 561 | + assertThat(attributes.asMap()) |
| 562 | + .containsExactly(entry(booleanArrayKey("key"), Arrays.asList(true, false))); |
453 | 563 | } |
454 | 564 |
|
455 | 565 | @Test |
|
0 commit comments