|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.exception; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 10 | + |
| 11 | +import java.lang.reflect.Type; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.DisplayName; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import io.jooby.StatusCode; |
| 17 | +import io.jooby.problem.HttpProblem; |
| 18 | + |
| 19 | +class TypeMismatchExceptionTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + @DisplayName("Verify constructor with cause stores name, message, and cause correctly") |
| 23 | + void testConstructorWithCause() { |
| 24 | + String name = "age"; |
| 25 | + Type type = int.class; |
| 26 | + Throwable cause = new NumberFormatException("For input string: \"abc\""); |
| 27 | + |
| 28 | + TypeMismatchException exception = new TypeMismatchException(name, type, cause); |
| 29 | + |
| 30 | + assertEquals(name, exception.getName()); |
| 31 | + assertEquals("Cannot convert value: 'age', to: 'int'", exception.getMessage()); |
| 32 | + assertEquals(cause, exception.getCause()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + @DisplayName("Verify constructor without cause sets cause to null") |
| 37 | + void testConstructorWithoutCause() { |
| 38 | + String name = "active"; |
| 39 | + Type type = boolean.class; |
| 40 | + |
| 41 | + TypeMismatchException exception = new TypeMismatchException(name, type); |
| 42 | + |
| 43 | + assertEquals(name, exception.getName()); |
| 44 | + assertEquals("Cannot convert value: 'active', to: 'boolean'", exception.getMessage()); |
| 45 | + assertNull(exception.getCause()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + @DisplayName("Verify toHttpProblem returns a correctly populated HttpProblem instance") |
| 50 | + void testToHttpProblem() { |
| 51 | + String name = "price"; |
| 52 | + Type type = double.class; |
| 53 | + TypeMismatchException exception = new TypeMismatchException(name, type); |
| 54 | + |
| 55 | + HttpProblem problem = exception.toHttpProblem(); |
| 56 | + |
| 57 | + // BadRequestException usually defaults to 400 |
| 58 | + assertEquals(StatusCode.BAD_REQUEST.value(), problem.getStatus()); |
| 59 | + assertEquals("Type Mismatch", problem.getTitle()); |
| 60 | + assertEquals("Cannot convert value: 'price', to: 'double'", problem.getDetail()); |
| 61 | + } |
| 62 | +} |
0 commit comments