Repository: open-telemetry/opentelemetry-java
File: sdk-extensions/autoconfigure/src/testIncubating/java/io/opentelemetry/sdk/autoconfigure/DeclarativeConfigurationTest.java (Java)

Code before:
    GlobalOpenTelemetry.resetForTest();
    GlobalConfigProvider.resetForTest();
  }

  @Test
  @SuppressLogger(AutoConfiguredOpenTelemetrySdkBuilder.class)
  void configFile_fileNotFound() {
    assertThatThrownBy(
            () ->
                AutoConfiguredOpenTelemetrySdk.builder()
                    .setConfig(
                        DefaultConfigProperties.createFromMap(
                            Collections.singletonMap("otel.experimental.config.file", "foo")))
                    .build())
        .isInstanceOf(ConfigurationException.class)
        .hasMessageContaining("Configuration file not found");

    assertThatThrownBy(
            () ->
                AutoConfiguredOpenTelemetrySdk.builder()
                    .setConfig(
                        DefaultConfigProperties.createFromMap(
                            Collections.singletonMap("otel.experimental.config.file", "")))
                    .build())
        .hasMessageContaining(
            "otel.metrics.exporter set to \"otlp\" but opentelemetry-exporter-otlp not found on "
                + "classpath. Make sure to add it as a dependency.");
  }

  @Test
  void configFile_Valid() {
    ConfigProperties config =
        DefaultConfigProperties.createFromMap(
            Collections.singletonMap("otel.experimental.config.file", configFilePath.toString()));
    OpenTelemetrySdk expectedSdk =
        OpenTelemetrySdk.builder()
            .setTracerProvider(
                SdkTracerProvider.builder()
                    .setResource(
                        Resource.getDefault().toBuilder().put("service.name", "test").build())
                    .addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()))
                    .build())
            .build();
    cleanup.addCloseable(expectedSdk);
    AutoConfiguredOpenTelemetrySdkBuilder builder = spy(AutoConfiguredOpenTelemetrySdk.builder());
    Thread thread = new Thread();
    doReturn(thread).when(builder).shutdownHook(any());

    AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk =
        builder.setConfig(config).build();
    cleanup.addCloseable(autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk());

Review comment (suggestion):
Is it the right error message in this scenario? I think we should rather report something like:
`No config file provided. otel.experimental.config.file property cannot be blank`

Code after:
            + "    example:\n"
            + "      key: value\n";
    configFilePath = tempDir.resolve("otel-config.yaml");
    Files.write(configFilePath, yaml.getBytes(StandardCharsets.UTF_8));
    GlobalOpenTelemetry.resetForTest();
    GlobalConfigProvider.resetForTest();
  }

  @Test
  @SuppressLogger(AutoConfiguredOpenTelemetrySdkBuilder.class)
  void configFile_fileNotFound() {
    assertThatThrownBy(
            () ->
                AutoConfiguredOpenTelemetrySdk.builder()
                    .setConfig(
                        DefaultConfigProperties.createFromMap(
                            Collections.singletonMap("otel.experimental.config.file", "foo")))
                    .build())
        .isInstanceOf(ConfigurationException.class)
        .hasMessageContaining("Configuration file not found");

    assertThatCode(
            () ->
                AutoConfiguredOpenTelemetrySdk.builder()
                    .addPropertiesSupplier(() -> singletonMap("otel.experimental.config.file", ""))
                    .addPropertiesSupplier(() -> singletonMap("otel.sdk.disabled", "true"))
                    .build())
        .doesNotThrowAnyException();
  }

  @Test
  void configFile_Valid() {
    ConfigProperties config =
        DefaultConfigProperties.createFromMap(
            Collections.singletonMap("otel.experimental.config.file", configFilePath.toString()));
    OpenTelemetrySdk expectedSdk =
        OpenTelemetrySdk.builder()
            .setTracerProvider(
                SdkTracerProvider.builder()
                    .setResource(
                        Resource.getDefault().toBuilder().put("service.name", "test").build())
                    .addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()))
                    .build())
            .build();
    cleanup.addCloseable(expectedSdk);
    AutoConfiguredOpenTelemetrySdkBuilder builder = spy(AutoConfiguredOpenTelemetrySdk.builder());
    Thread thread = new Thread();
    doReturn(thread).when(builder).shutdownHook(any());

    AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk =
        builder.setConfig(config).build();