NodeUtils { public static String findAddress(List<NodeAddress> addresses, NodeAddressType preferredAddressType) { if (addresses == null) { return null; } Map<String, String> addressMap = addresses.stream() .collect(Collectors.toMap(NodeAddress::getType, NodeAddress::getAddress, (address1, address2) -> { log.warn("Found multiple addresses with the same type. Only the first address '{}' will be used.", address1); return address1; })); if (preferredAddressType != null && addressMap.containsKey(preferredAddressType.toValue())) { return addressMap.get(preferredAddressType.toValue()); } if (addressMap.containsKey("ExternalDNS")) { return addressMap.get("ExternalDNS"); } else if (addressMap.containsKey("ExternalIP")) { return addressMap.get("ExternalIP"); } else if (addressMap.containsKey("InternalDNS")) { return addressMap.get("InternalDNS"); } else if (addressMap.containsKey("InternalIP")) { return addressMap.get("InternalIP"); } else if (addressMap.containsKey("Hostname")) { return addressMap.get("Hostname"); } return null; }  static String findAddress(List<NodeAddress> addresses, NodeAddressType preferredAddressType);  }
@Test public void testFindAddressWithAddressType() { String address = NodeUtils.findAddress(ADDRESSES, NodeAddressType.INTERNAL_DNS); assertThat(address, is("my.internal.address")); }
@Test public void testFindAddressReturnsExternalAddress() { String address = NodeUtils.findAddress(ADDRESSES, null); assertThat(address, is("my.external.address")); }
@Test public void testFindAddressNullWithInvalidAddressTypes() { List<NodeAddress> addresses = new ArrayList<>(3); addresses.add(new NodeAddressBuilder().withType("SomeAddress").withAddress("my.external.address").build()); addresses.add(new NodeAddressBuilder().withType("SomeOtherAddress").withAddress("my.internal.address").build()); addresses.add(new NodeAddressBuilder().withType("YetAnotherAddress").withAddress("192.168.2.94").build()); String address = NodeUtils.findAddress(addresses, null); assertThat(address, is(nullValue())); }
@Test public void testFindAddressNullWhenAddressesNull() { List<NodeAddress> addresses = null; String address = NodeUtils.findAddress(addresses, null); assertThat(address, is(nullValue())); }
@Test public void testFindAddressWithMultipleAddressesOfSameType() { List<NodeAddress> addresses = new ArrayList<>(3); addresses.add(new NodeAddressBuilder().withType("ExternalDNS").withAddress("my.external.address").build()); addresses.add(new NodeAddressBuilder().withType("ExternalDNS").withAddress("my.external.address2").build()); addresses.add(new NodeAddressBuilder().withType("InternalDNS").withAddress("my.internal.address").build()); addresses.add(new NodeAddressBuilder().withType("InternalDNS").withAddress("my.internal.address2").build()); addresses.add(new NodeAddressBuilder().withType("InternalIP").withAddress("192.168.2.94").build()); String address = NodeUtils.findAddress(addresses, null); assertThat(address, is("my.external.address")); }
@Test public void testFindAddressWithType() { String address = NodeUtils.findAddress(ADDRESSES, NodeAddressType.INTERNAL_DNS); assertThat(address, is("my.internal.address")); }
@Test public void testFindAddress() { String address = NodeUtils.findAddress(ADDRESSES, null); assertThat(address, is("my.external.address")); }
@Test public void testFindAddressNotFound() { List<NodeAddress> addresses = new ArrayList<>(3); addresses.add(new NodeAddressBuilder().withType("SomeAddress").withAddress("my.external.address").build()); addresses.add(new NodeAddressBuilder().withType("SomeOtherAddress").withAddress("my.internal.address").build()); addresses.add(new NodeAddressBuilder().withType("YetAnotherAddress").withAddress("192.168.2.94").build()); String address = NodeUtils.findAddress(addresses, null); assertThat(address, is(CoreMatchers.nullValue())); }
@Test public void testFindAddressesNull() { List<NodeAddress> addresses = null; String address = NodeUtils.findAddress(addresses, null); assertThat(address, is(CoreMatchers.nullValue())); }