diff --git a/service/api/src/main/java/org/eclipse/kapua/KapuaEntityUniquenessException.java b/service/api/src/main/java/org/eclipse/kapua/KapuaEntityUniquenessException.java index c004fc61e98..b0ca64825da 100644 --- a/service/api/src/main/java/org/eclipse/kapua/KapuaEntityUniquenessException.java +++ b/service/api/src/main/java/org/eclipse/kapua/KapuaEntityUniquenessException.java @@ -24,6 +24,8 @@ */ public class KapuaEntityUniquenessException extends KapuaException { + private static final long serialVersionUID = -5398114226720500553L; + private final String entityType; private final List> uniquesFieldValues; @@ -35,7 +37,7 @@ public class KapuaEntityUniquenessException extends KapuaException { * @since 1.0.0 */ public KapuaEntityUniquenessException(String entityType, List> uniquesFieldValues) { - super(KapuaErrorCodes.ENTITY_UNIQUENESS, uniquesFieldValues); + super(KapuaErrorCodes.ENTITY_UNIQUENESS, entityType, uniquesFieldValues); this.entityType = entityType; this.uniquesFieldValues = uniquesFieldValues; 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 2f73b408a01..dc85cfed465 100644 --- a/service/api/src/main/resources/kapua-service-error-messages.properties +++ b/service/api/src/main/resources/kapua-service-error-messages.properties @@ -20,6 +20,7 @@ ENTITY_CLONE_ERROR=Severe error while cloning: {0} ENTITY_NOT_FOUND=The entity of type {0} with id/name {1} was not found. ENTITY_PROPERTIES_READ_ERROR=Error while reading {0} from KapuaEntity. String-formatted value is: {1} ENTITY_PROPERTIES_WRITE_ERROR=Error while writing {0} to KapuaEntity. Properties are: {1} +ENTITY_UNIQUENESS=Duplicate {0} entry for values: {1} EXTERNAL_ID_ALREADY_EXIST_IN_ANOTHER_ACCOUNT=An entity with the same external id {0} already exists in another account. ILLEGAL_ACCESS=The current subject is not authorized for {0}. ILLEGAL_ARGUMENT=An illegal value was provided for the argument {0}: {1}. diff --git a/service/api/src/test/java/org/eclipse/kapua/KapuaEntityUniquenessExceptionTest.java b/service/api/src/test/java/org/eclipse/kapua/KapuaEntityUniquenessExceptionTest.java index 6174b2426da..0806cf5eda6 100644 --- a/service/api/src/test/java/org/eclipse/kapua/KapuaEntityUniquenessExceptionTest.java +++ b/service/api/src/test/java/org/eclipse/kapua/KapuaEntityUniquenessExceptionTest.java @@ -17,66 +17,36 @@ import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; -import org.mockito.Mockito; +import java.util.AbstractMap; import java.util.LinkedList; import java.util.List; import java.util.Map; - - @Category(JUnitTests.class) public class KapuaEntityUniquenessExceptionTest { String[] entityType; List> uniquesFieldValues; - Map.Entry mapEntry; @Before public void initialize() { - entityType = new String[]{"Entity Type", null}; + entityType = new String[] { "AEntity", "AnotherEntity" }; uniquesFieldValues = new LinkedList<>(); - mapEntry = Mockito.mock(Map.Entry.class); - uniquesFieldValues.add(mapEntry); - uniquesFieldValues.add(mapEntry); - uniquesFieldValues.add(null); - } - @Test - public void kapuaEntityUniquenessExceptionTest() { - for (String type : entityType) { - KapuaEntityUniquenessException kapuaEntityUniquenessException1 = new KapuaEntityUniquenessException(type, uniquesFieldValues); - Assert.assertEquals("Expected and actual values should be the same.", type, kapuaEntityUniquenessException1.getEntityType()); - Assert.assertEquals("Expected and actual values should be the same.", uniquesFieldValues, kapuaEntityUniquenessException1.getUniquesFieldValues()); - Assert.assertEquals("Expected and actual values should be the same.", KapuaErrorCodes.ENTITY_UNIQUENESS, kapuaEntityUniquenessException1.getCode()); - Assert.assertEquals("Expected and actual values should be the same.", "Error: " + uniquesFieldValues, kapuaEntityUniquenessException1.getMessage()); - Assert.assertNull("Null expected.", kapuaEntityUniquenessException1.getCause()); - } + uniquesFieldValues.add(new AbstractMap.SimpleEntry<>("aField", "aValue")); + uniquesFieldValues.add(new AbstractMap.SimpleEntry<>("anotherField", "anotherValue")); } @Test - public void kapuaEntityUniquenessExceptionNullFieldValuesTest() { - for (String type : entityType) { - KapuaEntityUniquenessException kapuaEntityUniquenessException2 = new KapuaEntityUniquenessException(type, null); - Assert.assertEquals("Expected and actual values should be the same.", type, kapuaEntityUniquenessException2.getEntityType()); - Assert.assertNull("Null expected.", kapuaEntityUniquenessException2.getUniquesFieldValues()); - Assert.assertEquals("Expected and actual values should be the same.", KapuaErrorCodes.ENTITY_UNIQUENESS, kapuaEntityUniquenessException2.getCode()); - Assert.assertEquals("Expected and actual values should be the same.", "Error: null", kapuaEntityUniquenessException2.getMessage()); - Assert.assertNull("Null expected.", kapuaEntityUniquenessException2.getCause()); - } - } - - @Test(expected = KapuaEntityUniquenessException.class) - public void throwingExceptionTest() throws KapuaEntityUniquenessException { - for (String type : entityType) { - throw new KapuaEntityUniquenessException(type, uniquesFieldValues); - } - } - - @Test(expected = KapuaEntityUniquenessException.class) - public void throwingExceptionNullFieldValuesTest() throws KapuaEntityUniquenessException { + public void kapuaEntityUniquenessExceptionTest() { for (String type : entityType) { - throw new KapuaEntityUniquenessException(type, null); + KapuaEntityUniquenessException kapuaEntityUniquenessException = new KapuaEntityUniquenessException(type, uniquesFieldValues); + Assert.assertEquals(type, kapuaEntityUniquenessException.getEntityType()); + Assert.assertEquals(uniquesFieldValues, kapuaEntityUniquenessException.getUniquesFieldValues()); + Assert.assertEquals(KapuaErrorCodes.ENTITY_UNIQUENESS, kapuaEntityUniquenessException.getCode()); + Assert.assertEquals("Duplicate " + type + " entry for values: " + uniquesFieldValues, kapuaEntityUniquenessException.getMessage()); + Assert.assertNull("Null expected.", kapuaEntityUniquenessException.getCause()); } } } diff --git a/service/api/src/test/java/org/eclipse/kapua/KapuaIllegalArgumentExceptionTest.java b/service/api/src/test/java/org/eclipse/kapua/KapuaIllegalArgumentExceptionTest.java index c09353c5cd2..eed9b85e3a2 100644 --- a/service/api/src/test/java/org/eclipse/kapua/KapuaIllegalArgumentExceptionTest.java +++ b/service/api/src/test/java/org/eclipse/kapua/KapuaIllegalArgumentExceptionTest.java @@ -80,7 +80,7 @@ public void kapuaIllegalArgumentExceptionKapuaErrorCodesStringParametersTest() { "Error: " + name + ", " + value, "An entity with the same name " + name + " already exists.", "An entity with the same external Id " + name + " already exists.", - "Error: " + name + ", " + value, + "Duplicate " + name + " entry for values: " + value, "The current subject is not authorized for " + name + ".", "An illegal value was provided for the argument " + name + ": " + value + ".", "An illegal null value was provided for the argument " + name + ".",