BAEL-5365 Count in Spring Data MongoDB Repository (#12481)
* BAEL-5370 - MongoDB Composite Key First Draft. * removing comments * BAEL-5370 Test could fail if ran in a different order: givenCompositeId_whenSearchingByIdObject_thenFound * BAEL-5370 removing compound index related stuff * removing first insert from assertThrows * first draft * removing Customer2 * first draft * adjustments * removed controlled * fixed formatting on Car
This commit is contained in:
parent
50955510ca
commit
8d894df26a
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.boot.count;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableMongoRepositories(basePackages = { "com.baeldung.boot.count" })
|
||||
public class SpringBootCountApplication {
|
||||
public static void main(String... args) {
|
||||
SpringApplication.run(SpringBootCountApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.boot.count.dao;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.data.mongodb.repository.Query;
|
||||
|
||||
import com.baeldung.boot.count.data.Car;
|
||||
|
||||
public interface CarRepository extends MongoRepository<Car, String> {
|
||||
@Query(value = "{brand: ?0}", count = true)
|
||||
public long countBrand(String brand);
|
||||
|
||||
Long countByBrand(String brand);
|
||||
|
||||
@Query(value = "{}", count = true)
|
||||
Long countWithAnnotation();
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.boot.count.data;
|
||||
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@Document
|
||||
public class Car {
|
||||
private String name;
|
||||
|
||||
private String brand;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package com.baeldung.boot.count.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.boot.count.dao.CarRepository;
|
||||
import com.baeldung.boot.count.data.Car;
|
||||
|
||||
@Service
|
||||
public class CountCarService {
|
||||
|
||||
@Autowired
|
||||
private CarRepository repo;
|
||||
|
||||
@Autowired
|
||||
private MongoTemplate mongo;
|
||||
|
||||
public List<Car> findCars() {
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
public Optional<Car> findCar(String id) {
|
||||
return repo.findById(id);
|
||||
}
|
||||
|
||||
public Car insertCar(Car item) {
|
||||
return repo.insert(item);
|
||||
}
|
||||
|
||||
public long getCountWithQueryAnnotation() {
|
||||
return repo.countWithAnnotation();
|
||||
}
|
||||
|
||||
public long getCountWithCrudRepository() {
|
||||
return repo.count();
|
||||
}
|
||||
|
||||
public long getCountBrandWithQueryMethod(String brand) {
|
||||
return repo.countByBrand(brand);
|
||||
}
|
||||
|
||||
public long getCountWithExample(Car item) {
|
||||
return repo.count(Example.of(item));
|
||||
}
|
||||
|
||||
public long getCountWithExampleCriteria(Car item) {
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.byExample(item));
|
||||
return mongo.count(query, Car.class);
|
||||
}
|
||||
|
||||
public long getCountBrandWithQueryAnnotation(String brand) {
|
||||
return repo.countBrand(brand);
|
||||
}
|
||||
|
||||
public long getCountBrandWithCriteria(String brand) {
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.where("brand")
|
||||
.is(brand));
|
||||
return mongo.count(query, Car.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
package com.baeldung.boot.count.service;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.count.data.Car;
|
||||
|
||||
@SpringBootTest
|
||||
@DirtiesContext
|
||||
@RunWith(SpringRunner.class)
|
||||
public class CountCarServiceIntegrationTest {
|
||||
@Autowired
|
||||
private CountCarService service;
|
||||
|
||||
Car car1 = new Car("B-A");
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
service.insertCar(car1);
|
||||
service.insertCar(new Car("B-B"));
|
||||
service.insertCar(new Car("B-C"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllDocs_whenQueryAnnotationCount_thenCountEqualsSize() {
|
||||
List<Car> all = service.findCars();
|
||||
|
||||
long count = service.getCountWithQueryAnnotation();
|
||||
|
||||
assertEquals(count, all.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllDocs_whenCrudRepositoryCount_thenCountEqualsSize() {
|
||||
List<Car> all = service.findCars();
|
||||
|
||||
long count = service.getCountWithCrudRepository();
|
||||
|
||||
assertEquals(count, all.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilteredDocs_whenCriteriaCountByBrand_thenCountEqualsSize() {
|
||||
String filter = "B-A";
|
||||
long all = service.findCars()
|
||||
.stream()
|
||||
.filter(car -> car.getBrand()
|
||||
.equals(filter))
|
||||
.count();
|
||||
|
||||
long count = service.getCountBrandWithCriteria(filter);
|
||||
|
||||
assertEquals(count, all);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueryAnnotation_whenCountingByBrand_thenCountEqualsSize() {
|
||||
String filter = "B-A";
|
||||
long all = service.findCars()
|
||||
.stream()
|
||||
.filter(car -> car.getBrand()
|
||||
.equals(filter))
|
||||
.count();
|
||||
|
||||
long count = service.getCountBrandWithQueryAnnotation(filter);
|
||||
|
||||
assertEquals(count, all);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilteredDocs_whenQueryMethodCountByBrand_thenCountEqualsSize() {
|
||||
String filter = "B-A";
|
||||
long all = service.findCars()
|
||||
.stream()
|
||||
.filter(car -> car.getBrand()
|
||||
.equals(filter))
|
||||
.count();
|
||||
|
||||
long count = service.getCountBrandWithQueryMethod(filter);
|
||||
|
||||
assertEquals(count, all);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilteredDocs_whenExampleCount_thenCountEqualsSize() {
|
||||
long all = service.findCars()
|
||||
.stream()
|
||||
.filter(car -> car.getBrand()
|
||||
.equals(car1.getBrand()))
|
||||
.count();
|
||||
|
||||
long count = service.getCountWithExample(car1);
|
||||
|
||||
assertEquals(count, all);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilteredDocs_whenExampleCriteriaCount_thenCountEqualsSize() {
|
||||
long all = service.findCars()
|
||||
.stream()
|
||||
.filter(car -> car.getBrand()
|
||||
.equals(car1.getBrand()))
|
||||
.count();
|
||||
|
||||
long count = service.getCountWithExampleCriteria(car1);
|
||||
|
||||
assertEquals(count, all);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue