ComponentsResource extends ComponentSupport implements Resource, ComponentsResourceDoc { @Override @GET public Page<ComponentXO> getComponents(@QueryParam("continuationToken") final String continuationToken, @QueryParam("repository") final String repositoryId) { Repository repository = repositoryManagerRESTAdapter.getRepository(repositoryId); PageResult<Component> componentBrowseResult = browseService .browseComponents(repository, new QueryOptions(null, "id", "asc", 0, 10, lastIdFromContinuationToken(continuationToken))); List<ComponentXO> componentXOs = componentBrowseResult.getResults().stream() .map(component -> fromComponent(component, repository)) .collect(toList()); return new Page<>(componentXOs, componentBrowseResult.getTotal() > componentBrowseResult.getResults().size() ? continuationTokenHelper.getTokenFromId(getLast(componentBrowseResult.getResults())) : null); } @Inject  ComponentsResource(final RepositoryManagerRESTAdapter repositoryManagerRESTAdapter,
                            final BrowseService browseService,
                            final ComponentEntityAdapter componentEntityAdapter,
                            final MaintenanceService maintenanceService,
                            @Named("component") final ContinuationTokenHelper continuationTokenHelper,
                            final UploadManager uploadManager,
                            final UploadConfiguration uploadConfiguration,
                            final ComponentXOFactory componentXOFactory,
                            final Set<ComponentsResourceExtension> componentsResourceExtensions); @Override @GET Page<ComponentXO> getComponents(@QueryParam("continuationToken") final String continuationToken,
                                         @QueryParam("repository") final String repositoryId); @Override @GET @Path("/{id}") ComponentXO getComponentById(@PathParam("id") final String id); @Override @DELETE @Path("/{id}") void deleteComponent(@PathParam("id") final String id); @Override @POST @Consumes(MediaType.MULTIPART_FORM_DATA) void uploadComponent(@QueryParam("repository") final String repositoryId,
                              @Context final HttpServletRequest request); static final String RESOURCE_URI; }
@Test public void getComponents_firstPage() throws Exception { when(componentBrowseResult.getTotal()).thenReturn(10L); when(componentBrowseResult.getResults()).thenReturn(Arrays.asList(componentOne, componentTwo)); when(browseService.browseComponents(eq(mavenReleases), queryOptionsCaptor.capture())) .thenReturn(componentBrowseResult); Page<ComponentXO> componentXOPage = underTest.getComponents(null, mavenReleasesId); assertThat(componentXOPage.getItems(), hasSize(2)); assertThat(componentXOPage.getContinuationToken(), is("88491cd1d185dd1318fdba4364e78406")); assertThat(queryOptionsCaptor.getValue().getLastId(), nullValue()); verify(componentsResourceExtension, times(2)).updateComponentXO(any(ComponentXO.class), any(Component.class)); componentXOPage.getItems().stream().forEach(componentXO -> assertThat(componentXO.getAssets(), hasSize(1))); }
@Test public void invalidContinuationTokenReturnsNotAcceptable() { doThrow(new IllegalArgumentException()).when(componentEntityAdapter) .recordIdentity(detachedEntityIdCaptor.capture()); thrown.expect(hasProperty("response", hasProperty("status", is(NOT_ACCEPTABLE)))); underTest.getComponents("whatever", mavenReleasesId); }
@Test public void getComponents_lastPage() throws Exception { when(componentBrowseResult.getTotal()).thenReturn(2L); when(componentBrowseResult.getResults()).thenReturn(Arrays.asList(componentOne, componentTwo)); when(browseService.browseComponents(eq(mavenReleases), queryOptionsCaptor.capture())) .thenReturn(componentBrowseResult); when(componentEntityAdapter.recordIdentity(detachedEntityIdCaptor.capture())).thenReturn(componentTwoORID); Page<ComponentXO> componentXOPage = underTest.getComponents("88491cd1d185dd1318fdba4364e78406", mavenReleasesId); assertThat(componentXOPage.getItems(), hasSize(2)); assertThat(componentXOPage.getContinuationToken(), isEmptyOrNullString()); assertThat(queryOptionsCaptor.getValue().getLastId(), notNullValue()); assertThat(detachedEntityIdCaptor.getValue(), notNullValue()); verify(componentsResourceExtension, times(2)).updateComponentXO(any(ComponentXO.class), any(Component.class)); componentXOPage.getItems().stream().forEach(componentXO -> assertThat(componentXO.getAssets(), hasSize(1))); }