Search in sources :

Example 1 with PaginationToken

use of org.broadinstitute.consent.http.models.PaginationToken in project consent by DataBiosphere.

the class DarCollectionServiceTest method testQueryCollectionsByFiltersAndUserRolesForResearcher.

@Test
public void testQueryCollectionsByFiltersAndUserRolesForResearcher() {
    User user = new User();
    user.setDacUserId(1);
    String dacRoleName = UserRoles.RESEARCHER.getRoleName();
    Integer dacRoleId = UserRoles.RESEARCHER.getRoleId();
    UserRole memberRole = new UserRole(dacRoleId, dacRoleName);
    user.addRole(memberRole);
    int pageSize = 10;
    int mockCollectionSize = 3;
    List<DarCollection> mockCollection = createMockCollections(mockCollectionSize);
    PaginationToken token = initPaginationToken("darCode", "DESC", "", pageSize);
    when(darCollectionDAO.returnUnfilteredResearcherCollectionCount(anyInt())).thenReturn(mockCollectionSize);
    when(darCollectionDAO.getFilteredListForResearcher(anyString(), anyString(), anyInt(), anyString())).thenReturn(mockCollection);
    initService();
    PaginationResponse<DarCollection> response = service.queryCollectionsByFiltersAndUserRoles(user, token, dacRoleName);
    assertNotNull(response);
    assertEquals(mockCollectionSize, (int) response.getUnfilteredCount());
    assertEquals(3, (int) response.getFilteredCount());
    assertEquals(1, (int) response.getFilteredPageCount());
}
Also used : User(org.broadinstitute.consent.http.models.User) UserRole(org.broadinstitute.consent.http.models.UserRole) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PaginationToken(org.broadinstitute.consent.http.models.PaginationToken) DarCollection(org.broadinstitute.consent.http.models.DarCollection) Test(org.junit.Test)

Example 2 with PaginationToken

use of org.broadinstitute.consent.http.models.PaginationToken in project consent by DataBiosphere.

the class DarCollectionServiceTest method testInitWithInvalidTokenValues.

@Test
public void testInitWithInvalidTokenValues() {
    int filteredCount = 5;
    int unfilteredCount = 20;
    PaginationToken token = new PaginationToken(2, 10, "darCode", "DESC", null, DarCollection.acceptableSortFields, DarCollection.defaultTokenSortField);
    initWithPaginationToken(token, unfilteredCount, filteredCount);
    // Start index will be > end index in this case since we're trying to get results 11-20 when
    // there are only 5 items in the results array, so there should be 0 results returned
    PaginationResponse<DarCollection> response = service.queryCollectionsByFiltersAndUserRoles(user, token, UserRoles.ADMIN.getRoleName());
    assertTrue(response.getResults().isEmpty());
}
Also used : PaginationToken(org.broadinstitute.consent.http.models.PaginationToken) DarCollection(org.broadinstitute.consent.http.models.DarCollection) Test(org.junit.Test)

Example 3 with PaginationToken

use of org.broadinstitute.consent.http.models.PaginationToken in project consent by DataBiosphere.

the class DarCollectionServiceTest method testGetCollectionsWithFiltersByPage.

@Test
public void testGetCollectionsWithFiltersByPage() {
    User user = createMockAdminUser();
    IntStream.rangeClosed(1, 8).forEach(page -> {
        int filteredCount = 75;
        int unfilteredCount = 100;
        PaginationToken token = new PaginationToken(page, 10, "darCode", "DESC", null, DarCollection.acceptableSortFields, DarCollection.defaultTokenSortField);
        List<DarCollection> unfilteredList = createMockCollections(unfilteredCount);
        List<DarCollection> filteredList = unfilteredList.subList(0, filteredCount);
        initService();
        when(darCollectionDAO.returnUnfilteredCollectionCount()).thenReturn(unfilteredCount);
        when(darCollectionDAO.getFilteredCollectionsForAdmin(anyString(), anyString(), anyString())).thenReturn(filteredList);
        PaginationResponse<DarCollection> response = service.queryCollectionsByFiltersAndUserRoles(user, token, UserRoles.ADMIN.getRoleName());
        // Assert that the results sizes are correct
        if (page == 8) {
            int lastPageSize = token.getFilteredCount() % token.getPageSize();
            assertEquals(lastPageSize, response.getResults().size());
        } else {
            assertEquals((int) token.getPageSize(), response.getResults().size());
        }
        // Assert that the returned results are what we expect them to be, based on ID
        int expectedCollectionId = (page * token.getPageSize()) - token.getPageSize() + 1;
        assertEquals(Integer.valueOf(expectedCollectionId), response.getResults().get(0).getDarCollectionId());
        assertEquals(filteredCount, response.getFilteredCount().intValue());
    });
}
Also used : User(org.broadinstitute.consent.http.models.User) PaginationToken(org.broadinstitute.consent.http.models.PaginationToken) DarCollection(org.broadinstitute.consent.http.models.DarCollection) Test(org.junit.Test)

