OpaDataFilterMongoRepositoryImpl extends SimpleMongoRepository<T, ID> implements OpaDataFilterMongoRepository<T, ID> { @Override public List<T> findAll() { LOGGER.trace("Filtering data with OPA, findAll()"); Query query = mongoQueryService.getMongoDBQuery(); if (query == null) { return Collections.emptyList(); } String collectionName = entityInformation.getCollectionName(); LOGGER.trace("Collection name: {}", collectionName); Class<T> javaType = entityInformation.getJavaType(); LOGGER.trace("Java type: {}", javaType.getName()); String queryJson = query.getQueryObject().toJson(); LOGGER.trace("Query: {}", queryJson); LOGGER.trace("MongoOperations: {}", mongoOperations); return mongoOperations.find(query, javaType, collectionName); } OpaDataFilterMongoRepositoryImpl(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations, MongoQueryService<T> mongoQueryService); @Override List<T> findAll(); @Override List<T> findAll(Sort sort); @Override Page<T> findAll(Pageable pageable);  }
@Test void shouldFindAllWithSort() { Sort sort = Sort.by("name"); PetDocument petDocument1 = new PetDocument(); petDocument1.setName("fluffy"); PetDocument petDocument2 = new PetDocument(); petDocument2.setName("browny"); Query query = new Query(); when(queryService.getMongoDBQuery()).thenReturn(query); when(entityInformation.getJavaType()).thenReturn(PetDocument.class); when(entityInformation.getCollectionName()).thenReturn("pets"); when(mongoOperations.find(query.with(sort), PetDocument.class, "pets")).thenReturn(List.of(petDocument2, petDocument1)); List<PetDocument> result = target.findAll(sort); assertThat(result.size(), is(2)); assertThat(result.get(0).getName(), is("browny")); }
@Test void shouldFindAllWithPageable() { Sort sort = Sort.by("name"); PetDocument petDocument1 = new PetDocument(); petDocument1.setName("fluffy"); PetDocument petDocument2 = new PetDocument(); petDocument2.setName("browny"); Query query = new Query(); when(queryService.getMongoDBQuery()).thenReturn(query); when(entityInformation.getJavaType()).thenReturn(PetDocument.class); when(entityInformation.getCollectionName()).thenReturn("pets"); when(mongoOperations.find(query.with(sort), PetDocument.class, "pets")).thenReturn(List.of(petDocument2, petDocument1)); Page<PetDocument> result = target.findAll(PageRequest.of(1, 1, Sort.by("name"))); assertThat(result, is(notNullValue())); assertThat(result.getSize(), is(1)); }
@Test void shouldFindAll() { Query query = new Query(); when(queryService.getMongoDBQuery()).thenReturn(query); when(entityInformation.getJavaType()).thenReturn(PetDocument.class); when(entityInformation.getCollectionName()).thenReturn("pets"); when(mongoOperations.find(query, PetDocument.class, "pets")).thenReturn(List.of(new PetDocument())); List<PetDocument> result = target.findAll(); assertThat(result.size(), is(1)); }