Skip to content

Commit 65e9853

Browse files
Merge pull request #111 from OpenElements/microprofile-fix
fix for running microprofile tests in GitHub Action
2 parents d2f5a27 + d2ad552 commit 65e9853

File tree

6 files changed

+173
-30
lines changed

6 files changed

+173
-30
lines changed

hedera-base/src/main/java/com/openelements/hedera/base/implementation/HederaNetwork.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.openelements.hedera.base.implementation;
22

3+
import java.util.Objects;
4+
import java.util.Optional;
5+
import java.util.stream.Stream;
6+
37
public enum HederaNetwork {
48

59
PREVIEWNET("previewnet", 297, "https://previewnet.mirrornode.hedera.com/", "https://previewnet.hashio.io/api"),
@@ -48,4 +52,11 @@ public String getName() {
4852
public String getMirrornodeEndpoint() {
4953
return mirrornodeEndpoint;
5054
}
55+
56+
public static Optional<HederaNetwork> findByName(final String name) {
57+
Objects.requireNonNull(name, "name must not be null");
58+
return Stream.of(HederaNetwork.values())
59+
.filter(v -> Objects.equals(v.name.toLowerCase(), name.toLowerCase()))
60+
.findAny();
61+
}
5162
}

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

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,88 @@
1818
import jakarta.enterprise.context.ApplicationScoped;
1919
import jakarta.enterprise.inject.Produces;
2020
import jakarta.inject.Inject;
21-
import java.util.Arrays;
21+
import java.util.List;
22+
import java.util.Map;
2223
import java.util.Objects;
23-
import org.eclipse.microprofile.config.inject.ConfigProperty;
24+
import java.util.stream.Collectors;
25+
import org.eclipse.microprofile.config.inject.ConfigProperties;
2426
import org.jspecify.annotations.NonNull;
2527

2628
public class ClientProvider {
2729

2830
@Inject
29-
@ConfigProperty(name = "hedera.accountId")
30-
private String accountIdAsString;
31+
@ConfigProperties
32+
private HieroOperatorConfiguration configuration;
3133

3234
@Inject
33-
@ConfigProperty(name = "hedera.privateKey")
34-
private String privateKeyAsString;
35+
@ConfigProperties
36+
private HieroNetworkConfiguration networkConfiguration;
3537

36-
@Inject
37-
@ConfigProperty(name = "hedera.network")
38-
private String network;
3938

4039
private AccountId getAccountId() {
40+
if (configuration == null) {
41+
throw new IllegalStateException("configuration is null");
42+
}
43+
final String accountId = configuration.getAccountId();
44+
if (accountId == null) {
45+
throw new IllegalStateException("accountId value is null");
46+
}
4147
try {
42-
return AccountId.fromString(accountIdAsString);
48+
return AccountId.fromString(accountId);
4349
} catch (Exception e) {
44-
throw new IllegalArgumentException("Can not parse 'hedera.newAccountId' property", e);
50+
throw new IllegalArgumentException(
51+
"Can not parse 'hedera.newAccountId' property: '" + accountId + "'", e);
4552
}
4653
}
4754

4855
private PrivateKey getPrivateKey() {
56+
if (configuration == null) {
57+
throw new IllegalStateException("configuration is null");
58+
}
59+
final String privateKey = configuration.getPrivateKey();
60+
if (privateKey == null) {
61+
throw new IllegalStateException("privateKey value is null");
62+
}
4963
try {
50-
return PrivateKey.fromString(privateKeyAsString);
64+
return PrivateKey.fromString(privateKey);
5165
} catch (Exception e) {
52-
throw new IllegalArgumentException("Can not parse 'hedera.privateKey' property", e);
66+
throw new IllegalArgumentException(
67+
"Can not parse 'hedera.privateKey' property: '" + privateKey + "'", e);
5368
}
5469
}
5570

5671
private HederaNetwork getHederaNetwork() {
57-
if (Arrays.stream(HederaNetwork.values()).anyMatch(v -> Objects.equals(v.getName(), network))) {
58-
try {
59-
return HederaNetwork.valueOf(network.toUpperCase());
60-
} catch (Exception e) {
61-
throw new IllegalArgumentException("Can not parse 'hedera.network' property", e);
62-
}
63-
} else {
64-
throw new IllegalArgumentException("'hedera.network' property must be set to a valid value");
72+
if (networkConfiguration == null) {
73+
throw new IllegalStateException("network value is null");
6574
}
75+
return networkConfiguration.getName()
76+
.map(n -> HederaNetwork.findByName(n).orElse(HederaNetwork.CUSTOM))
77+
.orElse(HederaNetwork.CUSTOM);
6678
}
6779

6880
private Client createClient() {
6981
final AccountId accountId = getAccountId();
7082
final PrivateKey privateKey = getPrivateKey();
7183
final HederaNetwork hederaNetwork = getHederaNetwork();
72-
return Client.forName(hederaNetwork.getName())
73-
.setOperator(accountId, privateKey);
84+
if (Objects.equals(HederaNetwork.CUSTOM, hederaNetwork)) {
85+
final Map<String, AccountId> nodes = networkConfiguration.getNodes()
86+
.stream().collect(Collectors.toMap(n -> n.getAddress(), n -> n.getAccountId()));
87+
Client client = Client.forNetwork(nodes);
88+
networkConfiguration.getMirrornode()
89+
.map(mirrorNode -> List.of(mirrorNode))
90+
.ifPresent(mirrorNodes -> {
91+
try {
92+
client.setMirrorNetwork(mirrorNodes);
93+
} catch (InterruptedException e) {
94+
throw new RuntimeException("Error setting mirror network", e);
95+
}
96+
});
97+
client.setOperator(accountId, privateKey);
98+
return client;
99+
} else {
100+
return Client.forName(hederaNetwork.getName())
101+
.setOperator(accountId, privateKey);
102+
}
74103
}
75104

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.openelements.hedera.microprofile.test;
22

3+
import com.hedera.hashgraph.sdk.FileId;
34
import com.openelements.hedera.base.FileClient;
45
import com.openelements.hedera.microprofile.ClientProvider;
56
import io.helidon.microprofile.tests.junit5.AddBean;
@@ -29,7 +30,14 @@ static void setup() {
2930

3031
@Test
3132
void testFileClient() throws Exception {
33+
//given
34+
final byte[] contents = "Hello, Hedera!".getBytes();
3235
Assertions.assertNotNull(fileClient);
33-
fileClient.createFile(new byte[0]);
36+
37+
//when
38+
final FileId fileId = fileClient.createFile(contents);
39+
40+
//then
41+
Assertions.assertNotNull(fileId);
3442
}
3543
}

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import io.github.cdimascio.dotenv.Dotenv;
44
import java.util.HashMap;
55
import java.util.Map;
6+
import java.util.Objects;
67
import java.util.Set;
78
import org.eclipse.microprofile.config.spi.ConfigSource;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
811

912
public class TestConfigSource implements ConfigSource {
1013

14+
private final static Logger log = LoggerFactory.getLogger(TestConfigSource.class);
15+
1116
private final Map<String, String> properties;
1217

1318
public TestConfigSource() {
@@ -17,24 +22,32 @@ public TestConfigSource() {
1722

1823
final String hederaAccountIdByEnv = System.getenv("HEDERA_ACCOUNT_ID");
1924
if (hederaAccountIdByEnv != null) {
20-
properties.put("hedera.accountId", hederaAccountIdByEnv);
25+
properties.put("hiero.accountId", hederaAccountIdByEnv);
2126
} else {
22-
properties.put("hedera.accountId", Dotenv.load().get("hedera.accountId"));
27+
properties.put("hiero.accountId", Dotenv.load().get("hedera.accountId"));
2328
}
2429

2530
final String hederaPrivateKeyByEnv = System.getenv("HEDERA_PRIVATE_KEY");
2631
if (hederaPrivateKeyByEnv != null) {
27-
properties.put("hedera.privateKey", hederaPrivateKeyByEnv);
32+
properties.put("hiero.privateKey", hederaPrivateKeyByEnv);
2833
} else {
29-
properties.put("hedera.privateKey", Dotenv.load().get("hedera.privateKey"));
34+
properties.put("hiero.privateKey", Dotenv.load().get("hedera.privateKey"));
3035
}
3136

3237
final String hederaNetwork = System.getenv("HEDERA_NETWORK");
3338
if (hederaNetwork != null) {
34-
properties.put("hedera.network", hederaNetwork);
39+
properties.put("hiero.network.name", hederaNetwork);
3540
} else {
36-
properties.put("hedera.network", "testnet");
41+
properties.put("hiero.network.name", Dotenv.load().get("hedera.network.name"));
42+
}
43+
44+
//TODO: Hardcoded for Solo tests,should be fixed later
45+
if (Objects.equals(properties.get("hiero.network.name"), "solo")) {
46+
properties.put("hiero.network.nodes", "127.0.0.1:50211:0.0.3");
47+
properties.put("hiero.network.mirrornode", "http://localhost:8080");
3748
}
49+
50+
properties.forEach((k, v) -> log.info("CONFIG: '" + k + "'->'" + v + "'"));
3851
}
3952

4053
@Override

0 commit comments

Comments
 (0)