From 3cac393249fa13ce8569d2e1fb41cd1871ad220d Mon Sep 17 00:00:00 2001 From: Alexey Date: Mon, 29 Jul 2019 22:39:49 +0300 Subject: [PATCH] Tests for 1.1.0 (#48) * Update to Iroha 1.1.0 Signed-off-by: Alexey Chernyshov * Add tests for QueryAPI Signed-off-by: Alexey Chernyshov --- .../jp/co/soramitsu/iroha/java/QueryAPI.java | 9 +- .../soramitsu/iroha/java/CommandsTest.groovy | 83 +++++++++++++++++++ .../soramitsu/iroha/java/QueryApiTest.groovy | 54 ++++++++---- 3 files changed, 125 insertions(+), 21 deletions(-) create mode 100644 client/src/test/groovy/jp/co/soramitsu/iroha/java/CommandsTest.groovy diff --git a/client/src/main/java/jp/co/soramitsu/iroha/java/QueryAPI.java b/client/src/main/java/jp/co/soramitsu/iroha/java/QueryAPI.java index 038982eac..88cb7ed33 100644 --- a/client/src/main/java/jp/co/soramitsu/iroha/java/QueryAPI.java +++ b/client/src/main/java/jp/co/soramitsu/iroha/java/QueryAPI.java @@ -1,6 +1,7 @@ package jp.co.soramitsu.iroha.java; import iroha.protocol.QryResponses.AccountAssetResponse; +import iroha.protocol.QryResponses.AccountDetailResponse; import iroha.protocol.QryResponses.AccountResponse; import iroha.protocol.QryResponses.AssetResponse; import iroha.protocol.QryResponses.BlockResponse; @@ -86,7 +87,7 @@ public String getAccountDetails( return adr.getDetail(); } - public String getAccountDetails( + public AccountDetailResponse getAccountDetails( String accountId, String writer, String key, @@ -109,12 +110,10 @@ public String getAccountDetails( checkErrorResponse(res); - val adr = res.getAccountDetailResponse(); - - return adr.getDetail(); + return res.getAccountDetailResponse(); } - public String getAccountDetails( + public AccountDetailResponse getAccountDetails( String accountId, String writer, String key, diff --git a/client/src/test/groovy/jp/co/soramitsu/iroha/java/CommandsTest.groovy b/client/src/test/groovy/jp/co/soramitsu/iroha/java/CommandsTest.groovy new file mode 100644 index 000000000..a9b915b5e --- /dev/null +++ b/client/src/test/groovy/jp/co/soramitsu/iroha/java/CommandsTest.groovy @@ -0,0 +1,83 @@ +package jp.co.soramitsu.iroha.java + +import jp.co.soramitsu.iroha.java.debug.Account +import jp.co.soramitsu.iroha.testcontainers.IrohaContainer +import jp.co.soramitsu.iroha.testcontainers.PeerConfig +import jp.co.soramitsu.iroha.testcontainers.detail.GenesisBlockBuilder +import spock.lang.Specification +import spock.lang.Unroll + +import static jp.co.soramitsu.iroha.java.Transaction.builder +import static jp.co.soramitsu.iroha.testcontainers.detail.GenesisBlockBuilder.defaultAccountId +import static jp.co.soramitsu.iroha.testcontainers.detail.GenesisBlockBuilder.defaultKeyPair + +class CommandsTest extends Specification { + + static Account account = Account.create("a@test") + + static IrohaAPI api + static def iroha = new IrohaContainer() + .withPeerConfig( + PeerConfig.builder() + .genesisBlock(getGenesisBlock()) + .build()) + + def setupSpec() { + iroha.start() + api = iroha.getApi() + } + + def cleanupSpec() { + iroha.stop() + } + + static def setDetail(Account account, String key, String value) { + return builder(account.id) + .setAccountDetail(account.id, key, value) + .sign(account.keyPair) + .build() + } + + static def createAccount(Account a) { + return Transaction.builder(defaultAccountId) + .createAccount(a.id, a.keyPair.public) + .sign(defaultKeyPair) + .build() + } + + static def getGenesisBlock() { + return new GenesisBlockBuilder() + .addDefaultTransaction() + .addTransaction(createAccount(account)) + .addTransaction(setDetail(account, "initial_key1", "initial_val")) + .addTransaction(setDetail(account, "initial_key2", "initial_val")) + .build() + } + + @Unroll + def "compareAndSet command: key=#key, value=#value, oldValue=#oldValue"() { + given: + def qapi = new QueryAPI(api, account) + + when: + def tx = Transaction.builder(account.getId()) + .compareAndSetAccountDetail(account.getId(), key, value, oldValue) + .sign(account.keyPair) + .build() + api.transaction(tx).blockingSubscribe() + + def actual_value = qapi.getAccountDetails(account.getId(), account.getId(), key, 1).getDetail() + + then: + actual_value == expected_value + + where: + key | value | oldValue | expected_value + "initial_key1" | "updated_val" | "wrong_val" | "{ \"a@test\" : { \"initial_key1\" : \"initial_val\" } }" + "initial_key1" | "updated_val" | null | "{ \"a@test\" : { \"initial_key1\" : \"initial_val\" } }" + "initial_key2" | "updated_val" | "initial_val" | "{ \"a@test\" : { \"initial_key2\" : \"updated_val\" } }" + // Seems to be failed in Iroha + //"empty1" | "value" | "wrong" | "{}" + "empty2" | "value" | null | "{ \"a@test\" : { \"empty2\" : \"value\" } }" + } +} diff --git a/client/src/test/groovy/jp/co/soramitsu/iroha/java/QueryApiTest.groovy b/client/src/test/groovy/jp/co/soramitsu/iroha/java/QueryApiTest.groovy index a115d690e..085b886b2 100644 --- a/client/src/test/groovy/jp/co/soramitsu/iroha/java/QueryApiTest.groovy +++ b/client/src/test/groovy/jp/co/soramitsu/iroha/java/QueryApiTest.groovy @@ -19,9 +19,9 @@ class QueryApiTest extends Specification { static IrohaAPI api static def iroha = new IrohaContainer() .withPeerConfig( - PeerConfig.builder() - .genesisBlock(getGenesisBlock()) - .build()) + PeerConfig.builder() + .genesisBlock(getGenesisBlock()) + .build()) def setupSpec() { iroha.start() @@ -74,9 +74,9 @@ class QueryApiTest extends Specification { def response = qapi.getPeers() then: - (response.getPeersCount() == 1) && - (response.getPeers(0).getAddress() == expected_address) && - (response.getPeers(0).getPeerKey() == expected_key) + response.getPeersCount() == 1 + response.getPeers(0).getAddress() == expected_address + response.getPeers(0).getPeerKey() == expected_key where: issuer | expected_address | expected_key @@ -89,7 +89,7 @@ class QueryApiTest extends Specification { def qapi = new QueryAPI(api, issuer) when: - def actual_value = qapi.getAccountDetails(accountId, writer, key) + def actual_value = qapi.getAccountDetails(accountId, writer, key, 10).getDetail() then: actual_value == expected_value @@ -109,20 +109,42 @@ class QueryApiTest extends Specification { A | B.id | A.id | "key2" | "{}" } + @Unroll + def "getAccountDetail pagination test: accountId=#accountId, writer=#writer, key=#key, nextWriter=#nextWriter, nextKet=#nextKey"() { + given: + def qapi = new QueryAPI(api, A) + + when: + def res = qapi.getAccountDetails(accountId, writer, key, 1, nextWriter, nextKey) + def actualValue = res.getDetail() + def actualNextWriter = res.getNextRecordId().writer + def actualNextKey = res.getNextRecordId().key + + then: + actualValue == expectedValue + actualNextWriter == expectedNextWriter + actualNextKey == expectedNextKey + + where: + accountId | writer | key | nextWriter | nextKey | expectedValue | expectedNextWriter | expectedNextKey + null | null | null | null | null | "{ \"a@test\" : { \"key1\" : \"Avalue1\" } }" | "a@test" | "key2" + null | null | null | "a@test" | "key2" | "{ \"a@test\" : { \"key2\" : \"Avalue3\" } }" | "a@test" | "key4" + } + @Unroll def "exception in @Deprecated getAccountDetails"(Account issuer, String accountId, String writer, String key) { given: def qapi = new QueryAPI(api, issuer) when: - qapi.getAccountDetails(accountId, writer, key) + qapi.getAccountDetails(accountId, writer, key, 1) then: thrown ErrorResponseException where: - issuer | accountId | writer | key - A | "nonexistent@domain" | null | null + issuer | accountId | writer | key + A | "nonexistent@domain" | null | null } def "validation exception in getAccountDetails"(Account issuer, String accountId, String writer, String key, Integer pageSize) { @@ -136,10 +158,10 @@ class QueryApiTest extends Specification { thrown ValidationException where: - issuer | accountId | writer | key | pageSize - A | "invalid" | null | null | 10 - A | null | null | null | -10 - A | null | null | null | 0 + issuer | accountId | writer | key | pageSize + A | "invalid" | null | null | 10 + A | null | null | null | -10 + A | null | null | null | 0 } def "exception in getAccountDetails"(Account issuer, String accountId, String writer, String key, Integer pageSize) { @@ -153,8 +175,8 @@ class QueryApiTest extends Specification { thrown ErrorResponseException where: - issuer | accountId | writer | key | pageSize - A | "nonexistent@domain" | null | null | 10 + issuer | accountId | writer | key | pageSize + A | "nonexistent@domain" | null | null | 10 } @Unroll