|
| 1 | +/** |
| 2 | + * Copyright (C) 2019-2023 Expedia, Inc. |
| 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 | + * http://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 | +package com.expediagroup.beekeeper.api.error; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.springframework.http.HttpStatus; |
| 20 | +import org.springframework.http.ResponseEntity; |
| 21 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 22 | +import org.springframework.data.mapping.PropertyReferenceException; |
| 23 | +import org.springframework.data.mapping.PropertyPath; |
| 24 | +import org.springframework.data.util.ClassTypeInformation; |
| 25 | +import org.springframework.data.util.TypeInformation; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +import com.expediagroup.beekeeper.api.error.BeekeeperExceptionHandler; |
| 30 | +import com.expediagroup.beekeeper.api.error.ErrorResponse; |
| 31 | +import com.expediagroup.beekeeper.core.model.HousekeepingPath; |
| 32 | + |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +public class BeekeeperExceptionHandlerTest { |
| 37 | + |
| 38 | + private final BeekeeperExceptionHandler exceptionHandler = new BeekeeperExceptionHandler(); |
| 39 | + |
| 40 | + @Test |
| 41 | + public void handlePropertyReferenceException_ShouldReturnBadRequest() { |
| 42 | + String propertyName = "string"; |
| 43 | + TypeInformation<?> typeInformation = ClassTypeInformation.from(HousekeepingPath.class); |
| 44 | + List<PropertyPath> baseProperty = Collections.emptyList(); |
| 45 | + PropertyReferenceException exception = new PropertyReferenceException(propertyName, typeInformation, baseProperty); |
| 46 | + |
| 47 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 48 | + request.setRequestURI("/api/v1/database/testDb/table/testTable/unreferenced-paths"); |
| 49 | + |
| 50 | + ResponseEntity<ErrorResponse> response = exceptionHandler.handlePropertyReferenceException(exception, request); |
| 51 | + |
| 52 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); |
| 53 | + |
| 54 | + ErrorResponse errorResponse = response.getBody(); |
| 55 | + assertThat(errorResponse).isNotNull(); |
| 56 | + assertThat(errorResponse.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); |
| 57 | + assertThat(errorResponse.getError()).isEqualTo("Bad Request"); |
| 58 | + assertThat(errorResponse.getMessage()).isEqualTo(exception.getMessage()); |
| 59 | + assertThat(errorResponse.getPath()).isEqualTo("/api/v1/database/testDb/table/testTable/unreferenced-paths"); |
| 60 | + assertThat(errorResponse.getTimestamp()).isNotNull(); |
| 61 | + } |
| 62 | +} |
0 commit comments