Search in sources :

Example 16 with ServerErrorMessage

use of org.postgresql.util.ServerErrorMessage in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateForeignKeyViolationStillReferenced.

@Test
public void translateForeignKeyViolationStillReferenced() {
    ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
    when(serverErrorMessage.getSQLState()).thenReturn("23503");
    when(serverErrorMessage.getTable()).thenReturn("myTable");
    when(serverErrorMessage.getMessage()).thenReturn("update or delete on table \"myDependentTable\" violates foreign key constraint \"myTable_myAttr_fkey\" on table \"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 'myAttr' is referenced by entity 'myEntity'.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 17 with ServerErrorMessage

use of org.postgresql.util.ServerErrorMessage in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateUniqueKeyViolationBadMessage.

@Test(expectedExceptions = RuntimeException.class)
public void translateUniqueKeyViolationBadMessage() {
    ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
    when(serverErrorMessage.getSQLState()).thenReturn("23505");
    when(serverErrorMessage.getTable()).thenReturn("mytable");
    when(serverErrorMessage.getDetail()).thenReturn("xxxyyyyzzz");
    // noinspection ThrowableResultOfMethodCallIgnored
    postgreSqlExceptionTranslator.translateUniqueKeyViolation(new PSQLException(serverErrorMessage));
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) Test(org.testng.annotations.Test)

Example 18 with ServerErrorMessage

use of org.postgresql.util.ServerErrorMessage in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateNotNullViolationBadMessage.

@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));
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) Test(org.testng.annotations.Test)

Example 19 with ServerErrorMessage

use of org.postgresql.util.ServerErrorMessage in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateInvalidIntegerExceptionInteger.

@Test
public void translateInvalidIntegerExceptionInteger() {
    ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
    when(serverErrorMessage.getMessage()).thenReturn("invalid input syntax for integer: \"str1\"");
    // noinspection ThrowableResultOfMethodCallIgnored
    MolgenisValidationException e = PostgreSqlExceptionTranslator.translateInvalidIntegerException(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "Value [str1] of this entity attribute is not of type [INT or LONG].");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 20 with ServerErrorMessage

use of org.postgresql.util.ServerErrorMessage in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateNotNullViolationNoDoubleQuotes.

@Test
public void translateNotNullViolationNoDoubleQuotes() {
    ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
    when(serverErrorMessage.getSQLState()).thenReturn("23502");
    when(serverErrorMessage.getTable()).thenReturn("myTable");
    when(serverErrorMessage.getMessage()).thenReturn("null value in column myColumn violates not-null constraint");
    // noinspection ThrowableResultOfMethodCallIgnored
    MolgenisValidationException e = postgreSqlExceptionTranslator.translateNotNullViolation(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "The attribute 'myAttr' of entity 'myEntity' can not be null.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Aggregations

ServerErrorMessage (org.postgresql.util.ServerErrorMessage)36 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)31 PSQLException (org.postgresql.util.PSQLException)28 Test (org.testng.annotations.Test)27 ConstraintViolation (org.molgenis.data.validation.ConstraintViolation)7 Matcher (java.util.regex.Matcher)6 EntityTypeDescription (org.molgenis.data.postgresql.identifier.EntityTypeDescription)2 String.format (java.lang.String.format)1 BatchUpdateException (java.sql.BatchUpdateException)1 SQLException (java.sql.SQLException)1 Collections.singleton (java.util.Collections.singleton)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 Collectors.joining (java.util.stream.Collectors.joining)1 Collectors.toCollection (java.util.stream.Collectors.toCollection)1 DataSource (javax.sql.DataSource)1