NpmSearchGroupHandler extends GroupHandler { @Override protected Response doGet(@Nonnull final Context context, @Nonnull final DispatchedRepositories dispatched) throws Exception { checkNotNull(context); checkNotNull(dispatched); Request request = context.getRequest(); Parameters parameters = request.getParameters(); String text = npmSearchParameterExtractor.extractText(parameters); NpmSearchResponse response; if (text.isEmpty()) { response = npmSearchResponseFactory.buildEmptyResponse(); } else { response = searchMembers(context, dispatched, parameters); } String content = npmSearchResponseMapper.writeString(response); return HttpResponses.ok(new StringPayload(content, ContentTypes.APPLICATION_JSON)); } @Inject  NpmSearchGroupHandler(final NpmSearchParameterExtractor npmSearchParameterExtractor,
                               final NpmSearchResponseFactory npmSearchResponseFactory,
                               final NpmSearchResponseMapper npmSearchResponseMapper,
                               @Named("${nexus.npm.v1SearchMaxResults:-250}") final int v1SearchMaxResults);   }
@Test public void testSearchWithEmptyString() throws Exception { when(npmSearchResponseFactory.buildEmptyResponse()).thenReturn(searchResponse); when(npmSearchParameterExtractor.extractText(parameters)).thenReturn(""); when(npmSearchResponseMapper.writeString(any(NpmSearchResponse.class))).thenReturn("response"); Response response = underTest.doGet(context, dispatchedRepositories); assertThat(response.getStatus().getCode(), is(OK)); assertThat(response.getPayload(), not(nullValue())); try (InputStream in = response.getPayload().openInputStream()) { assertThat(CharStreams.toString(new InputStreamReader(in, UTF_8)), is("response")); } verify(npmSearchResponseFactory).buildEmptyResponse(); verifyNoMoreInteractions(viewFacet1); verifyNoMoreInteractions(viewFacet2); }
@Test public void testMergingResultsWithDifferentPackageNames() throws Exception { Response response = underTest.doGet(context, dispatchedRepositories); assertThat(response.getStatus().getCode(), is(OK)); assertThat(response.getPayload(), not(nullValue())); try (InputStream in = response.getPayload().openInputStream()) { assertThat(CharStreams.toString(new InputStreamReader(in, UTF_8)), is("response")); } verify(viewFacet1).dispatch(request, context); verify(viewFacet2).dispatch(request, context); ArgumentCaptor<NpmSearchResponse> searchResponseCaptor = ArgumentCaptor.forClass(NpmSearchResponse.class); verify(npmSearchResponseMapper).writeString(searchResponseCaptor.capture()); NpmSearchResponse searchResponse = searchResponseCaptor.getValue(); assertThat(searchResponse.getTotal(), is(2)); assertThat(searchResponse.getObjects(), hasSize(2)); assertThat(searchResponse.getObjects(), contains(npmSearchResponseObject2, npmSearchResponseObject1)); }
@Test public void testMergingResultsWithSamePackageName() throws Exception { when(npmSearchResponsePackage2.getName()).thenReturn("package-1"); Response response = underTest.doGet(context, dispatchedRepositories); assertThat(response.getStatus().getCode(), is(OK)); assertThat(response.getPayload(), not(nullValue())); try (InputStream in = response.getPayload().openInputStream()) { assertThat(CharStreams.toString(new InputStreamReader(in, UTF_8)), is("response")); } verify(viewFacet1).dispatch(request, context); verify(viewFacet2).dispatch(request, context); ArgumentCaptor<NpmSearchResponse> searchResponseCaptor = ArgumentCaptor.forClass(NpmSearchResponse.class); verify(npmSearchResponseMapper).writeString(searchResponseCaptor.capture()); NpmSearchResponse searchResponse = searchResponseCaptor.getValue(); assertThat(searchResponse.getTotal(), is(1)); assertThat(searchResponse.getObjects(), hasSize(1)); assertThat(searchResponse.getObjects(), contains(npmSearchResponseObject1)); }
@Test public void testPropagateRequests() throws Exception { when(npmSearchParameterExtractor.extractSize(parameters)).thenReturn(100); when(npmSearchParameterExtractor.extractFrom(parameters)).thenReturn(50); Response response = underTest.doGet(context, dispatchedRepositories); assertThat(response.getStatus().getCode(), is(OK)); assertThat(response.getPayload(), not(nullValue())); try (InputStream in = response.getPayload().openInputStream()) { assertThat(CharStreams.toString(new InputStreamReader(in, UTF_8)), is("response")); } verify(parameters).replace("from", "0"); verify(parameters).replace("size", Integer.toString(MAX_SEARCH_RESULTS)); verify(viewFacet1).dispatch(request, context); verify(viewFacet2).dispatch(request, context); }
@Test public void testSkipResultWithMissingPackage() throws Exception { when(npmSearchResponseObject1.getPackageEntry()).thenReturn(null); Response response = underTest.doGet(context, dispatchedRepositories); assertThat(response.getStatus().getCode(), is(OK)); assertThat(response.getPayload(), not(nullValue())); try (InputStream in = response.getPayload().openInputStream()) { assertThat(CharStreams.toString(new InputStreamReader(in, UTF_8)), is("response")); } ArgumentCaptor<NpmSearchResponse> searchResponseCaptor = ArgumentCaptor.forClass(NpmSearchResponse.class); verify(npmSearchResponseMapper).writeString(searchResponseCaptor.capture()); NpmSearchResponse searchResponse = searchResponseCaptor.getValue(); assertThat(searchResponse.getTotal(), is(1)); assertThat(searchResponse.getObjects(), hasSize(1)); assertThat(searchResponse.getObjects(), contains(npmSearchResponseObject2)); }
@Test public void testSkipResultWithMissingPackageName() throws Exception { when(npmSearchResponsePackage1.getName()).thenReturn(null); Response response = underTest.doGet(context, dispatchedRepositories); assertThat(response.getStatus().getCode(), is(OK)); assertThat(response.getPayload(), not(nullValue())); try (InputStream in = response.getPayload().openInputStream()) { assertThat(CharStreams.toString(new InputStreamReader(in, UTF_8)), is("response")); } ArgumentCaptor<NpmSearchResponse> searchResponseCaptor = ArgumentCaptor.forClass(NpmSearchResponse.class); verify(npmSearchResponseMapper).writeString(searchResponseCaptor.capture()); NpmSearchResponse searchResponse = searchResponseCaptor.getValue(); assertThat(searchResponse.getTotal(), is(1)); assertThat(searchResponse.getObjects(), hasSize(1)); assertThat(searchResponse.getObjects(), contains(npmSearchResponseObject2)); }
@Test public void testSkipResultWithMissingScore() throws Exception { when(npmSearchResponseObject1.getSearchScore()).thenReturn(null); Response response = underTest.doGet(context, dispatchedRepositories); assertThat(response.getStatus().getCode(), is(OK)); assertThat(response.getPayload(), not(nullValue())); try (InputStream in = response.getPayload().openInputStream()) { assertThat(CharStreams.toString(new InputStreamReader(in, UTF_8)), is("response")); } ArgumentCaptor<NpmSearchResponse> searchResponseCaptor = ArgumentCaptor.forClass(NpmSearchResponse.class); verify(npmSearchResponseMapper).writeString(searchResponseCaptor.capture()); NpmSearchResponse searchResponse = searchResponseCaptor.getValue(); assertThat(searchResponse.getTotal(), is(1)); assertThat(searchResponse.getObjects(), hasSize(1)); assertThat(searchResponse.getObjects(), contains(npmSearchResponseObject2)); }
@Test public void testSkipResultWithBadResponse() throws Exception { when(status1.getCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR); Response response = underTest.doGet(context, dispatchedRepositories); assertThat(response.getStatus().getCode(), is(OK)); assertThat(response.getPayload(), not(nullValue())); try (InputStream in = response.getPayload().openInputStream()) { assertThat(CharStreams.toString(new InputStreamReader(in, UTF_8)), is("response")); } ArgumentCaptor<NpmSearchResponse> searchResponseCaptor = ArgumentCaptor.forClass(NpmSearchResponse.class); verify(npmSearchResponseMapper).writeString(searchResponseCaptor.capture()); NpmSearchResponse searchResponse = searchResponseCaptor.getValue(); assertThat(searchResponse.getTotal(), is(1)); assertThat(searchResponse.getObjects(), hasSize(1)); assertThat(searchResponse.getObjects(), contains(npmSearchResponseObject2)); }
@Test public void testSkipResultWithMissingPayload() throws Exception { when(response1.getPayload()).thenReturn(null); Response response = underTest.doGet(context, dispatchedRepositories); assertThat(response.getStatus().getCode(), is(OK)); assertThat(response.getPayload(), not(nullValue())); try (InputStream in = response.getPayload().openInputStream()) { assertThat(CharStreams.toString(new InputStreamReader(in, UTF_8)), is("response")); } ArgumentCaptor<NpmSearchResponse> searchResponseCaptor = ArgumentCaptor.forClass(NpmSearchResponse.class); verify(npmSearchResponseMapper).writeString(searchResponseCaptor.capture()); NpmSearchResponse searchResponse = searchResponseCaptor.getValue(); assertThat(searchResponse.getTotal(), is(1)); assertThat(searchResponse.getObjects(), hasSize(1)); assertThat(searchResponse.getObjects(), contains(npmSearchResponseObject2)); }