diff --git a/commons/src/main/java/org/eclipse/kapua/commons/model/id/IdGenerator.java b/commons/src/main/java/org/eclipse/kapua/commons/model/id/IdGenerator.java index feda5687e76..e178e8cb6f4 100644 --- a/commons/src/main/java/org/eclipse/kapua/commons/model/id/IdGenerator.java +++ b/commons/src/main/java/org/eclipse/kapua/commons/model/id/IdGenerator.java @@ -38,9 +38,7 @@ private IdGenerator() { * @return */ public static BigInteger generate() { - byte[] bytes = new byte[ID_SIZE]; - SECURE_RANDOM.nextBytes(bytes); - return new BigInteger(bytes); + return new BigInteger(ID_SIZE, SECURE_RANDOM); } } diff --git a/commons/src/main/java/org/eclipse/kapua/commons/setting/system/SystemSettingKey.java b/commons/src/main/java/org/eclipse/kapua/commons/setting/system/SystemSettingKey.java index cbdeff2286b..519d8343b19 100644 --- a/commons/src/main/java/org/eclipse/kapua/commons/setting/system/SystemSettingKey.java +++ b/commons/src/main/java/org/eclipse/kapua/commons/setting/system/SystemSettingKey.java @@ -167,7 +167,7 @@ public enum SystemSettingKey implements SettingKey { OSGI_CONTEXT("commons.osgi.context"), /** - * Set the Kapua key size + * Set the Kapua key size (the size is expressed in bits) */ KAPUA_KEY_SIZE("commons.entity.key.size"), diff --git a/commons/src/main/resources/kapua-environment-setting.properties b/commons/src/main/resources/kapua-environment-setting.properties index 75674b693a6..19c3dfe20af 100644 --- a/commons/src/main/resources/kapua-environment-setting.properties +++ b/commons/src/main/resources/kapua-environment-setting.properties @@ -58,5 +58,6 @@ commons.osgi.context=false # # Entity settings # -commons.entity.key.size=8 +#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type) +commons.entity.key.size=63 commons.entity.insert.max.retry=3 \ No newline at end of file diff --git a/commons/src/test/java/org/eclipse/kapua/commons/model/KapuaIdGeneratorTest.java b/commons/src/test/java/org/eclipse/kapua/commons/model/KapuaIdGeneratorTest.java index 0ec86d719ff..9eb209a03c9 100644 --- a/commons/src/test/java/org/eclipse/kapua/commons/model/KapuaIdGeneratorTest.java +++ b/commons/src/test/java/org/eclipse/kapua/commons/model/KapuaIdGeneratorTest.java @@ -13,11 +13,13 @@ package org.eclipse.kapua.commons.model; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; import java.math.BigInteger; import org.eclipse.kapua.KapuaException; +import org.eclipse.kapua.commons.model.id.IdGenerator; import org.eclipse.kapua.commons.model.misc.CollisionEntity; import org.eclipse.kapua.commons.model.misc.CollisionIdGenerator; import org.eclipse.kapua.commons.model.misc.CollisionServiceImpl; @@ -109,4 +111,19 @@ public void testKeyNoKeyCollision() { collisionIdGenerator.getGeneretedValuesCount()); } } + + @Test + /** + * Just generate few ids and check if the numbers are fitted into the expected limits + * + * @throws Exception + */ + public void testIdGeneratorBound() throws Exception { + int idSize = SystemSetting.getInstance().getInt(SystemSettingKey.KAPUA_KEY_SIZE); + BigInteger upperLimit = BigInteger.valueOf(2).pow(idSize); + for (int i = 0; i < 1000; i++) { + BigInteger generated = IdGenerator.generate(); + assertFalse("The generated id is out of the expected bounds!", generated.compareTo(BigInteger.ZERO) < 0 || generated.compareTo(upperLimit) != -1); + } + } } diff --git a/commons/src/test/resources/kapua-environment-setting.properties b/commons/src/test/resources/kapua-environment-setting.properties index a02f2417422..cb3af26332a 100644 --- a/commons/src/test/resources/kapua-environment-setting.properties +++ b/commons/src/test/resources/kapua-environment-setting.properties @@ -59,5 +59,6 @@ commons.osgi.context=false # # Entity settings # -commons.entity.key.size=8 +#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type) +commons.entity.key.size=63 commons.entity.insert.max.retry=3 \ No newline at end of file diff --git a/service/account/internal/src/test/resources/kapua-environment-setting.properties b/service/account/internal/src/test/resources/kapua-environment-setting.properties index 36197419da3..2ce364b2809 100644 --- a/service/account/internal/src/test/resources/kapua-environment-setting.properties +++ b/service/account/internal/src/test/resources/kapua-environment-setting.properties @@ -55,15 +55,10 @@ broker.port=1884 # # Entity settings # -commons.entity.key.size=8 +#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type) +commons.entity.key.size=63 commons.entity.insert.max.retry=3 character.encoding=UTF-8 commons.osgi.context=false - -# -# Entity settings -# -commons.entity.key.size=8 -commons.entity.insert.max.retry=3 \ No newline at end of file diff --git a/service/datastore/internal/src/test/resources/kapua-environment-setting.properties b/service/datastore/internal/src/test/resources/kapua-environment-setting.properties index c1d7ccf4271..73a68eb4beb 100644 --- a/service/datastore/internal/src/test/resources/kapua-environment-setting.properties +++ b/service/datastore/internal/src/test/resources/kapua-environment-setting.properties @@ -58,5 +58,6 @@ commons.osgi.context=false # # Entity settings # -commons.entity.key.size=8 +#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type) +commons.entity.key.size=63 commons.entity.insert.max.retry=3 \ No newline at end of file diff --git a/service/device/registry/internal/src/test/resources/kapua-environment-setting.properties b/service/device/registry/internal/src/test/resources/kapua-environment-setting.properties index a02f2417422..cb3af26332a 100644 --- a/service/device/registry/internal/src/test/resources/kapua-environment-setting.properties +++ b/service/device/registry/internal/src/test/resources/kapua-environment-setting.properties @@ -59,5 +59,6 @@ commons.osgi.context=false # # Entity settings # -commons.entity.key.size=8 +#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type) +commons.entity.key.size=63 commons.entity.insert.max.retry=3 \ No newline at end of file diff --git a/service/security/shiro/src/test/resources/kapua-environment-setting.properties b/service/security/shiro/src/test/resources/kapua-environment-setting.properties index 2d271ad16f0..2b2e0df0808 100644 --- a/service/security/shiro/src/test/resources/kapua-environment-setting.properties +++ b/service/security/shiro/src/test/resources/kapua-environment-setting.properties @@ -60,5 +60,6 @@ commons.osgi.context=false # # Entity settings # -commons.entity.key.size=8 +#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type) +commons.entity.key.size=63 commons.entity.insert.max.retry=3 \ No newline at end of file diff --git a/service/user/internal/src/test/resources/kapua-environment-setting.properties b/service/user/internal/src/test/resources/kapua-environment-setting.properties index a02f2417422..cb3af26332a 100644 --- a/service/user/internal/src/test/resources/kapua-environment-setting.properties +++ b/service/user/internal/src/test/resources/kapua-environment-setting.properties @@ -59,5 +59,6 @@ commons.osgi.context=false # # Entity settings # -commons.entity.key.size=8 +#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type) +commons.entity.key.size=63 commons.entity.insert.max.retry=3 \ No newline at end of file