DATACOUCH-533 - Promote id field if no annotation id.
Promote id field if no annotation id. Note that there is no longer any Couchbase @Id annotation - only the Spring @id annotation.
This commit is contained in:
@@ -23,6 +23,7 @@ import java.lang.reflect.Field;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
import org.springframework.data.mapping.model.Property;
|
||||
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
@@ -82,6 +83,98 @@ public class BasicCouchbasePersistentPropertyTests {
|
||||
assertThat(test.getIdProperty()).isEqualTo(springIdProperty);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAnnotationIdFieldOnly() { // only has @springId
|
||||
class TestIdField {
|
||||
@org.springframework.data.couchbase.core.mapping.Field String name;
|
||||
String description;
|
||||
@Id private String springId;
|
||||
}
|
||||
BasicCouchbasePersistentEntity<TestIdField> test = new BasicCouchbasePersistentEntity<>(
|
||||
ClassTypeInformation.from(TestIdField.class));
|
||||
Field springIdField = ReflectionUtils.findField(TestIdField.class, "springId");
|
||||
CouchbasePersistentProperty springIdProperty = getPropertyFor(springIdField);
|
||||
test.addPersistentProperty(springIdProperty);
|
||||
assertThat(test.getIdProperty()).isEqualTo(springIdProperty);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIdFieldOnly() { // only has id
|
||||
class TestIdField {
|
||||
@org.springframework.data.couchbase.core.mapping.Field String name;
|
||||
String description;
|
||||
private String id;
|
||||
}
|
||||
Field idField = ReflectionUtils.findField(TestIdField.class, "id");
|
||||
CouchbasePersistentProperty idProperty = getPropertyFor(idField);
|
||||
BasicCouchbasePersistentEntity<TestIdField> test = new BasicCouchbasePersistentEntity<>(
|
||||
ClassTypeInformation.from(TestIdField.class));
|
||||
test.addPersistentProperty(idProperty);
|
||||
assertThat(test.getIdProperty()).isEqualTo(idProperty);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIdFieldAndAnnotationIdField() { // has @springId and id
|
||||
class TestIdField {
|
||||
@org.springframework.data.couchbase.core.mapping.Field String name;
|
||||
String description;
|
||||
@Id private String springId;
|
||||
private String id;
|
||||
}
|
||||
BasicCouchbasePersistentEntity<TestIdField> test = new BasicCouchbasePersistentEntity<>(
|
||||
ClassTypeInformation.from(TestIdField.class));
|
||||
Field springIdField = ReflectionUtils.findField(TestIdField.class, "springId");
|
||||
Field idField = ReflectionUtils.findField(TestIdField.class, "id");
|
||||
CouchbasePersistentProperty idProperty = getPropertyFor(idField);
|
||||
CouchbasePersistentProperty springIdProperty = getPropertyFor(springIdField);
|
||||
// here this simulates the order in which the annotations would be found
|
||||
// when "overriding" Spring @Id with SDK's @Id...
|
||||
test.addPersistentProperty(idProperty);
|
||||
// replace id with springId
|
||||
test.addPersistentProperty(springIdProperty);
|
||||
assertThat(test.getIdProperty()).isEqualTo(springIdProperty);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTwoAnnotationIdFields() { // has @Id springId and @Id id
|
||||
class TestIdField {
|
||||
@org.springframework.data.couchbase.core.mapping.Field String name;
|
||||
String description;
|
||||
@Id private String springId;
|
||||
@Id private String id;
|
||||
}
|
||||
Field springIdField = ReflectionUtils.findField(TestIdField.class, "springId");
|
||||
Field idField = ReflectionUtils.findField(TestIdField.class, "id");
|
||||
CouchbasePersistentProperty idProperty = getPropertyFor(idField);
|
||||
CouchbasePersistentProperty springIdProperty = getPropertyFor(springIdField);
|
||||
BasicCouchbasePersistentEntity<TestIdField> test = new BasicCouchbasePersistentEntity<>(
|
||||
ClassTypeInformation.from(TestIdField.class));
|
||||
test.addPersistentProperty(springIdProperty);
|
||||
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> {
|
||||
test.addPersistentProperty(idProperty);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTwoIdFields() { // has @Field("id") springId and id
|
||||
class TestIdField {
|
||||
@org.springframework.data.couchbase.core.mapping.Field String name;
|
||||
String description;
|
||||
@org.springframework.data.couchbase.core.mapping.Field("id") private String springId;
|
||||
private String id;
|
||||
}
|
||||
Field springIdField = ReflectionUtils.findField(TestIdField.class, "springId");
|
||||
Field idField = ReflectionUtils.findField(TestIdField.class, "id");
|
||||
CouchbasePersistentProperty idProperty = getPropertyFor(idField);
|
||||
CouchbasePersistentProperty springIdProperty = getPropertyFor(springIdField);
|
||||
BasicCouchbasePersistentEntity<TestIdField> test = new BasicCouchbasePersistentEntity<>(
|
||||
ClassTypeInformation.from(TestIdField.class));
|
||||
test.addPersistentProperty(springIdProperty);
|
||||
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> {
|
||||
test.addPersistentProperty(idProperty);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a property out of the field.
|
||||
*
|
||||
|
||||
@@ -955,4 +955,104 @@ public class MappingCouchbaseConverterTests {
|
||||
assertThat(converted.getId()).isEqualTo(entity.id);
|
||||
assertThat(converted.getId()).isEqualTo(entity.prefix1 + '.' + entity.someId + '.' + entity.suffix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void idHasIdFieldOnly() {
|
||||
class Entity {
|
||||
public String id = "123";
|
||||
}
|
||||
Entity entity = new Entity();
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
converter.write(entity, converted);
|
||||
assertThat(converted.getId()).isEqualTo(entity.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
void idHasIdFieldAndAnnotatedId() {
|
||||
class Entity {
|
||||
public String id = "123";
|
||||
@Id public String otherId = "456";
|
||||
}
|
||||
Entity entity = new Entity();
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
converter.write(entity, converted);
|
||||
assertThat(converted.getId()).isEqualTo(entity.otherId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void idHasIdFieldAndAnnotatedIdReverse() {
|
||||
class Entity {
|
||||
@Id public String otherId = "456";
|
||||
public String id = "123";
|
||||
}
|
||||
Entity entity = new Entity();
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
converter.write(entity, converted);
|
||||
assertThat(converted.getId()).isEqualTo(entity.otherId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void idHasTwoAnnotatedIdFields() {
|
||||
class Entity {
|
||||
@Id public String id = "123";
|
||||
@Id public String otherId = "456";
|
||||
}
|
||||
Entity entity = new Entity();
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> {
|
||||
converter.write(entity, converted);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void idHasTwoIdFields() {
|
||||
class Entity {
|
||||
public String id = "123";
|
||||
@Field("id") public String otherId = "456";
|
||||
}
|
||||
Entity entity = new Entity();
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> {
|
||||
converter.write(entity, converted);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void idHasAnnotatedIdAndMultipleIdFields() {
|
||||
class Entity { // @Id has precedence, multiple 'id' fields is irrelevant
|
||||
@Id public String annotatedId = "123";
|
||||
@Field("id") public String otherId0 = "456";
|
||||
@Field("id") public String otherId1 = "789";
|
||||
}
|
||||
Entity entity = new Entity();
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
converter.write(entity, converted);
|
||||
assertThat(converted.getId()).isEqualTo(entity.annotatedId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void idHasAnnotatedIdAndMultipleIdFieldsReverse() {
|
||||
class Entity { // exception will be thrown at otherId1 as it is the second 'id' before @Id has been processed
|
||||
@Field("id") public String otherId0 = "456";
|
||||
@Field("id") public String otherId1 = "789";
|
||||
@Id public String annotatedId = "123";
|
||||
}
|
||||
Entity entity = new Entity();
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> {
|
||||
converter.write(entity, converted);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void idHasNoId() {
|
||||
class Entity {
|
||||
public String notId = "123";
|
||||
}
|
||||
Entity entity = new Entity();
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> {
|
||||
converter.write(entity, converted);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user