Skip to content

Commit

Permalink
test: unit testing database mgmt
Browse files Browse the repository at this point in the history
Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

test: update test script

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>
  • Loading branch information
jeroiraz committed Nov 17, 2022
1 parent ef0bf9e commit 547121b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 40 deletions.
11 changes: 6 additions & 5 deletions src/main/java/io/codenotary/immudb4j/ImmuClient.java
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -108,5 +108,4 @@ public void t2() {

immuClient.closeSession();
}

}
Expand Up @@ -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) {
Expand All @@ -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();
}

Expand Down
7 changes: 6 additions & 1 deletion src/test/java/io/codenotary/immudb4j/ListDatabasesTest.java
Expand Up @@ -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<String> databases = immuClient.databases();
Expand Down
51 changes: 45 additions & 6 deletions src/test/java/io/codenotary/immudb4j/MultidatabaseTest.java
Expand Up @@ -24,19 +24,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) {
Expand All @@ -47,7 +66,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) {
Expand Down Expand Up @@ -86,4 +105,24 @@ public void t1() throws VerificationException {
immuClient.closeSession();
}

@Test(testName = "create, unload, load and delete database")
public void t6() {
immuClient.openSession("defaultdb", "immudb", "immudb");

immuClient.createDatabase("manageddb");

immuClient.unloadDatabase("manageddb");

immuClient.loadDatabase("manageddb");

immuClient.closeSession();

immuClient.openSession("manageddb", "immudb", "immudb");

immuClient.unloadDatabase("manageddb");

immuClient.deleteDatabase("manageddb");

immuClient.closeSession();
}
}
39 changes: 24 additions & 15 deletions src/test/java/io/codenotary/immudb4j/StateTest.java
Expand Up @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -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.
Expand All @@ -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());
Expand Down
17 changes: 12 additions & 5 deletions tests.sh
Expand Up @@ -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"

# -----------------------------------------------------------------------------

Expand Down

0 comments on commit 547121b

Please sign in to comment.