|
| 1 | +package backend.fullstack.exceptions; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.springframework.http.HttpStatus; |
| 9 | + |
| 10 | +class ExceptionsTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + void appExceptionStoresStatusAndErrorCodeWithoutCause() { |
| 14 | + TestAppException exception = new TestAppException("boom", HttpStatus.BAD_REQUEST, "TEST_ERROR"); |
| 15 | + |
| 16 | + assertEquals("boom", exception.getMessage()); |
| 17 | + assertEquals(HttpStatus.BAD_REQUEST, exception.getHttpStatus()); |
| 18 | + assertEquals("TEST_ERROR", exception.getErrorCode()); |
| 19 | + assertNull(exception.getCause()); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + void appExceptionStoresStatusAndErrorCodeWithCause() { |
| 24 | + RuntimeException cause = new RuntimeException("root"); |
| 25 | + TestAppException exception = new TestAppException("boom", cause, HttpStatus.CONFLICT, "TEST_ERROR"); |
| 26 | + |
| 27 | + assertEquals("boom", exception.getMessage()); |
| 28 | + assertEquals(HttpStatus.CONFLICT, exception.getHttpStatus()); |
| 29 | + assertEquals("TEST_ERROR", exception.getErrorCode()); |
| 30 | + assertSame(cause, exception.getCause()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void accessDeniedExceptionSingleArgumentConstructor() { |
| 35 | + AccessDeniedException exception = new AccessDeniedException("forbidden"); |
| 36 | + |
| 37 | + assertEquals("forbidden", exception.getMessage()); |
| 38 | + assertEquals(HttpStatus.FORBIDDEN, exception.getHttpStatus()); |
| 39 | + assertEquals("ACCESS_DENIED", exception.getErrorCode()); |
| 40 | + assertNull(exception.getCause()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void accessDeniedExceptionMessageAndCauseConstructor() { |
| 45 | + RuntimeException cause = new RuntimeException("root"); |
| 46 | + AccessDeniedException exception = new AccessDeniedException("forbidden", cause); |
| 47 | + |
| 48 | + assertEquals("forbidden", exception.getMessage()); |
| 49 | + assertEquals(HttpStatus.FORBIDDEN, exception.getHttpStatus()); |
| 50 | + assertEquals("ACCESS_DENIED", exception.getErrorCode()); |
| 51 | + assertSame(cause, exception.getCause()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void invalidPasswordExceptionSetsUnauthorizedMetadata() { |
| 56 | + InvalidPasswordException exception = new InvalidPasswordException("invalid credentials"); |
| 57 | + |
| 58 | + assertEquals("invalid credentials", exception.getMessage()); |
| 59 | + assertEquals(HttpStatus.UNAUTHORIZED, exception.getHttpStatus()); |
| 60 | + assertEquals("INVALID_PASSWORD", exception.getErrorCode()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void invalidThresholdExceptionUsesDefaultMessageAndMetadata() { |
| 65 | + InvalidThresholdException exception = new InvalidThresholdException(); |
| 66 | + |
| 67 | + assertEquals("Invalid thresholds: expected minThreshold < targetTemperature < maxThreshold", exception.getMessage()); |
| 68 | + assertEquals(HttpStatus.BAD_REQUEST, exception.getHttpStatus()); |
| 69 | + assertEquals("INVALID_THRESHOLD", exception.getErrorCode()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void locationExceptionSingleArgumentConstructor() { |
| 74 | + LocationException exception = new LocationException("location conflict"); |
| 75 | + |
| 76 | + assertEquals("location conflict", exception.getMessage()); |
| 77 | + assertEquals(HttpStatus.CONFLICT, exception.getHttpStatus()); |
| 78 | + assertEquals("LOCATION_ERROR", exception.getErrorCode()); |
| 79 | + assertNull(exception.getCause()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void locationExceptionMessageAndCauseConstructor() { |
| 84 | + RuntimeException cause = new RuntimeException("root"); |
| 85 | + LocationException exception = new LocationException("location conflict", cause); |
| 86 | + |
| 87 | + assertEquals("location conflict", exception.getMessage()); |
| 88 | + assertEquals(HttpStatus.CONFLICT, exception.getHttpStatus()); |
| 89 | + assertEquals("LOCATION_ERROR", exception.getErrorCode()); |
| 90 | + assertSame(cause, exception.getCause()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void organizationConflictExceptionSetsConflictMetadata() { |
| 95 | + OrganizationConflictException exception = new OrganizationConflictException("organization exists"); |
| 96 | + |
| 97 | + assertEquals("organization exists", exception.getMessage()); |
| 98 | + assertEquals(HttpStatus.CONFLICT, exception.getHttpStatus()); |
| 99 | + assertEquals("ORGANIZATION_CONFLICT", exception.getErrorCode()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void passwordExceptionSingleArgumentConstructor() { |
| 104 | + PasswordException exception = new PasswordException("bad password"); |
| 105 | + |
| 106 | + assertEquals("bad password", exception.getMessage()); |
| 107 | + assertEquals(HttpStatus.BAD_REQUEST, exception.getHttpStatus()); |
| 108 | + assertEquals("PASSWORD_ERROR", exception.getErrorCode()); |
| 109 | + assertNull(exception.getCause()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void passwordExceptionMessageAndCauseConstructor() { |
| 114 | + RuntimeException cause = new RuntimeException("root"); |
| 115 | + PasswordException exception = new PasswordException("bad password", cause); |
| 116 | + |
| 117 | + assertEquals("bad password", exception.getMessage()); |
| 118 | + assertEquals(HttpStatus.BAD_REQUEST, exception.getHttpStatus()); |
| 119 | + assertEquals("PASSWORD_ERROR", exception.getErrorCode()); |
| 120 | + assertSame(cause, exception.getCause()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void resourceNotFoundExceptionMessageConstructor() { |
| 125 | + ResourceNotFoundException exception = new ResourceNotFoundException("not found"); |
| 126 | + |
| 127 | + assertEquals("not found", exception.getMessage()); |
| 128 | + assertEquals(HttpStatus.NOT_FOUND, exception.getHttpStatus()); |
| 129 | + assertEquals("RESOURCE_NOT_FOUND", exception.getErrorCode()); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void resourceNotFoundExceptionResourceTypeAndIdConstructor() { |
| 134 | + ResourceNotFoundException exception = new ResourceNotFoundException("User", 42L); |
| 135 | + |
| 136 | + assertEquals("User not found with ID: 42", exception.getMessage()); |
| 137 | + assertEquals(HttpStatus.NOT_FOUND, exception.getHttpStatus()); |
| 138 | + assertEquals("RESOURCE_NOT_FOUND", exception.getErrorCode()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void resourceNotFoundExceptionResourceTypeAndIdentifierConstructor() { |
| 143 | + ResourceNotFoundException exception = new ResourceNotFoundException("User", "alice@example.com"); |
| 144 | + |
| 145 | + assertEquals("User not found: alice@example.com", exception.getMessage()); |
| 146 | + assertEquals(HttpStatus.NOT_FOUND, exception.getHttpStatus()); |
| 147 | + assertEquals("RESOURCE_NOT_FOUND", exception.getErrorCode()); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void resourceNotFoundExceptionMessageAndCauseConstructor() { |
| 152 | + RuntimeException cause = new RuntimeException("root"); |
| 153 | + ResourceNotFoundException exception = new ResourceNotFoundException("missing", cause); |
| 154 | + |
| 155 | + assertEquals("missing", exception.getMessage()); |
| 156 | + assertEquals(HttpStatus.NOT_FOUND, exception.getHttpStatus()); |
| 157 | + assertEquals("RESOURCE_NOT_FOUND", exception.getErrorCode()); |
| 158 | + assertSame(cause, exception.getCause()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void roleExceptionSingleArgumentConstructor() { |
| 163 | + RoleException exception = new RoleException("invalid role"); |
| 164 | + |
| 165 | + assertEquals("invalid role", exception.getMessage()); |
| 166 | + assertEquals(HttpStatus.BAD_REQUEST, exception.getHttpStatus()); |
| 167 | + assertEquals("ROLE_ERROR", exception.getErrorCode()); |
| 168 | + assertNull(exception.getCause()); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + void roleExceptionMessageAndCauseConstructor() { |
| 173 | + RuntimeException cause = new RuntimeException("root"); |
| 174 | + RoleException exception = new RoleException("invalid role", cause); |
| 175 | + |
| 176 | + assertEquals("invalid role", exception.getMessage()); |
| 177 | + assertEquals(HttpStatus.BAD_REQUEST, exception.getHttpStatus()); |
| 178 | + assertEquals("ROLE_ERROR", exception.getErrorCode()); |
| 179 | + assertSame(cause, exception.getCause()); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void unitInactiveExceptionFormatsMessageAndMetadata() { |
| 184 | + UnitInactiveException exception = new UnitInactiveException(7L); |
| 185 | + |
| 186 | + assertEquals("Temperature unit is inactive: 7", exception.getMessage()); |
| 187 | + assertEquals(HttpStatus.CONFLICT, exception.getHttpStatus()); |
| 188 | + assertEquals("UNIT_INACTIVE", exception.getErrorCode()); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + void unitNotFoundExceptionFormatsMessageAndMetadata() { |
| 193 | + UnitNotFoundException exception = new UnitNotFoundException(9L); |
| 194 | + |
| 195 | + assertEquals("Temperature unit not found with id: 9", exception.getMessage()); |
| 196 | + assertEquals(HttpStatus.NOT_FOUND, exception.getHttpStatus()); |
| 197 | + assertEquals("UNIT_NOT_FOUND", exception.getErrorCode()); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + void userConflictExceptionSingleArgumentConstructor() { |
| 202 | + UserConflictException exception = new UserConflictException("duplicate user"); |
| 203 | + |
| 204 | + assertEquals("duplicate user", exception.getMessage()); |
| 205 | + assertEquals(HttpStatus.CONFLICT, exception.getHttpStatus()); |
| 206 | + assertEquals("USER_CONFLICT", exception.getErrorCode()); |
| 207 | + assertNull(exception.getCause()); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + void userConflictExceptionMessageAndCauseConstructor() { |
| 212 | + RuntimeException cause = new RuntimeException("root"); |
| 213 | + UserConflictException exception = new UserConflictException("duplicate user", cause); |
| 214 | + |
| 215 | + assertEquals("duplicate user", exception.getMessage()); |
| 216 | + assertEquals(HttpStatus.CONFLICT, exception.getHttpStatus()); |
| 217 | + assertEquals("USER_CONFLICT", exception.getErrorCode()); |
| 218 | + assertSame(cause, exception.getCause()); |
| 219 | + } |
| 220 | + |
| 221 | + private static final class TestAppException extends AppException { |
| 222 | + |
| 223 | + private TestAppException(String message, HttpStatus httpStatus, String errorCode) { |
| 224 | + super(message, httpStatus, errorCode); |
| 225 | + } |
| 226 | + |
| 227 | + private TestAppException(String message, Throwable cause, HttpStatus httpStatus, String errorCode) { |
| 228 | + super(message, cause, httpStatus, errorCode); |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments