NpmSearchResponseFactory extends ComponentSupport { public NpmSearchResponse buildResponseForResults(final List<Terms.Bucket> buckets, final int size, final int from) { List<NpmSearchResponseObject> objects = buckets.stream() .map(bucket -> (TopHits) bucket.getAggregations().get("versions")) .map(TopHits::getHits) .map(searchHits -> searchHits.getAt(0)) .map(this::buildSearchResponseObject) .skip(from) .limit(size) .collect(toList()); return buildResponseForObjects(objects); } @Inject  NpmSearchResponseFactory(final NpmSearchHitExtractor npmSearchHitExtractor); NpmSearchResponse buildEmptyResponse(); NpmSearchResponse buildResponseForResults(final List<Terms.Bucket> buckets, final int size, final int from); NpmSearchResponse buildResponseForObjects(final List<NpmSearchResponseObject> objects);  }
@Test public void testBuildResponseWithResult() { NpmSearchResponse response = underTest.buildResponseForResults(generateBuckets(1, true, true), 20, 0); assertThat(response.getObjects(), hasSize(1)); assertThat(response.getTime(), not(nullValue())); assertThat(response.getTotal(), is(1)); NpmSearchResponseObject object = response.getObjects().get(0); NpmSearchResponsePackage entry = object.getPackageEntry(); assertThat(entry.getName(), is("name-0")); assertThat(entry.getVersion(), is("version-0")); assertThat(entry.getDescription(), is("description-0")); assertThat(entry.getKeywords(), is(singletonList("keyword-0"))); assertThat(entry.getPublisher().getUsername(), is("author-name-0")); assertThat(entry.getPublisher().getEmail(), is("author-email-0")); assertThat(entry.getDate(), not(nullValue())); assertThat(entry.getMaintainers(), hasSize(1)); assertThat(entry.getMaintainers().get(0).getUsername(), is("author-name-0")); assertThat(entry.getMaintainers().get(0).getEmail(), is("author-email-0")); assertThat(entry.getLinks(), not(nullValue())); assertThat(entry.getLinks().getBugs(), is("bugs-url-0")); assertThat(entry.getLinks().getHomepage(), is("homepage-url-0")); assertThat(entry.getLinks().getRepository(), is("repository-url-0")); assertThat(entry.getLinks().getNpm(), is(nullValue())); assertThat(object.getSearchScore(), is(1.0)); assertThat(object.getScore(), not(nullValue())); assertThat(object.getScore().getFinalScore(), is(0.0)); assertThat(object.getScore().getDetail(), not(nullValue())); assertThat(object.getScore().getDetail().getQuality(), is(0.0)); assertThat(object.getScore().getDetail().getMaintenance(), is(0.0)); assertThat(object.getScore().getDetail().getPopularity(), is(0.0)); }
@Test public void testBuildResponseWithSize() { NpmSearchResponse response = underTest.buildResponseForResults(generateBuckets(5, true, true), 3, 0); assertThat(response.getObjects(), hasSize(3)); assertThat(response.getTime(), not(nullValue())); assertThat(response.getTotal(), is(3)); assertThat(response.getObjects().get(0).getPackageEntry().getName(), is("name-0")); assertThat(response.getObjects().get(1).getPackageEntry().getName(), is("name-1")); assertThat(response.getObjects().get(2).getPackageEntry().getName(), is("name-2")); }
@Test public void testBuildResponseWithFrom() { NpmSearchResponse response = underTest.buildResponseForResults(generateBuckets(10, true, true), 20, 5); assertThat(response.getObjects(), hasSize(5)); assertThat(response.getTime(), not(nullValue())); assertThat(response.getTotal(), is(5)); assertThat(response.getObjects().get(0).getPackageEntry().getName(), is("name-5")); assertThat(response.getObjects().get(1).getPackageEntry().getName(), is("name-6")); assertThat(response.getObjects().get(2).getPackageEntry().getName(), is("name-7")); assertThat(response.getObjects().get(3).getPackageEntry().getName(), is("name-8")); assertThat(response.getObjects().get(4).getPackageEntry().getName(), is("name-9")); }
@Test public void testBuildResponseWithoutAuthorInformation() { NpmSearchResponse response = underTest.buildResponseForResults(generateBuckets(1, false, false), 20, 0); assertThat(response.getObjects(), hasSize(1)); assertThat(response.getTime(), not(nullValue())); assertThat(response.getTotal(), is(1)); NpmSearchResponseObject object = response.getObjects().get(0); NpmSearchResponsePackage entry = object.getPackageEntry(); assertThat(entry.getPublisher(), is(nullValue())); assertThat(entry.getMaintainers(), hasSize(0)); }
@Test public void testBuildResponseWithAuthorNameOnly() { NpmSearchResponse response = underTest.buildResponseForResults(generateBuckets(1, true, false), 20, 0); assertThat(response.getObjects(), hasSize(1)); assertThat(response.getTime(), not(nullValue())); assertThat(response.getTotal(), is(1)); NpmSearchResponseObject object = response.getObjects().get(0); NpmSearchResponsePackage entry = object.getPackageEntry(); assertThat(entry.getPublisher().getUsername(), is("author-name-0")); assertThat(entry.getPublisher().getEmail(), is(nullValue())); assertThat(entry.getMaintainers(), hasSize(1)); assertThat(entry.getMaintainers().get(0).getUsername(), is("author-name-0")); assertThat(entry.getMaintainers().get(0).getEmail(), is(nullValue())); }
@Test public void testBuildResponseWithAuthorEmailOnly() { NpmSearchResponse response = underTest.buildResponseForResults(generateBuckets(1, false, true), 20, 0); assertThat(response.getObjects(), hasSize(1)); assertThat(response.getTime(), not(nullValue())); assertThat(response.getTotal(), is(1)); NpmSearchResponseObject object = response.getObjects().get(0); NpmSearchResponsePackage entry = object.getPackageEntry(); assertThat(entry.getPublisher().getUsername(), is(nullValue())); assertThat(entry.getPublisher().getEmail(), is("author-email-0")); assertThat(entry.getMaintainers(), hasSize(1)); assertThat(entry.getMaintainers().get(0).getUsername(), is(nullValue())); assertThat(entry.getMaintainers().get(0).getEmail(), is("author-email-0")); }