DATACOUCH-569 - Add tests for DATACOUCH-560 - serialization of nested objects (CouchbaseDocument) and lists (CouchbaseList)
This commit is contained in:
@@ -35,7 +35,9 @@ import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversion
|
||||
import org.springframework.data.couchbase.core.convert.CouchbaseJsr310Converters.LocalDateTimeToLongConverter;
|
||||
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
|
||||
import org.springframework.data.couchbase.core.mapping.id.*;
|
||||
import org.springframework.data.couchbase.domain.Address;
|
||||
import org.springframework.data.couchbase.domain.Config;
|
||||
import org.springframework.data.couchbase.domain.Person;
|
||||
import org.springframework.data.couchbase.domain.User;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
|
||||
@@ -573,6 +575,37 @@ public class MappingCouchbaseConverterTests {
|
||||
assertThat(read.deleted.truncatedTo(ChronoUnit.MILLIS)).isEqualTo(deleted.truncatedTo(ChronoUnit.MILLIS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writesAndReadsNestedClass() {
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
|
||||
final String street = "Easy Street";
|
||||
final Person person = new Person();
|
||||
person.setFirstname("firstname");
|
||||
person.setLastname("lastname");
|
||||
final Address addr = new Address();
|
||||
addr.setStreet(street);
|
||||
person.setAddress(addr);
|
||||
|
||||
converter.write(person, converted);
|
||||
Map<String, Object> result = converted.export();
|
||||
|
||||
assertThat(result.get("_class")).isEqualTo(person.getClass().getName());
|
||||
assertThat(result.get("address")).isEqualTo(new HashMap<String, Object>() {
|
||||
{
|
||||
put("street", street);
|
||||
}
|
||||
});
|
||||
|
||||
CouchbaseDocument source = new CouchbaseDocument();
|
||||
source.put("_class", Person.class.getName());
|
||||
CouchbaseDocument addressDoc = new CouchbaseDocument();
|
||||
addressDoc.put("street", street);
|
||||
source.put("address", addressDoc);
|
||||
Person readConverted = converter.read(Person.class, source);
|
||||
assertThat(readConverted.getAddress().getStreet()).isEqualTo(person.getAddress().getStreet());
|
||||
}
|
||||
|
||||
@WritingConverter
|
||||
public enum BigDecimalToStringConverter implements Converter<BigDecimal, String> {
|
||||
INSTANCE;
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.springframework.data.couchbase.domain;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
||||
import org.springframework.data.couchbase.core.mapping.Document;
|
||||
import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
|
||||
import org.springframework.data.couchbase.core.mapping.id.GenerationStrategy;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Document
|
||||
public class Address extends AbstractEntity {
|
||||
|
||||
private String street;
|
||||
|
||||
public Address() {}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{\"street\"=\"");
|
||||
sb.append(getStreet());
|
||||
sb.append("\"}");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -18,34 +18,36 @@ package org.springframework.data.couchbase.domain;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.annotation.*;
|
||||
import com.couchbase.client.core.deps.com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedBy;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
import org.springframework.data.annotation.Version;
|
||||
import org.springframework.data.couchbase.core.mapping.Document;
|
||||
import org.springframework.data.couchbase.core.mapping.Field;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@Document
|
||||
@TypeAlias("p") // this will result in p : <className> or p : "t" using the CustomMappingCouchbaseConverter
|
||||
public class Person extends AbstractEntity {
|
||||
Optional<String> firstname;
|
||||
Optional<String> lastname;
|
||||
@Nullable Optional<String> lastname;
|
||||
|
||||
@CreatedBy
|
||||
private String creator;
|
||||
@CreatedBy private String creator;
|
||||
|
||||
@LastModifiedBy
|
||||
private String lastModifiedBy;
|
||||
@LastModifiedBy private String lastModifiedBy;
|
||||
|
||||
@LastModifiedDate
|
||||
private long lastModification;
|
||||
@LastModifiedDate private long lastModification;
|
||||
|
||||
@CreatedDate
|
||||
private long creationDate;
|
||||
@CreatedDate private long creationDate;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
@Version private long version;
|
||||
|
||||
private Object middlename;
|
||||
@Nullable @Field("nickname") private String middlename;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
private Address address;
|
||||
|
||||
public Person() {}
|
||||
|
||||
public Person(String firstname, String lastname) {
|
||||
this();
|
||||
@@ -93,16 +95,34 @@ public class Person extends AbstractEntity {
|
||||
this.lastname = lastname;
|
||||
}
|
||||
|
||||
public String getMiddlename() {
|
||||
return middlename;
|
||||
}
|
||||
|
||||
public void setMiddlename(String middlename) {
|
||||
this.middlename = middlename;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Person : {\n");
|
||||
sb.append(" id : " + getId());
|
||||
sb.append(optional(", firstname", firstname));
|
||||
sb.append(optional(", lastname", lastname));
|
||||
if (middlename != null)
|
||||
sb.append(", middlename : " + middlename);
|
||||
sb.append(", version : " + version);
|
||||
if (creator != null) {
|
||||
sb.append(", creator : " + creator);
|
||||
@@ -116,6 +136,9 @@ public class Person extends AbstractEntity {
|
||||
if (lastModification != 0) {
|
||||
sb.append(", lastModification : " + lastModification);
|
||||
}
|
||||
if (getAddress() != null) {
|
||||
sb.append(", address : " + getAddress().toString());
|
||||
}
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
@@ -131,7 +154,4 @@ public class Person extends AbstractEntity {
|
||||
return "";
|
||||
}
|
||||
|
||||
public void setMiddlename(String middlename) {
|
||||
this.middlename = middlename;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user