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();
}
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();
}
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();
});
}
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();
});
}
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();
}
Aggregations