Example 4 with PaginationToken

use of org.broadinstitute.consent.http.models.PaginationToken in project consent by DataBiosphere.

the class DarCollectionServiceTest method testGetCollectionsWithFiltersByPageLessThanPageSize.

@Test
public void testGetCollectionsWithFiltersByPageLessThanPageSize() {
    int filteredCount = 3;
    int unfilteredCount = 5;
    User user = createMockAdminUser();
    PaginationToken token = new PaginationToken(1, 10, "darCode", "DESC", null, DarCollection.acceptableSortFields, DarCollection.defaultTokenSortField);
    List<DarCollection> mockCollections = createMockCollections(3);
    when(darCollectionDAO.returnUnfilteredCollectionCount()).thenReturn(unfilteredCount);
    when(darCollectionDAO.getFilteredCollectionsForAdmin(anyString(), anyString(), anyString())).thenReturn(mockCollections);
    initService();
    PaginationResponse<DarCollection> response = service.queryCollectionsByFiltersAndUserRoles(user, token, UserRoles.ADMIN.getRoleName());
    assertEquals(1, response.getFilteredPageCount().intValue());
    assertEquals(filteredCount, response.getResults().size());
    assertEquals(filteredCount, response.getFilteredCount().intValue());
}
Also used : User(org.broadinstitute.consent.http.models.User) PaginationToken(org.broadinstitute.consent.http.models.PaginationToken) DarCollection(org.broadinstitute.consent.http.models.DarCollection) Test(org.junit.Test)

Example 5 with PaginationToken

use of org.broadinstitute.consent.http.models.PaginationToken in project consent by DataBiosphere.

the class DarCollectionServiceTest method testGetCollectionsWithFilters_EmptyUnfiltered.

@Test
public void testGetCollectionsWithFilters_EmptyUnfiltered() {
    User user = createMockAdminUser();
    PaginationToken token = new PaginationToken(1, 10, "darCode", "DESC", null, DarCollection.acceptableSortFields, DarCollection.defaultTokenSortField);
    when(darCollectionDAO.returnUnfilteredCollectionCount()).thenReturn(0);
    initService();
    PaginationResponse<DarCollection> response = service.queryCollectionsByFiltersAndUserRoles(user, token, UserRoles.ADMIN.getRoleName());
    assertEquals(1, response.getFilteredPageCount().intValue());
    assertEquals(0, response.getUnfilteredCount().intValue());
    assertEquals(0, response.getFilteredCount().intValue());
}
Also used : User(org.broadinstitute.consent.http.models.User) PaginationToken(org.broadinstitute.consent.http.models.PaginationToken) DarCollection(org.broadinstitute.consent.http.models.DarCollection) Test(org.junit.Test)

Aggregations

DarCollection (org.broadinstitute.consent.http.models.DarCollection)11 PaginationToken (org.broadinstitute.consent.http.models.PaginationToken)11 User (org.broadinstitute.consent.http.models.User)10 Test (org.junit.Test)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 UserRole (org.broadinstitute.consent.http.models.UserRole)4 BadRequestException (javax.ws.rs.BadRequestException)2 ForbiddenException (javax.ws.rs.ForbiddenException)2 GET (javax.ws.rs.GET)2 NotFoundException (javax.ws.rs.NotFoundException)2 Path (javax.ws.rs.Path)2 AuthUser (org.broadinstitute.consent.http.models.AuthUser)2 PermitAll (javax.annotation.security.PermitAll)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 Consumes (javax.ws.rs.Consumes)1 Produces (javax.ws.rs.Produces)1