Skip to content

Commit

Permalink
Added the Test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
parth1601 committed Jul 11, 2023
1 parent c9171df commit 288ee22
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public SdkLoggerProvider build() {
*/
public SdkLoggerProviderBuilder addResource(Resource resource) {
Objects.requireNonNull(resource, "resource");
this.resource.merge(resource);
this.resource = this.resource.merge(resource);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.logs;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.resources.Resource;
import org.junit.jupiter.api.Test;

public class SdkLoggerProviderBuilderTest {

@Test
void addResource() {
Resource customResource =
Resource.create(
Attributes.of(
AttributeKey.stringKey("custom_attribute_key"), "custom_attribute_value"));

SdkLoggerProvider sdkLoggerProvider =
SdkLoggerProvider.builder().addResource(customResource).build();

assertThat(sdkLoggerProvider)
.extracting("sharedState")
.hasFieldOrPropertyWithValue("resource", Resource.getDefault().merge(customResource));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public SdkMeterProvider build() {
*/
public SdkMeterProviderBuilder addResource(Resource resource) {
Objects.requireNonNull(resource, "resource");
this.resource.merge(resource);
this.resource = this.resource.merge(resource);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import org.junit.jupiter.api.Test;
Expand All @@ -23,4 +25,22 @@ void defaultResource() {
.extracting("sharedState")
.hasFieldOrPropertyWithValue("resource", Resource.getDefault());
}

@Test
void addResource() {
Resource customResource =
Resource.create(
Attributes.of(
AttributeKey.stringKey("custom_attribute_key"), "custom_attribute_value"));

SdkMeterProvider sdkMeterProvider =
SdkMeterProvider.builder()
.registerMetricReader(InMemoryMetricReader.create())
.addResource(customResource)
.build();

assertThat(sdkMeterProvider)
.extracting("sharedState")
.hasFieldOrPropertyWithValue("resource", Resource.getDefault().merge(customResource));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public SdkTracerProvider build() {
*/
public SdkTracerProviderBuilder addResource(Resource resource) {
Objects.requireNonNull(resource, "resource");
this.resource.merge(resource);
this.resource = this.resource.merge(resource);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.trace;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.resources.Resource;
import org.junit.jupiter.api.Test;

public class SdkTracerProviderBuilderTest {

@Test
void addResource() {
Resource customResource =
Resource.create(
Attributes.of(
AttributeKey.stringKey("custom_attribute_key"), "custom_attribute_value"));

SdkTracerProvider sdkTracerProvider =
SdkTracerProvider.builder().addResource(customResource).build();

assertThat(sdkTracerProvider)
.extracting("sharedState")
.hasFieldOrPropertyWithValue("resource", Resource.getDefault().merge(customResource));
}
}

0 comments on commit 288ee22

Please sign in to comment.