Skip to content

Commit

Permalink
fix: changed error message for foreign key constraint violations to s…
Browse files Browse the repository at this point in the history
…upport different datastores scenario and changed related tests
  • Loading branch information
angelo.andreussi committed Jan 28, 2025
1 parent e4a1cb4 commit 933a0f5
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*******************************************************************************/
package org.eclipse.kapua.commons.rest.errors;

import org.eclipse.kapua.KapuaSQLIntegrityConstraintViolationException;
import org.eclipse.kapua.kapuaIntegrityConstraintViolationException;
import org.eclipse.kapua.commons.rest.model.errors.ExceptionInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -24,20 +24,20 @@
import javax.ws.rs.ext.Provider;

@Provider
public class KapuaSQLIntegrityConstraintViolationExceptionMapper implements ExceptionMapper<KapuaSQLIntegrityConstraintViolationException> {
private static final Logger LOG = LoggerFactory.getLogger(KapuaSQLIntegrityConstraintViolationException.class);
public class KapuaIntegrityConstraintViolationExceptionMapper implements ExceptionMapper<kapuaIntegrityConstraintViolationException> {
private static final Logger LOG = LoggerFactory.getLogger(kapuaIntegrityConstraintViolationException.class);

private static final Status STATUS = Status.CONFLICT;
@Inject
public ExceptionConfigurationProvider exceptionConfigurationProvider;

@Override
public Response toResponse(KapuaSQLIntegrityConstraintViolationException kapuaSQLIntegrityConstraintViolationException) {
public Response toResponse(kapuaIntegrityConstraintViolationException kapuaIntegrityConstraintViolationException) {
final boolean showStackTrace = exceptionConfigurationProvider.showStackTrace();
LOG.error(kapuaSQLIntegrityConstraintViolationException.getMessage(), kapuaSQLIntegrityConstraintViolationException);
LOG.error(kapuaIntegrityConstraintViolationException.getMessage(), kapuaIntegrityConstraintViolationException);
return Response
.status(STATUS)
.entity(new ExceptionInfo(STATUS.getStatusCode(), kapuaSQLIntegrityConstraintViolationException, showStackTrace))
.entity(new ExceptionInfo(STATUS.getStatusCode(), kapuaIntegrityConstraintViolationException, showStackTrace))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaIllegalNullArgumentException;
import org.eclipse.kapua.KapuaOptimisticLockingException;
import org.eclipse.kapua.KapuaSQLIntegrityConstraintViolationException;
import org.eclipse.kapua.kapuaIntegrityConstraintViolationException;
import org.eclipse.persistence.exceptions.DatabaseException;

import javax.persistence.OptimisticLockException;
Expand Down Expand Up @@ -103,8 +103,8 @@ public static KapuaException convertPersistenceException(Exception he) {
break;
default: {
if (cve.getInternalException() instanceof SQLIntegrityConstraintViolationException) {
String message = cve.getMessage().contains("FOREIGN KEY") ? "Check if some foreign key relation exists between this entity and another one in the platform" : "";
ee = new KapuaSQLIntegrityConstraintViolationException(message);
String message = cve.getMessage().contains("FOREIGN KEY") ? "This entity relates to other entities and cannot be deleted." : "";
ee = new kapuaIntegrityConstraintViolationException(message);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaIllegalNullArgumentException;
import org.eclipse.kapua.KapuaOptimisticLockingException;
import org.eclipse.kapua.KapuaSQLIntegrityConstraintViolationException;
import org.eclipse.kapua.kapuaIntegrityConstraintViolationException;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;

import org.eclipse.persistence.exceptions.DatabaseException;
Expand Down Expand Up @@ -117,12 +117,12 @@ public void convertPersistenceDatabaseExceptionTest() {
SQLIntegrityConstraintViolationException mockedDatabaseException2 = Mockito.mock(SQLIntegrityConstraintViolationException.class);
Mockito.when(mockedDatabaseException.getInternalException()).thenReturn(mockedDatabaseException2);
Mockito.when(mockedDatabaseException.getMessage()).thenReturn("FOREIGN KEY");
KapuaSQLIntegrityConstraintViolationException ke = new KapuaSQLIntegrityConstraintViolationException("Check if some foreign key relation exists between this entity and another one in the platform");
kapuaIntegrityConstraintViolationException ke = new kapuaIntegrityConstraintViolationException("This entity relates to other entities and cannot be deleted.");
Assert.assertEquals("ComparisonFailure not expected for: " + exception,ke.toString(), KapuaExceptionUtils.convertPersistenceException(exception).toString());

//generic SQL constraint violation
Mockito.when(mockedDatabaseException.getMessage()).thenReturn("another message different from for3ign key but always SQL integrity constraint violation stuff");
KapuaSQLIntegrityConstraintViolationException ke2 = new KapuaSQLIntegrityConstraintViolationException("");
kapuaIntegrityConstraintViolationException ke2 = new kapuaIntegrityConstraintViolationException("");
Assert.assertEquals("ComparisonFailure not expected for: " + exception,ke2.toString(), KapuaExceptionUtils.convertPersistenceException(exception).toString());

Mockito.verify(mockedDatabaseException, Mockito.times(15)).getInternalException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Feature: Domain Service tests
| test_name_1 | read,write |
Then A domain was created
And The domain matches the creator
Given I expect the exception "KapuaSQLIntegrityConstraintViolationException" with the text "SQL integrity constraint violation!"
Given I expect the exception "KapuaSQLIntegrityConstraintViolationException" with the text "Entity constraint violation error."
When I create the domain
| name | actions |
| test_name_1 | read,write |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ Feature: User Coupling
And I set the reserved user for the connection from device "device-1" in account "test-acc-1" to "test-user-1"
Then I set the user coupling mode for the connection from device "device-2" in account "test-acc-1" to "STRICT"
# Try to set a duplicate reserved user
Given I expect the exception "KapuaSQLIntegrityConstraintViolationException" with the text "SQL integrity constraint violation!"
Given I expect the exception "KapuaSQLIntegrityConstraintViolationException" with the text "Entity constraint violation error."
When I set the reserved user for the connection from device "device-2" in account "test-acc-1" to "test-user-1"
Then An exception was thrown
# Reserved users must be unique!
Expand Down Expand Up @@ -873,7 +873,7 @@ Feature: User Coupling
And I set the reserved user for the connection from device "device-1" in account "test-acc-1" to "test-user-1"
Then I set the user coupling mode for the connection from device "device-2" in account "test-acc-1" to "STRICT"
# Try to set a duplicate reserved user
Given I expect the exception "KapuaSQLIntegrityConstraintViolationException" with the text "SQL integrity constraint violation!"
Given I expect the exception "KapuaSQLIntegrityConstraintViolationException" with the text "Entity constraint violation error."
When I set the reserved user for the connection from device "device-2" in account "test-acc-1" to "test-user-1"
Then An exception was thrown
# Reserved users must be unique!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public enum KapuaErrorCodes implements KapuaErrorCode {
* Sql integrity has been violated for some reason
* @since 2.0.0
*/
SQL_INTEGRITY_VIOLATION
DATASTORE_INTEGRITY_VIOLATION


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@
package org.eclipse.kapua;

/**
* KapuaSQLIntegrityConstraintViolationException is thrown when the value of a method parameter is invalid.
* kapuaIntegrityConstraintViolationException is thrown when the integrity constraints of the underlying datastore have been violated
*
* @since 2.0.0
*/
public class KapuaSQLIntegrityConstraintViolationException extends KapuaException {
public class kapuaIntegrityConstraintViolationException extends KapuaException {

/**
* Constructor.
*
* @since 2.0.0
*/
public KapuaSQLIntegrityConstraintViolationException() {
super(KapuaErrorCodes.SQL_INTEGRITY_VIOLATION);
public kapuaIntegrityConstraintViolationException() {
super(KapuaErrorCodes.DATASTORE_INTEGRITY_VIOLATION);
}

/**
* Constructor.
*
* @since 2.0.0
*/
public KapuaSQLIntegrityConstraintViolationException(String detailedMessage) {
super(KapuaErrorCodes.SQL_INTEGRITY_VIOLATION, detailedMessage);
public kapuaIntegrityConstraintViolationException(String detailedMessage) {
super(KapuaErrorCodes.DATASTORE_INTEGRITY_VIOLATION, detailedMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ UNAUTHENTICATED=No authenticated Subject found in context.
USER_ALREADY_RESERVED_BY_ANOTHER_CONNECTION=This user is already reserved for another connection. Please select different user for this connection.
DEVICE_NOT_FOUND=The selected devices were not found. Please refresh device list.
PARSING_ERROR=Error while parsing: {0}
SQL_INTEGRITY_VIOLATION=SQL integrity constraint violation! {0}
DATASTORE_INTEGRITY_VIOLATION=Entity constraint violation error. {0}

0 comments on commit 933a0f5

Please sign in to comment.