@Test(expectedExceptions = RuntimeException.class) public void translateNotNullViolationBadMessage() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("23502"); when(serverErrorMessage.getTable()).thenReturn("mytable"); when(serverErrorMessage.getMessage()).thenReturn("xxxyyyzzzz"); //noinspection ThrowableResultOfMethodCallIgnored PostgreSqlExceptionTranslator.translateNotNullViolation(new PSQLException(serverErrorMessage)); }
@Test public void translateDependentObjectsStillExistNoDoubleQuotes() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("2BP01"); when(serverErrorMessage.getDetail()) .thenReturn("constraint my_foreign_key_constraint on table xxx depends on table yyy"); //noinspection ThrowableResultOfMethodCallIgnored MolgenisValidationException e = PostgreSqlExceptionTranslator.translateDependentObjectsStillExist( new PSQLException(serverErrorMessage)); assertEquals(e.getMessage(), "Cannot delete entity 'xxx' because entity 'yyy' depends on it."); }
@Test public void translateUndefinedColumnException() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("42703"); when(serverErrorMessage.getMessage()) .thenReturn("Undefined column: 7 ERROR: column \"test\" does not exist"); //noinspection ThrowableResultOfMethodCallIgnored MolgenisValidationException e = PostgreSqlExceptionTranslator.translateUndefinedColumnException( new PSQLException(serverErrorMessage)); assertEquals(e.getMessage(), "Undefined column: 7 ERROR: column \"test\" does not exist"); }
@Test public void translateNotNullViolationNoDoubleQuotes() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("23502"); when(serverErrorMessage.getTable()).thenReturn("xxx"); when(serverErrorMessage.getMessage()) .thenReturn("null value in column yyy violates not-null constraint"); //noinspection ThrowableResultOfMethodCallIgnored MolgenisValidationException e = PostgreSqlExceptionTranslator.translateNotNullViolation( new PSQLException(serverErrorMessage)); assertEquals(e.getMessage(), "The attribute 'yyy' of entity 'xxx' can not be null."); }
@Test public void translateUniqueKeyViolation() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("23505"); when(serverErrorMessage.getTable()).thenReturn("mytable"); when(serverErrorMessage.getDetail()).thenReturn("Key (mycolumn)=(myvalue) already exists."); //noinspection ThrowableResultOfMethodCallIgnored MolgenisValidationException e = PostgreSqlExceptionTranslator.translateUniqueKeyViolation( new PSQLException(serverErrorMessage)); assertEquals( e.getMessage(), "Duplicate value 'myvalue' for unique attribute 'mycolumn' from entity 'mytable'."); }
@Test public void translateForeignKeyViolation() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("23503"); when(serverErrorMessage.getTable()).thenReturn("mytable"); when(serverErrorMessage.getDetail()).thenReturn("... (mycolumn) ... (myvalue) ..."); //noinspection ThrowableResultOfMethodCallIgnored MolgenisValidationException e = PostgreSqlExceptionTranslator.translateForeignKeyViolation( new PSQLException(serverErrorMessage)); assertEquals( e.getMessage(), "Unknown xref value 'myvalue' for attribute 'mycolumn' of entity 'mytable'."); }
@Test public void translateDependentObjectsStillExistMultipleDependentTables() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("2BP01"); when(serverErrorMessage.getDetail()) .thenReturn( "constraint my_foreign_key_constraint on table \"myTable\" depends on table \"myDependentTable\"\nconstraint myOther_foreign_key_constraint on table \"myTable\" depends on table \"myOtherDependentTable\""); //noinspection ThrowableResultOfMethodCallIgnored MolgenisValidationException e = PostgreSqlExceptionTranslator.translateDependentObjectsStillExist( new PSQLException(serverErrorMessage)); assertEquals( e.getMessage(), "Cannot delete entity 'myTable' because entities 'myDependentTable, myOtherDependentTable' depend on it."); }
@Test public void translateForeignKeyViolationStillReferenced() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("23503"); when(serverErrorMessage.getTable()).thenReturn("mytable"); when(serverErrorMessage.getDetail()) .thenReturn("Key (mycolumn)=(myvalue) is still referenced from table \"mytable\""); //noinspection ThrowableResultOfMethodCallIgnored MolgenisValidationException e = PostgreSqlExceptionTranslator.translateForeignKeyViolation( new PSQLException(serverErrorMessage)); assertEquals( e.getMessage(), "Value 'myvalue' for attribute 'mycolumn' is referenced by entity 'mytable'."); }