Search in sources :

Example 1 with TopicMessageSubmitTransaction

use of com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction 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 2 with TopicMessageSubmitTransaction

use of com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction 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 3 with TopicMessageSubmitTransaction

use of com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction 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)

Example 4 with TopicMessageSubmitTransaction

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

the class TopicMessageSubmitIntegrationTest method canSubmitALargeTopicMessage.

@Test
@DisplayName("Can submit a large topic message")
void canSubmitALargeTopicMessage() {
    // Skip if using PreviewNet
    Assumptions.assumeTrue(!System.getProperty("HEDERA_NETWORK").equals("previewnet"));
    assertThatNoException().isThrownBy(() -> {
        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 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 responses = new TopicMessageSubmitTransaction().setTopicId(topicId).setMaxChunks(15).setMessage(Contents.BIG_CONTENTS).executeAll(testEnv.client);
        for (var resp : responses) {
            resp.getReceipt(testEnv.client);
        }
        info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
        assertThat(info.topicId).isEqualTo(topicId);
        assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
        assertThat(info.sequenceNumber).isEqualTo(14);
        assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
        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) Var(com.google.errorprone.annotations.Var) 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 TopicMessageSubmitTransaction

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

the class ScheduleCreateIntegrationTest method canScheduleTopicMessage.

@Test
@DisplayName("Can schedule topic message")
void canScheduleTopicMessage() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    // Generate 3 random keys
    var key1 = PrivateKey.generateED25519();
    // This is the submit key
    var key2 = PrivateKey.generateED25519();
    var key3 = PrivateKey.generateED25519();
    var keyList = new KeyList();
    keyList.add(key1.getPublicKey());
    keyList.add(key2.getPublicKey());
    keyList.add(key3.getPublicKey());
    var response = new AccountCreateTransaction().setInitialBalance(new Hbar(100)).setKey(keyList).execute(testEnv.client);
    assertThat(response.getReceipt(testEnv.client).accountId).isNotNull();
    var topicId = Objects.requireNonNull(new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setAutoRenewAccountId(testEnv.operatorId).setTopicMemo("HCS Topic_").setSubmitKey(key2.getPublicKey()).execute(testEnv.client).getReceipt(testEnv.client).topicId);
    var transaction = new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("scheduled hcs message".getBytes(StandardCharsets.UTF_8));
    // create schedule
    var scheduledTx = transaction.schedule().setAdminKey(testEnv.operatorKey).setPayerAccountId(testEnv.operatorId).setScheduleMemo("mirror scheduled E2E signature on create and sign_" + Instant.now());
    var scheduled = scheduledTx.freezeWith(testEnv.client);
    var scheduleId = Objects.requireNonNull(scheduled.execute(testEnv.client).getReceipt(testEnv.client).scheduleId);
    // verify schedule has been created and has 1 of 2 signatures
    @Var var info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info).isNotNull();
    assertThat(info.scheduleId).isEqualTo(scheduleId);
    var infoTransaction = (TopicMessageSubmitTransaction) info.getScheduledTransaction();
    assertThat(transaction.getTopicId()).isEqualTo(infoTransaction.getTopicId());
    assertThat(transaction.getNodeAccountIds()).isEqualTo(infoTransaction.getNodeAccountIds());
    var scheduleSign = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(testEnv.client);
    scheduleSign.sign(key2).execute(testEnv.client).getReceipt(testEnv.client);
    info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info.executedAt).isNotNull();
    testEnv.close();
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) Var(com.google.errorprone.annotations.Var) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

TopicMessageSubmitTransaction (com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)15 TopicCreateTransaction (com.hedera.hashgraph.sdk.TopicCreateTransaction)10 Test (org.junit.jupiter.api.Test)10 DisplayName (org.junit.jupiter.api.DisplayName)7 TopicDeleteTransaction (com.hedera.hashgraph.sdk.TopicDeleteTransaction)6 Var (com.google.errorprone.annotations.Var)4 TopicInfoQuery (com.hedera.hashgraph.sdk.TopicInfoQuery)4 TopicMessageQuery (com.hedera.hashgraph.sdk.TopicMessageQuery)4 ByteString (com.google.protobuf.ByteString)3 Client (com.hedera.hashgraph.sdk.Client)3 Hbar (com.hedera.hashgraph.sdk.Hbar)3 AbstractTransactionSupplierTest (com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)3 TopicId (com.hedera.hashgraph.sdk.TopicId)2 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)2 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 STRING (org.assertj.core.api.InstanceOfAssertFactories.STRING)2 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)1 KeyList (com.hedera.hashgraph.sdk.KeyList)1 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)1