private List<String> selectUuidsByQuery(Consumer<Builder> query, Pagination pagination) { Builder builder = newOrganizationQueryBuilder(); query.accept(builder); return underTest.selectByQuery(dbSession, builder.build(), pagination).stream() .map(OrganizationDto::getUuid) .collect(Collectors.toList()); }
public OrganizationQuery build() { return new OrganizationQuery(this); } }
@Test public void selectByQuery_returns_empty_on_table_with_single_row_when_not_requesting_the_first_page() { insertOrganization(ORGANIZATION_DTO_1); assertThat(underTest.selectByQuery(dbSession, returnAll(), forPage(2).andSize(1))).isEmpty(); assertThat(underTest.selectByQuery(dbSession, returnAll(), forPage(Math.abs(new Random().nextInt(10)) + 2).andSize(1))).isEmpty(); assertThat(underTest.selectByQuery(dbSession, returnAll(), forPage(2).andSize(10))).isEmpty(); }
private static OrganizationQuery newQueryWithKeys(String... keys) { return newOrganizationQueryBuilder().setKeys(Arrays.asList(keys)).build(); }
public OrganizationQuery build() { return new OrganizationQuery(this); } }
@Test public void selectByQuery_returns_empty_when_table_is_empty() { assertThat(underTest.selectByQuery(dbSession, returnAll(), forPage(2).andSize(1))).isEmpty(); }
private OrganizationQuery buildDbQuery(Request request) { return newOrganizationQueryBuilder() .setKeys(request.paramAsStrings(PARAM_ORGANIZATIONS)) .setMember(getUserIdIfFilterOnMembership(request)) .build(); }
@Test public void selectByQuery_returns_single_row_of_table_when_requesting_first_page_of_size_1_or_more() { insertOrganization(ORGANIZATION_DTO_1); assertThat(underTest.selectByQuery(dbSession, returnAll(), forPage(1).andSize(1))) .hasSize(1) .extracting("uuid") .containsOnly(ORGANIZATION_DTO_1.getUuid()); assertThat(underTest.selectByQuery(dbSession, returnAll(), forPage(1).andSize(10))) .hasSize(1) .extracting("uuid") .containsOnly(ORGANIZATION_DTO_1.getUuid()); }
@Test public void selectByQuery_with_empty_list_of_keys_returns_all() { insertOrganization(ORGANIZATION_DTO_1); insertOrganization(ORGANIZATION_DTO_2); OrganizationQuery organizationQuery = newOrganizationQueryBuilder().setKeys(Lists.emptyList()).build(); assertThat(underTest.selectByQuery(dbSession, organizationQuery, forPage(1).andSize(10))) .extracting(OrganizationDto::getUuid) .containsOnly(ORGANIZATION_DTO_1.getUuid(), ORGANIZATION_DTO_2.getUuid()); }
@Test public void countByQuery() { insertOrganization(copyOf(ORGANIZATION_DTO_1).setUuid("uuid3").setKey("key-3")); insertOrganization(copyOf(ORGANIZATION_DTO_1).setUuid("uuid1").setKey("key-1")); insertOrganization(copyOf(ORGANIZATION_DTO_1).setUuid("uuid2").setKey("key-2")); insertOrganization(copyOf(ORGANIZATION_DTO_1).setUuid("uuid5").setKey("key-5")); insertOrganization(copyOf(ORGANIZATION_DTO_1).setUuid("uuid4").setKey("key-4")); assertThat(underTest.countByQuery(dbSession, returnAll())).isEqualTo(5); assertThat(underTest.countByQuery(dbSession, newQueryWithKeys("key-1", "key-2"))).isEqualTo(2); assertThat(underTest.countByQuery(dbSession, newQueryWithKeys("unknown"))).isZero(); }
@Test public void selectByQuery_filter_on_a_member() { OrganizationDto organization = db.organizations().insert(); OrganizationDto anotherOrganization = db.organizations().insert(); OrganizationDto organizationWithoutMember = db.organizations().insert(); UserDto user = db.users().insertUser(); db.organizations().addMember(organization, user); db.organizations().addMember(anotherOrganization, user); List<OrganizationDto> result = underTest.selectByQuery(dbSession, newOrganizationQueryBuilder().setMember(user.getId()).build(), forPage(1).andSize(100)); assertThat(result).extracting(OrganizationDto::getUuid) .containsExactlyInAnyOrder(organization.getUuid(), anotherOrganization.getUuid()) .doesNotContain(organizationWithoutMember.getUuid()); }
@Test public void selectByQuery_filter_on_a_member_and_keys() { OrganizationDto organization = db.organizations().insert(); OrganizationDto anotherOrganization = db.organizations().insert(); OrganizationDto organizationWithoutKeyProvided = db.organizations().insert(); OrganizationDto organizationWithoutMember = db.organizations().insert(); UserDto user = db.users().insertUser(); db.organizations().addMember(organization, user); db.organizations().addMember(anotherOrganization, user); db.organizations().addMember(organizationWithoutKeyProvided, user); List<OrganizationDto> result = underTest.selectByQuery(dbSession, newOrganizationQueryBuilder() .setKeys(Arrays.asList(organization.getKey(), anotherOrganization.getKey(), organizationWithoutMember.getKey())) .setMember(user.getId()).build(), forPage(1).andSize(100)); assertThat(result).extracting(OrganizationDto::getUuid) .containsExactlyInAnyOrder(organization.getUuid(), anotherOrganization.getUuid()) .doesNotContain(organizationWithoutKeyProvided.getUuid(), organizationWithoutMember.getUuid()); }
private OrganizationQuery buildDbQuery(Request request) { return newOrganizationQueryBuilder() .setKeys(request.paramAsStrings(PARAM_ORGANIZATIONS)) .setMember(getUserIdIfFilterOnMembership(request)) .build(); }