Skip to content

Commit 59fbd1b

Browse files
authored
Introduce builder to TestDiscoveryNode (#96062)
1 parent 35fb8fe commit 59fbd1b

File tree

1 file changed

+87
-23
lines changed

1 file changed

+87
-23
lines changed

test/framework/src/main/java/org/elasticsearch/cluster/node/TestDiscoveryNode.java

Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,35 @@
99
package org.elasticsearch.cluster.node;
1010

1111
import org.elasticsearch.Version;
12+
import org.elasticsearch.common.UUIDs;
1213
import org.elasticsearch.common.transport.TransportAddress;
13-
import org.elasticsearch.test.ESTestCase;
1414

15-
import java.util.Collections;
1615
import java.util.Map;
16+
import java.util.Objects;
1717
import java.util.Set;
1818

19+
import static org.elasticsearch.test.ESTestCase.buildNewFakeTransportAddress;
20+
1921
public class TestDiscoveryNode {
2022

2123
public static DiscoveryNode create(String id) {
22-
return new DiscoveryNode(
23-
null,
24-
id,
25-
ESTestCase.buildNewFakeTransportAddress(),
26-
Collections.emptyMap(),
27-
DiscoveryNodeRole.roles(),
28-
null
29-
);
24+
return builder(id).build();
3025
}
3126

3227
public static DiscoveryNode create(String name, String id) {
33-
return new DiscoveryNode(
34-
name,
35-
id,
36-
ESTestCase.buildNewFakeTransportAddress(),
37-
Collections.emptyMap(),
38-
DiscoveryNodeRole.roles(),
39-
null
40-
);
28+
return builder(id).name(name).build();
4129
}
4230

4331
public static DiscoveryNode create(String id, TransportAddress address) {
44-
return new DiscoveryNode(null, id, address, Collections.emptyMap(), DiscoveryNodeRole.roles(), null);
32+
return builder(id).address(address).build();
4533
}
4634

4735
public static DiscoveryNode create(String id, TransportAddress address, Version version) {
48-
return new DiscoveryNode(null, id, address, Collections.emptyMap(), DiscoveryNodeRole.roles(), version);
36+
return builder(id).address(address).version(version).build();
4937
}
5038

5139
public static DiscoveryNode create(String id, TransportAddress address, Map<String, String> attributes, Set<DiscoveryNodeRole> roles) {
52-
return new DiscoveryNode(null, id, address, attributes, roles, null);
40+
return builder(id).address(address).attributes(attributes).roles(roles).build();
5341
}
5442

5543
public static DiscoveryNode create(
@@ -59,7 +47,7 @@ public static DiscoveryNode create(
5947
Set<DiscoveryNodeRole> roles,
6048
Version version
6149
) {
62-
return new DiscoveryNode(null, id, address, attributes, roles, version);
50+
return builder(id).address(address).attributes(attributes).roles(roles).version(version).build();
6351
}
6452

6553
public static DiscoveryNode create(
@@ -69,6 +57,82 @@ public static DiscoveryNode create(
6957
Map<String, String> attributes,
7058
Set<DiscoveryNodeRole> roles
7159
) {
72-
return new DiscoveryNode(nodeName, nodeId, address, attributes, roles, null);
60+
return builder(nodeId).name(nodeName).address(address).attributes(attributes).roles(roles).build();
61+
}
62+
63+
public static Builder builder(String id) {
64+
return new Builder(id);
65+
}
66+
67+
public static class Builder {
68+
private final String id;
69+
private String name;
70+
private String ephemeralId = UUIDs.randomBase64UUID();
71+
private String hostName;
72+
private String hostAddress;
73+
private TransportAddress address;
74+
private Map<String, String> attributes = Map.of();
75+
private Set<DiscoveryNodeRole> roles = DiscoveryNodeRole.roles();
76+
private Version version;
77+
private String externalId;
78+
79+
private Builder(String id) {
80+
this.id = Objects.requireNonNull(id);
81+
}
82+
83+
public Builder name(String name) {
84+
this.name = name;
85+
return this;
86+
}
87+
88+
public Builder ephemeralId(String ephemeralId) {
89+
this.ephemeralId = Objects.requireNonNull(ephemeralId);
90+
return this;
91+
}
92+
93+
public Builder address(TransportAddress address) {
94+
return address(null, null, address);
95+
}
96+
97+
public Builder address(String hostName, String hostAddress, TransportAddress address) {
98+
this.hostName = hostName;
99+
this.hostAddress = hostAddress;
100+
this.address = Objects.requireNonNull(address);
101+
return this;
102+
}
103+
104+
public Builder attributes(Map<String, String> attributes) {
105+
this.attributes = Objects.requireNonNull(attributes);
106+
return this;
107+
}
108+
109+
public Builder roles(Set<DiscoveryNodeRole> roles) {
110+
this.roles = Objects.requireNonNull(roles);
111+
return this;
112+
}
113+
114+
public Builder version(Version version) {
115+
this.version = version;
116+
return this;
117+
}
118+
119+
public Builder externalId(String externalId) {
120+
this.externalId = externalId;
121+
return this;
122+
}
123+
124+
public DiscoveryNode build() {
125+
if (address == null) {
126+
address = buildNewFakeTransportAddress();
127+
}
128+
if (hostName == null) {
129+
hostName = address.address().getHostString();
130+
}
131+
if (hostAddress == null) {
132+
hostAddress = address.getAddress();
133+
}
134+
135+
return new DiscoveryNode(name, id, ephemeralId, hostName, hostAddress, address, attributes, roles, version, externalId);
136+
}
73137
}
74138
}

0 commit comments

Comments
 (0)