### Question:
Resources { public static String getResourceName(Message resource) { if (resource instanceof Cluster) { return ((Cluster) resource).getName(); } if (resource instanceof ClusterLoadAssignment) { return ((ClusterLoadAssignment) resource).getClusterName(); } if (resource instanceof Listener) { return ((Listener) resource).getName(); } if (resource instanceof RouteConfiguration) { return ((RouteConfiguration) resource).getName(); } if (resource instanceof Secret) { return ((Secret) resource).getName(); } if (resource instanceof io.envoyproxy.envoy.config.cluster.v3.Cluster) { return ((io.envoyproxy.envoy.config.cluster.v3.Cluster) resource).getName(); } if (resource instanceof io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment) { return ((io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment) resource).getClusterName(); } if (resource instanceof io.envoyproxy.envoy.config.listener.v3.Listener) { return ((io.envoyproxy.envoy.config.listener.v3.Listener) resource).getName(); } if (resource instanceof io.envoyproxy.envoy.config.route.v3.RouteConfiguration) { return ((io.envoyproxy.envoy.config.route.v3.RouteConfiguration) resource).getName(); } if (resource instanceof io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.Secret) { return ((io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.Secret) resource).getName(); } return ""; } private  Resources(); static String getResourceName(Message resource); static String getResourceName(Any anyResource); static Set<String> getResourceReferences(Collection<? extends Message> resources); static ApiVersion getResourceApiVersion(String typeUrl); static final List<ResourceType> RESOURCE_TYPES_IN_ORDER; static final Map<String, String> V3_TYPE_URLS_TO_V2; static final Map<String, String> V2_TYPE_URLS_TO_V3; static final Map<String, ResourceType> TYPE_URLS_TO_RESOURCE_TYPE; static final Map<String, Class<? extends Message>> RESOURCE_TYPE_BY_URL; }### Answer:
@Test public void getResourceNameAnyThrowsOnBadClass() { assertThatThrownBy(() -> Resources.getResourceName(Any.newBuilder().setTypeUrl("garbage").build())) .isInstanceOf(NullPointerException.class) .hasMessageContaining("cannot unpack"); }
@Test public void getResourceNameReturnsExpectedNameForValidResourceMessage() { Map<Message, String> cases = ImmutableMap.of( CLUSTER, CLUSTER_NAME, ENDPOINT, CLUSTER_NAME, LISTENER, LISTENER_NAME, ROUTE, ROUTE_NAME, SECRET, SECRET_NAME); cases.forEach((resource, expectedName) -> assertThat(Resources.getResourceName(resource)).isEqualTo(expectedName)); }
@Test public void getResourceNameReturnsExpectedNameForValidResourceMessageV3() { Map<Message, String> cases = ImmutableMap.of( V3_CLUSTER, V3_CLUSTER_NAME, V3_ENDPOINT, V3_CLUSTER_NAME, V3_LISTENER, V3_LISTENER_NAME, V3_ROUTE, V3_ROUTE_NAME, V3_SECRET, V3_SECRET_NAME); cases.forEach((resource, expectedName) -> assertThat(Resources.getResourceName(resource)).isEqualTo(expectedName)); }
@Test public void getResourceNameReturnsEmptyStringForNonResourceMessage() { Message message = Color.newBuilder().build(); assertThat(Resources.getResourceName(message)).isEmpty(); }