### Question:
SpansToDependencyLinks implements Function<Iterable<Span>, Iterable<Dependency>> { protected Optional<Dependency> sharedSpanDependency(Set<Span> sharedSpans) { String clientService = null; String serverService = null; for (Span span: sharedSpans) { for (KeyValue tag: span.getTags()) { if (Tags.SPAN_KIND_CLIENT.equals(tag.getValueString()) || Tags.SPAN_KIND_PRODUCER.equals(tag.getValueString())) { clientService = span.getProcess().getServiceName(); } else if (Tags.SPAN_KIND_SERVER.equals(tag.getValueString()) || Tags.SPAN_KIND_CONSUMER.equals(tag.getValueString())) { serverService = span.getProcess().getServiceName(); } if (clientService != null && serverService != null) { return Optional.of(new Dependency(clientService, serverService)); } } } return Optional.empty(); } SpansToDependencyLinks(String peerServiceTag); @Override Iterable<Dependency> call(Iterable<Span> trace); public String peerServiceTag; }### Answer:
@Test public void shouldReturnDependencyWithClientAndServerSpans() { SpansToDependencyLinks spansToDependencyLinks = new SpansToDependencyLinks(""); Set<Span> sharedSpans = new HashSet<>(); sharedSpans.add(createSpan("clientName", Tags.SPAN_KIND_CLIENT)); sharedSpans.add(createSpan("serverName", Tags.SPAN_KIND_SERVER)); Optional<Dependency> result = spansToDependencyLinks.sharedSpanDependency(sharedSpans); assertTrue(result.isPresent()); assertEquals(new Dependency("clientName", "serverName"), result.get()); }
@Test public void shouldReturnDependencyWithConsumerAndProducer() { SpansToDependencyLinks spansToDependencyLinks = new SpansToDependencyLinks(""); Set<Span> sharedSpans = new HashSet<>(); sharedSpans.add(createSpan("consumerName", Tags.SPAN_KIND_CONSUMER)); sharedSpans.add(createSpan("producerName", Tags.SPAN_KIND_PRODUCER)); Optional<Dependency> result = spansToDependencyLinks.sharedSpanDependency(sharedSpans); assertTrue(result.isPresent()); assertEquals(new Dependency("producerName", "consumerName"), result.get()); }
@Test public void shouldReturnEmptyDependencyForSpansWithoutSpanKindDefinition() { SpansToDependencyLinks spansToDependencyLinks = new SpansToDependencyLinks(""); Set<Span> sharedSpans = new HashSet<>(); sharedSpans.add(createSpan("consumerName", "tag")); sharedSpans.add(createSpan("producerName", "tag")); Optional<Dependency> result = spansToDependencyLinks.sharedSpanDependency(sharedSpans); assertFalse(result.isPresent()); }