@Test public void testFindByCriteria() { Membership membership = new Membership(); membership.setRoleId("role#1"); membership.setReferenceType(ReferenceType.ORGANIZATION); membership.setReferenceId(ORGANIZATION_ID); membership.setMemberType(MemberType.USER); membership.setMemberId("user#1"); Membership groupMembership = new Membership(); groupMembership.setRoleId("role#1"); groupMembership.setReferenceType(ReferenceType.ORGANIZATION); groupMembership.setReferenceId(ORGANIZATION_ID); groupMembership.setMemberType(MemberType.GROUP); groupMembership.setMemberId("group#1"); membershipRepository.create(membership).blockingGet(); membershipRepository.create(groupMembership).blockingGet(); MembershipCriteria criteria = new MembershipCriteria(); TestSubscriber<Membership> obs = membershipRepository.findByCriteria(ReferenceType.ORGANIZATION, ORGANIZATION_ID, criteria).test(); obs.awaitTerminalEvent(); obs.assertValueCount(2); criteria.setUserId("user#1"); obs = membershipRepository.findByCriteria(ReferenceType.ORGANIZATION, ORGANIZATION_ID, criteria).test(); obs.awaitTerminalEvent(); obs.assertValueCount(1); obs.assertValue(m -> m.getMemberType() == MemberType.USER && m.getMemberId().equals("user#1")); criteria.setUserId(null); criteria.setGroupIds(Arrays.asList("group#1")); obs = membershipRepository.findByCriteria(ReferenceType.ORGANIZATION, ORGANIZATION_ID, criteria).test(); obs.awaitTerminalEvent(); obs.assertValueCount(1); obs.assertValue(m -> m.getMemberType() == MemberType.GROUP && m.getMemberId().equals("group#1")); criteria.setUserId("user#1"); criteria.setGroupIds(Arrays.asList("group#1")); obs = membershipRepository.findByCriteria(ReferenceType.ORGANIZATION, ORGANIZATION_ID, criteria).test(); obs.awaitTerminalEvent(); obs.assertNoValues(); obs.assertNoErrors(); criteria.setUserId("user#1"); criteria.setGroupIds(Arrays.asList("group#1")); criteria.setLogicalOR(true); obs = membershipRepository.findByCriteria(ReferenceType.ORGANIZATION, ORGANIZATION_ID, criteria).test(); obs.awaitTerminalEvent(); obs.assertValueCount(2); }
@Override public Flowable<Membership> findByCriteria(ReferenceType referenceType, String referenceId, MembershipCriteria criteria) { Bson eqReference = and(eq(FIELD_REFERENCE_TYPE, referenceType.name()), eq(FIELD_REFERENCE_ID, referenceId)); Bson eqGroupId = null; Bson eqUserId = null; if (criteria.getGroupIds().isPresent()) { eqGroupId = and(eq(FIELD_MEMBER_TYPE, MemberType.GROUP.name()), in(FIELD_MEMBER_ID, criteria.getGroupIds().get())); } if (criteria.getUserId().isPresent()) { eqUserId = and(eq(FIELD_MEMBER_TYPE, MemberType.USER.name()), eq(FIELD_MEMBER_ID, criteria.getUserId().get())); } if (criteria.getRoleId().isPresent()) { eqUserId = eq(FIELD_ROLE, criteria.getRoleId().get()); } return toBsonFilter(criteria.isLogicalOR(), eqGroupId, eqUserId) .map(filter -> and(eqReference, filter)) .switchIfEmpty(Single.just(eqReference)) .flatMapPublisher(filter -> Flowable.fromPublisher(membershipsCollection.find(filter))).map(this::convert); }
MongoMembershipRepository extends AbstractManagementMongoRepository implements MembershipRepository { @Override public Flowable<Membership> findByCriteria(ReferenceType referenceType, String referenceId, MembershipCriteria criteria) { Bson eqReference = and(eq(FIELD_REFERENCE_TYPE, referenceType.name()), eq(FIELD_REFERENCE_ID, referenceId)); Bson eqGroupId = null; Bson eqUserId = null; if (criteria.getGroupIds().isPresent()) { eqGroupId = and(eq(FIELD_MEMBER_TYPE, MemberType.GROUP.name()), in(FIELD_MEMBER_ID, criteria.getGroupIds().get())); } if (criteria.getUserId().isPresent()) { eqUserId = and(eq(FIELD_MEMBER_TYPE, MemberType.USER.name()), eq(FIELD_MEMBER_ID, criteria.getUserId().get())); } if (criteria.getRoleId().isPresent()) { eqUserId = eq(FIELD_ROLE, criteria.getRoleId().get()); } return toBsonFilter(criteria.isLogicalOR(), eqGroupId, eqUserId) .map(filter -> and(eqReference, filter)) .switchIfEmpty(Single.just(eqReference)) .flatMapPublisher(filter -> Flowable.fromPublisher(membershipsCollection.find(filter))).map(this::convert); } }
MongoMembershipRepository extends AbstractManagementMongoRepository implements MembershipRepository { @Override public Flowable<Membership> findByCriteria(ReferenceType referenceType, String referenceId, MembershipCriteria criteria) { Bson eqReference = and(eq(FIELD_REFERENCE_TYPE, referenceType.name()), eq(FIELD_REFERENCE_ID, referenceId)); Bson eqGroupId = null; Bson eqUserId = null; if (criteria.getGroupIds().isPresent()) { eqGroupId = and(eq(FIELD_MEMBER_TYPE, MemberType.GROUP.name()), in(FIELD_MEMBER_ID, criteria.getGroupIds().get())); } if (criteria.getUserId().isPresent()) { eqUserId = and(eq(FIELD_MEMBER_TYPE, MemberType.USER.name()), eq(FIELD_MEMBER_ID, criteria.getUserId().get())); } if (criteria.getRoleId().isPresent()) { eqUserId = eq(FIELD_ROLE, criteria.getRoleId().get()); } return toBsonFilter(criteria.isLogicalOR(), eqGroupId, eqUserId) .map(filter -> and(eqReference, filter)) .switchIfEmpty(Single.just(eqReference)) .flatMapPublisher(filter -> Flowable.fromPublisher(membershipsCollection.find(filter))).map(this::convert); }  }
MongoMembershipRepository extends AbstractManagementMongoRepository implements MembershipRepository { @Override public Flowable<Membership> findByCriteria(ReferenceType referenceType, String referenceId, MembershipCriteria criteria) { Bson eqReference = and(eq(FIELD_REFERENCE_TYPE, referenceType.name()), eq(FIELD_REFERENCE_ID, referenceId)); Bson eqGroupId = null; Bson eqUserId = null; if (criteria.getGroupIds().isPresent()) { eqGroupId = and(eq(FIELD_MEMBER_TYPE, MemberType.GROUP.name()), in(FIELD_MEMBER_ID, criteria.getGroupIds().get())); } if (criteria.getUserId().isPresent()) { eqUserId = and(eq(FIELD_MEMBER_TYPE, MemberType.USER.name()), eq(FIELD_MEMBER_ID, criteria.getUserId().get())); } if (criteria.getRoleId().isPresent()) { eqUserId = eq(FIELD_ROLE, criteria.getRoleId().get()); } return toBsonFilter(criteria.isLogicalOR(), eqGroupId, eqUserId) .map(filter -> and(eqReference, filter)) .switchIfEmpty(Single.just(eqReference)) .flatMapPublisher(filter -> Flowable.fromPublisher(membershipsCollection.find(filter))).map(this::convert); }  @PostConstruct void init(); @Override Single<List<Membership>> findByReference(String referenceId, ReferenceType referenceType); @Override Flowable<Membership> findByCriteria(ReferenceType referenceType, String referenceId, MembershipCriteria criteria); @Override Maybe<Membership> findByReferenceAndMember(ReferenceType referenceType, String referenceId, MemberType memberType, String memberId); @Override Maybe<Membership> findById(String id); @Override Single<Membership> create(Membership item); @Override Single<Membership> update(Membership item); @Override Completable delete(String id); }
MongoMembershipRepository extends AbstractManagementMongoRepository implements MembershipRepository { @Override public Flowable<Membership> findByCriteria(ReferenceType referenceType, String referenceId, MembershipCriteria criteria) { Bson eqReference = and(eq(FIELD_REFERENCE_TYPE, referenceType.name()), eq(FIELD_REFERENCE_ID, referenceId)); Bson eqGroupId = null; Bson eqUserId = null; if (criteria.getGroupIds().isPresent()) { eqGroupId = and(eq(FIELD_MEMBER_TYPE, MemberType.GROUP.name()), in(FIELD_MEMBER_ID, criteria.getGroupIds().get())); } if (criteria.getUserId().isPresent()) { eqUserId = and(eq(FIELD_MEMBER_TYPE, MemberType.USER.name()), eq(FIELD_MEMBER_ID, criteria.getUserId().get())); } if (criteria.getRoleId().isPresent()) { eqUserId = eq(FIELD_ROLE, criteria.getRoleId().get()); } return toBsonFilter(criteria.isLogicalOR(), eqGroupId, eqUserId) .map(filter -> and(eqReference, filter)) .switchIfEmpty(Single.just(eqReference)) .flatMapPublisher(filter -> Flowable.fromPublisher(membershipsCollection.find(filter))).map(this::convert); }  @PostConstruct void init(); @Override Single<List<Membership>> findByReference(String referenceId, ReferenceType referenceType); @Override Flowable<Membership> findByCriteria(ReferenceType referenceType, String referenceId, MembershipCriteria criteria); @Override Maybe<Membership> findByReferenceAndMember(ReferenceType referenceType, String referenceId, MemberType memberType, String memberId); @Override Maybe<Membership> findById(String id); @Override Single<Membership> create(Membership item); @Override Single<Membership> update(Membership item); @Override Completable delete(String id); static final String FIELD_MEMBER_TYPE; static final String FIELD_ROLE; }