private ScoredCandidates<Container> scoreContainers(
      Multiset<String> parents, int children, ResultDescription desc) {
    Builder<Container> candidates = DefaultScoredCandidates.fromSource(NAME);

    ResolvedContent containers = resolver.findByCanonicalUris(parents.elementSet());

    for (Multiset.Entry<String> parent : parents.entrySet()) {
      Maybe<Identified> possibledContainer = containers.get(parent.getElement());
      if (possibledContainer.hasValue()) {
        Identified identified = possibledContainer.requireValue();
        if (identified instanceof Container) {
          Container container = (Container) identified;
          Score score = score(parent.getCount(), children);
          candidates.addEquivalent(container, score);
          desc.appendText(
              "%s: scored %s (%s)", container.getCanonicalUri(), score, container.getTitle());
        } else {
          desc.appendText("%s: %s not container", parent, identified.getClass().getSimpleName());
        }
      } else {
        desc.appendText("%s: missing", parent);
      }
    }

    return candidates.build();
  }
  @Test
  public void testScoresOneWhenSeriesContainerWithSeriesAndEpisodeCountsInRange() {

    final Brand subject = brandWithSeries(5);
    final Brand candidate = brandWithSeries(6);

    when(contentResolver.findByCanonicalUris(
            ImmutableList.copyOf(Iterables.transform(subject.getSeriesRefs(), SeriesRef.TO_URI))))
        .thenReturn(ResolvedContent.builder().putAll(series(5)).build());
    when(contentResolver.findByCanonicalUris(
            ImmutableList.copyOf(Iterables.transform(candidate.getSeriesRefs(), SeriesRef.TO_URI))))
        .thenReturn(ResolvedContent.builder().putAll(series(6)).build());

    ScoredCandidates<Container> score =
        scorer.score(subject, ImmutableSet.<Container>of(candidate), desc());

    assertThat(Iterables.getOnlyElement(score.candidates().values()), is(Score.ONE));
  }
Example #3
0
  private Map<String, SeriesRef> getSeriesRefs(Container container) {
    Map<String, SeriesRef> seriesRefs = Maps.newLinkedHashMap();

    if (container instanceof Brand) {
      ResolvedContent resolvedSeries =
          resolver.findByCanonicalUris(
              Iterables.transform(((Brand) container).getSeriesRefs(), SeriesRef.TO_URI));
      for (Series series : Iterables.filter(resolvedSeries.getAllResolvedResults(), Series.class)) {
        seriesRefs.put(series.getCanonicalUri(), series.seriesRef());
      }
    } else if (container instanceof Series) {
      // if this is a top level series then all its children *series* ref is to this too.
      seriesRefs.put(container.getCanonicalUri(), ((Series) container).seriesRef());
    } else {
      throw new IllegalArgumentException(
          "Unexpected Container type: " + container.getClass().getSimpleName());
    }
    return seriesRefs;
  }
Example #4
0
  private Multimap<String, ChildRef> getChildRefs(Container container) {
    Multimap<String, ChildRef> seriesChildRefs = LinkedListMultimap.create();

    ResolvedContent resolvedEpisodes =
        resolver.findByCanonicalUris(
            Iterables.transform(container.getChildRefs(), ChildRef.TO_URI));
    for (Item item : Iterables.filter(resolvedEpisodes.getAllResolvedResults(), Item.class)) {
      if (!item.isActivelyPublished()) {
        continue;
      }

      ChildRef childRef = item.childRef();
      if (item instanceof Episode && ((Episode) item).getSeriesRef() != null) {
        String seriesUri = ((Episode) item).getSeriesRef().getUri();
        seriesChildRefs.put(seriesUri, childRef);
      } else {
        seriesChildRefs.put("none", childRef);
      }
    }
    return seriesChildRefs;
  }
  @Test
  @SuppressWarnings("unchecked")
  public void testScoresNullWhenCandidateHasNoSeries() {

    Brand subject = brandWithSeries(1);
    Brand candidate = brandWithSeries(0);

    when(contentResolver.findByCanonicalUris(
            ImmutableList.copyOf(Iterables.transform(subject.getSeriesRefs(), SeriesRef.TO_URI))))
        .thenReturn(ResolvedContent.builder().putAll(series(1)).build());

    ScoredCandidates<Container> score = scorer.score(subject, ImmutableSet.of(candidate), desc());

    assertThat(Iterables.getOnlyElement(score.candidates().values()), is(Score.nullScore()));
    verify(contentResolver).findByCanonicalUris((Iterable<String>) any());
  }
Example #6
0
 private Maybe<Identified> resolveExisting(Content content) {
   ImmutableSet<String> uris = ImmutableSet.of(content.getCanonicalUri());
   ResolvedContent resolved = resolver.findByCanonicalUris(uris);
   return resolved.get(content.getCanonicalUri());
 }
 @Override
 public Iterable<Item> apply(@Nullable Container input) {
   Iterable<String> childUris = Iterables.transform(input.getChildRefs(), ChildRef.TO_URI);
   ResolvedContent children = contentResolver.findByCanonicalUris(childUris);
   return Iterables.filter(children.getAllResolvedResults(), Item.class);
 }
 private Iterable<Episode> childrenOf(Container container) {
   ImmutableList<ChildRef> childRefs = container.getChildRefs();
   Iterable<String> childUris = Iterables.transform(childRefs, TO_URI);
   ResolvedContent children = contentResolver.findByCanonicalUris(childUris);
   return Iterables.filter(children.getAllResolvedResults(), Episode.class);
 }