DATACOUCH-145 - Use Id/Field annotations from Couchbase SDK

The @Field annotation has been removed in favor of the one from the Couchbase SDK.

Similarly, CouchbasePersistentEntity now recognize the SDK's @Id annotation. Note that if both Spring Data and Couchbase @Id are present (on two distinct fields), the one from Spring Data will be taken into account.
This commit is contained in:
Simon Baslé
2015-07-20 17:12:05 +02:00
parent 712bc66006
commit 5114ddb867
11 changed files with 166 additions and 54 deletions

View File

@@ -17,7 +17,7 @@
package org.springframework.data.couchbase.core;
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.Field;
import com.couchbase.client.java.repository.annotation.Field;
/**

View File

@@ -17,6 +17,8 @@
package org.springframework.data.couchbase.core.mapping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Field;
@@ -24,6 +26,7 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
@@ -68,6 +71,55 @@ public class BasicCouchbasePersistentPropertyTests {
assertEquals("foobar", getPropertyFor(field).getFieldName());
}
@Test
public void testPrefersSpringIdAnnotation() {
BasicCouchbasePersistentEntity<Beer> test = new BasicCouchbasePersistentEntity<Beer>(
ClassTypeInformation.from(Beer.class));
Field sdkIdField = ReflectionUtils.findField(Beer.class, "sdkId");
CouchbasePersistentProperty sdkIdProperty = getPropertyFor(sdkIdField);
Field springIdField = ReflectionUtils.findField(Beer.class, "springId");
CouchbasePersistentProperty springIdProperty = getPropertyFor(springIdField);
test.addPersistentProperty(sdkIdProperty);
test.addPersistentProperty(springIdProperty);
assertEquals("sdkId", sdkIdProperty.getFieldName());
assertEquals("springId", springIdProperty.getFieldName());
assertTrue(sdkIdProperty.isIdProperty());
assertTrue(springIdProperty.isIdProperty());
assertEquals(springIdProperty, test.getIdProperty());
}
@Test
public void testAcceptsSdkIdAnnotation() {
BasicCouchbasePersistentEntity<SdkIdentified> test = new BasicCouchbasePersistentEntity<SdkIdentified>(
ClassTypeInformation.from(SdkIdentified.class));
Field id = ReflectionUtils.findField(SdkIdentified.class, "id");
CouchbasePersistentProperty idProperty = getPropertyFor(id);
test.addPersistentProperty(idProperty);
assertEquals(idProperty, test.getIdProperty());
}
@Test
public void testSdkIdAnnotationEvaluatedAfterSpringIdAnnotationIsIgnored() {
BasicCouchbasePersistentEntity<Beer> test = new BasicCouchbasePersistentEntity<Beer>(
ClassTypeInformation.from(Beer.class));
Field sdkIdField = ReflectionUtils.findField(Beer.class, "sdkId");
CouchbasePersistentProperty sdkIdProperty = getPropertyFor(sdkIdField);
Field springIdField = ReflectionUtils.findField(Beer.class, "springId");
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(springIdProperty);
assertEquals(springIdProperty, test.getIdProperty());
test.addPersistentProperty(sdkIdProperty);
assertEquals(springIdProperty, test.getIdProperty());
}
/**
* Helper method to create a property out of the field.
*
@@ -84,16 +136,29 @@ public class BasicCouchbasePersistentPropertyTests {
*/
public class Beer {
@Id
private String id;
@com.couchbase.client.java.repository.annotation.Id
private String sdkId;
@org.springframework.data.couchbase.core.mapping.Field("foobar")
@Id
private String springId;
@com.couchbase.client.java.repository.annotation.Field("foobar")
String name;
String description;
public String getId() {
return id;
return springId;
}
}
/**
* Simple POJO to test that a single ID property from the SDK is taken into account.
*/
public class SdkIdentified {
@com.couchbase.client.java.repository.annotation.Id
private String id;
String value;
}
}

View File

@@ -25,13 +25,14 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
import com.couchbase.client.java.repository.annotation.Field;
import com.couchbase.client.java.repository.annotation.Id;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.annotation.Id;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.couchbase.UnitTestApplicationConfig;
@@ -121,7 +122,7 @@ public class CustomConvertersTests {
}
public static class BlogPost {
@Id
@Id //also tests DATACOUCH-145 (this is SDK's @Id)
public String id = "key";
@Field

View File

@@ -483,6 +483,25 @@ public class MappingCouchbaseConverterTests {
assertEquals(deleted.toDate().getTime(), read.deleted.toDate().getTime());
}
@Test
public void shouldPrioritizeSpringIdOverSdkId() {
SpringIdentified entity = new SpringIdentified();
CouchbaseDocument converted = new CouchbaseDocument();
converter.write(entity, converted);
assertEquals("realId", converted.getId());
}
@Test
public void shouldIgnoreSdkIdIfSpringIdFound() {
AmbiguousIdentified entity = new AmbiguousIdentified();
CouchbaseDocument converted = new CouchbaseDocument();
converter.write(entity, converted);
assertEquals("springId", converted.getId());
}
static class EntityWithoutID {
private String attr0;
@@ -661,4 +680,21 @@ public class MappingCouchbaseConverterTests {
}
}
static class SdkIdentified {
@com.couchbase.client.java.repository.annotation.Id
public String id = "id";
}
static class SpringIdentified extends SdkIdentified {
@org.springframework.data.annotation.Id
public String realId = "realId";
}
static class AmbiguousIdentified {
@org.springframework.data.annotation.Id
public String springId = "springId";
@com.couchbase.client.java.repository.annotation.Id
public String sdkId = "sdkId";
}
}