From 4c2c0cf099083c2387a5281476909b5ea3890be7 Mon Sep 17 00:00:00 2001 From: "angelo.andreussi" Date: Fri, 10 Jan 2025 14:42:06 +0100 Subject: [PATCH 1/5] fix: improved error message in the case of SQL constraint violation on foreign keys --- ...ityConstraintViolationExceptionMapper.java | 44 +++++++++++++++++++ .../commons/util/KapuaExceptionUtils.java | 8 ++++ .../org/eclipse/kapua/KapuaErrorCodes.java | 10 ++++- ...IntegrityConstraintViolationException.java | 39 ++++++++++++++++ .../kapua-service-error-messages.properties | 3 +- 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaSQLIntegrityConstraintViolationExceptionMapper.java create mode 100644 service/api/src/main/java/org/eclipse/kapua/KapuaSQLIntegrityConstraintViolationException.java diff --git a/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaSQLIntegrityConstraintViolationExceptionMapper.java b/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaSQLIntegrityConstraintViolationExceptionMapper.java new file mode 100644 index 00000000000..304a9dfac64 --- /dev/null +++ b/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaSQLIntegrityConstraintViolationExceptionMapper.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2018, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.commons.rest.errors; + +import org.eclipse.kapua.KapuaSQLIntegrityConstraintViolationException; +import org.eclipse.kapua.commons.rest.model.errors.ExceptionInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +@Provider +public class KapuaSQLIntegrityConstraintViolationExceptionMapper implements ExceptionMapper { + private static final Logger LOG = LoggerFactory.getLogger(KapuaSQLIntegrityConstraintViolationException.class); + + private static final Status STATUS = Status.CONFLICT; + @Inject + public ExceptionConfigurationProvider exceptionConfigurationProvider; + + @Override + public Response toResponse(KapuaSQLIntegrityConstraintViolationException kapuaSQLIntegrityConstraintViolationException) { + final boolean showStackTrace = exceptionConfigurationProvider.showStackTrace(); + LOG.error(kapuaSQLIntegrityConstraintViolationException.getMessage(), kapuaSQLIntegrityConstraintViolationException); + return Response + .status(STATUS) + .entity(new ExceptionInfo(STATUS.getStatusCode(), kapuaSQLIntegrityConstraintViolationException, showStackTrace)) + .build(); + } + +} diff --git a/commons/src/main/java/org/eclipse/kapua/commons/util/KapuaExceptionUtils.java b/commons/src/main/java/org/eclipse/kapua/commons/util/KapuaExceptionUtils.java index b7c59516cfd..36513a61966 100644 --- a/commons/src/main/java/org/eclipse/kapua/commons/util/KapuaExceptionUtils.java +++ b/commons/src/main/java/org/eclipse/kapua/commons/util/KapuaExceptionUtils.java @@ -16,11 +16,13 @@ import org.eclipse.kapua.KapuaException; import org.eclipse.kapua.KapuaIllegalNullArgumentException; import org.eclipse.kapua.KapuaOptimisticLockingException; +import org.eclipse.kapua.KapuaSQLIntegrityConstraintViolationException; import org.eclipse.persistence.exceptions.DatabaseException; import javax.persistence.OptimisticLockException; import javax.persistence.PersistenceException; import javax.persistence.RollbackException; +import java.sql.SQLIntegrityConstraintViolationException; /** * Exception utilities @@ -99,6 +101,12 @@ 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); + } + } } } } diff --git a/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java b/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java index 75a07354d13..59d50c62c89 100644 --- a/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java +++ b/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java @@ -139,5 +139,13 @@ public enum KapuaErrorCodes implements KapuaErrorCode { * Some parsing failed for some reason * @since 2.0.0 */ - PARSING_ERROR + PARSING_ERROR, + + /** + * Sql integrity has been violated for some reason + * @since 2.0.0 + */ + SQL_INTEGRITY_VIOLATION + + } diff --git a/service/api/src/main/java/org/eclipse/kapua/KapuaSQLIntegrityConstraintViolationException.java b/service/api/src/main/java/org/eclipse/kapua/KapuaSQLIntegrityConstraintViolationException.java new file mode 100644 index 00000000000..a27f2f32833 --- /dev/null +++ b/service/api/src/main/java/org/eclipse/kapua/KapuaSQLIntegrityConstraintViolationException.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua; + +/** + * KapuaSQLIntegrityConstraintViolationException is thrown when the value of a method parameter is invalid. + * + * @since 2.0.0 + */ +public class KapuaSQLIntegrityConstraintViolationException extends KapuaException { + + /** + * Constructor. + * + * @since 2.0.0 + */ + public KapuaSQLIntegrityConstraintViolationException() { + super(KapuaErrorCodes.SQL_INTEGRITY_VIOLATION); + } + + /** + * Constructor. + * + * @since 2.0.0 + */ + public KapuaSQLIntegrityConstraintViolationException(String detailedMessage) { + super(KapuaErrorCodes.SQL_INTEGRITY_VIOLATION, detailedMessage); + } +} diff --git a/service/api/src/main/resources/kapua-service-error-messages.properties b/service/api/src/main/resources/kapua-service-error-messages.properties index 1fcf0fcb8ea..f9b0b65de3a 100644 --- a/service/api/src/main/resources/kapua-service-error-messages.properties +++ b/service/api/src/main/resources/kapua-service-error-messages.properties @@ -39,4 +39,5 @@ UNAUTHENTICATED=No authenticated Subject found in context. # Deprecated codes 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} \ No newline at end of file +PARSING_ERROR=Error while parsing: {0} +SQL_INTEGRITY_VIOLATION=SQL integrity constraint violation! {0} \ No newline at end of file From 6ee4dc10665477e2d9d147199e7db2fa57d5d269 Mon Sep 17 00:00:00 2001 From: "angelo.andreussi" Date: Mon, 13 Jan 2025 15:09:33 +0100 Subject: [PATCH 2/5] fix: tests adapted to modifications on new exceptions for sql integrity violations --- .../eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java | 2 +- .../resources/features/authorization/DomainService.feature | 2 +- .../resources/features/connection/UserCouplingI9n.feature | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java b/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java index 6c0a0aa882c..96cef19661d 100644 --- a/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java +++ b/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java @@ -111,7 +111,7 @@ public void convertPersistenceDatabaseExceptionTest() { Mockito.when(mockedDatabaseException.getInternalException()).thenReturn(mockedDatabaseException); Assert.assertEquals("ComparisonFailure not expected for: " + exception,kapuaException.toString(), KapuaExceptionUtils.convertPersistenceException(exception).toString()); - Mockito.verify(mockedDatabaseException, Mockito.times(12)).getInternalException(); + Mockito.verify(mockedDatabaseException, Mockito.times(13)).getInternalException(); Mockito.verify(mockedDatabaseException, Mockito.times(12)).getMessage(); Mockito.verify(mockedDatabaseException, Mockito.times(13)).getErrorCode(); } diff --git a/qa/integration/src/test/resources/features/authorization/DomainService.feature b/qa/integration/src/test/resources/features/authorization/DomainService.feature index ae10890b4bd..a2d2ee978ab 100644 --- a/qa/integration/src/test/resources/features/authorization/DomainService.feature +++ b/qa/integration/src/test/resources/features/authorization/DomainService.feature @@ -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 "KapuaException" with the text "Error during Persistence Operation" + Given I expect the exception "KapuaSQLIntegrityConstraintViolationException" with the text "SQL integrity constraint violation!" When I create the domain | name | actions | | test_name_1 | read,write | diff --git a/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature b/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature index 24d464404a3..61542fa7509 100644 --- a/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature +++ b/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature @@ -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 "KapuaException" with the text "Error during Persistence Operation" + Given I expect the exception "KapuaSQLIntegrityConstraintViolationException" with the text "SQL integrity constraint violation!" 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! @@ -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 "KapuaException" with the text "Error during Persistence Operation" + Given I expect the exception "KapuaSQLIntegrityConstraintViolationException" with the text "SQL integrity constraint violation!" 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! From e4a1cb478f7a660d15403f670cf3cad8d6c1b53e Mon Sep 17 00:00:00 2001 From: "angelo.andreussi" Date: Mon, 13 Jan 2025 16:00:42 +0100 Subject: [PATCH 3/5] fix: added some scenarios to test "convertPersistenceDatabase" on new SQL integrity constraint violation exceptions --- .../commons/util/KapuaExceptionUtilsTest.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java b/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java index 96cef19661d..d839c131df1 100644 --- a/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java +++ b/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java @@ -13,6 +13,7 @@ package org.eclipse.kapua.commons.util; import java.lang.reflect.Constructor; +import java.sql.SQLIntegrityConstraintViolationException; import javax.persistence.OptimisticLockException; import javax.persistence.PersistenceException; @@ -22,6 +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.qa.markers.junit.JUnitTests; import org.eclipse.persistence.exceptions.DatabaseException; @@ -111,9 +113,21 @@ public void convertPersistenceDatabaseExceptionTest() { Mockito.when(mockedDatabaseException.getInternalException()).thenReturn(mockedDatabaseException); Assert.assertEquals("ComparisonFailure not expected for: " + exception,kapuaException.toString(), KapuaExceptionUtils.convertPersistenceException(exception).toString()); - Mockito.verify(mockedDatabaseException, Mockito.times(13)).getInternalException(); - Mockito.verify(mockedDatabaseException, Mockito.times(12)).getMessage(); - Mockito.verify(mockedDatabaseException, Mockito.times(13)).getErrorCode(); + //SQL foreign key constraint violation + 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"); + 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(""); + Assert.assertEquals("ComparisonFailure not expected for: " + exception,ke2.toString(), KapuaExceptionUtils.convertPersistenceException(exception).toString()); + + Mockito.verify(mockedDatabaseException, Mockito.times(15)).getInternalException(); + Mockito.verify(mockedDatabaseException, Mockito.times(14)).getMessage(); + Mockito.verify(mockedDatabaseException, Mockito.times(15)).getErrorCode(); } @Test From 933a0f53f130f22cd7cc0840ef3696d585ab6bcf Mon Sep 17 00:00:00 2001 From: "angelo.andreussi" Date: Thu, 23 Jan 2025 11:45:19 +0100 Subject: [PATCH 4/5] fix: changed error message for foreign key constraint violations to support different datastores scenario and changed related tests --- ...IntegrityConstraintViolationExceptionMapper.java} | 12 ++++++------ .../kapua/commons/util/KapuaExceptionUtils.java | 6 +++--- .../kapua/commons/util/KapuaExceptionUtilsTest.java | 6 +++--- .../features/authorization/DomainService.feature | 2 +- .../features/connection/UserCouplingI9n.feature | 4 ++-- .../main/java/org/eclipse/kapua/KapuaErrorCodes.java | 2 +- ... kapuaIntegrityConstraintViolationException.java} | 12 ++++++------ .../kapua-service-error-messages.properties | 2 +- 8 files changed, 23 insertions(+), 23 deletions(-) rename commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/{KapuaSQLIntegrityConstraintViolationExceptionMapper.java => KapuaIntegrityConstraintViolationExceptionMapper.java} (66%) rename service/api/src/main/java/org/eclipse/kapua/{KapuaSQLIntegrityConstraintViolationException.java => kapuaIntegrityConstraintViolationException.java} (59%) diff --git a/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaSQLIntegrityConstraintViolationExceptionMapper.java b/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaIntegrityConstraintViolationExceptionMapper.java similarity index 66% rename from commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaSQLIntegrityConstraintViolationExceptionMapper.java rename to commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaIntegrityConstraintViolationExceptionMapper.java index 304a9dfac64..493d2d6fe93 100644 --- a/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaSQLIntegrityConstraintViolationExceptionMapper.java +++ b/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaIntegrityConstraintViolationExceptionMapper.java @@ -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; @@ -24,20 +24,20 @@ import javax.ws.rs.ext.Provider; @Provider -public class KapuaSQLIntegrityConstraintViolationExceptionMapper implements ExceptionMapper { - private static final Logger LOG = LoggerFactory.getLogger(KapuaSQLIntegrityConstraintViolationException.class); +public class KapuaIntegrityConstraintViolationExceptionMapper implements ExceptionMapper { + 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(); } diff --git a/commons/src/main/java/org/eclipse/kapua/commons/util/KapuaExceptionUtils.java b/commons/src/main/java/org/eclipse/kapua/commons/util/KapuaExceptionUtils.java index 36513a61966..8eb4cbbff4c 100644 --- a/commons/src/main/java/org/eclipse/kapua/commons/util/KapuaExceptionUtils.java +++ b/commons/src/main/java/org/eclipse/kapua/commons/util/KapuaExceptionUtils.java @@ -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; @@ -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); } } } diff --git a/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java b/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java index d839c131df1..2ec2ec5c3bf 100644 --- a/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java +++ b/commons/src/test/java/org/eclipse/kapua/commons/util/KapuaExceptionUtilsTest.java @@ -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; @@ -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(); diff --git a/qa/integration/src/test/resources/features/authorization/DomainService.feature b/qa/integration/src/test/resources/features/authorization/DomainService.feature index a2d2ee978ab..befc584a79e 100644 --- a/qa/integration/src/test/resources/features/authorization/DomainService.feature +++ b/qa/integration/src/test/resources/features/authorization/DomainService.feature @@ -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 | diff --git a/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature b/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature index 61542fa7509..b60cefbe9a3 100644 --- a/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature +++ b/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature @@ -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! @@ -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! diff --git a/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java b/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java index 59d50c62c89..6b92a09b55f 100644 --- a/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java +++ b/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java @@ -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 } diff --git a/service/api/src/main/java/org/eclipse/kapua/KapuaSQLIntegrityConstraintViolationException.java b/service/api/src/main/java/org/eclipse/kapua/kapuaIntegrityConstraintViolationException.java similarity index 59% rename from service/api/src/main/java/org/eclipse/kapua/KapuaSQLIntegrityConstraintViolationException.java rename to service/api/src/main/java/org/eclipse/kapua/kapuaIntegrityConstraintViolationException.java index a27f2f32833..64ea723c0f5 100644 --- a/service/api/src/main/java/org/eclipse/kapua/KapuaSQLIntegrityConstraintViolationException.java +++ b/service/api/src/main/java/org/eclipse/kapua/kapuaIntegrityConstraintViolationException.java @@ -13,19 +13,19 @@ 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); } /** @@ -33,7 +33,7 @@ public KapuaSQLIntegrityConstraintViolationException() { * * @since 2.0.0 */ - public KapuaSQLIntegrityConstraintViolationException(String detailedMessage) { - super(KapuaErrorCodes.SQL_INTEGRITY_VIOLATION, detailedMessage); + public kapuaIntegrityConstraintViolationException(String detailedMessage) { + super(KapuaErrorCodes.DATASTORE_INTEGRITY_VIOLATION, detailedMessage); } } diff --git a/service/api/src/main/resources/kapua-service-error-messages.properties b/service/api/src/main/resources/kapua-service-error-messages.properties index f9b0b65de3a..2ebbb483f36 100644 --- a/service/api/src/main/resources/kapua-service-error-messages.properties +++ b/service/api/src/main/resources/kapua-service-error-messages.properties @@ -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} \ No newline at end of file +DATASTORE_INTEGRITY_VIOLATION=Entity constraint violation error. {0} \ No newline at end of file From 01df705de3214c4b2c5b306743ca28b991e52ff0 Mon Sep 17 00:00:00 2001 From: "angelo.andreussi" Date: Mon, 3 Feb 2025 16:36:06 +0100 Subject: [PATCH 5/5] fix: fixed integration tests with new name for datastore constraint violation exception --- .../resources/features/authorization/DomainService.feature | 2 +- .../resources/features/connection/UserCouplingI9n.feature | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/qa/integration/src/test/resources/features/authorization/DomainService.feature b/qa/integration/src/test/resources/features/authorization/DomainService.feature index befc584a79e..0d603bb206d 100644 --- a/qa/integration/src/test/resources/features/authorization/DomainService.feature +++ b/qa/integration/src/test/resources/features/authorization/DomainService.feature @@ -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 "Entity constraint violation error." + Given I expect the exception "kapuaIntegrityConstraintViolationException" with the text "Entity constraint violation error." When I create the domain | name | actions | | test_name_1 | read,write | diff --git a/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature b/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature index b60cefbe9a3..9b78f43b7fd 100644 --- a/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature +++ b/qa/integration/src/test/resources/features/connection/UserCouplingI9n.feature @@ -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 "Entity constraint violation error." + Given I expect the exception "kapuaIntegrityConstraintViolationException" 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! @@ -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 "Entity constraint violation error." + Given I expect the exception "kapuaIntegrityConstraintViolationException" 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!