formatting work
This commit is contained in:
@@ -19,22 +19,27 @@ public class Campus {
|
||||
@Field
|
||||
@NotNull
|
||||
private Point location;
|
||||
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Point getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(Point location) {
|
||||
this.location = location;
|
||||
}
|
||||
@@ -42,13 +47,13 @@ public class Campus {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 1;
|
||||
if(id != null) {
|
||||
if (id != null) {
|
||||
hash = hash * 31 + id.hashCode();
|
||||
}
|
||||
if(name != null) {
|
||||
if (name != null) {
|
||||
hash = hash * 31 + name.hashCode();
|
||||
}
|
||||
if(location != null) {
|
||||
if (location != null) {
|
||||
hash = hash * 31 + location.hashCode();
|
||||
}
|
||||
return hash;
|
||||
@@ -56,30 +61,33 @@ public class Campus {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if((obj == null) || (obj.getClass() != this.getClass())) return false;
|
||||
if(obj == this) return true;
|
||||
if ((obj == null) || (obj.getClass() != this.getClass()))
|
||||
return false;
|
||||
if (obj == this)
|
||||
return true;
|
||||
Campus other = (Campus) obj;
|
||||
return this.hashCode() == other.hashCode();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private Campus() {}
|
||||
|
||||
private Campus() {
|
||||
}
|
||||
|
||||
public Campus(Builder b) {
|
||||
this.id = b.id;
|
||||
this.name = b.name;
|
||||
this.location = b.location;
|
||||
}
|
||||
|
||||
|
||||
public static class Builder {
|
||||
private String id;
|
||||
private String name;
|
||||
private Point location;
|
||||
|
||||
|
||||
public static Builder newInstance() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
|
||||
public Campus build() {
|
||||
return new Campus(this);
|
||||
}
|
||||
@@ -93,7 +101,7 @@ public class Campus {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Builder location(Point location) {
|
||||
this.location = location;
|
||||
return this;
|
||||
|
||||
@@ -24,7 +24,7 @@ public class Person {
|
||||
private DateTime created;
|
||||
@Field
|
||||
private DateTime updated;
|
||||
|
||||
|
||||
public Person(String id, String firstName, String lastName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
@@ -34,44 +34,53 @@ public class Person {
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public DateTime getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(DateTime created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
public DateTime getUpdated() {
|
||||
return updated;
|
||||
}
|
||||
|
||||
public void setUpdated(DateTime updated) {
|
||||
this.updated = updated;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 1;
|
||||
if(id != null) {
|
||||
if (id != null) {
|
||||
hash = hash * 31 + id.hashCode();
|
||||
}
|
||||
if(firstName != null) {
|
||||
if (firstName != null) {
|
||||
hash = hash * 31 + firstName.hashCode();
|
||||
}
|
||||
if(lastName != null) {
|
||||
if (lastName != null) {
|
||||
hash = hash * 31 + lastName.hashCode();
|
||||
}
|
||||
return hash;
|
||||
@@ -79,8 +88,10 @@ public class Person {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if((obj == null) || (obj.getClass() != this.getClass())) return false;
|
||||
if(obj == this) return true;
|
||||
if ((obj == null) || (obj.getClass() != this.getClass()))
|
||||
return false;
|
||||
if (obj == this)
|
||||
return true;
|
||||
Person other = (Person) obj;
|
||||
return this.hashCode() == other.hashCode();
|
||||
}
|
||||
|
||||
@@ -20,13 +20,13 @@ public class Student {
|
||||
private String id;
|
||||
@Field
|
||||
@NotNull
|
||||
@Size(min=1, max=20)
|
||||
@Pattern(regexp=NAME_REGEX)
|
||||
@Size(min = 1, max = 20)
|
||||
@Pattern(regexp = NAME_REGEX)
|
||||
private String firstName;
|
||||
@Field
|
||||
@NotNull
|
||||
@Size(min=1, max=20)
|
||||
@Pattern(regexp=NAME_REGEX)
|
||||
@Size(min = 1, max = 20)
|
||||
@Pattern(regexp = NAME_REGEX)
|
||||
private String lastName;
|
||||
@Field
|
||||
@Past
|
||||
@@ -38,8 +38,9 @@ public class Student {
|
||||
private DateTime updated;
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public Student() {}
|
||||
|
||||
public Student() {
|
||||
}
|
||||
|
||||
public Student(String id, String firstName, String lastName, DateTime dateOfBirth) {
|
||||
this.id = id;
|
||||
@@ -51,36 +52,47 @@ public class Student {
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public DateTime getDateOfBirth() {
|
||||
return dateOfBirth;
|
||||
}
|
||||
|
||||
public void setDateOfBirth(DateTime dateOfBirth) {
|
||||
this.dateOfBirth = dateOfBirth;
|
||||
}
|
||||
|
||||
public DateTime getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(DateTime created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
public DateTime getUpdated() {
|
||||
return updated;
|
||||
}
|
||||
|
||||
public void setUpdated(DateTime updated) {
|
||||
this.updated = updated;
|
||||
}
|
||||
@@ -88,16 +100,16 @@ public class Student {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 1;
|
||||
if(id != null) {
|
||||
if (id != null) {
|
||||
hash = hash * 31 + id.hashCode();
|
||||
}
|
||||
if(firstName != null) {
|
||||
if (firstName != null) {
|
||||
hash = hash * 31 + firstName.hashCode();
|
||||
}
|
||||
if(lastName != null) {
|
||||
if (lastName != null) {
|
||||
hash = hash * 31 + lastName.hashCode();
|
||||
}
|
||||
if(dateOfBirth != null) {
|
||||
if (dateOfBirth != null) {
|
||||
hash = hash * 31 + dateOfBirth.hashCode();
|
||||
}
|
||||
return hash;
|
||||
@@ -105,8 +117,10 @@ public class Student {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if((obj == null) || (obj.getClass() != this.getClass())) return false;
|
||||
if(obj == this) return true;
|
||||
if ((obj == null) || (obj.getClass() != this.getClass()))
|
||||
return false;
|
||||
if (obj == this)
|
||||
return true;
|
||||
Student other = (Student) obj;
|
||||
return this.hashCode() == other.hashCode();
|
||||
}
|
||||
|
||||
@@ -17,9 +17,6 @@ public class CustomStudentRepositoryImpl implements CustomStudentRepository {
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
public List<Student> findByFirstNameStartsWith(String s) {
|
||||
return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName")
|
||||
.startKey(s)
|
||||
.stale(Stale.FALSE),
|
||||
Student.class);
|
||||
return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName").startKey(s).stale(Stale.FALSE), Student.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,5 +7,6 @@ import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, String> {
|
||||
List<Person> findByFirstName(String firstName);
|
||||
|
||||
List<Person> findByLastName(String lastName);
|
||||
}
|
||||
|
||||
@@ -7,5 +7,6 @@ import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface StudentRepository extends CrudRepository<Student, String>, CustomStudentRepository {
|
||||
List<Student> findByFirstName(String firstName);
|
||||
|
||||
List<Student> findByLastName(String lastName);
|
||||
}
|
||||
|
||||
@@ -14,8 +14,9 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
@Qualifier("PersonRepositoryService")
|
||||
public class PersonRepositoryService implements PersonService {
|
||||
|
||||
|
||||
private PersonRepository repo;
|
||||
|
||||
@Autowired
|
||||
public void setPersonRepository(PersonRepository repo) {
|
||||
this.repo = repo;
|
||||
@@ -28,7 +29,7 @@ public class PersonRepositoryService implements PersonService {
|
||||
public List<Person> findAll() {
|
||||
List<Person> people = new ArrayList<Person>();
|
||||
Iterator<Person> it = repo.findAll().iterator();
|
||||
while(it.hasNext()) {
|
||||
while (it.hasNext()) {
|
||||
people.add(it.next());
|
||||
}
|
||||
return people;
|
||||
|
||||
@@ -14,17 +14,18 @@ import com.couchbase.client.java.view.ViewQuery;
|
||||
@Service
|
||||
@Qualifier("PersonTemplateService")
|
||||
public class PersonTemplateService implements PersonService {
|
||||
|
||||
|
||||
private static final String DESIGN_DOC = "person";
|
||||
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
@Autowired
|
||||
public void setCouchbaseTemplate(CouchbaseTemplate template) {
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
public Person findOne(String id) {
|
||||
return template.findById(id, Person.class);
|
||||
public Person findOne(String id) {
|
||||
return template.findById(id, Person.class);
|
||||
}
|
||||
|
||||
public List<Person> findAll() {
|
||||
|
||||
@@ -14,8 +14,9 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
@Qualifier("StudentRepositoryService")
|
||||
public class StudentRepositoryService implements StudentService {
|
||||
|
||||
|
||||
private StudentRepository repo;
|
||||
|
||||
@Autowired
|
||||
public void setStudentRepository(StudentRepository repo) {
|
||||
this.repo = repo;
|
||||
@@ -28,7 +29,7 @@ public class StudentRepositoryService implements StudentService {
|
||||
public List<Student> findAll() {
|
||||
List<Student> people = new ArrayList<Student>();
|
||||
Iterator<Student> it = repo.findAll().iterator();
|
||||
while(it.hasNext()) {
|
||||
while (it.hasNext()) {
|
||||
people.add(it.next());
|
||||
}
|
||||
return people;
|
||||
|
||||
@@ -14,17 +14,18 @@ import com.couchbase.client.java.view.ViewQuery;
|
||||
@Service
|
||||
@Qualifier("StudentTemplateService")
|
||||
public class StudentTemplateService implements StudentService {
|
||||
|
||||
|
||||
private static final String DESIGN_DOC = "student";
|
||||
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
@Autowired
|
||||
public void setCouchbaseTemplate(CouchbaseTemplate template) {
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
public Student findOne(String id) {
|
||||
return template.findById(id, Student.class);
|
||||
public Student findOne(String id) {
|
||||
return template.findById(id, Student.class);
|
||||
}
|
||||
|
||||
public List<Student> findAll() {
|
||||
|
||||
@@ -11,9 +11,9 @@ import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface CampusRepository extends CrudRepository<Campus, String> {
|
||||
|
||||
@View(designDocument="campus", viewName="byName")
|
||||
@View(designDocument = "campus", viewName = "byName")
|
||||
Set<Campus> findByName(String name);
|
||||
|
||||
@Dimensional(dimensions=2, designDocument="campus_spatial", spatialViewName="byLocation")
|
||||
@Dimensional(dimensions = 2, designDocument = "campus_spatial", spatialViewName = "byLocation")
|
||||
Set<Campus> findByLocationNear(Point point, Distance distance);
|
||||
}
|
||||
|
||||
@@ -7,5 +7,6 @@ import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, String> {
|
||||
List<Person> findByFirstName(String firstName);
|
||||
|
||||
List<Person> findByLastName(String lastName);
|
||||
}
|
||||
|
||||
@@ -7,5 +7,6 @@ import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface StudentRepository extends CrudRepository<Student, String> {
|
||||
List<Student> findByFirstName(String firstName);
|
||||
|
||||
List<Student> findByLastName(String lastName);
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@ import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Point;
|
||||
|
||||
public interface CampusService {
|
||||
|
||||
|
||||
Campus find(String id);
|
||||
|
||||
Set<Campus> findByName(String name);
|
||||
|
||||
Set<Campus> findByLocationNear(Point point, Distance distance);
|
||||
|
||||
|
||||
Set<Campus> findAll();
|
||||
|
||||
|
||||
void save(Campus campus);
|
||||
}
|
||||
|
||||
@@ -15,21 +15,22 @@ import org.springframework.stereotype.Service;
|
||||
public class CampusServiceImpl implements CampusService {
|
||||
|
||||
private CampusRepository repo;
|
||||
|
||||
@Autowired
|
||||
public void setCampusRepository(CampusRepository repo) {
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Campus find(String id) {
|
||||
return repo.findOne(id);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Set<Campus> findByName(String name) {
|
||||
return repo.findByName(name);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Set<Campus> findByLocationNear(Point point, Distance distance) {
|
||||
return repo.findByLocationNear(point, distance);
|
||||
@@ -39,7 +40,7 @@ public class CampusServiceImpl implements CampusService {
|
||||
public Set<Campus> findAll() {
|
||||
Set<Campus> campuses = new HashSet<>();
|
||||
Iterator<Campus> it = repo.findAll().iterator();
|
||||
while(it.hasNext()) {
|
||||
while (it.hasNext()) {
|
||||
campuses.add(it.next());
|
||||
}
|
||||
return campuses;
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
|
||||
public class PersonServiceImpl implements PersonService {
|
||||
|
||||
private PersonRepository repo;
|
||||
|
||||
@Autowired
|
||||
public void setPersonRepository(PersonRepository repo) {
|
||||
this.repo = repo;
|
||||
@@ -26,7 +27,7 @@ public class PersonServiceImpl implements PersonService {
|
||||
public List<Person> findAll() {
|
||||
List<Person> people = new ArrayList<Person>();
|
||||
Iterator<Person> it = repo.findAll().iterator();
|
||||
while(it.hasNext()) {
|
||||
while (it.hasNext()) {
|
||||
people.add(it.next());
|
||||
}
|
||||
return people;
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
|
||||
public class StudentServiceImpl implements StudentService {
|
||||
|
||||
private StudentRepository repo;
|
||||
|
||||
@Autowired
|
||||
public void setStudentRepository(StudentRepository repo) {
|
||||
this.repo = repo;
|
||||
@@ -26,7 +27,7 @@ public class StudentServiceImpl implements StudentService {
|
||||
public List<Student> findAll() {
|
||||
List<Student> people = new ArrayList<Student>();
|
||||
Iterator<Student> it = repo.findAll().iterator();
|
||||
while(it.hasNext()) {
|
||||
while (it.hasNext()) {
|
||||
people.add(it.next());
|
||||
}
|
||||
return people;
|
||||
|
||||
@@ -12,9 +12,9 @@ import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepos
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
@Configuration
|
||||
@EnableCouchbaseRepositories(basePackages={"org.baeldung.spring.data.couchbase"})
|
||||
@EnableCouchbaseRepositories(basePackages = { "org.baeldung.spring.data.couchbase" })
|
||||
public class MyCouchbaseConfig extends AbstractCouchbaseConfiguration {
|
||||
|
||||
|
||||
public static final List<String> NODE_LIST = Arrays.asList("localhost");
|
||||
public static final String BUCKET_NAME = "baeldung";
|
||||
public static final String BUCKET_PASSWORD = "";
|
||||
|
||||
@@ -30,27 +30,17 @@ public abstract class StudentServiceTest extends IntegrationTest {
|
||||
static final String joeCollegeId = "student:" + joe + ":" + college;
|
||||
static final DateTime joeCollegeDob = DateTime.now().minusYears(21);
|
||||
static final Student joeCollege = new Student(joeCollegeId, joe, college, joeCollegeDob);
|
||||
static final JsonObject jsonJoeCollege = JsonObject.empty()
|
||||
.put(typeField, Student.class.getName())
|
||||
.put("firstName", joe)
|
||||
.put("lastName", college)
|
||||
.put("created", DateTime.now().getMillis())
|
||||
.put("version", 1);
|
||||
static final JsonObject jsonJoeCollege = JsonObject.empty().put(typeField, Student.class.getName()).put("firstName", joe).put("lastName", college).put("created", DateTime.now().getMillis()).put("version", 1);
|
||||
|
||||
static final String judy = "Judy";
|
||||
static final String jetson = "Jetson";
|
||||
static final String judyJetsonId = "student:" + judy + ":" + jetson;
|
||||
static final DateTime judyJetsonDob = DateTime.now().minusYears(19).minusMonths(5).minusDays(3);
|
||||
static final Student judyJetson = new Student(judyJetsonId, judy, jetson, judyJetsonDob);
|
||||
static final JsonObject jsonJudyJetson = JsonObject.empty()
|
||||
.put(typeField, Student.class.getName())
|
||||
.put("firstName", judy)
|
||||
.put("lastName", jetson)
|
||||
.put("created", DateTime.now().getMillis())
|
||||
.put("version", 1);
|
||||
|
||||
static final JsonObject jsonJudyJetson = JsonObject.empty().put(typeField, Student.class.getName()).put("firstName", judy).put("lastName", jetson).put("created", DateTime.now().getMillis()).put("version", 1);
|
||||
|
||||
StudentService studentService;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setupBeforeClass() {
|
||||
Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST);
|
||||
@@ -60,7 +50,7 @@ public abstract class StudentServiceTest extends IntegrationTest {
|
||||
bucket.close();
|
||||
cluster.disconnect();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenCreatingStudent_thenDocumentIsPersisted() {
|
||||
String firstName = "Eric";
|
||||
@@ -75,7 +65,7 @@ public abstract class StudentServiceTest extends IntegrationTest {
|
||||
assertEquals(expectedStudent.getId(), actualStudent.getId());
|
||||
}
|
||||
|
||||
@Test(expected=ConstraintViolationException.class)
|
||||
@Test(expected = ConstraintViolationException.class)
|
||||
public void whenCreatingStudentWithInvalidFirstName_thenConstraintViolationException() {
|
||||
String firstName = "Er+ic";
|
||||
String lastName = "Stratton";
|
||||
@@ -85,7 +75,7 @@ public abstract class StudentServiceTest extends IntegrationTest {
|
||||
studentService.create(student);
|
||||
}
|
||||
|
||||
@Test(expected=ConstraintViolationException.class)
|
||||
@Test(expected = ConstraintViolationException.class)
|
||||
public void whenCreatingStudentWithFutureDob_thenConstraintViolationException() {
|
||||
String firstName = "Jane";
|
||||
String lastName = "Doe";
|
||||
@@ -130,11 +120,11 @@ public abstract class StudentServiceTest extends IntegrationTest {
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(allResultsContainExpectedLastName(resultList, expectedLastName));
|
||||
}
|
||||
|
||||
|
||||
private boolean resultContains(List<Student> resultList, Student student) {
|
||||
boolean found = false;
|
||||
for(Student p : resultList) {
|
||||
if(p.getId().equals(student.getId())) {
|
||||
for (Student p : resultList) {
|
||||
if (p.getId().equals(student.getId())) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
@@ -144,8 +134,8 @@ public abstract class StudentServiceTest extends IntegrationTest {
|
||||
|
||||
private boolean allResultsContainExpectedFirstName(List<Student> resultList, String firstName) {
|
||||
boolean found = false;
|
||||
for(Student p : resultList) {
|
||||
if(p.getFirstName().equals(firstName)) {
|
||||
for (Student p : resultList) {
|
||||
if (p.getFirstName().equals(firstName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
@@ -155,8 +145,8 @@ public abstract class StudentServiceTest extends IntegrationTest {
|
||||
|
||||
private boolean allResultsContainExpectedLastName(List<Student> resultList, String lastName) {
|
||||
boolean found = false;
|
||||
for(Student p : resultList) {
|
||||
if(p.getLastName().equals(lastName)) {
|
||||
for (Student p : resultList) {
|
||||
if (p.getLastName().equals(lastName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -38,29 +38,25 @@ public class MultiBucketCouchbaseConfig extends AbstractCouchbaseConfiguration {
|
||||
protected String getBucketPassword() {
|
||||
return DEFAULT_BUCKET_PASSWORD;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public Bucket campusBucket() throws Exception {
|
||||
return couchbaseCluster().openBucket("baeldung2", "");
|
||||
}
|
||||
|
||||
|
||||
@Bean(name = "campusTemplate")
|
||||
public CouchbaseTemplate campusTemplate() throws Exception {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(
|
||||
couchbaseClusterInfo(),
|
||||
campusBucket(),
|
||||
mappingCouchbaseConverter(),
|
||||
translationService());
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(couchbaseClusterInfo(), campusBucket(), mappingCouchbaseConverter(), translationService());
|
||||
template.setDefaultConsistency(getDefaultConsistency());
|
||||
return template;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
|
||||
try {
|
||||
baseMapping.mapEntity(Campus.class, campusTemplate());
|
||||
} catch (Exception e) {
|
||||
//custom Exception handling
|
||||
// custom Exception handling
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,61 +19,29 @@ import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.geo.Point;
|
||||
|
||||
public class CampusServiceImplTest extends MultiBucketIntegationTest {
|
||||
|
||||
|
||||
@Autowired
|
||||
private CampusServiceImpl campusService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private CampusRepository campusRepo;
|
||||
|
||||
private final Campus Brown = Campus.Builder.newInstance()
|
||||
.id("campus:Brown")
|
||||
.name("Brown")
|
||||
.location(new Point(71.4025, 51.8268))
|
||||
.build();
|
||||
|
||||
private final Campus Cornell = Campus.Builder.newInstance()
|
||||
.id("campus:Cornell")
|
||||
.name("Cornell")
|
||||
.location(new Point(76.4833, 42.4459))
|
||||
.build();
|
||||
|
||||
private final Campus Columbia = Campus.Builder.newInstance()
|
||||
.id("campus:Columbia")
|
||||
.name("Columbia")
|
||||
.location(new Point(73.9626, 40.8075))
|
||||
.build();
|
||||
|
||||
private final Campus Dartmouth = Campus.Builder.newInstance()
|
||||
.id("campus:Dartmouth")
|
||||
.name("Dartmouth")
|
||||
.location(new Point(72.2887, 43.7044))
|
||||
.build();
|
||||
private final Campus Brown = Campus.Builder.newInstance().id("campus:Brown").name("Brown").location(new Point(71.4025, 51.8268)).build();
|
||||
|
||||
private final Campus Harvard = Campus.Builder.newInstance()
|
||||
.id("campus:Harvard")
|
||||
.name("Harvard")
|
||||
.location(new Point(71.1167, 42.3770))
|
||||
.build();
|
||||
private final Campus Cornell = Campus.Builder.newInstance().id("campus:Cornell").name("Cornell").location(new Point(76.4833, 42.4459)).build();
|
||||
|
||||
private final Campus Penn = Campus.Builder.newInstance()
|
||||
.id("campus:Penn")
|
||||
.name("Penn")
|
||||
.location(new Point(75.1932, 39.9522))
|
||||
.build();
|
||||
private final Campus Columbia = Campus.Builder.newInstance().id("campus:Columbia").name("Columbia").location(new Point(73.9626, 40.8075)).build();
|
||||
|
||||
private final Campus Dartmouth = Campus.Builder.newInstance().id("campus:Dartmouth").name("Dartmouth").location(new Point(72.2887, 43.7044)).build();
|
||||
|
||||
private final Campus Harvard = Campus.Builder.newInstance().id("campus:Harvard").name("Harvard").location(new Point(71.1167, 42.3770)).build();
|
||||
|
||||
private final Campus Penn = Campus.Builder.newInstance().id("campus:Penn").name("Penn").location(new Point(75.1932, 39.9522)).build();
|
||||
|
||||
private final Campus Princeton = Campus.Builder.newInstance().id("campus:Princeton").name("Princeton").location(new Point(74.6514, 40.3340)).build();
|
||||
|
||||
private final Campus Yale = Campus.Builder.newInstance().id("campus:Yale").name("Yale").location(new Point(72.9223, 41.3163)).build();
|
||||
|
||||
private final Campus Princeton = Campus.Builder.newInstance()
|
||||
.id("campus:Princeton")
|
||||
.name("Princeton")
|
||||
.location(new Point(74.6514, 40.3340))
|
||||
.build();
|
||||
|
||||
private final Campus Yale = Campus.Builder.newInstance()
|
||||
.id("campus:Yale")
|
||||
.name("Yale")
|
||||
.location(new Point(72.9223, 41.3163))
|
||||
.build();
|
||||
|
||||
private final Point Boston = new Point(71.0589, 42.3601);
|
||||
private final Point NewYorkCity = new Point(74.0059, 40.7128);
|
||||
|
||||
@@ -88,7 +56,7 @@ public class CampusServiceImplTest extends MultiBucketIntegationTest {
|
||||
campusRepo.save(Princeton);
|
||||
campusRepo.save(Yale);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public final void givenNameHarvard_whenFindByName_thenReturnsHarvard() throws Exception {
|
||||
Set<Campus> campuses = campusService.findByName(Harvard.getName());
|
||||
@@ -97,14 +65,14 @@ public class CampusServiceImplTest extends MultiBucketIntegationTest {
|
||||
assertTrue(campuses.size() == 1);
|
||||
assertTrue(campuses.contains(Harvard));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public final void givenHarvardId_whenFind_thenReturnsHarvard() throws Exception {
|
||||
Campus actual = campusService.find(Harvard.getId());
|
||||
assertNotNull(actual);
|
||||
assertEquals(Harvard, actual);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public final void whenFindAll_thenReturnsAll() throws Exception {
|
||||
Set<Campus> campuses = campusService.findAll();
|
||||
@@ -125,7 +93,7 @@ public class CampusServiceImplTest extends MultiBucketIntegationTest {
|
||||
assertTrue(campuses.contains(Harvard));
|
||||
assertFalse(campuses.contains(Columbia));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public final void whenFindByLocationNearNewYorkCity_thenResultContainsColumbia() throws Exception {
|
||||
Set<Campus> campuses = campusService.findByLocationNear(NewYorkCity, new Distance(1, Metrics.NEUTRAL));
|
||||
|
||||
@@ -31,28 +31,18 @@ public class StudentServiceImplTest extends MultiBucketIntegationTest {
|
||||
static final String joeCollegeId = "student:" + joe + ":" + college;
|
||||
static final DateTime joeCollegeDob = DateTime.now().minusYears(21);
|
||||
static final Student joeCollege = new Student(joeCollegeId, joe, college, joeCollegeDob);
|
||||
static final JsonObject jsonJoeCollege = JsonObject.empty()
|
||||
.put(typeField, Student.class.getName())
|
||||
.put("firstName", joe)
|
||||
.put("lastName", college)
|
||||
.put("created", DateTime.now().getMillis())
|
||||
.put("version", 1);
|
||||
static final JsonObject jsonJoeCollege = JsonObject.empty().put(typeField, Student.class.getName()).put("firstName", joe).put("lastName", college).put("created", DateTime.now().getMillis()).put("version", 1);
|
||||
|
||||
static final String judy = "Judy";
|
||||
static final String jetson = "Jetson";
|
||||
static final String judyJetsonId = "student:" + judy + ":" + jetson;
|
||||
static final DateTime judyJetsonDob = DateTime.now().minusYears(19).minusMonths(5).minusDays(3);
|
||||
static final Student judyJetson = new Student(judyJetsonId, judy, jetson, judyJetsonDob);
|
||||
static final JsonObject jsonJudyJetson = JsonObject.empty()
|
||||
.put(typeField, Student.class.getName())
|
||||
.put("firstName", judy)
|
||||
.put("lastName", jetson)
|
||||
.put("created", DateTime.now().getMillis())
|
||||
.put("version", 1);
|
||||
|
||||
static final JsonObject jsonJudyJetson = JsonObject.empty().put(typeField, Student.class.getName()).put("firstName", judy).put("lastName", jetson).put("created", DateTime.now().getMillis()).put("version", 1);
|
||||
|
||||
@Autowired
|
||||
StudentServiceImpl studentService;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setupBeforeClass() {
|
||||
Cluster cluster = CouchbaseCluster.create(MultiBucketCouchbaseConfig.NODE_LIST);
|
||||
@@ -62,7 +52,7 @@ public class StudentServiceImplTest extends MultiBucketIntegationTest {
|
||||
bucket.close();
|
||||
cluster.disconnect();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenCreatingStudent_thenDocumentIsPersisted() {
|
||||
String firstName = "Eric";
|
||||
@@ -77,7 +67,7 @@ public class StudentServiceImplTest extends MultiBucketIntegationTest {
|
||||
assertEquals(expectedStudent.getId(), actualStudent.getId());
|
||||
}
|
||||
|
||||
@Test(expected=ConstraintViolationException.class)
|
||||
@Test(expected = ConstraintViolationException.class)
|
||||
public void whenCreatingStudentWithInvalidFirstName_thenConstraintViolationException() {
|
||||
String firstName = "Er+ic";
|
||||
String lastName = "Stratton";
|
||||
@@ -87,7 +77,7 @@ public class StudentServiceImplTest extends MultiBucketIntegationTest {
|
||||
studentService.create(student);
|
||||
}
|
||||
|
||||
@Test(expected=ConstraintViolationException.class)
|
||||
@Test(expected = ConstraintViolationException.class)
|
||||
public void whenCreatingStudentWithFutureDob_thenConstraintViolationException() {
|
||||
String firstName = "Jane";
|
||||
String lastName = "Doe";
|
||||
@@ -132,11 +122,11 @@ public class StudentServiceImplTest extends MultiBucketIntegationTest {
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(allResultsContainExpectedLastName(resultList, expectedLastName));
|
||||
}
|
||||
|
||||
|
||||
private boolean resultContains(List<Student> resultList, Student student) {
|
||||
boolean found = false;
|
||||
for(Student p : resultList) {
|
||||
if(p.getId().equals(student.getId())) {
|
||||
for (Student p : resultList) {
|
||||
if (p.getId().equals(student.getId())) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
@@ -146,8 +136,8 @@ public class StudentServiceImplTest extends MultiBucketIntegationTest {
|
||||
|
||||
private boolean allResultsContainExpectedFirstName(List<Student> resultList, String firstName) {
|
||||
boolean found = false;
|
||||
for(Student p : resultList) {
|
||||
if(p.getFirstName().equals(firstName)) {
|
||||
for (Student p : resultList) {
|
||||
if (p.getFirstName().equals(firstName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
@@ -157,8 +147,8 @@ public class StudentServiceImplTest extends MultiBucketIntegationTest {
|
||||
|
||||
private boolean allResultsContainExpectedLastName(List<Student> resultList, String lastName) {
|
||||
boolean found = false;
|
||||
for(Student p : resultList) {
|
||||
if(p.getLastName().equals(lastName)) {
|
||||
for (Student p : resultList) {
|
||||
if (p.getLastName().equals(lastName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user