Random QA tests failures
Hi all,

after merging #583 some random test failures started to appear more frequently. We had already some random failures on qa tests but they were occasional.

After digging a bit I discovered that is a problem related to how Liquibase script are loaded and the fact that the Kapua Liquibase Client  is run from many different places concurrently. 

The KapuaLiquibaseClient was implemented to copy resources from the jar to a tmp directory and then execute it immediately. With the improvement contained on #583  all scripts gets copied and then executed. 

For the fact that KapuaLiquibaseClient is run from different places it can happen that the copied resources gets overwritten by another KapuaLiquibaseClient is executing them, with even more chance that now that apply of Liquibase scripts takes a bit longer.

I'll fix this making execution of Liquibase script `synchronized` as a simple patch.

Regards,

_Alberto_
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 feda5687e7..e178e8cb6f 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 @@ public class 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 cbdeff2286..519d8343b1 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 75674b693a..19c3dfe20a 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 0ec86d719f..9eb209a03c 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 class KapuaIdGeneratorTest extends AbstractCommonServiceTest {
                     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 a02f241742..cb3af26332 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 36197419da..2ce364b280 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 c1d7ccf427..73a68eb4be 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 a02f241742..cb3af26332 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 2d271ad16f..2b2e0df080 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 a02f241742..cb3af26332 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
['commons/src/test/resources/kapua-environment-setting.properties', 'service/security/shiro/src/test/resources/kapua-environment-setting.properties', 'commons/src/main/java/org/eclipse/kapua/commons/setting/system/SystemSettingKey.java', 'service/device/registry/internal/src/test/resources/kapua-environment-setting.properties', 'commons/src/test/java/org/eclipse/kapua/commons/model/KapuaIdGeneratorTest.java', 'commons/src/main/java/org/eclipse/kapua/commons/model/id/IdGenerator.java', 'service/datastore/internal/src/test/resources/kapua-environment-setting.properties', 'service/user/internal/src/test/resources/kapua-environment-setting.properties', 'service/account/internal/src/test/resources/kapua-environment-setting.properties', 'commons/src/main/resources/kapua-environment-setting.properties']