|
|
@ -1,5 +1,5 @@
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Copyright 2012-2018 the original author or authors.
|
|
|
|
* Copyright 2012-2019 the original author or authors.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
@ -16,23 +16,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.actuate.metrics.web.client;
|
|
|
|
package org.springframework.boot.actuate.metrics.web.client;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
|
|
import io.micrometer.core.instrument.Tag;
|
|
|
|
import io.micrometer.core.instrument.Tag;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
|
|
|
import org.springframework.http.client.ClientHttpResponse;
|
|
|
|
import org.springframework.mock.http.client.MockClientHttpResponse;
|
|
|
|
import org.springframework.mock.http.client.MockClientHttpResponse;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
import static org.mockito.BDDMockito.given;
|
|
|
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Tests for {@link RestTemplateExchangeTags}.
|
|
|
|
* Tests for {@link RestTemplateExchangeTags}.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Nishant Raut
|
|
|
|
* @author Nishant Raut
|
|
|
|
|
|
|
|
* @author Brian Clozel
|
|
|
|
|
|
|
|
* @author Brian Clozel
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class RestTemplateExchangeTagsTests {
|
|
|
|
public class RestTemplateExchangeTagsTests {
|
|
|
|
|
|
|
|
|
|
|
|
private MockClientHttpResponse response;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void outcomeTagIsUnknownWhenResponseStatusIsNull() {
|
|
|
|
public void outcomeTagIsUnknownWhenResponseStatusIsNull() {
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(null);
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(null);
|
|
|
@ -41,40 +46,58 @@ public class RestTemplateExchangeTagsTests {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void outcomeTagIsInformationalWhenResponseIs1xx() {
|
|
|
|
public void outcomeTagIsInformationalWhenResponseIs1xx() {
|
|
|
|
this.response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.CONTINUE);
|
|
|
|
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(),
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(this.response);
|
|
|
|
HttpStatus.CONTINUE);
|
|
|
|
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(response);
|
|
|
|
assertThat(tag.getValue()).isEqualTo("INFORMATIONAL");
|
|
|
|
assertThat(tag.getValue()).isEqualTo("INFORMATIONAL");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void outcomeTagIsSuccessWhenResponseIs2xx() {
|
|
|
|
public void outcomeTagIsSuccessWhenResponseIs2xx() {
|
|
|
|
this.response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.OK);
|
|
|
|
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(),
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(this.response);
|
|
|
|
HttpStatus.OK);
|
|
|
|
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(response);
|
|
|
|
assertThat(tag.getValue()).isEqualTo("SUCCESS");
|
|
|
|
assertThat(tag.getValue()).isEqualTo("SUCCESS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void outcomeTagIsRedirectionWhenResponseIs3xx() {
|
|
|
|
public void outcomeTagIsRedirectionWhenResponseIs3xx() {
|
|
|
|
this.response = new MockClientHttpResponse("foo".getBytes(),
|
|
|
|
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(),
|
|
|
|
HttpStatus.MOVED_PERMANENTLY);
|
|
|
|
HttpStatus.MOVED_PERMANENTLY);
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(this.response);
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(response);
|
|
|
|
assertThat(tag.getValue()).isEqualTo("REDIRECTION");
|
|
|
|
assertThat(tag.getValue()).isEqualTo("REDIRECTION");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void outcomeTagIsClientErrorWhenResponseIs4xx() {
|
|
|
|
public void outcomeTagIsClientErrorWhenResponseIs4xx() {
|
|
|
|
this.response = new MockClientHttpResponse("foo".getBytes(),
|
|
|
|
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(),
|
|
|
|
HttpStatus.BAD_REQUEST);
|
|
|
|
HttpStatus.BAD_REQUEST);
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(this.response);
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(response);
|
|
|
|
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
|
|
|
|
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void outcomeTagIsServerErrorWhenResponseIs5xx() {
|
|
|
|
public void outcomeTagIsServerErrorWhenResponseIs5xx() {
|
|
|
|
this.response = new MockClientHttpResponse("foo".getBytes(),
|
|
|
|
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(),
|
|
|
|
HttpStatus.BAD_GATEWAY);
|
|
|
|
HttpStatus.BAD_GATEWAY);
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(this.response);
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(response);
|
|
|
|
assertThat(tag.getValue()).isEqualTo("SERVER_ERROR");
|
|
|
|
assertThat(tag.getValue()).isEqualTo("SERVER_ERROR");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void outcomeTagIsUnknownWhenResponseThrowsIOException() throws Exception {
|
|
|
|
|
|
|
|
ClientHttpResponse response = mock(ClientHttpResponse.class);
|
|
|
|
|
|
|
|
given(response.getStatusCode()).willThrow(IOException.class);
|
|
|
|
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(response);
|
|
|
|
|
|
|
|
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void outcomeTagIsUnknownForCustomResponseStatus() throws Exception {
|
|
|
|
|
|
|
|
ClientHttpResponse response = mock(ClientHttpResponse.class);
|
|
|
|
|
|
|
|
given(response.getStatusCode()).willThrow(IllegalArgumentException.class);
|
|
|
|
|
|
|
|
Tag tag = RestTemplateExchangeTags.outcome(response);
|
|
|
|
|
|
|
|
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|