Test ResourceBuilder with null inputs (#3672)

This commit is contained in:
Anuraag Agrawal 2021-09-29 01:16:52 +09:00 committed by GitHub
parent 534eeac73d
commit b7e469f861
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View File

@ -143,7 +143,7 @@ public class ResourceBuilder {
/** Puts a {@link AttributeKey} with associated value into this. */
public <T> ResourceBuilder put(AttributeKey<T> key, T value) {
if (key != null && key.getKey() != null && key.getKey().length() > 0 && value != null) {
if (key != null && key.getKey() != null && !key.getKey().isEmpty() && value != null) {
attributesBuilder.put(key, value);
}
return this;
@ -151,7 +151,7 @@ public class ResourceBuilder {
/** Puts a {@link AttributeKey} with associated value into this. */
public ResourceBuilder put(AttributeKey<Long> key, int value) {
if (key != null && key.getKey() != null) {
if (key != null && key.getKey() != null && !key.getKey().isEmpty()) {
attributesBuilder.put(key, value);
}
return this;

View File

@ -17,6 +17,7 @@ import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.testing.EqualsTester;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
@ -93,6 +94,35 @@ class ResourceTest {
assertThat(resource.getAttributes().size()).isEqualTo(7);
}
@Test
void builder_ignoreNull() {
Resource resource =
Resource.builder()
.put((String) null, "cat")
.put("bear", (String) null)
.put(null, 1.0)
.put(null, false)
.put(null, "foo", "bar")
.put("dog", (String[]) null)
.put(null, 1.0, 2.0)
.put("mouse", (double[]) null)
.put(null, true, false)
.put("elephant", (boolean[]) null)
.put((AttributeKey<String>) null, "foo")
.put(stringKey("monkey"), null)
.put(stringKey(null), "foo")
.put(stringKey(""), "foo")
.put((AttributeKey<Long>) null, 10)
.put(longKey(null), 10)
.put(longKey(""), 10)
.putAll((Attributes) null)
.putAll((Resource) null)
.setSchemaUrl(null)
.build();
assertThat(resource).isEqualTo(Resource.empty());
}
@Test
void create_NullEmptyArray() {
AttributesBuilder attributes = Attributes.builder();