[BAEL-9696] - Moved persistence-related modules into the persistence folder
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
package org.baeldung;
|
||||
|
||||
import org.baeldung.spring.data.couchbase2b.MultiBucketCouchbaseConfig;
|
||||
import org.baeldung.spring.data.couchbase2b.MultiBucketIntegrationTestConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { MultiBucketCouchbaseConfig.class, MultiBucketIntegrationTestConfig.class })
|
||||
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class })
|
||||
public class SpringContextIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package org.baeldung.spring.data.couchbase;
|
||||
|
||||
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
|
||||
|
||||
public class CustomTypeKeyCouchbaseConfig extends MyCouchbaseConfig {
|
||||
|
||||
@Override
|
||||
public String typeKey() {
|
||||
return MappingCouchbaseConverter.TYPEKEY_SYNCGATEWAY_COMPATIBLE;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package org.baeldung.spring.data.couchbase;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { MyCouchbaseConfig.class, IntegrationTestConfig.class })
|
||||
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class })
|
||||
public abstract class IntegrationTest {
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package org.baeldung.spring.data.couchbase;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "org.baeldung.spring.data.couchbase")
|
||||
public class IntegrationTestConfig {
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package org.baeldung.spring.data.couchbase;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
||||
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener;
|
||||
import org.springframework.data.couchbase.core.query.Consistency;
|
||||
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
@Configuration
|
||||
@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 = "";
|
||||
|
||||
@Override
|
||||
protected List<String> getBootstrapHosts() {
|
||||
return NODE_LIST;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketName() {
|
||||
return BUCKET_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketPassword() {
|
||||
return BUCKET_PASSWORD;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Consistency getDefaultConsistency() {
|
||||
return Consistency.READ_YOUR_OWN_WRITES;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalValidatorFactoryBean localValidatorFactoryBean() {
|
||||
return new LocalValidatorFactoryBean();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ValidatingCouchbaseEventListener validatingCouchbaseEventListener() {
|
||||
return new ValidatingCouchbaseEventListener(localValidatorFactoryBean());
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package org.baeldung.spring.data.couchbase;
|
||||
|
||||
import org.springframework.data.couchbase.core.query.Consistency;
|
||||
|
||||
public class ReadYourOwnWritesCouchbaseConfig extends MyCouchbaseConfig {
|
||||
|
||||
@Override
|
||||
public Consistency getDefaultConsistency() {
|
||||
return Consistency.READ_YOUR_OWN_WRITES;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package org.baeldung.spring.data.couchbase.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
||||
public class PersonRepositoryServiceIntegrationTest extends PersonServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("PersonRepositoryService")
|
||||
public void setPersonService(PersonService service) {
|
||||
this.personService = service;
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package org.baeldung.spring.data.couchbase.service;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.spring.data.couchbase.IntegrationTest;
|
||||
import org.baeldung.spring.data.couchbase.MyCouchbaseConfig;
|
||||
import org.baeldung.spring.data.couchbase.model.Person;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.CouchbaseCluster;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
|
||||
public abstract class PersonServiceIntegrationTest extends IntegrationTest {
|
||||
|
||||
static final String typeField = "_class";
|
||||
static final String john = "John";
|
||||
static final String smith = "Smith";
|
||||
static final String johnSmithId = "person:" + john + ":" + smith;
|
||||
static final Person johnSmith = new Person(johnSmithId, john, smith);
|
||||
static final JsonObject jsonJohnSmith = JsonObject.empty().put(typeField, Person.class.getName()).put("firstName", john).put("lastName", smith).put("created", DateTime.now().getMillis());
|
||||
|
||||
static final String foo = "Foo";
|
||||
static final String bar = "Bar";
|
||||
static final String foobarId = "person:" + foo + ":" + bar;
|
||||
static final Person foobar = new Person(foobarId, foo, bar);
|
||||
static final JsonObject jsonFooBar = JsonObject.empty().put(typeField, Person.class.getName()).put("firstName", foo).put("lastName", bar).put("created", DateTime.now().getMillis());
|
||||
|
||||
PersonService personService;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupBeforeClass() {
|
||||
final Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST);
|
||||
final Bucket bucket = cluster.openBucket(MyCouchbaseConfig.BUCKET_NAME, MyCouchbaseConfig.BUCKET_PASSWORD);
|
||||
bucket.upsert(JsonDocument.create(johnSmithId, jsonJohnSmith));
|
||||
bucket.upsert(JsonDocument.create(foobarId, jsonFooBar));
|
||||
bucket.close();
|
||||
cluster.disconnect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingPersonByJohnSmithId_thenReturnsJohnSmith() {
|
||||
final Person actualPerson = personService.findOne(johnSmithId);
|
||||
assertNotNull(actualPerson);
|
||||
assertNotNull(actualPerson.getCreated());
|
||||
assertEquals(johnSmith, actualPerson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingAllPersons_thenReturnsTwoOrMorePersonsIncludingJohnSmithAndFooBar() {
|
||||
final List<Person> resultList = personService.findAll();
|
||||
assertNotNull(resultList);
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(resultContains(resultList, johnSmith));
|
||||
assertTrue(resultContains(resultList, foobar));
|
||||
assertTrue(resultList.size() >= 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingByFirstNameJohn_thenReturnsOnlyPersonsNamedJohn() {
|
||||
final String expectedFirstName = john;
|
||||
final List<Person> resultList = personService.findByFirstName(expectedFirstName);
|
||||
assertNotNull(resultList);
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(allResultsContainExpectedFirstName(resultList, expectedFirstName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingByLastNameSmith_thenReturnsOnlyPersonsNamedSmith() {
|
||||
final String expectedLastName = smith;
|
||||
final List<Person> resultList = personService.findByLastName(expectedLastName);
|
||||
assertNotNull(resultList);
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(allResultsContainExpectedLastName(resultList, expectedLastName));
|
||||
}
|
||||
|
||||
private boolean resultContains(List<Person> resultList, Person person) {
|
||||
boolean found = false;
|
||||
for (final Person p : resultList) {
|
||||
if (p.equals(person)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private boolean allResultsContainExpectedFirstName(List<Person> resultList, String firstName) {
|
||||
boolean found = false;
|
||||
for (final Person p : resultList) {
|
||||
if (p.getFirstName().equals(firstName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private boolean allResultsContainExpectedLastName(List<Person> resultList, String lastName) {
|
||||
boolean found = false;
|
||||
for (final Person p : resultList) {
|
||||
if (p.getLastName().equals(lastName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package org.baeldung.spring.data.couchbase.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
||||
public class PersonTemplateServiceIntegrationTest extends PersonServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("PersonTemplateService")
|
||||
public void setPersonService(PersonService service) {
|
||||
this.personService = service;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package org.baeldung.spring.data.couchbase.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
||||
public class StudentRepositoryServiceIntegrationTest extends StudentServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("StudentRepositoryService")
|
||||
public void setStudentService(StudentService service) {
|
||||
this.studentService = service;
|
||||
}
|
||||
}
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
package org.baeldung.spring.data.couchbase.service;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import org.baeldung.spring.data.couchbase.IntegrationTest;
|
||||
import org.baeldung.spring.data.couchbase.MyCouchbaseConfig;
|
||||
import org.baeldung.spring.data.couchbase.model.Student;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.CouchbaseCluster;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
|
||||
public abstract class StudentServiceIntegrationTest extends IntegrationTest {
|
||||
|
||||
static final String typeField = "_class";
|
||||
static final String joe = "Joe";
|
||||
static final String college = "College";
|
||||
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 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);
|
||||
|
||||
StudentService studentService;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupBeforeClass() {
|
||||
Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST);
|
||||
Bucket bucket = cluster.openBucket(MyCouchbaseConfig.BUCKET_NAME, MyCouchbaseConfig.BUCKET_PASSWORD);
|
||||
bucket.upsert(JsonDocument.create(joeCollegeId, jsonJoeCollege));
|
||||
bucket.upsert(JsonDocument.create(judyJetsonId, jsonJudyJetson));
|
||||
bucket.close();
|
||||
cluster.disconnect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingStudent_thenDocumentIsPersisted() {
|
||||
String firstName = "Eric";
|
||||
String lastName = "Stratton";
|
||||
DateTime dateOfBirth = DateTime.now().minusYears(25);
|
||||
String id = "student:" + firstName + ":" + lastName;
|
||||
Student expectedStudent = new Student(id, firstName, lastName, dateOfBirth);
|
||||
studentService.create(expectedStudent);
|
||||
Student actualStudent = studentService.findOne(id);
|
||||
assertNotNull(actualStudent.getCreated());
|
||||
assertNotNull(actualStudent);
|
||||
assertEquals(expectedStudent.getId(), actualStudent.getId());
|
||||
}
|
||||
|
||||
@Test(expected = ConstraintViolationException.class)
|
||||
public void whenCreatingStudentWithInvalidFirstName_thenConstraintViolationException() {
|
||||
String firstName = "Er+ic";
|
||||
String lastName = "Stratton";
|
||||
DateTime dateOfBirth = DateTime.now().minusYears(25);
|
||||
String id = "student:" + firstName + ":" + lastName;
|
||||
Student student = new Student(id, firstName, lastName, dateOfBirth);
|
||||
studentService.create(student);
|
||||
}
|
||||
|
||||
@Test(expected = ConstraintViolationException.class)
|
||||
public void whenCreatingStudentWithFutureDob_thenConstraintViolationException() {
|
||||
String firstName = "Jane";
|
||||
String lastName = "Doe";
|
||||
DateTime dateOfBirth = DateTime.now().plusDays(1);
|
||||
String id = "student:" + firstName + ":" + lastName;
|
||||
Student student = new Student(id, firstName, lastName, dateOfBirth);
|
||||
studentService.create(student);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingStudentByJohnSmithId_thenReturnsJohnSmith() {
|
||||
Student actualStudent = studentService.findOne(joeCollegeId);
|
||||
assertNotNull(actualStudent);
|
||||
assertNotNull(actualStudent.getCreated());
|
||||
assertEquals(joeCollegeId, actualStudent.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingAllStudents_thenReturnsTwoOrMoreStudentsIncludingJoeCollegeAndJudyJetson() {
|
||||
List<Student> resultList = studentService.findAll();
|
||||
assertNotNull(resultList);
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(resultContains(resultList, joeCollege));
|
||||
assertTrue(resultContains(resultList, judyJetson));
|
||||
assertTrue(resultList.size() >= 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingByFirstNameJohn_thenReturnsOnlyStudentsNamedJohn() {
|
||||
String expectedFirstName = joe;
|
||||
List<Student> resultList = studentService.findByFirstName(expectedFirstName);
|
||||
assertNotNull(resultList);
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(allResultsContainExpectedFirstName(resultList, expectedFirstName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingByLastNameSmith_thenReturnsOnlyStudentsNamedSmith() {
|
||||
String expectedLastName = college;
|
||||
List<Student> resultList = studentService.findByLastName(expectedLastName);
|
||||
assertNotNull(resultList);
|
||||
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())) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private boolean allResultsContainExpectedFirstName(List<Student> resultList, String firstName) {
|
||||
boolean found = false;
|
||||
for (Student p : resultList) {
|
||||
if (p.getFirstName().equals(firstName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private boolean allResultsContainExpectedLastName(List<Student> resultList, String lastName) {
|
||||
boolean found = false;
|
||||
for (Student p : resultList) {
|
||||
if (p.getLastName().equals(lastName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package org.baeldung.spring.data.couchbase.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
||||
public class StudentTemplateServiceIntegrationTest extends StudentServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("StudentTemplateService")
|
||||
public void setStudentService(StudentService service) {
|
||||
this.studentService = service;
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package org.baeldung.spring.data.couchbase2b;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.spring.data.couchbase.model.Campus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener;
|
||||
import org.springframework.data.couchbase.core.query.Consistency;
|
||||
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
|
||||
import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
|
||||
@Configuration
|
||||
@EnableCouchbaseRepositories(basePackages = { "org.baeldung.spring.data.couchbase2b" })
|
||||
public class MultiBucketCouchbaseConfig extends AbstractCouchbaseConfiguration {
|
||||
|
||||
public static final List<String> NODE_LIST = Arrays.asList("localhost");
|
||||
public static final String DEFAULT_BUCKET_NAME = "baeldung";
|
||||
public static final String DEFAULT_BUCKET_PASSWORD = "";
|
||||
|
||||
@Override
|
||||
protected List<String> getBootstrapHosts() {
|
||||
return NODE_LIST;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketName() {
|
||||
return DEFAULT_BUCKET_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
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());
|
||||
template.setDefaultConsistency(getDefaultConsistency());
|
||||
return template;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
|
||||
try {
|
||||
baseMapping.mapEntity(Campus.class, campusTemplate());
|
||||
} catch (Exception e) {
|
||||
// custom Exception handling
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Consistency getDefaultConsistency() {
|
||||
return Consistency.READ_YOUR_OWN_WRITES;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalValidatorFactoryBean localValidatorFactoryBean() {
|
||||
return new LocalValidatorFactoryBean();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ValidatingCouchbaseEventListener validatingCouchbaseEventListener() {
|
||||
return new ValidatingCouchbaseEventListener(localValidatorFactoryBean());
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.spring.data.couchbase2b;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { MultiBucketCouchbaseConfig.class, MultiBucketIntegrationTestConfig.class })
|
||||
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class })
|
||||
public abstract class MultiBucketIntegrationTest {
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.spring.data.couchbase2b;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = { "org.baeldung.spring.data.couchbase2b" })
|
||||
public class MultiBucketIntegrationTestConfig {
|
||||
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package org.baeldung.spring.data.couchbase2b.service;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.baeldung.spring.data.couchbase.model.Campus;
|
||||
import org.baeldung.spring.data.couchbase2b.MultiBucketIntegrationTest;
|
||||
import org.baeldung.spring.data.couchbase2b.repos.CampusRepository;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.geo.Point;
|
||||
|
||||
public class CampusServiceImplIntegrationTest extends MultiBucketIntegrationTest {
|
||||
|
||||
@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 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 Point Boston = new Point(71.0589, 42.3601);
|
||||
private final Point NewYorkCity = new Point(74.0059, 40.7128);
|
||||
|
||||
@PostConstruct
|
||||
private void loadCampuses() throws Exception {
|
||||
campusRepo.save(Brown);
|
||||
campusRepo.save(Columbia);
|
||||
campusRepo.save(Cornell);
|
||||
campusRepo.save(Dartmouth);
|
||||
campusRepo.save(Harvard);
|
||||
campusRepo.save(Penn);
|
||||
campusRepo.save(Princeton);
|
||||
campusRepo.save(Yale);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenNameHarvard_whenFindByName_thenReturnsHarvard() throws Exception {
|
||||
Set<Campus> campuses = campusService.findByName(Harvard.getName());
|
||||
assertNotNull(campuses);
|
||||
assertFalse(campuses.isEmpty());
|
||||
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();
|
||||
assertTrue(campuses.contains(Brown));
|
||||
assertTrue(campuses.contains(Columbia));
|
||||
assertTrue(campuses.contains(Cornell));
|
||||
assertTrue(campuses.contains(Dartmouth));
|
||||
assertTrue(campuses.contains(Harvard));
|
||||
assertTrue(campuses.contains(Penn));
|
||||
assertTrue(campuses.contains(Princeton));
|
||||
assertTrue(campuses.contains(Yale));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenFindByLocationNearBoston_thenResultContainsHarvard() throws Exception {
|
||||
Set<Campus> campuses = campusService.findByLocationNear(Boston, new Distance(1, Metrics.NEUTRAL));
|
||||
assertFalse(campuses.isEmpty());
|
||||
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));
|
||||
assertFalse(campuses.isEmpty());
|
||||
assertTrue(campuses.contains(Columbia));
|
||||
assertFalse(campuses.contains(Harvard));
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package org.baeldung.spring.data.couchbase2b.service;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.spring.data.couchbase.model.Person;
|
||||
import org.baeldung.spring.data.couchbase2b.MultiBucketCouchbaseConfig;
|
||||
import org.baeldung.spring.data.couchbase2b.MultiBucketIntegrationTest;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.CouchbaseCluster;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
|
||||
public class PersonServiceImplIntegrationTest extends MultiBucketIntegrationTest {
|
||||
|
||||
static final String typeField = "_class";
|
||||
static final String john = "John";
|
||||
static final String smith = "Smith";
|
||||
static final String johnSmithId = "person:" + john + ":" + smith;
|
||||
static final Person johnSmith = new Person(johnSmithId, john, smith);
|
||||
static final JsonObject jsonJohnSmith = JsonObject.empty().put(typeField, Person.class.getName()).put("firstName", john).put("lastName", smith).put("created", DateTime.now().getMillis());
|
||||
|
||||
static final String foo = "Foo";
|
||||
static final String bar = "Bar";
|
||||
static final String foobarId = "person:" + foo + ":" + bar;
|
||||
static final Person foobar = new Person(foobarId, foo, bar);
|
||||
static final JsonObject jsonFooBar = JsonObject.empty().put(typeField, Person.class.getName()).put("firstName", foo).put("lastName", bar).put("created", DateTime.now().getMillis());
|
||||
|
||||
@Autowired
|
||||
private PersonServiceImpl personService;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupBeforeClass() {
|
||||
final Cluster cluster = CouchbaseCluster.create(MultiBucketCouchbaseConfig.NODE_LIST);
|
||||
final Bucket bucket = cluster.openBucket(MultiBucketCouchbaseConfig.DEFAULT_BUCKET_NAME, MultiBucketCouchbaseConfig.DEFAULT_BUCKET_PASSWORD);
|
||||
bucket.upsert(JsonDocument.create(johnSmithId, jsonJohnSmith));
|
||||
bucket.upsert(JsonDocument.create(foobarId, jsonFooBar));
|
||||
bucket.close();
|
||||
cluster.disconnect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingPersonByJohnSmithId_thenReturnsJohnSmith() {
|
||||
final Person actualPerson = personService.findOne(johnSmithId);
|
||||
assertNotNull(actualPerson);
|
||||
assertNotNull(actualPerson.getCreated());
|
||||
assertEquals(johnSmith, actualPerson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingAllPersons_thenReturnsTwoOrMorePersonsIncludingJohnSmithAndFooBar() {
|
||||
final List<Person> resultList = personService.findAll();
|
||||
assertNotNull(resultList);
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(resultContains(resultList, johnSmith));
|
||||
assertTrue(resultContains(resultList, foobar));
|
||||
assertTrue(resultList.size() >= 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingByFirstNameJohn_thenReturnsOnlyPersonsNamedJohn() {
|
||||
final String expectedFirstName = john;
|
||||
final List<Person> resultList = personService.findByFirstName(expectedFirstName);
|
||||
assertNotNull(resultList);
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(allResultsContainExpectedFirstName(resultList, expectedFirstName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingByLastNameSmith_thenReturnsOnlyPersonsNamedSmith() {
|
||||
final String expectedLastName = smith;
|
||||
final List<Person> resultList = personService.findByLastName(expectedLastName);
|
||||
assertNotNull(resultList);
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(allResultsContainExpectedLastName(resultList, expectedLastName));
|
||||
}
|
||||
|
||||
private boolean resultContains(List<Person> resultList, Person person) {
|
||||
boolean found = false;
|
||||
for (final Person p : resultList) {
|
||||
if (p.equals(person)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private boolean allResultsContainExpectedFirstName(List<Person> resultList, String firstName) {
|
||||
boolean found = false;
|
||||
for (final Person p : resultList) {
|
||||
if (p.getFirstName().equals(firstName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private boolean allResultsContainExpectedLastName(List<Person> resultList, String lastName) {
|
||||
boolean found = false;
|
||||
for (final Person p : resultList) {
|
||||
if (p.getLastName().equals(lastName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
package org.baeldung.spring.data.couchbase2b.service;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import org.baeldung.spring.data.couchbase.model.Student;
|
||||
import org.baeldung.spring.data.couchbase2b.MultiBucketCouchbaseConfig;
|
||||
import org.baeldung.spring.data.couchbase2b.MultiBucketIntegrationTest;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.CouchbaseCluster;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
|
||||
public class StudentServiceImplIntegrationTest extends MultiBucketIntegrationTest {
|
||||
|
||||
static final String typeField = "_class";
|
||||
static final String joe = "Joe";
|
||||
static final String college = "College";
|
||||
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 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);
|
||||
|
||||
@Autowired
|
||||
StudentServiceImpl studentService;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupBeforeClass() {
|
||||
Cluster cluster = CouchbaseCluster.create(MultiBucketCouchbaseConfig.NODE_LIST);
|
||||
Bucket bucket = cluster.openBucket(MultiBucketCouchbaseConfig.DEFAULT_BUCKET_NAME, MultiBucketCouchbaseConfig.DEFAULT_BUCKET_PASSWORD);
|
||||
bucket.upsert(JsonDocument.create(joeCollegeId, jsonJoeCollege));
|
||||
bucket.upsert(JsonDocument.create(judyJetsonId, jsonJudyJetson));
|
||||
bucket.close();
|
||||
cluster.disconnect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingStudent_thenDocumentIsPersisted() {
|
||||
String firstName = "Eric";
|
||||
String lastName = "Stratton";
|
||||
DateTime dateOfBirth = DateTime.now().minusYears(25);
|
||||
String id = "student:" + firstName + ":" + lastName;
|
||||
Student expectedStudent = new Student(id, firstName, lastName, dateOfBirth);
|
||||
studentService.create(expectedStudent);
|
||||
Student actualStudent = studentService.findOne(id);
|
||||
assertNotNull(actualStudent.getCreated());
|
||||
assertNotNull(actualStudent);
|
||||
assertEquals(expectedStudent.getId(), actualStudent.getId());
|
||||
}
|
||||
|
||||
@Test(expected = ConstraintViolationException.class)
|
||||
public void whenCreatingStudentWithInvalidFirstName_thenConstraintViolationException() {
|
||||
String firstName = "Er+ic";
|
||||
String lastName = "Stratton";
|
||||
DateTime dateOfBirth = DateTime.now().minusYears(25);
|
||||
String id = "student:" + firstName + ":" + lastName;
|
||||
Student student = new Student(id, firstName, lastName, dateOfBirth);
|
||||
studentService.create(student);
|
||||
}
|
||||
|
||||
@Test(expected = ConstraintViolationException.class)
|
||||
public void whenCreatingStudentWithFutureDob_thenConstraintViolationException() {
|
||||
String firstName = "Jane";
|
||||
String lastName = "Doe";
|
||||
DateTime dateOfBirth = DateTime.now().plusDays(1);
|
||||
String id = "student:" + firstName + ":" + lastName;
|
||||
Student student = new Student(id, firstName, lastName, dateOfBirth);
|
||||
studentService.create(student);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingStudentByJohnSmithId_thenReturnsJohnSmith() {
|
||||
Student actualStudent = studentService.findOne(joeCollegeId);
|
||||
assertNotNull(actualStudent);
|
||||
assertNotNull(actualStudent.getCreated());
|
||||
assertEquals(joeCollegeId, actualStudent.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingAllStudents_thenReturnsTwoOrMoreStudentsIncludingJoeCollegeAndJudyJetson() {
|
||||
List<Student> resultList = studentService.findAll();
|
||||
assertNotNull(resultList);
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(resultContains(resultList, joeCollege));
|
||||
assertTrue(resultContains(resultList, judyJetson));
|
||||
assertTrue(resultList.size() >= 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingByFirstNameJohn_thenReturnsOnlyStudentsNamedJohn() {
|
||||
String expectedFirstName = joe;
|
||||
List<Student> resultList = studentService.findByFirstName(expectedFirstName);
|
||||
assertNotNull(resultList);
|
||||
assertFalse(resultList.isEmpty());
|
||||
assertTrue(allResultsContainExpectedFirstName(resultList, expectedFirstName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingByLastNameSmith_thenReturnsOnlyStudentsNamedSmith() {
|
||||
String expectedLastName = college;
|
||||
List<Student> resultList = studentService.findByLastName(expectedLastName);
|
||||
assertNotNull(resultList);
|
||||
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())) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private boolean allResultsContainExpectedFirstName(List<Student> resultList, String firstName) {
|
||||
boolean found = false;
|
||||
for (Student p : resultList) {
|
||||
if (p.getFirstName().equals(firstName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private boolean allResultsContainExpectedLastName(List<Student> resultList, String lastName) {
|
||||
boolean found = false;
|
||||
for (Student p : resultList) {
|
||||
if (p.getLastName().equals(lastName)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user