

package org.springframework.boot.actuate.metrics.web.client;

import io.micrometer.observation.ObservationConvention;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import org.junit.jupiter.api.Test;

import org.springframework.http.client.observation.DefaultClientHttpObservationConvention;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;

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

/**
 * Tests for {@link ObservationRestTemplateCustomizer}.
 *
 * @author Brian Clozel
 */
class ObservationRestTemplateCustomizerTests {

	private static final String TEST_METRIC_NAME = "http.test.metric.name";

	private ObservationRegistry observationRegistry = TestObservationRegistry.create();

	private RestTemplate restTemplate = new RestTemplate();

	private ObservationRestTemplateCustomizer customizer = new ObservationRestTemplateCustomizer(
			this.observationRegistry, new DefaultClientHttpObservationConvention(TEST_METRIC_NAME));

	@Test
	void shouldCustomizeObservationConfiguration() {
		this.customizer.customize(this.restTemplate);
		assertThat((ObservationRegistry) ReflectionTestUtils.getField(this.restTemplate, "observationRegistry"))
				.isEqualTo(this.observationRegistry);
		assertThat((ObservationConvention<?>) ReflectionTestUtils.getField(this.restTemplate, "observationConvention"))
				.isInstanceOf(DefaultClientHttpObservationConvention.class).extracting("name")
				.isEqualTo(TEST_METRIC_NAME);
	}

}
