Skip to content

Commit

Permalink
Add a convenience method in NetworkEndpointUtils to extract a HostAnd…
Browse files Browse the repository at this point in the history
…Port object.

PiperOrigin-RevId: 340473401
Change-Id: I5894f3326f89d32a101978b46bf9346450dfa01c
  • Loading branch information
Jia Tan authored and copybara-github committed Nov 3, 2020
1 parent e8fd188 commit 23c730e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,21 @@ public static boolean isIpV6Endpoint(NetworkEndpoint networkEndpoint) {
* </ul>
*/
public static String toUriAuthority(NetworkEndpoint networkEndpoint) {
return toHostAndPort(networkEndpoint).toString();
}

public static HostAndPort toHostAndPort(NetworkEndpoint networkEndpoint) {
switch (networkEndpoint.getType()) {
case IP:
return HostAndPort.fromHost(networkEndpoint.getIpAddress().getAddress()).toString();
return HostAndPort.fromHost(networkEndpoint.getIpAddress().getAddress());
case IP_PORT:
return HostAndPort.fromParts(
networkEndpoint.getIpAddress().getAddress(),
networkEndpoint.getPort().getPortNumber())
.toString();
networkEndpoint.getIpAddress().getAddress(), networkEndpoint.getPort().getPortNumber());
case HOSTNAME:
return HostAndPort.fromHost(networkEndpoint.getHostname().getName()).toString();
return HostAndPort.fromHost(networkEndpoint.getHostname().getName());
case HOSTNAME_PORT:
return HostAndPort.fromParts(
networkEndpoint.getHostname().getName(), networkEndpoint.getPort().getPortNumber())
.toString();
networkEndpoint.getHostname().getName(), networkEndpoint.getPort().getPortNumber());
case UNRECOGNIZED:
case TYPE_UNSPECIFIED:
throw new AssertionError("Type for NetworkEndpoint must be specified.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.common.net.HostAndPort;
import com.google.tsunami.proto.AddressFamily;
import com.google.tsunami.proto.Hostname;
import com.google.tsunami.proto.IpAddress;
Expand Down Expand Up @@ -167,6 +168,54 @@ public void toUriString_withHostnameAndPortEndpoint_returnsHostnameAndPort() {
.isEqualTo("localhost:8888");
}

@Test
public void toHostAndPort_withIpAddress_returnsHostWithIp() {
NetworkEndpoint ipV4Endpoint =
NetworkEndpoint.newBuilder()
.setType(NetworkEndpoint.Type.IP)
.setIpAddress(
IpAddress.newBuilder().setAddress("1.2.3.4").setAddressFamily(AddressFamily.IPV4))
.build();
assertThat(NetworkEndpointUtils.toHostAndPort(ipV4Endpoint))
.isEqualTo(HostAndPort.fromHost("1.2.3.4"));
}

@Test
public void toHostAndPort_withIpAddressAndPort_returnsHostWithIpAndPort() {
NetworkEndpoint ipV4AndPortEndpoint =
NetworkEndpoint.newBuilder()
.setType(NetworkEndpoint.Type.IP_PORT)
.setPort(Port.newBuilder().setPortNumber(8888))
.setIpAddress(
IpAddress.newBuilder().setAddress("1.2.3.4").setAddressFamily(AddressFamily.IPV4))
.build();
assertThat(NetworkEndpointUtils.toHostAndPort(ipV4AndPortEndpoint))
.isEqualTo(HostAndPort.fromParts("1.2.3.4", 8888));
}

@Test
public void toHostAndPort_withHostname_returnsHostWithHostname() {
NetworkEndpoint hostnameEndpoint =
NetworkEndpoint.newBuilder()
.setType(NetworkEndpoint.Type.HOSTNAME)
.setHostname(Hostname.newBuilder().setName("localhost"))
.build();
assertThat(NetworkEndpointUtils.toHostAndPort(hostnameEndpoint))
.isEqualTo(HostAndPort.fromHost("localhost"));
}

@Test
public void toHostAndPort_withHostnameAndPort_returnsHostWithHostnameAndPort() {
NetworkEndpoint hostnameAndPortEndpoint =
NetworkEndpoint.newBuilder()
.setType(NetworkEndpoint.Type.HOSTNAME_PORT)
.setPort(Port.newBuilder().setPortNumber(8888))
.setHostname(Hostname.newBuilder().setName("localhost"))
.build();
assertThat(NetworkEndpointUtils.toHostAndPort(hostnameAndPortEndpoint))
.isEqualTo(HostAndPort.fromParts("localhost", 8888));
}

@Test
public void forIp_withIpV4Address_returnsIpV4NetworkEndpoint() {
assertThat(NetworkEndpointUtils.forIp("1.2.3.4"))
Expand Down

0 comments on commit 23c730e

Please sign in to comment.