|
| 1 | +/* |
| 2 | + * Copyright 2016-2020 the original author or authors. |
| 3 | + * |
| 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 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 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 | + |
| 17 | +package org.springframework.credhub.core; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import reactor.core.publisher.Mono; |
| 21 | +import reactor.test.StepVerifier; |
| 22 | + |
| 23 | +import org.springframework.http.HttpStatus; |
| 24 | +import org.springframework.http.ResponseEntity; |
| 25 | +import org.springframework.web.reactive.function.client.ClientResponse; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 29 | + |
| 30 | +class ExceptionUtilsUnitTests { |
| 31 | + |
| 32 | + @Test |
| 33 | + void throwExceptionOnErrorPreservesStatusCode() { |
| 34 | + ResponseEntity<String> response = ResponseEntity.status(HttpStatus.FORBIDDEN).body("access denied"); |
| 35 | + |
| 36 | + assertThatExceptionOfType(CredHubException.class) |
| 37 | + .isThrownBy(() -> ExceptionUtils.throwExceptionOnError(response)) |
| 38 | + .satisfies((ex) -> assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN)); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void throwExceptionOnErrorDoesNothingForOk() { |
| 43 | + ResponseEntity<String> response = ResponseEntity.ok("success"); |
| 44 | + |
| 45 | + ExceptionUtils.throwExceptionOnError(response); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void buildErrorPreservesStatusCodeAndResponseBody() { |
| 50 | + ClientResponse clientResponse = ClientResponse.create(HttpStatus.FORBIDDEN) |
| 51 | + .body("{\"error\":\"access denied\"}") |
| 52 | + .build(); |
| 53 | + |
| 54 | + Mono<Throwable> error = ExceptionUtils.buildError(clientResponse); |
| 55 | + |
| 56 | + StepVerifier.create(error).assertNext((ex) -> { |
| 57 | + assertThat(ex).isInstanceOf(CredHubException.class); |
| 58 | + CredHubException credHubException = (CredHubException) ex; |
| 59 | + assertThat(credHubException.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); |
| 60 | + assertThat(credHubException.getResponseBodyAsString()).isEqualTo("{\"error\":\"access denied\"}"); |
| 61 | + }).verifyComplete(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void buildErrorHandlesEmptyBody() { |
| 66 | + ClientResponse clientResponse = ClientResponse.create(HttpStatus.INTERNAL_SERVER_ERROR).build(); |
| 67 | + |
| 68 | + Mono<Throwable> error = ExceptionUtils.buildError(clientResponse); |
| 69 | + |
| 70 | + StepVerifier.create(error).assertNext((ex) -> { |
| 71 | + assertThat(ex).isInstanceOf(CredHubException.class); |
| 72 | + CredHubException credHubException = (CredHubException) ex; |
| 73 | + assertThat(credHubException.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); |
| 74 | + assertThat(credHubException.getResponseBodyAsString()).isEmpty(); |
| 75 | + }).verifyComplete(); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments