Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert changes to make tests compatible with Java 8
Browse files Browse the repository at this point in the history
psx95 committed Jun 24, 2024
1 parent c04a59f commit cfa27e5
Showing 5 changed files with 347 additions and 423 deletions.
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import com.google.cloud.opentelemetry.detection.DetectedPlatform;
import com.google.cloud.opentelemetry.detection.GCPPlatformDetector;
@@ -38,7 +37,7 @@ public class GCPResourceTest {
private static final String DUMMY_PROJECT_ID = "google-pid";
private final ConfigProperties mockConfigProps = Mockito.mock(ConfigProperties.class);
private final Map<String, String> mockGKECommonAttributes =
new HashMap<String, String>() {
new HashMap<>() {
{
put(GKE_CLUSTER_NAME, "gke-cluster");
put(GKE_HOST_ID, "host1");
@@ -48,7 +47,7 @@ public class GCPResourceTest {
// Mock Platforms
private DetectedPlatform generateMockGCEPlatform() {
Map<String, String> mockAttributes =
new HashMap<String, String>() {
new HashMap<>() {
{
put(GCE_CLOUD_REGION, "australia-southeast1");
put(GCE_AVAILABILITY_ZONE, "australia-southeast1-b");
@@ -93,7 +92,7 @@ private DetectedPlatform generateMockServerlessPlatform(
throw new IllegalArgumentException();
}
Map<String, String> mockAttributes =
new HashMap<String, String>() {
new HashMap<>() {
{
put(SERVERLESS_COMPUTE_NAME, "serverless-app");
put(SERVERLESS_COMPUTE_REVISION, "v2");
@@ -111,7 +110,7 @@ private DetectedPlatform generateMockServerlessPlatform(

private DetectedPlatform generateMockGAEPlatform() {
Map<String, String> mockAttributes =
new HashMap<String, String>() {
new HashMap<>() {
{
put(GAE_MODULE_NAME, "gae-app");
put(GAE_APP_VERSION, "v1");
@@ -130,7 +129,7 @@ private DetectedPlatform generateMockGAEPlatform() {

private DetectedPlatform generateMockUnknownPlatform() {
Map<String, String> mockAttributes =
new HashMap<String, String>() {
new HashMap<>() {
{
put(GCE_INSTANCE_ID, "instance-id");
put(GCE_CLOUD_REGION, "australia-southeast1");
@@ -386,12 +385,8 @@ public void testUnknownPlatformResourceAttributesMapping() {
public void findsWithServiceLoader() {
ServiceLoader<ResourceProvider> services =
ServiceLoader.load(ResourceProvider.class, getClass().getClassLoader());
while (services.iterator().hasNext()) {
ResourceProvider provider = services.iterator().next();
if (provider instanceof GCPResource) {
return;
}
}
fail("Could not load GCP Resource detector using serviceloader");
assertTrue(
services.stream().anyMatch(provider -> provider.type().equals(GCPResource.class)),
"Could not load GCP Resource detector using serviceloader, found: " + services);
}
}
Original file line number Diff line number Diff line change
@@ -85,9 +85,7 @@
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryData;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@@ -421,8 +419,7 @@ public void testExportWithNonSupportedMetricTypeReturnsFailure() {
public void testExportWithMonitoredResourceMappingSucceeds() {
MonitoredResourceDescription monitoredResourceDescription =
new MonitoredResourceDescription(
"custom_mr_instance",
new HashSet<>(Arrays.asList("service_instance_id", "host_id", "location")));
"custom_mr_instance", Set.of("service_instance_id", "host_id", "location"));

// controls which resource attributes end up in metric labels
Predicate<AttributeKey<?>> customAttributesFilter =
@@ -621,8 +618,7 @@ public void testExportWithMonitoredResourceMappingSucceeds_NoMRLabels() {
public void testExportWithMonitoredResourceMappingSucceeds_NoResourceLabels() {
MonitoredResourceDescription monitoredResourceDescription =
new MonitoredResourceDescription(
"custom_mr_instance",
new HashSet<>(Arrays.asList("service_instance_id", "host_id", "location")));
"custom_mr_instance", Set.of("service_instance_id", "host_id", "location"));

// controls which resource attributes end up in metric labels
Predicate<AttributeKey<?>> customAttributesFilter =
Original file line number Diff line number Diff line change
@@ -29,9 +29,8 @@
import com.google.cloud.opentelemetry.metric.MetricConfiguration.Builder;
import io.opentelemetry.api.common.AttributeKey;
import java.time.Duration;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -62,8 +61,7 @@ public void testDefaultConfigurationSucceeds() {
public void testSetAllConfigurationFieldsSucceeds() {
Predicate<AttributeKey<?>> allowAllPredicate = attributeKey -> true;
MonitoredResourceDescription customMRMapping =
new MonitoredResourceDescription(
"custom_mr", new HashSet<>(Arrays.asList("instance_id", "foo_bar", "host_id")));
new MonitoredResourceDescription("custom_mr", Set.of("instance_id", "foo_bar", "host_id"));

MetricConfiguration configuration =
MetricConfiguration.builder()
Original file line number Diff line number Diff line change
@@ -20,12 +20,8 @@
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurablePropagatorProvider;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -37,20 +33,19 @@ public class AutoConfigureTest {
public void findsWithServiceLoader() {
ServiceLoader<ConfigurablePropagatorProvider> services =
ServiceLoader.load(ConfigurablePropagatorProvider.class, getClass().getClassLoader());

List<ConfigurablePropagatorProvider> servicesList = new ArrayList<>();
services.iterator().forEachRemaining(servicesList::add);

assertTrue(
"Could not load gcp_oneway propagator using serviceloader, found: " + services,
servicesList.stream()
services.stream()
.anyMatch(
provider -> (provider instanceof OneWayXCloudTraceConfigurablePropagatorProvider)));
provider ->
provider.type().equals(OneWayXCloudTraceConfigurablePropagatorProvider.class)));

assertTrue(
"Could not load gcp propagator using serviceloader, found: " + services,
servicesList.stream()
.anyMatch(provider -> (provider instanceof XCloudTraceConfigurablePropagatorProvider)));
services.stream()
.anyMatch(
provider ->
provider.type().equals(XCloudTraceConfigurablePropagatorProvider.class)));
}

@Test
@@ -69,12 +64,15 @@ private static ContextPropagators findsWithAutoConfigure(String propagator) {
.disableShutdownHook()
.addPropertiesSupplier(
() ->
Stream.of(
new SimpleEntry<>("otel.propagators", propagator),
new SimpleEntry<>("otel.traces.exporter", "none"),
new SimpleEntry<>("otel.metrics.exporter", "none"),
new SimpleEntry<>("otel.logs.exporter", "none"))
.collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue)))
Map.of(
"otel.propagators",
propagator,
"otel.traces.exporter",
"none",
"otel.metrics.exporter",
"none",
"otel.logs.exporter",
"none"))
.build();
return sdk.getOpenTelemetrySdk().getPropagators();
}
Loading

0 comments on commit cfa27e5

Please sign in to comment.