Skip to content

Commit b5ceea7

Browse files
committed
fix for running microprofile tests in GitHub Action
1 parent afb8c16 commit b5ceea7

File tree

4 files changed

+129
-24
lines changed

4 files changed

+129
-24
lines changed

hedera-microprofile/src/main/java/com/openelements/hedera/microprofile/ClientProvider.java

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,88 @@
1818
import jakarta.enterprise.context.ApplicationScoped;
1919
import jakarta.enterprise.inject.Produces;
2020
import jakarta.inject.Inject;
21-
import org.eclipse.microprofile.config.inject.ConfigProperty;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Objects;
25+
import org.eclipse.microprofile.config.inject.ConfigProperties;
2226
import org.jspecify.annotations.NonNull;
2327

2428
public class ClientProvider {
2529

2630
@Inject
27-
@ConfigProperty(name = "hedera.accountId")
28-
private String accountIdAsString;
31+
@ConfigProperties
32+
private HieroOperatorConfiguration configuration;
2933

3034
@Inject
31-
@ConfigProperty(name = "hedera.privateKey")
32-
private String privateKeyAsString;
35+
@ConfigProperties
36+
private HieroNetworkConfiguration networkConfiguration;
3337

34-
@Inject
35-
@ConfigProperty(name = "hedera.network")
36-
private String network;
3738

3839
private AccountId getAccountId() {
39-
if (network == null) {
40+
if (configuration == null) {
41+
throw new IllegalStateException("configuration is null");
42+
}
43+
final String accountId = configuration.getAccountId();
44+
if (accountId == null) {
4045
throw new IllegalStateException("accountId value is null");
4146
}
4247
try {
43-
return AccountId.fromString(accountIdAsString);
48+
return AccountId.fromString(accountId);
4449
} catch (Exception e) {
4550
throw new IllegalArgumentException(
46-
"Can not parse 'hedera.newAccountId' property: '" + accountIdAsString + "'", e);
51+
"Can not parse 'hedera.newAccountId' property: '" + accountId + "'", e);
4752
}
4853
}
4954

5055
private PrivateKey getPrivateKey() {
51-
if (network == null) {
56+
if (configuration == null) {
57+
throw new IllegalStateException("configuration is null");
58+
}
59+
final String privateKey = configuration.getPrivateKey();
60+
if (privateKey == null) {
5261
throw new IllegalStateException("privateKey value is null");
5362
}
5463
try {
55-
return PrivateKey.fromString(privateKeyAsString);
64+
return PrivateKey.fromString(privateKey);
5665
} catch (Exception e) {
5766
throw new IllegalArgumentException(
58-
"Can not parse 'hedera.privateKey' property: '" + privateKeyAsString + "'", e);
67+
"Can not parse 'hedera.privateKey' property: '" + privateKey + "'", e);
5968
}
6069
}
6170

6271
private HederaNetwork getHederaNetwork() {
63-
if (network == null) {
72+
if (networkConfiguration == null) {
6473
throw new IllegalStateException("network value is null");
6574
}
66-
return HederaNetwork.findByName(network)
75+
final String networkName = networkConfiguration.getName();
76+
if (networkName == null) {
77+
throw new IllegalStateException("networkName is null");
78+
}
79+
return HederaNetwork.findByName(networkName)
6780
.orElse(HederaNetwork.CUSTOM);
6881
}
6982

7083
private Client createClient() {
7184
final AccountId accountId = getAccountId();
7285
final PrivateKey privateKey = getPrivateKey();
7386
final HederaNetwork hederaNetwork = getHederaNetwork();
74-
return Client.forName(hederaNetwork.getName())
75-
.setOperator(accountId, privateKey);
87+
if (Objects.equals(HederaNetwork.CUSTOM, hederaNetwork)) {
88+
final Map<String, AccountId> nodes = new HashMap<>();
89+
networkConfiguration.getNodes()
90+
.forEach(node -> nodes.put(node.ip() + ":" + node.port(), AccountId.fromString(node.account())));
91+
Client client = Client.forNetwork(nodes);
92+
try {
93+
client.setMirrorNetwork(List.of(networkConfiguration.getMirrornode()));
94+
} catch (InterruptedException e) {
95+
throw new RuntimeException("Error setting mirror network", e);
96+
}
97+
client.setOperator(accountId, privateKey);
98+
return client;
99+
} else {
100+
return Client.forName(hederaNetwork.getName())
101+
.setOperator(accountId, privateKey);
102+
}
76103
}
77104

78105
@Produces
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.openelements.hedera.microprofile;
2+
3+
import jakarta.enterprise.context.Dependent;
4+
import jakarta.inject.Inject;
5+
import java.util.Set;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
import org.eclipse.microprofile.config.inject.ConfigProperties;
9+
import org.eclipse.microprofile.config.inject.ConfigProperty;
10+
11+
@ConfigProperties(prefix = "hiero.network")
12+
@Dependent
13+
public class HieroNetworkConfiguration {
14+
15+
public record Node(String ip, String port, String account) {
16+
}
17+
18+
private String name;
19+
20+
@Inject
21+
@ConfigProperty(defaultValue = " ") //TODO: We need a better default value
22+
private String[] nodes;
23+
24+
@ConfigProperty(defaultValue = " ") //TODO: We need a better default value
25+
private String mirrornode;
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public String getMirrornode() {
32+
return mirrornode;
33+
}
34+
35+
public Set<Node> getNodes() {
36+
if (nodes == null) {
37+
return Set.of();
38+
}
39+
return Stream.of(nodes)
40+
.map(n -> {
41+
// 172.234.134.4:8080:0.0.3
42+
final String[] split = n.split(":");
43+
if (split.length != 3) {
44+
throw new IllegalStateException("Can not parse node for '" + n + "'");
45+
}
46+
final String ip = split[0];
47+
final String port = split[1];
48+
final String account = split[2];
49+
return new Node(ip, port, account);
50+
}).collect(Collectors.toUnmodifiableSet());
51+
}
52+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.openelements.hedera.microprofile;
2+
3+
import jakarta.enterprise.context.Dependent;
4+
import org.eclipse.microprofile.config.inject.ConfigProperties;
5+
6+
@ConfigProperties(prefix = "hiero")
7+
@Dependent
8+
public class HieroOperatorConfiguration {
9+
10+
private String accountId;
11+
12+
private String privateKey;
13+
14+
public String getAccountId() {
15+
return accountId;
16+
}
17+
18+
public String getPrivateKey() {
19+
return privateKey;
20+
}
21+
}

hedera-microprofile/src/test/java/com/openelements/hedera/microprofile/test/TestConfigSource.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,28 @@ public TestConfigSource() {
1717

1818
final String hederaAccountIdByEnv = System.getenv("HEDERA_ACCOUNT_ID");
1919
if (hederaAccountIdByEnv != null) {
20-
properties.put("hedera.accountId", hederaAccountIdByEnv);
20+
properties.put("hiero.accountId", hederaAccountIdByEnv);
2121
} else {
22-
properties.put("hedera.accountId", Dotenv.load().get("hedera.accountId"));
22+
properties.put("hiero.accountId", Dotenv.load().get("hedera.accountId"));
2323
}
2424

2525
final String hederaPrivateKeyByEnv = System.getenv("HEDERA_PRIVATE_KEY");
2626
if (hederaPrivateKeyByEnv != null) {
27-
properties.put("hedera.privateKey", hederaPrivateKeyByEnv);
27+
properties.put("hiero.privateKey", hederaPrivateKeyByEnv);
2828
} else {
29-
properties.put("hedera.privateKey", Dotenv.load().get("hedera.privateKey"));
29+
properties.put("hiero.privateKey", Dotenv.load().get("hedera.privateKey"));
3030
}
3131

3232
final String hederaNetwork = System.getenv("HEDERA_NETWORK");
3333
if (hederaNetwork != null) {
34-
properties.put("hedera.network", hederaNetwork);
34+
properties.put("hiero.network.name", hederaNetwork);
35+
//TODO: Hardcoded for Solo tests,should be fixed later
36+
if (hederaNetwork == "solo") {
37+
properties.put("hiero.network.nodes", "127.0.0.1:50211:0.0.3");
38+
properties.put("hiero.network.mirrornode", "http://localhost:8080");
39+
}
3540
} else {
36-
properties.put("hedera.network", "testnet");
41+
properties.put("hiero.network.name", "testnet");
3742
}
3843
}
3944

0 commit comments

Comments
 (0)