diff --git a/src/main/java/io/codenotary/immudb4j/ImmuClient.java b/src/main/java/io/codenotary/immudb4j/ImmuClient.java index 6dbd868..01d4dd2 100644 --- a/src/main/java/io/codenotary/immudb4j/ImmuClient.java +++ b/src/main/java/io/codenotary/immudb4j/ImmuClient.java @@ -109,10 +109,6 @@ protected synchronized Session getSession() { return session; } - public synchronized void openSession(String database) { - openSession(database, "", ""); - } - public synchronized void openSession(String database, String username, String password) { if (session != null) { throw new IllegalStateException("session already opened"); @@ -211,13 +207,18 @@ public synchronized ImmuState currentState() throws VerificationException { // ========== DATABASE ========== // - public synchronized void createDatabase(String database) { + public void createDatabase(String database) { + createDatabase(database, false); + } + + public synchronized void createDatabase(String database, boolean ifNotExists) { if (session == null) { throw new IllegalStateException("no open session"); } final ImmudbProto.CreateDatabaseRequest req = ImmudbProto.CreateDatabaseRequest.newBuilder() .setName(database) + .setIfNotExists(ifNotExists) .build(); blockingStub.createDatabaseV2(req); diff --git a/src/test/java/io/codenotary/immudb4j/BasicImmuClientTest.java b/src/test/java/io/codenotary/immudb4j/BasicImmuClientTest.java index 030664d..9642dd1 100644 --- a/src/test/java/io/codenotary/immudb4j/BasicImmuClientTest.java +++ b/src/test/java/io/codenotary/immudb4j/BasicImmuClientTest.java @@ -108,5 +108,4 @@ public void t2() { immuClient.closeSession(); } - } diff --git a/src/test/java/io/codenotary/immudb4j/HealthCheckAndIndexCompactionTest.java b/src/test/java/io/codenotary/immudb4j/HealthCheckAndIndexCompactionTest.java index cf6abff..9b57b5c 100644 --- a/src/test/java/io/codenotary/immudb4j/HealthCheckAndIndexCompactionTest.java +++ b/src/test/java/io/codenotary/immudb4j/HealthCheckAndIndexCompactionTest.java @@ -44,13 +44,8 @@ public void t2() { immuClient.openSession("defaultdb", "immudb", "incorrect_password"); } - @Test(testName = "openSession (with wrong credentials)", expectedExceptions = StatusRuntimeException.class) - public void t3() { - immuClient.openSession("defaultdb"); - } - @Test(testName = "openSession with session already open", expectedExceptions = IllegalStateException.class) - public void t4() throws UnexpectedException { + public void t3() throws UnexpectedException { try { immuClient.openSession("defaultdb", "immudb", "immudb"); } catch (Exception e) { @@ -65,7 +60,7 @@ public void t4() throws UnexpectedException { } @Test(testName = "openSession with no open session", expectedExceptions = IllegalStateException.class) - public void t5() { + public void t4() { immuClient.closeSession(); } diff --git a/src/test/java/io/codenotary/immudb4j/ListDatabasesTest.java b/src/test/java/io/codenotary/immudb4j/ListDatabasesTest.java index d45e578..8a6bf72 100644 --- a/src/test/java/io/codenotary/immudb4j/ListDatabasesTest.java +++ b/src/test/java/io/codenotary/immudb4j/ListDatabasesTest.java @@ -21,8 +21,13 @@ public class ListDatabasesTest extends ImmuClientIntegrationTest { + @Test(testName = "databases without open session", expectedExceptions = IllegalStateException.class) + public void t1() { + immuClient.databases(); + } + @Test(testName = "databases") - public void t1() { + public void t2() { immuClient.openSession("defaultdb", "immudb", "immudb"); List databases = immuClient.databases(); diff --git a/src/test/java/io/codenotary/immudb4j/MultidatabaseTest.java b/src/test/java/io/codenotary/immudb4j/MultidatabaseTest.java index 2006746..d4f4391 100644 --- a/src/test/java/io/codenotary/immudb4j/MultidatabaseTest.java +++ b/src/test/java/io/codenotary/immudb4j/MultidatabaseTest.java @@ -17,6 +17,8 @@ import io.codenotary.immudb4j.exceptions.CorruptedDataException; import io.codenotary.immudb4j.exceptions.VerificationException; +import io.grpc.StatusRuntimeException; + import org.testng.Assert; import org.testng.annotations.Test; @@ -24,19 +26,38 @@ public class MultidatabaseTest extends ImmuClientIntegrationTest { - @Test(testName = "Interacting with multiple databases (creating them, setting, and getting, listing)") - public void t1() throws VerificationException { + @Test(testName = "createDatabase without open session", expectedExceptions = IllegalStateException.class) + public void t1() { + immuClient.createDatabase("db1"); + } + + @Test(testName = "loadDatabase without open session", expectedExceptions = IllegalStateException.class) + public void t2() { + immuClient.loadDatabase("db1"); + } + @Test(testName = "unloadDatabase without open session", expectedExceptions = IllegalStateException.class) + public void t3() { + immuClient.unloadDatabase("db1"); + } + + @Test(testName = "deleteDatabase without open session", expectedExceptions = IllegalStateException.class) + public void t4() { + immuClient.deleteDatabase("db1"); + } + + @Test(testName = "Interacting with multiple databases (creating them, setting, and getting, listing)") + public void t5() throws VerificationException { immuClient.openSession("defaultdb", "immudb", "immudb"); - immuClient.createDatabase("db1"); - immuClient.createDatabase("db2"); + immuClient.createDatabase("db1", true); + immuClient.createDatabase("db2", true); immuClient.closeSession(); immuClient.openSession("db1", "immudb", "immudb"); - byte[] v0 = new byte[]{0, 1, 2, 3}; + byte[] v0 = new byte[] { 0, 1, 2, 3 }; try { immuClient.set("k0", v0); } catch (CorruptedDataException e) { @@ -47,7 +68,7 @@ public void t1() throws VerificationException { immuClient.openSession("db2", "immudb", "immudb"); - byte[] v1 = new byte[]{3, 2, 1, 0}; + byte[] v1 = new byte[] { 3, 2, 1, 0 }; try { immuClient.set("k1", v1); } catch (CorruptedDataException e) { @@ -86,4 +107,23 @@ public void t1() throws VerificationException { immuClient.closeSession(); } + @Test(testName = "create, unload and delete database") + public void t6() { + immuClient.openSession("defaultdb", "immudb", "immudb"); + + immuClient.createDatabase("manageddb"); + + immuClient.unloadDatabase("manageddb"); + + immuClient.deleteDatabase("manageddb"); + + try { + immuClient.loadDatabase("manageddb"); + Assert.fail("exception expected"); + } catch (StatusRuntimeException e) { + Assert.assertTrue(e.getMessage().contains("database does not exist")); + } + + immuClient.closeSession(); + } } diff --git a/src/test/java/io/codenotary/immudb4j/StateTest.java b/src/test/java/io/codenotary/immudb4j/StateTest.java index 0201149..5040c1c 100644 --- a/src/test/java/io/codenotary/immudb4j/StateTest.java +++ b/src/test/java/io/codenotary/immudb4j/StateTest.java @@ -28,6 +28,11 @@ public class StateTest extends ImmuClientIntegrationTest { private static final String publicKeyResource = "test_public_key.pem"; + @Test(testName = "currentState without open session", expectedExceptions = IllegalStateException.class) + public void t1() throws VerificationException { + immuClient.currentState(); + } + @Test(testName = "currentState") public void t2() throws VerificationException { immuClient.openSession("defaultdb", "immudb", "immudb"); @@ -52,11 +57,13 @@ public void t2() throws VerificationException { return; } - // The signature verification in this case should fail for the same aforementioned reason. + // The signature verification in this case should fail for the same + // aforementioned reason. Assert.assertFalse(currState.checkSignature(publicKey)); // Again, "covering" `checkSignature` when there is a `signature` attached. - ImmuState someState = new ImmuState(currState.getDatabase(), currState.getTxId(), currState.getTxHash(), new byte[1]); + ImmuState someState = new ImmuState(currState.getDatabase(), currState.getTxId(), currState.getTxHash(), + new byte[1]); Assert.assertFalse(someState.checkSignature(publicKey)); immuClient.closeSession(); @@ -86,23 +93,23 @@ public void t3() { try { immuClient.currentState(); - Assert.fail("Did not fail as it should in this case when the signingKey is provisioned only on the client side"); + Assert.fail( + "Did not fail as it should in this case when the signingKey is provisioned only on the client side"); } catch (VerificationException ignored) { - // Expected this since in the current tests setup, immudb does not have that state signature feature active. - // (this feature is active when starting it like: `immudb --signingKey test_private_key.pem`). + // Expected this since in the current tests setup, immudb does not have that + // state signature feature active. + // (this feature is active when starting it like: `immudb --signingKey + // test_private_key.pem`). } immuClient.closeSession(); } - - - @Test(testName = "currentState with server signature checking", - description = "Testing `checkSignature` (indirectly, through `currentState`), " + - "the (state signing) feature being set up on both server and client side. " + - "This could remain a manual test, that's why it is disabled." + - "Of course, it must be `enabled = true`, if you want to run it from IDE or cli.", - enabled = false) + @Test(testName = "currentState with server signature checking", description = "Testing `checkSignature` (indirectly, through `currentState`), " + + + "the (state signing) feature being set up on both server and client side. " + + "This could remain a manual test, that's why it is disabled." + + "Of course, it must be `enabled = true`, if you want to run it from IDE or cli.", enabled = false) public void t4() { // Provisioning the client side with the public key file. @@ -128,8 +135,10 @@ public void t4() { try { ImmuState state = immuClient.currentState(); - // In this case, it should be ok as long as the immudb server has been started accordingly - // from `immudb` directory (on this repo root) using: `./immudb --signingKey test_private_key.pem` + // In this case, it should be ok as long as the immudb server has been started + // accordingly + // from `immudb` directory (on this repo root) using: `./immudb --signingKey + // test_private_key.pem` Assert.assertNotNull(state); } catch (VerificationException e) { Assert.fail(e.getMessage(), e.getCause()); diff --git a/tests.sh b/tests.sh index 3095cc7..05f6b60 100755 --- a/tests.sh +++ b/tests.sh @@ -9,20 +9,27 @@ echo ## Unit Tests TESTS="${TESTS} BasicImmuClientTest" +TESTS="${TESTS} BasicsTest" +TESTS="${TESTS} CryptoUtilsTest" +TESTS="${TESTS} ExceptionsTest" +TESTS="${TESTS} FileImmuStateHolderTest" +TESTS="${TESTS} HealthCheckAndIndexCompactionTest" TESTS="${TESTS} HistoryTest" TESTS="${TESTS} HTreeTest" -TESTS="${TESTS} ListDatabasesTest ListUsersTest" -TESTS="${TESTS} LoginAndHealthCheckAndCleanIndexTest" -TESTS="${TESTS} MultidatabaseTest MultithreadTest" +TESTS="${TESTS} ListDatabasesTest" +TESTS="${TESTS} ListUsersTest" +TESTS="${TESTS} MultidatabaseTest" +TESTS="${TESTS} MultithreadTest" TESTS="${TESTS} ReferenceTest" TESTS="${TESTS} ScanTest" -TESTS="${TESTS} SetAllAndGetAllTest SetAndGetTest" +TESTS="${TESTS} SetAllAndGetAllTest" +TESTS="${TESTS} SetAndGetTest" +TESTS="${TESTS} ShutdownTest" TESTS="${TESTS} StateTest" TESTS="${TESTS} TxTest" TESTS="${TESTS} UserMgmtTest" TESTS="${TESTS} VerifiedSetAndGetTest" TESTS="${TESTS} ZAddTest" -TESTS="${TESTS} ShutdownTest" # -----------------------------------------------------------------------------