Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mongo.repository;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
* Each endpoint has its own document type and collection, so that a document that
* makes one endpoint return 200 cannot accidentally do it for another one.
*/
@Document(collection = "countAnnotatedCollection")
public class CountAnnotatedData {

@Id
public String id;

public String city;

public int age;

public CountAnnotatedData() {
}

public CountAnnotatedData(String city, int age) {
this.city = city;
this.age = age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mongo.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

public interface CountAnnotatedRepository extends MongoRepository<CountAnnotatedData, String> {

@Query(value = "{ 'city': ?0, 'age': { $gte: ?1 } }", count = true)
long countAnnotated(String city, int minAge);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mongo.repository;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
* Each endpoint has its own document type and collection, so that a document that
* makes one endpoint return 200 cannot accidentally do it for another one.
*/
@Document(collection = "countDerivedCollection")
public class CountDerivedData {

@Id
public String id;

public String city;

public int age;

public CountDerivedData() {
}

public CountDerivedData(String city, int age) {
this.city = city;
this.age = age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mongo.repository;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface CountDerivedRepository extends MongoRepository<CountDerivedData, String> {

long countByCityAndAgeGreaterThanEqual(String city, int minAge);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mongo.repository;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
* Each endpoint has its own document type and collection, so that a document that
* makes one endpoint return 200 cannot accidentally do it for another one.
*/
@Document(collection = "deleteAnnotatedCollection")
public class DeleteAnnotatedData {

@Id
public String id;

public String city;

public int age;

public DeleteAnnotatedData() {
}

public DeleteAnnotatedData(String city, int age) {
this.city = city;
this.age = age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mongo.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

public interface DeleteAnnotatedRepository extends MongoRepository<DeleteAnnotatedData, String> {

@Query(value = "{ 'city': ?0, 'age': { $gte: ?1 } }", delete = true)
long deleteAnnotated(String city, int minAge);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mongo.repository;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
* Each endpoint has its own document type and collection, so that a document that
* makes one endpoint return 200 cannot accidentally do it for another one.
*/
@Document(collection = "deleteDerivedCollection")
public class DeleteDerivedData {

@Id
public String id;

public String city;

public int age;

public DeleteDerivedData() {
}

public DeleteDerivedData(String city, int age) {
this.city = city;
this.age = age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mongo.repository;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface DeleteDerivedRepository extends MongoRepository<DeleteDerivedData, String> {

long deleteByCityAndAgeGreaterThanEqual(String city, int minAge);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mongo.repository;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
* Each endpoint has its own document type and collection, so that a document that
* makes one endpoint return 200 cannot accidentally do it for another one.
*/
@Document(collection = "existsAnnotatedCollection")
public class ExistsAnnotatedData {

@Id
public String id;

public String city;

public int age;

public ExistsAnnotatedData() {
}

public ExistsAnnotatedData(String city, int age) {
this.city = city;
this.age = age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mongo.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

public interface ExistsAnnotatedRepository extends MongoRepository<ExistsAnnotatedData, String> {

@Query(value = "{ 'city': ?0, 'age': { $gte: ?1 } }", exists = true)
boolean existsAnnotated(String city, int minAge);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mongo.repository;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
* Each endpoint has its own document type and collection, so that a document that
* makes one endpoint return 200 cannot accidentally do it for another one.
*/
@Document(collection = "existsDerivedCollection")
public class ExistsDerivedData {

@Id
public String id;

public String city;

public int age;

public ExistsDerivedData() {
}

public ExistsDerivedData(String city, int age) {
this.city = city;
this.age = age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mongo.repository;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface ExistsDerivedRepository extends MongoRepository<ExistsDerivedData, String> {

boolean existsByCityAndAgeGreaterThanEqual(String city, int minAge);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mongo.repository;

import com.mongo.SwaggerConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class MongoRepositoryApp extends SwaggerConfiguration {
public MongoRepositoryApp() {
super("mongorepository");
}

public static void main(String[] args) {
SpringApplication.run(MongoRepositoryApp.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.mongo.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* Exercises exists/count/delete through Spring Data repositories (derived and annotated queries),
* so the Mongo driver is only called from within Spring Data and never from SUT code.
*/
@RestController
@RequestMapping(path = "/mongorepository")
public class MongoRepositoryController {

@Autowired
private ExistsDerivedRepository existsDerivedRepository;

@Autowired
private ExistsAnnotatedRepository existsAnnotatedRepository;

@Autowired
private CountDerivedRepository countDerivedRepository;

@Autowired
private CountAnnotatedRepository countAnnotatedRepository;

@Autowired
private DeleteDerivedRepository deleteDerivedRepository;

@Autowired
private DeleteAnnotatedRepository deleteAnnotatedRepository;

private static ResponseEntity<Void> response(boolean found) {
return ResponseEntity.status(found ? 200 : 404).build();
}

/**
* Derived exists query on a Spring Data repository
*/
@GetMapping("existsDerived")
public ResponseEntity<Void> existsDerived(@RequestParam(name = "city") String city,
@RequestParam(name = "minAge") int minAge) {
return response(existsDerivedRepository.existsByCityAndAgeGreaterThanEqual(city, minAge));
}

/**
* Annotated exists query on a Spring Data repository
*/
@GetMapping("existsAnnotated")
public ResponseEntity<Void> existsAnnotated(@RequestParam(name = "city") String city,
@RequestParam(name = "minAge") int minAge) {
return response(existsAnnotatedRepository.existsAnnotated(city, minAge));
}

/**
* Derived count query on a Spring Data repository
*/
@GetMapping("countDerived")
public ResponseEntity<Void> countDerived(@RequestParam(name = "city") String city,
@RequestParam(name = "minAge") int minAge) {
return response(countDerivedRepository.countByCityAndAgeGreaterThanEqual(city, minAge) > 0);
}

/**
* Annotated count query on a Spring Data repository
*/
@GetMapping("countAnnotated")
public ResponseEntity<Void> countAnnotated(@RequestParam(name = "city") String city,
@RequestParam(name = "minAge") int minAge) {
return response(countAnnotatedRepository.countAnnotated(city, minAge) > 0);
}

/**
* Derived delete query on a Spring Data repository
*/
@DeleteMapping("deleteDerived")
public ResponseEntity<Void> deleteDerived(@RequestParam(name = "city") String city,
@RequestParam(name = "minAge") int minAge) {
return response(deleteDerivedRepository.deleteByCityAndAgeGreaterThanEqual(city, minAge) > 0);
}

/**
* Annotated delete query on a Spring Data repository
*/
@DeleteMapping("deleteAnnotated")
public ResponseEntity<Void> deleteAnnotated(@RequestParam(name = "city") String city,
@RequestParam(name = "minAge") int minAge) {
return response(deleteAnnotatedRepository.deleteAnnotated(city, minAge) > 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.foo.spring.rest.mongo.repository;

import com.foo.spring.rest.mongo.MongoController;
import com.mongo.repository.MongoRepositoryApp;

public class MongoRepositoryAppController extends MongoController {
public MongoRepositoryAppController() {
super("repository", MongoRepositoryApp.class);
}

@Override
public String getPackagePrefixesToCover() {
return "com.mongo.repository";
}
}
Loading
Loading