/**
 * Copyright 2017 Pivotal Software, Inc.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micrometer.spring.web.client;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.client.match.MockRestRequestMatchers;
import org.springframework.test.web.client.response.MockRestResponseCreators;
import org.springframework.web.client.RestTemplate;

import static java.util.stream.StreamSupport.stream;
import static org.assertj.core.api.Assertions.assertThat;

/**
 * Tests for {@link MetricsRestTemplateCustomizer}.
 *
 * @author Jon Schneider
 */
public class MetricsRestTemplateCustomizerTest {

    private MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
    private RestTemplate restTemplate = new RestTemplate();
    private MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);

    @Before
    public void before() {
        MetricsRestTemplateCustomizer customizer = new MetricsRestTemplateCustomizer(
            registry, new DefaultRestTemplateExchangeTagsProvider(), "http.client.requests");
        customizer.customize(restTemplate);
    }

    @Test
    public void interceptRestTemplate() {
        mockServer.expect(MockRestRequestMatchers.requestTo("/test/123"))
            .andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
            .andRespond(MockRestResponseCreators.withSuccess("OK",
                MediaType.APPLICATION_JSON));

        String result = restTemplate.getForObject("/test/{id}", String.class, 123);

        assertThat(registry.get("http.client.requests").meters())
            .anySatisfy(m -> assertThat(stream(m.getId().getTags().spliterator(), false).map(Tag::getKey)).doesNotContain("bucket"));

        assertThat(registry.get("http.client.requests")
            .tags("method", "GET", "uri", "/test/{id}", "status", "200")
            .timer().count()).isEqualTo(1L);

        assertThat(result).isEqualTo("OK");

        mockServer.verify();
    }

    /**
     * Issue #283
     */
    @Test
    public void normalizeUriToContainLeadingSlash() {
        mockServer.expect(MockRestRequestMatchers.requestTo("test/123"))
            .andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
            .andRespond(MockRestResponseCreators.withSuccess("OK",
                MediaType.APPLICATION_JSON));

        String result = restTemplate.getForObject("test/{id}", String.class, 123);

        assertThat(registry.find("http.client.requests").tags("uri", "/test/{id}").timer())
            .isNotNull();
        assertThat(result).isEqualTo("OK");

        mockServer.verify();
    }
}

@@ -1321,16 +1321,127 @@
 plate;%0A%0A
+import java.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0A
 import s
@@ -3659,32 +3659,811 @@
 EqualTo(%22OK%22);%0A%0A
+        mockServer.verify();%0A    %7D%0A%0A    @Test%0A    public void interceptRestTemplateWithUri() throws URISyntaxException %7B%0A        mockServer.expect(MockRestRequestMatchers.requestTo(%22http://localhost/test/123%22))%0A            .andExpect(MockRestRequestMatchers.method(HttpMethod.GET))%0A            .andRespond(MockRestResponseCreators.withSuccess(%22OK%22,%0A                MediaType.APPLICATION_JSON));%0A%0A        String result = restTemplate.getForObject(new URI(%22http://localhost/test/123%22), String.class);%0A%0A        List%3CTag%3E tags = Collections.emptyList();%0A        registry.find(%22http.client.requests%22).tags(tags);%0A        %0A        assertThat(registry.find(%22http.client.requests%22).tags(%22uri%22, %22/test/123%22).timer())%0A            .isNotNull();%0A        assertThat(result).isEqualTo(%22OK%22);%0A%0A
         mockServ

Add failing test to check RestTemplate URI support