Skip to content

Commit 3118f45

Browse files
committed
HieroTestContext updated to use NetworkSettings
Signed-off-by: Hendrik Ebbers <[email protected]>
1 parent 4b81c80 commit 3118f45

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/HieroTestContext.java

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,67 @@
55
import com.hedera.hashgraph.sdk.PrivateKey;
66
import com.hedera.hashgraph.sdk.PublicKey;
77
import com.openelements.hiero.base.HieroContext;
8+
import com.openelements.hiero.base.config.NetworkSettings;
89
import com.openelements.hiero.base.data.Account;
910
import io.github.cdimascio.dotenv.Dotenv;
1011
import java.util.HashMap;
11-
import java.util.List;
1212
import java.util.Map;
13-
import java.util.Objects;
13+
import java.util.Optional;
1414
import org.jspecify.annotations.NonNull;
15+
import org.slf4j.Logger;
1516

1617
public class HieroTestContext implements HieroContext {
1718

19+
private final static Logger log = org.slf4j.LoggerFactory.getLogger(HieroTestContext.class);
20+
1821
private final Account operationalAccount;
1922

2023
private final Client client;
2124

2225
public HieroTestContext() {
23-
final String hieroAccountIdByEnv = System.getenv("HEDERA_ACCOUNT_ID");
24-
final String hieroPrivateKeyByEnv = System.getenv("HEDERA_PRIVATE_KEY");
25-
final String hieroNetwork = System.getenv("HEDERA_NETWORK");
26+
final Dotenv dotenv = Dotenv.load();
27+
28+
final String hieroAccountIdByEnv = Optional.ofNullable(System.getenv("HEDERA_ACCOUNT_ID"))
29+
.orElse(dotenv.get("hiero.accountId"));
30+
final String hieroPrivateKeyByEnv = Optional.ofNullable(System.getenv("HEDERA_PRIVATE_KEY"))
31+
.orElse(dotenv.get("hiero.privateKey"));
32+
final String hieroNetwork = Optional.ofNullable(System.getenv("HEDERA_NETWORK"))
33+
.or(() -> Optional.ofNullable(dotenv.get("hiero.network")))
34+
.orElse("hedera-testnet");
35+
36+
if (hieroAccountIdByEnv == null) {
37+
throw new IllegalStateException(
38+
"AccountId for operator account is not set. Please set 'HEDERA_ACCOUNT_ID' or 'hiero.accountId' in .env file.");
39+
}
40+
if (hieroPrivateKeyByEnv == null) {
41+
throw new IllegalStateException(
42+
"PrivateKey for operator account is not set. Please set 'HEDERA_PRIVATE_KEY' or 'hiero.privateKey' in .env file.");
43+
}
44+
45+
log.info("Using operator account: {}", hieroAccountIdByEnv);
46+
log.info("Using network: {}", hieroNetwork);
47+
48+
final AccountId accountId = AccountId.fromString(hieroAccountIdByEnv);
49+
final PrivateKey privateKey = PrivateKey.fromString(hieroPrivateKeyByEnv);
50+
final PublicKey publicKey = privateKey.getPublicKey();
51+
operationalAccount = new Account(accountId, publicKey, privateKey);
52+
53+
final NetworkSettings networkSettings = NetworkSettings.forIdentifier(hieroNetwork)
54+
.orElseThrow(() -> new IllegalStateException("ENV 'HEDERA_NETWORK' is set to '" + hieroNetwork
55+
+ "' but no network settings are available for this network."));
2656

27-
if (hieroAccountIdByEnv != null && hieroPrivateKeyByEnv != null) {
28-
final AccountId accountId = AccountId.fromString(hieroAccountIdByEnv);
29-
final PrivateKey privateKey = PrivateKey.fromString(hieroPrivateKeyByEnv);
30-
final PublicKey publicKey = privateKey.getPublicKey();
31-
operationalAccount = new Account(accountId, publicKey, privateKey);
32-
if (Objects.equals(hieroNetwork, "testnet")) {
33-
client = Client.forTestnet();
34-
client.setOperator(accountId, privateKey);
35-
} else {
36-
//SOLO
37-
final Map<String, AccountId> nodes = new HashMap<>();
38-
nodes.put("127.0.0.1:50211", AccountId.fromString("0.0.3"));
39-
client = Client.forNetwork(nodes);
40-
try {
41-
client.setMirrorNetwork(List.of("localhost:8080"));
42-
} catch (Exception e) {
43-
throw new IllegalStateException("Error setting mirror network", e);
44-
}
45-
client.setOperator(accountId, privateKey);
57+
final Map<String, AccountId> nodes = new HashMap<>();
58+
networkSettings.getConsensusNodes()
59+
.forEach(consensusNode -> nodes.put(consensusNode.getAddress(), consensusNode.getAccountId()));
60+
client = Client.forNetwork(nodes);
61+
if (!networkSettings.getMirrorNodeAddresses().isEmpty()) {
62+
try {
63+
client.setMirrorNetwork(networkSettings.getMirrorNodeAddresses().stream().toList());
64+
} catch (InterruptedException e) {
65+
throw new RuntimeException("Error in configuring Mirror Node", e);
4666
}
47-
} else {
48-
final Dotenv dotenv = Dotenv.load();
49-
final String accountIdAsString = dotenv.get("hiero.accountId");
50-
final String privateKeyAsString = dotenv.get("hiero.privateKey");
51-
final AccountId accountId = AccountId.fromString(accountIdAsString);
52-
final PrivateKey privateKey = PrivateKey.fromString(privateKeyAsString);
53-
final PublicKey publicKey = privateKey.getPublicKey();
54-
operationalAccount = new Account(accountId, publicKey, privateKey);
55-
client = Client.forTestnet();
56-
client.setOperator(accountId, privateKey);
5767
}
68+
client.setOperator(accountId, privateKey);
5869
}
5970

6071
@Override

hiero-enterprise-base/src/test/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
requires org.junit.jupiter.api;
66
requires org.junit.jupiter.params;
77
requires org.mockito;
8+
requires org.slf4j;
89
}

0 commit comments

Comments
 (0)