JAVA-20166 Migrating spring-date-eclipselink (#13959)

* JAVA-20166 Migrating spring-date-eclipselink

* JAVA-20166 Fix failed tests address already bind

* JAVA-20166 Migrating spring-date-couchbase-2

* JAVA-20166 Replace with optional instead of returning null

---------

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1
2023-05-19 09:53:28 +03:00
committed by GitHub
parent d9182f160c
commit 09e421c3cd
40 changed files with 266 additions and 220 deletions
@@ -1,13 +1,12 @@
package com.baeldung.spring.data.couchbase.model;
import javax.validation.constraints.NotNull;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.core.mapping.Field;
import org.springframework.data.geo.Point;
import com.couchbase.client.java.repository.annotation.Field;
@Document
public class Campus {
@@ -1,12 +1,11 @@
package com.baeldung.spring.data.couchbase.model;
import javax.validation.constraints.NotNull;
import jakarta.validation.constraints.NotNull;
import org.joda.time.DateTime;
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.Document;
import com.couchbase.client.java.repository.annotation.Field;
import org.springframework.data.couchbase.core.mapping.Field;
@Document
public class Person {
@@ -1,16 +1,15 @@
package com.baeldung.spring.data.couchbase.model;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Past;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import org.joda.time.DateTime;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.couchbase.core.mapping.Document;
import com.couchbase.client.java.repository.annotation.Field;
import org.springframework.data.couchbase.core.mapping.Field;
@Document
public class Student {
@@ -5,9 +5,7 @@ import java.util.List;
import com.baeldung.spring.data.couchbase.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import com.couchbase.client.java.view.Stale;
import com.couchbase.client.java.view.ViewQuery;
import org.springframework.data.couchbase.core.query.QueryCriteria;
public class CustomStudentRepositoryImpl implements CustomStudentRepository {
@@ -17,6 +15,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.findByQuery(Student.class).matching(QueryCriteria.where("firstName").startingWith(s)).all();
}
}
@@ -3,6 +3,7 @@ package com.baeldung.spring.data.couchbase.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Person;
import com.baeldung.spring.data.couchbase.repos.PersonRepository;
@@ -22,12 +23,12 @@ public class PersonRepositoryService implements PersonService {
this.repo = repo;
}
public Person findOne(String id) {
return repo.findOne(id);
public Optional<Person> findOne(String id) {
return repo.findById(id);
}
public List<Person> findAll() {
List<Person> people = new ArrayList<Person>();
List<Person> people = new ArrayList<>();
Iterator<Person> it = repo.findAll().iterator();
while (it.hasNext()) {
people.add(it.next());
@@ -1,12 +1,13 @@
package com.baeldung.spring.data.couchbase.service;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Person;
public interface PersonService {
Person findOne(String id);
Optional<Person> findOne(String id);
List<Person> findAll();
@@ -1,6 +1,9 @@
package com.baeldung.spring.data.couchbase.service;
import static org.springframework.data.couchbase.core.query.QueryCriteria.where;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Person;
import org.joda.time.DateTime;
@@ -9,8 +12,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.stereotype.Service;
import com.couchbase.client.java.view.ViewQuery;
@Service
@Qualifier("PersonTemplateService")
public class PersonTemplateService implements PersonService {
@@ -24,33 +25,33 @@ public class PersonTemplateService implements PersonService {
this.template = template;
}
public Person findOne(String id) {
return template.findById(id, Person.class);
public Optional<Person> findOne(String id) {
return Optional.of(template.findById(Person.class).one(id));
}
public List<Person> findAll() {
return template.findByView(ViewQuery.from(DESIGN_DOC, "all"), Person.class);
return template.findByQuery(Person.class).all();
}
public List<Person> findByFirstName(String firstName) {
return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName"), Person.class);
return template.findByQuery(Person.class).matching(where("firstName").is(firstName)).all();
}
public List<Person> findByLastName(String lastName) {
return template.findByView(ViewQuery.from(DESIGN_DOC, "byLastName"), Person.class);
return template.findByQuery(Person.class).matching(where("lastName").is(lastName)).all();
}
public void create(Person person) {
person.setCreated(DateTime.now());
template.insert(person);
template.insertById(Person.class).one(person);
}
public void update(Person person) {
person.setUpdated(DateTime.now());
template.update(person);
template.removeById(Person.class).oneEntity(person);
}
public void delete(Person person) {
template.remove(person);
template.removeById(Person.class).oneEntity(person);
}
}
@@ -3,6 +3,7 @@ package com.baeldung.spring.data.couchbase.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Student;
import com.baeldung.spring.data.couchbase.repos.StudentRepository;
@@ -22,12 +23,12 @@ public class StudentRepositoryService implements StudentService {
this.repo = repo;
}
public Student findOne(String id) {
return repo.findOne(id);
public Optional<Student> findOne(String id) {
return repo.findById(id);
}
public List<Student> findAll() {
List<Student> people = new ArrayList<Student>();
List<Student> people = new ArrayList<>();
Iterator<Student> it = repo.findAll().iterator();
while (it.hasNext()) {
people.add(it.next());
@@ -1,12 +1,13 @@
package com.baeldung.spring.data.couchbase.service;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Student;
public interface StudentService {
Student findOne(String id);
Optional<Student> findOne(String id);
List<Student> findAll();
@@ -1,6 +1,9 @@
package com.baeldung.spring.data.couchbase.service;
import static org.springframework.data.couchbase.core.query.QueryCriteria.where;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Student;
import org.joda.time.DateTime;
@@ -9,8 +12,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.stereotype.Service;
import com.couchbase.client.java.view.ViewQuery;
@Service
@Qualifier("StudentTemplateService")
public class StudentTemplateService implements StudentService {
@@ -24,33 +25,33 @@ public class StudentTemplateService implements StudentService {
this.template = template;
}
public Student findOne(String id) {
return template.findById(id, Student.class);
public Optional<Student> findOne(String id) {
return Optional.of(template.findById(Student.class).one(id));
}
public List<Student> findAll() {
return template.findByView(ViewQuery.from(DESIGN_DOC, "all"), Student.class);
return template.findByQuery(Student.class).all();
}
public List<Student> findByFirstName(String firstName) {
return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName"), Student.class);
return template.findByQuery(Student.class).matching(where("firstName").is(firstName)).all();
}
public List<Student> findByLastName(String lastName) {
return template.findByView(ViewQuery.from(DESIGN_DOC, "byLastName"), Student.class);
return template.findByQuery(Student.class).matching(where("lastName").is(lastName)).all();
}
public void create(Student student) {
student.setCreated(DateTime.now());
template.insert(student);
template.insertById(Student.class).one(student);
}
public void update(Student student) {
student.setUpdated(DateTime.now());
template.update(student);
template.upsertById(Student.class).one(student);
}
public void delete(Student student) {
template.remove(student);
template.removeById(Student.class).oneEntity(student);
}
}
@@ -1,5 +1,6 @@
package com.baeldung.spring.data.couchbase2b.service;
import java.util.Optional;
import java.util.Set;
import com.baeldung.spring.data.couchbase.model.Campus;
@@ -8,7 +9,7 @@ import org.springframework.data.geo.Point;
public interface CampusService {
Campus find(String id);
Optional<Campus> find(String id);
Set<Campus> findByName(String name);
@@ -2,6 +2,7 @@ package com.baeldung.spring.data.couchbase2b.service;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
import com.baeldung.spring.data.couchbase2b.repos.CampusRepository;
@@ -22,8 +23,8 @@ public class CampusServiceImpl implements CampusService {
}
@Override
public Campus find(String id) {
return repo.findOne(id);
public Optional<Campus> find(String id) {
return repo.findById(id);
}
@Override
@@ -1,12 +1,13 @@
package com.baeldung.spring.data.couchbase2b.service;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Person;
public interface PersonService {
Person findOne(String id);
Optional<Person> findOne(String id);
List<Person> findAll();
@@ -3,6 +3,7 @@ package com.baeldung.spring.data.couchbase2b.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase2b.repos.PersonRepository;
import com.baeldung.spring.data.couchbase.model.Person;
@@ -20,12 +21,12 @@ public class PersonServiceImpl implements PersonService {
this.repo = repo;
}
public Person findOne(String id) {
return repo.findOne(id);
public Optional<Person> findOne(String id) {
return repo.findById(id);
}
public List<Person> findAll() {
List<Person> people = new ArrayList<Person>();
List<Person> people = new ArrayList<>();
Iterator<Person> it = repo.findAll().iterator();
while (it.hasNext()) {
people.add(it.next());
@@ -1,12 +1,13 @@
package com.baeldung.spring.data.couchbase2b.service;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Student;
public interface StudentService {
Student findOne(String id);
Optional<Student> findOne(String id);
List<Student> findAll();
@@ -3,6 +3,7 @@ package com.baeldung.spring.data.couchbase2b.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import com.baeldung.spring.data.couchbase2b.repos.StudentRepository;
import com.baeldung.spring.data.couchbase.model.Student;
@@ -20,12 +21,12 @@ public class StudentServiceImpl implements StudentService {
this.repo = repo;
}
public Student findOne(String id) {
return repo.findOne(id);
public Optional<Student> findOne(String id) {
return repo.findById(id);
}
public List<Student> findAll() {
List<Student> people = new ArrayList<Student>();
List<Student> people = new ArrayList<>();
Iterator<Student> it = repo.findAll().iterator();
while (it.hasNext()) {
people.add(it.next());
@@ -4,6 +4,21 @@ import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter
public class CustomTypeKeyCouchbaseConfig extends MyCouchbaseConfig {
@Override
public String getConnectionString() {
return NODE_LIST;
}
@Override
public String getUserName() {
return BUCKET_USERNAME;
}
@Override
public String getPassword() {
return BUCKET_PASSWORD;
}
@Override
public String typeKey() {
return MappingCouchbaseConverter.TYPEKEY_SYNCGATEWAY_COMPATIBLE;
@@ -1,42 +1,46 @@
package com.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;
import com.couchbase.client.java.query.QueryScanConsistency;
@Configuration
@EnableCouchbaseRepositories(basePackages = { "com.baeldung.spring.data.couchbase" })
public class MyCouchbaseConfig extends AbstractCouchbaseConfiguration {
public static final List<String> NODE_LIST = Arrays.asList("localhost");
public static final String NODE_LIST = "localhost";
public static final String BUCKET_NAME = "baeldung";
public static final String BUCKET_PASSWORD = "";
public static final String BUCKET_USERNAME = "baeldung";
public static final String BUCKET_PASSWORD = "baeldung";
@Override
protected List<String> getBootstrapHosts() {
public String getConnectionString() {
return NODE_LIST;
}
@Override
protected String getBucketName() {
return BUCKET_NAME;
public String getUserName() {
return BUCKET_USERNAME;
}
@Override
protected String getBucketPassword() {
public String getPassword() {
return BUCKET_PASSWORD;
}
@Override
protected Consistency getDefaultConsistency() {
return Consistency.READ_YOUR_OWN_WRITES;
public String getBucketName() {
return BUCKET_NAME;
}
@Override
public QueryScanConsistency getDefaultConsistency() {
return QueryScanConsistency.REQUEST_PLUS;
}
@Bean
@@ -1,11 +1,11 @@
package com.baeldung.spring.data.couchbase;
import org.springframework.data.couchbase.core.query.Consistency;
import com.couchbase.client.java.query.QueryScanConsistency;
public class ReadYourOwnWritesCouchbaseConfig extends MyCouchbaseConfig {
@Override
public Consistency getDefaultConsistency() {
return Consistency.READ_YOUR_OWN_WRITES;
public QueryScanConsistency getDefaultConsistency() {
return QueryScanConsistency.REQUEST_PLUS;
}
}
@@ -1,11 +1,15 @@
package com.baeldung.spring.data.couchbase.service;
import static com.baeldung.spring.data.couchbase.MyCouchbaseConfig.BUCKET_PASSWORD;
import static com.baeldung.spring.data.couchbase.MyCouchbaseConfig.BUCKET_USERNAME;
import static com.baeldung.spring.data.couchbase.MyCouchbaseConfig.NODE_LIST;
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 java.util.Optional;
import com.baeldung.spring.data.couchbase.IntegrationTest;
import com.baeldung.spring.data.couchbase.MyCouchbaseConfig;
@@ -16,9 +20,8 @@ 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;
import com.couchbase.client.java.Collection;
import com.couchbase.client.java.json.JsonObject;
public abstract class PersonServiceLiveTest extends IntegrationTest {
@@ -27,32 +30,32 @@ public abstract class PersonServiceLiveTest extends IntegrationTest {
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 JsonObject jsonJohnSmith = JsonObject.create().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());
static final JsonObject jsonFooBar = JsonObject.create().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();
final Cluster cluster = Cluster.connect(NODE_LIST, BUCKET_USERNAME, BUCKET_PASSWORD);
final Bucket bucket = cluster.bucket(MyCouchbaseConfig.BUCKET_NAME);
final Collection collection = bucket.defaultCollection();
collection.upsert(johnSmithId, JsonObject.create().put(johnSmithId, jsonJohnSmith));
collection.upsert(foobarId, JsonObject.create().put(foobarId, jsonFooBar));
cluster.disconnect();
}
@Test
public void whenFindingPersonByJohnSmithId_thenReturnsJohnSmith() {
final Person actualPerson = personService.findOne(johnSmithId);
assertNotNull(actualPerson);
assertNotNull(actualPerson.getCreated());
assertEquals(johnSmith, actualPerson);
final Optional<Person> actualPerson = personService.findOne(johnSmithId);
assertTrue(actualPerson.isPresent());
assertNotNull(actualPerson.get().getCreated());
assertEquals(johnSmith, actualPerson.get());
}
@Test
@@ -1,13 +1,17 @@
package com.baeldung.spring.data.couchbase.service;
import static com.baeldung.spring.data.couchbase.MyCouchbaseConfig.BUCKET_PASSWORD;
import static com.baeldung.spring.data.couchbase.MyCouchbaseConfig.BUCKET_USERNAME;
import static com.baeldung.spring.data.couchbase.MyCouchbaseConfig.NODE_LIST;
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 java.util.Optional;
import javax.validation.ConstraintViolationException;
import jakarta.validation.ConstraintViolationException;
import com.baeldung.spring.data.couchbase.IntegrationTest;
import com.baeldung.spring.data.couchbase.MyCouchbaseConfig;
@@ -18,9 +22,8 @@ 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;
import com.couchbase.client.java.Collection;
import com.couchbase.client.java.json.JsonObject;
public abstract class StudentServiceLiveTest extends IntegrationTest {
@@ -30,24 +33,24 @@ public abstract class StudentServiceLiveTest 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.create().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.create().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();
final Cluster cluster = Cluster.connect(NODE_LIST, BUCKET_USERNAME, BUCKET_PASSWORD);
final Bucket bucket = cluster.bucket(MyCouchbaseConfig.BUCKET_NAME);
final Collection collection = bucket.defaultCollection();
collection.upsert(joeCollegeId, JsonObject.create().put(joeCollegeId, jsonJoeCollege));
collection.upsert(judyJetsonId, JsonObject.create().put(judyJetsonId, jsonJudyJetson));
cluster.disconnect();
}
@@ -59,10 +62,10 @@ public abstract class StudentServiceLiveTest extends IntegrationTest {
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());
Optional<Student> actualStudent = studentService.findOne(id);
assertTrue(actualStudent.isPresent());
assertNotNull(actualStudent.get().getCreated());
assertEquals(expectedStudent.getId(), actualStudent.get().getId());
}
@Test(expected = ConstraintViolationException.class)
@@ -87,10 +90,10 @@ public abstract class StudentServiceLiveTest extends IntegrationTest {
@Test
public void whenFindingStudentByJohnSmithId_thenReturnsJohnSmith() {
Student actualStudent = studentService.findOne(joeCollegeId);
assertNotNull(actualStudent);
assertNotNull(actualStudent.getCreated());
assertEquals(joeCollegeId, actualStudent.getId());
Optional<Student> actualStudent = studentService.findOne(joeCollegeId);
assertTrue(actualStudent.isPresent());
assertNotNull(actualStudent.get().getCreated());
assertEquals(joeCollegeId, actualStudent.get().getId());
}
@Test
@@ -1,54 +1,47 @@
package com.baeldung.spring.data.couchbase2b;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.baeldung.spring.data.couchbase.model.Campus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
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.core.env.PasswordAuthenticator;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.env.ClusterEnvironment;
@Configuration
@EnableCouchbaseRepositories(basePackages = { "com.baeldung.spring.data.couchbase2b" })
public class MultiBucketCouchbaseConfig extends AbstractCouchbaseConfiguration {
public static final List<String> NODE_LIST = Arrays.asList("localhost");
public static final String NODE_LIST = "localhost";
public static final String DEFAULT_BUCKET_NAME = "baeldung";
public static final String DEFAULT_BUCKET_PASSWORD = "";
public static final String DEFAULT_BUCKET_USERNAME = "baeldung";
public static final String DEFAULT_BUCKET_PASSWORD = "baeldung";
@Override
protected List<String> getBootstrapHosts() {
return NODE_LIST;
}
@Override
protected String getBucketName() {
return DEFAULT_BUCKET_NAME;
}
@Override
protected String getBucketPassword() {
return DEFAULT_BUCKET_PASSWORD;
}
@Autowired
private MappingCouchbaseConverter mappingCouchbaseConverter;
@Bean
public Bucket campusBucket() throws Exception {
return couchbaseCluster().openBucket("baeldung2", "");
public Bucket campusBucket() {
return couchbaseCluster(ClusterEnvironment.create()).bucket("baeldung2");
}
@Bean(name = "campusTemplate")
public CouchbaseTemplate campusTemplate() throws Exception {
CouchbaseTemplate template = new CouchbaseTemplate(couchbaseClusterInfo(), campusBucket(), mappingCouchbaseConverter(), translationService());
template.setDefaultConsistency(getDefaultConsistency());
return template;
public CouchbaseTemplate campusTemplate() {
return new CouchbaseTemplate(new SimpleCouchbaseClientFactory(NODE_LIST,
PasswordAuthenticator.create(DEFAULT_BUCKET_USERNAME, DEFAULT_BUCKET_PASSWORD), DEFAULT_BUCKET_NAME), mappingCouchbaseConverter);
}
@Override
@@ -60,11 +53,6 @@ public class MultiBucketCouchbaseConfig extends AbstractCouchbaseConfiguration {
}
}
@Override
protected Consistency getDefaultConsistency() {
return Consistency.READ_YOUR_OWN_WRITES;
}
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
@@ -74,4 +62,24 @@ public class MultiBucketCouchbaseConfig extends AbstractCouchbaseConfiguration {
public ValidatingCouchbaseEventListener validatingCouchbaseEventListener() {
return new ValidatingCouchbaseEventListener(localValidatorFactoryBean());
}
@Override
public String getConnectionString() {
return NODE_LIST;
}
@Override
public String getUserName() {
return DEFAULT_BUCKET_USERNAME;
}
@Override
public String getPassword() {
return DEFAULT_BUCKET_PASSWORD;
}
@Override
public String getBucketName() {
return DEFAULT_BUCKET_NAME;
}
}
@@ -5,9 +5,10 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Optional;
import java.util.Set;
import javax.annotation.PostConstruct;
import jakarta.annotation.PostConstruct;
import com.baeldung.spring.data.couchbase.model.Campus;
import com.baeldung.spring.data.couchbase2b.MultiBucketLiveTest;
@@ -46,7 +47,7 @@ public class CampusServiceImplLiveTest extends MultiBucketLiveTest {
private final Point NewYorkCity = new Point(74.0059, 40.7128);
@PostConstruct
private void loadCampuses() throws Exception {
private void loadCampuses() {
campusRepo.save(Brown);
campusRepo.save(Columbia);
campusRepo.save(Cornell);
@@ -58,7 +59,7 @@ public class CampusServiceImplLiveTest extends MultiBucketLiveTest {
}
@Test
public final void givenNameHarvard_whenFindByName_thenReturnsHarvard() throws Exception {
public final void givenNameHarvard_whenFindByName_thenReturnsHarvard() {
Set<Campus> campuses = campusService.findByName(Harvard.getName());
assertNotNull(campuses);
assertFalse(campuses.isEmpty());
@@ -67,14 +68,14 @@ public class CampusServiceImplLiveTest extends MultiBucketLiveTest {
}
@Test
public final void givenHarvardId_whenFind_thenReturnsHarvard() throws Exception {
Campus actual = campusService.find(Harvard.getId());
assertNotNull(actual);
assertEquals(Harvard, actual);
public final void givenHarvardId_whenFind_thenReturnsHarvard() {
Optional<Campus> actual = campusService.find(Harvard.getId());
assertTrue(actual.isPresent());
assertEquals(Harvard, actual.get());
}
@Test
public final void whenFindAll_thenReturnsAll() throws Exception {
public final void whenFindAll_thenReturnsAll() {
Set<Campus> campuses = campusService.findAll();
assertTrue(campuses.contains(Brown));
assertTrue(campuses.contains(Columbia));
@@ -87,7 +88,7 @@ public class CampusServiceImplLiveTest extends MultiBucketLiveTest {
}
@Test
public final void whenFindByLocationNearBoston_thenResultContainsHarvard() throws Exception {
public final void whenFindByLocationNearBoston_thenResultContainsHarvard() {
Set<Campus> campuses = campusService.findByLocationNear(Boston, new Distance(1, Metrics.NEUTRAL));
assertFalse(campuses.isEmpty());
assertTrue(campuses.contains(Harvard));
@@ -95,7 +96,7 @@ public class CampusServiceImplLiveTest extends MultiBucketLiveTest {
}
@Test
public final void whenFindByLocationNearNewYorkCity_thenResultContainsColumbia() throws Exception {
public final void whenFindByLocationNearNewYorkCity_thenResultContainsColumbia() {
Set<Campus> campuses = campusService.findByLocationNear(NewYorkCity, new Distance(1, Metrics.NEUTRAL));
assertFalse(campuses.isEmpty());
assertTrue(campuses.contains(Columbia));
@@ -1,11 +1,15 @@
package com.baeldung.spring.data.couchbase2b.service;
import static com.baeldung.spring.data.couchbase2b.MultiBucketCouchbaseConfig.DEFAULT_BUCKET_PASSWORD;
import static com.baeldung.spring.data.couchbase2b.MultiBucketCouchbaseConfig.DEFAULT_BUCKET_USERNAME;
import static com.baeldung.spring.data.couchbase2b.MultiBucketCouchbaseConfig.NODE_LIST;
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 java.util.Optional;
import com.baeldung.spring.data.couchbase.model.Person;
import com.baeldung.spring.data.couchbase2b.MultiBucketLiveTest;
@@ -17,9 +21,8 @@ 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;
import com.couchbase.client.java.Collection;
import com.couchbase.client.java.json.JsonObject;
public class PersonServiceImplLiveTest extends MultiBucketLiveTest {
@@ -28,33 +31,33 @@ public class PersonServiceImplLiveTest extends MultiBucketLiveTest {
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 JsonObject jsonJohnSmith = JsonObject.create().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());
static final JsonObject jsonFooBar = JsonObject.create().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();
final Cluster cluster = Cluster.connect(NODE_LIST, DEFAULT_BUCKET_USERNAME, DEFAULT_BUCKET_PASSWORD);
final Bucket bucket = cluster.bucket(MultiBucketCouchbaseConfig.DEFAULT_BUCKET_NAME);
final Collection collection = bucket.defaultCollection();
collection.upsert(johnSmithId, JsonObject.create().put(johnSmithId, jsonJohnSmith));
collection.upsert(foobarId, JsonObject.create().put(foobarId, jsonFooBar));
cluster.disconnect();
}
@Test
public void whenFindingPersonByJohnSmithId_thenReturnsJohnSmith() {
final Person actualPerson = personService.findOne(johnSmithId);
assertNotNull(actualPerson);
assertNotNull(actualPerson.getCreated());
assertEquals(johnSmith, actualPerson);
final Optional<Person> actualPerson = personService.findOne(johnSmithId);
assertTrue(actualPerson.isPresent());
assertNotNull(actualPerson.get().getCreated());
assertEquals(johnSmith, actualPerson.get());
}
@Test
@@ -1,13 +1,17 @@
package com.baeldung.spring.data.couchbase2b.service;
import static com.baeldung.spring.data.couchbase2b.MultiBucketCouchbaseConfig.DEFAULT_BUCKET_PASSWORD;
import static com.baeldung.spring.data.couchbase2b.MultiBucketCouchbaseConfig.DEFAULT_BUCKET_USERNAME;
import static com.baeldung.spring.data.couchbase2b.MultiBucketCouchbaseConfig.NODE_LIST;
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 java.util.Optional;
import javax.validation.ConstraintViolationException;
import jakarta.validation.ConstraintViolationException;
import com.baeldung.spring.data.couchbase.model.Student;
import com.baeldung.spring.data.couchbase2b.MultiBucketCouchbaseConfig;
@@ -19,9 +23,8 @@ 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;
import com.couchbase.client.java.Collection;
import com.couchbase.client.java.json.JsonObject;
public class StudentServiceImplLiveTest extends MultiBucketLiveTest {
@@ -31,25 +34,25 @@ public class StudentServiceImplLiveTest extends MultiBucketLiveTest {
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.create().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.create().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 cluster = Cluster.connect(NODE_LIST, DEFAULT_BUCKET_USERNAME, DEFAULT_BUCKET_PASSWORD);
Bucket bucket = cluster.bucket(MultiBucketCouchbaseConfig.DEFAULT_BUCKET_NAME);
final Collection collection = bucket.defaultCollection();
collection.upsert(joeCollegeId, JsonObject.create().put(joeCollegeId, jsonJoeCollege));
collection.upsert(judyJetsonId, JsonObject.create().put(judyJetsonId, jsonJudyJetson));
cluster.disconnect();
}
@@ -61,10 +64,10 @@ public class StudentServiceImplLiveTest extends MultiBucketLiveTest {
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());
Optional<Student> actualStudent = studentService.findOne(id);
assertTrue(actualStudent.isPresent());
assertNotNull(actualStudent.get().getCreated());
assertEquals(expectedStudent.getId(), actualStudent.get().getId());
}
@Test(expected = ConstraintViolationException.class)
@@ -89,10 +92,10 @@ public class StudentServiceImplLiveTest extends MultiBucketLiveTest {
@Test
public void whenFindingStudentByJohnSmithId_thenReturnsJohnSmith() {
Student actualStudent = studentService.findOne(joeCollegeId);
assertNotNull(actualStudent);
assertNotNull(actualStudent.getCreated());
assertEquals(joeCollegeId, actualStudent.getId());
Optional<Student> actualStudent = studentService.findOne(joeCollegeId);
assertTrue(actualStudent.isPresent());
assertNotNull(actualStudent.get().getCreated());
assertEquals(joeCollegeId, actualStudent.get().getId());
}
@Test