|
| 1 | +/** |
| 2 | + * Copyright 2019 Pivotal Software, Inc. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micrometer.spring.web.client; |
| 17 | + |
| 18 | +import io.micrometer.core.instrument.Tag; |
| 19 | +import org.junit.Test; |
| 20 | +import org.springframework.http.HttpStatus; |
| 21 | +import org.springframework.http.client.ClientHttpResponse; |
| 22 | +import org.springframework.mock.http.client.MockClientHttpResponse; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.mockito.BDDMockito.given; |
| 28 | +import static org.mockito.Mockito.mock; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link RestTemplateExchangeTags}. |
| 32 | + * |
| 33 | + * @author Nishant Raut |
| 34 | + * @author Brian Clozel |
| 35 | + */ |
| 36 | +public class RestTemplateExchangeTagsTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void outcomeTagIsUnknownWhenResponseIsNull() { |
| 40 | + Tag tag = RestTemplateExchangeTags.outcome(null); |
| 41 | + assertThat(tag.getValue()).isEqualTo("UNKNOWN"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void outcomeTagIsInformationalWhenResponseIs1xx() { |
| 46 | + ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.CONTINUE); |
| 47 | + Tag tag = RestTemplateExchangeTags.outcome(response); |
| 48 | + assertThat(tag.getValue()).isEqualTo("INFORMATIONAL"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void outcomeTagIsSuccessWhenResponseIs2xx() { |
| 53 | + ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.OK); |
| 54 | + Tag tag = RestTemplateExchangeTags.outcome(response); |
| 55 | + assertThat(tag.getValue()).isEqualTo("SUCCESS"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void outcomeTagIsRedirectionWhenResponseIs3xx() { |
| 60 | + ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.MOVED_PERMANENTLY); |
| 61 | + Tag tag = RestTemplateExchangeTags.outcome(response); |
| 62 | + assertThat(tag.getValue()).isEqualTo("REDIRECTION"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void outcomeTagIsClientErrorWhenResponseIs4xx() { |
| 67 | + ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.BAD_REQUEST); |
| 68 | + Tag tag = RestTemplateExchangeTags.outcome(response); |
| 69 | + assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void outcomeTagIsServerErrorWhenResponseIs5xx() { |
| 74 | + ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.BAD_GATEWAY); |
| 75 | + Tag tag = RestTemplateExchangeTags.outcome(response); |
| 76 | + assertThat(tag.getValue()).isEqualTo("SERVER_ERROR"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void outcomeTagIsUnknownWhenResponseThrowsIOException() throws Exception { |
| 81 | + ClientHttpResponse response = mock(ClientHttpResponse.class); |
| 82 | + given(response.getRawStatusCode()).willThrow(IOException.class); |
| 83 | + Tag tag = RestTemplateExchangeTags.outcome(response); |
| 84 | + assertThat(tag.getValue()).isEqualTo("UNKNOWN"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void outcomeTagIsUnknownForCustomResponseStatus() throws Exception { |
| 89 | + ClientHttpResponse response = mock(ClientHttpResponse.class); |
| 90 | + given(response.getRawStatusCode()).willThrow(IllegalArgumentException.class); |
| 91 | + Tag tag = RestTemplateExchangeTags.outcome(response); |
| 92 | + assertThat(tag.getValue()).isEqualTo("UNKNOWN"); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void outcomeTagIsClientErrorWhenResponseIsNonStandardInClientSeries() throws IOException { |
| 97 | + ClientHttpResponse response = mock(ClientHttpResponse.class); |
| 98 | + given(response.getRawStatusCode()).willReturn(490); |
| 99 | + Tag tag = RestTemplateExchangeTags.outcome(response); |
| 100 | + assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void outcomeTagIsUnknownWhenResponseStatusIsInUnknownSeries() throws IOException { |
| 105 | + ClientHttpResponse response = mock(ClientHttpResponse.class); |
| 106 | + given(response.getRawStatusCode()).willReturn(701); |
| 107 | + Tag tag = RestTemplateExchangeTags.outcome(response); |
| 108 | + assertThat(tag.getValue()).isEqualTo("UNKNOWN"); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments