diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/SpringBootCountApplication.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/SpringBootCountApplication.java new file mode 100644 index 0000000000..bb7351383c --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/SpringBootCountApplication.java @@ -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); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/dao/CarRepository.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/dao/CarRepository.java new file mode 100644 index 0000000000..b03298852e --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/dao/CarRepository.java @@ -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 { + @Query(value = "{brand: ?0}", count = true) + public long countBrand(String brand); + + Long countByBrand(String brand); + + @Query(value = "{}", count = true) + Long countWithAnnotation(); +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/data/Car.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/data/Car.java new file mode 100644 index 0000000000..42b80d70b0 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/data/Car.java @@ -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; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/service/CountCarService.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/service/CountCarService.java new file mode 100644 index 0000000000..d4685847fb --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/service/CountCarService.java @@ -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 findCars() { + return repo.findAll(); + } + + public Optional 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); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/count/service/CountCarServiceIntegrationTest.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/count/service/CountCarServiceIntegrationTest.java new file mode 100644 index 0000000000..421ecd3a34 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/count/service/CountCarServiceIntegrationTest.java @@ -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 all = service.findCars(); + + long count = service.getCountWithQueryAnnotation(); + + assertEquals(count, all.size()); + } + + @Test + public void givenAllDocs_whenCrudRepositoryCount_thenCountEqualsSize() { + List 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); + } +}