|
2 | 2 |
|
3 | 3 | import static java.util.Collections.emptyList; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
5 | 7 | import static org.mockito.Mockito.mock; |
6 | 8 | import static org.mockito.Mockito.when; |
7 | 9 |
|
| 10 | +import java.util.Collections; |
8 | 11 | import java.util.List; |
9 | 12 | import java.util.Map; |
| 13 | +import java.util.Optional; |
10 | 14 | import org.hypertrace.core.datamodel.AttributeValue; |
11 | 15 | import org.hypertrace.core.datamodel.Attributes; |
12 | 16 | import org.hypertrace.core.datamodel.Event; |
| 17 | +import org.hypertrace.core.datamodel.Resource; |
| 18 | +import org.hypertrace.core.datamodel.StructuredTrace; |
13 | 19 | import org.junit.jupiter.api.Test; |
14 | 20 | import org.junit.jupiter.params.ParameterizedTest; |
15 | 21 | import org.junit.jupiter.params.provider.ValueSource; |
@@ -63,4 +69,27 @@ void testGetStringListAttribute_multiValuedList() { |
63 | 69 | final List<String> result = SpanAttributeUtils.getStringListAttribute(event, "attributeKey"); |
64 | 70 | assertEquals(List.of("value1", "value2", "3"), result); |
65 | 71 | } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testGetResourceStringAttribute() { |
| 75 | + final Event event = mock(Event.class); |
| 76 | + final StructuredTrace trace = mock(StructuredTrace.class); |
| 77 | + Resource resource = |
| 78 | + Resource.newBuilder() |
| 79 | + .setAttributes( |
| 80 | + Attributes.newBuilder() |
| 81 | + .setAttributeMap( |
| 82 | + Map.of("key1", AttributeValue.newBuilder().setValue("value1").build())) |
| 83 | + .build()) |
| 84 | + .build(); |
| 85 | + when(event.getResourceIndex()).thenReturn(0); |
| 86 | + when(trace.getResourceList()).thenReturn(Collections.singletonList(resource)); |
| 87 | + Optional<String> valueAbsent = |
| 88 | + SpanAttributeUtils.getResourceStringAttribute(trace, event, "key2"); |
| 89 | + Optional<String> valuePresent = |
| 90 | + SpanAttributeUtils.getResourceStringAttribute(trace, event, "key1"); |
| 91 | + assertFalse(valueAbsent.isPresent()); |
| 92 | + assertTrue(valuePresent.isPresent()); |
| 93 | + assertEquals("value1", valuePresent.get()); |
| 94 | + } |
66 | 95 | } |
0 commit comments