Search in sources :

Example 1 with TopicCreateTransaction

use of com.hedera.hashgraph.sdk.TopicCreateTransaction in project hedera-sdk-java by hashgraph.

the class TopicCreateIntegrationTest method canCreateTopicWithNoFieldsSet.

@Test
@DisplayName("Can create topic with no field set")
void canCreateTopicWithNoFieldsSet() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var response = new TopicCreateTransaction().execute(testEnv.client);
    assertThat(response.getReceipt(testEnv.client).topicId).isNotNull();
    testEnv.close();
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with TopicCreateTransaction

use of com.hedera.hashgraph.sdk.TopicCreateTransaction in project hedera-sdk-java by hashgraph.

the class TopicInfoIntegrationTest method getCostInsufficientTxFeeQueryTopicInfo.

@Test
@DisplayName("Can get cost for topic info query")
void getCostInsufficientTxFeeQueryTopicInfo() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
    var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
    var infoQuery = new TopicInfoQuery().setTopicId(topicId);
    var cost = infoQuery.getCost(testEnv.client);
    assertThat(cost).isNotNull();
    assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
        infoQuery.setQueryPayment(Hbar.fromTinybars(1)).execute(testEnv.client);
    }).satisfies(error -> assertThat(error.status.toString()).isEqualTo("INSUFFICIENT_TX_FEE"));
    new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TopicInfoQuery(com.hedera.hashgraph.sdk.TopicInfoQuery) TopicDeleteTransaction(com.hedera.hashgraph.sdk.TopicDeleteTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with TopicCreateTransaction

use of com.hedera.hashgraph.sdk.TopicCreateTransaction in project hedera-sdk-java by hashgraph.

the class TopicMessageIntegrationTest method canReceiveATopicMessage.

@Test
@DisplayName("Can receive a topic message")
void canReceiveATopicMessage() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
    var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
    var info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
    assertThat(info.topicId).isEqualTo(topicId);
    assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
    assertThat(info.sequenceNumber).isEqualTo(0);
    assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
    Thread.sleep(3000);
    var receivedMessage = new boolean[] { false };
    var start = Instant.now();
    var handle = new TopicMessageQuery().setTopicId(topicId).setStartTime(Instant.EPOCH).subscribe(testEnv.client, (message) -> {
        receivedMessage[0] = new String(message.contents, StandardCharsets.UTF_8).equals("Hello, from HCS!");
    });
    Thread.sleep(3000);
    new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("Hello, from HCS!").execute(testEnv.client).getReceipt(testEnv.client);
    while (!receivedMessage[0]) {
        if (Duration.between(start, Instant.now()).compareTo(Duration.ofSeconds(60)) > 0) {
            throw new Exception("TopicMessage was not received in 60 seconds or less");
        }
        Thread.sleep(2000);
    }
    handle.unsubscribe();
    new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TopicInfoQuery(com.hedera.hashgraph.sdk.TopicInfoQuery) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) TopicDeleteTransaction(com.hedera.hashgraph.sdk.TopicDeleteTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with TopicCreateTransaction

use of com.hedera.hashgraph.sdk.TopicCreateTransaction in project hedera-sdk-java by hashgraph.

the class TopicMessageIntegrationTest method canReceiveALargeTopicMessage.

@Test
@DisplayName("Can receive a large topic message")
void canReceiveALargeTopicMessage() throws Exception {
    var testEnv = new IntegrationTestEnv(2);
    var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
    var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
    Thread.sleep(5000);
    var info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
    assertThat(info.topicId).isEqualTo(topicId);
    assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
    assertThat(info.sequenceNumber).isEqualTo(0);
    assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
    var receivedMessage = new boolean[] { false };
    var start = Instant.now();
    var handle = new TopicMessageQuery().setTopicId(topicId).setStartTime(Instant.EPOCH).subscribe(testEnv.client, (message) -> {
        receivedMessage[0] = new String(message.contents, StandardCharsets.UTF_8).equals(Contents.BIG_CONTENTS);
    });
    new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage(Contents.BIG_CONTENTS).execute(testEnv.client).getReceipt(testEnv.client);
    while (!receivedMessage[0]) {
        if (Duration.between(start, Instant.now()).compareTo(Duration.ofSeconds(60)) > 0) {
            throw new Exception("TopicMessage was not received in 60 seconds or less");
        }
        Thread.sleep(1000);
    }
    handle.unsubscribe();
    new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TopicInfoQuery(com.hedera.hashgraph.sdk.TopicInfoQuery) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) TopicDeleteTransaction(com.hedera.hashgraph.sdk.TopicDeleteTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with TopicCreateTransaction

use of com.hedera.hashgraph.sdk.TopicCreateTransaction in project hedera-sdk-java by hashgraph.

the class TopicMessageSubmitIntegrationTest method cannotSubmitMessageWhenMessageIsNotSet.

@Test
@DisplayName("Cannot submit message when message is not set")
void cannotSubmitMessageWhenMessageIsNotSet() {
    // Skip if using PreviewNet
    Assumptions.assumeTrue(!System.getProperty("HEDERA_NETWORK").equals("previewnet"));
    assertThatNoException().isThrownBy(() -> {
        var testEnv = new IntegrationTestEnv(1);
        var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
        var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
        assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
            new TopicMessageSubmitTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
        }).withMessageContaining(Status.INVALID_TOPIC_MESSAGE.toString());
        new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
        testEnv.close();
    });
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) TopicDeleteTransaction(com.hedera.hashgraph.sdk.TopicDeleteTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

TopicCreateTransaction (com.hedera.hashgraph.sdk.TopicCreateTransaction)26 Test (org.junit.jupiter.api.Test)19 DisplayName (org.junit.jupiter.api.DisplayName)17 TopicDeleteTransaction (com.hedera.hashgraph.sdk.TopicDeleteTransaction)15 TopicInfoQuery (com.hedera.hashgraph.sdk.TopicInfoQuery)10 TopicMessageSubmitTransaction (com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)10 Var (com.google.errorprone.annotations.Var)4 TopicMessageQuery (com.hedera.hashgraph.sdk.TopicMessageQuery)4 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)4 Client (com.hedera.hashgraph.sdk.Client)3 PublicKey (com.hedera.hashgraph.sdk.PublicKey)3 TopicId (com.hedera.hashgraph.sdk.TopicId)3 Hbar (com.hedera.hashgraph.sdk.Hbar)2 KeyList (com.hedera.hashgraph.sdk.KeyList)2 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)2 AbstractTransactionSupplierTest (com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)2 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)1 MaxQueryPaymentExceededException (com.hedera.hashgraph.sdk.MaxQueryPaymentExceededException)1 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)1 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)1