Repository: olingo-odata2 Updated Branches: refs/heads/master cd4810b9b -> 798f898ed
[OLINGO-1014] Preserve existing key values of an entity with multiple keys when saving to in-memory data store. Signed-off-by: mibo <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/798f898e Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/798f898e Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/798f898e Branch: refs/heads/master Commit: 798f898ed588bc2d57a48febd19c7e1cb5ab29f8 Parents: cd4810b Author: Michael Strasser <[email protected]> Authored: Sun Aug 28 21:23:06 2016 +1000 Committer: mibo <[email protected]> Committed: Sun Aug 28 21:06:53 2016 +0200 ---------------------------------------------------------------------- .../processor/core/datasource/DataStore.java | 22 ++++++--- .../datasource/AnnotationsInMemoryDsTest.java | 52 ++++++++++++++++++++ 2 files changed, 67 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/798f898e/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStore.java ---------------------------------------------------------------------- diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStore.java b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStore.java index 98f86f1..6e7bd13 100644 --- a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStore.java +++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/datasource/DataStore.java @@ -113,10 +113,15 @@ public class DataStore<T> { return create(object, keyElement); } + /** + * Store an entity, preserving any existing keys if possible. If the combination of + * existing and generated keys would produce a duplicate entry, replace all keys. + */ private T create(final T object, final KeyElement keyElement) throws DataStoreException { synchronized (dataStore) { - if (keyElement.keyValuesMissing() || dataStore.containsKey(keyElement)) { - KeyElement newKey = createSetAndGetKeys(object); + final boolean replaceKeys = dataStore.containsKey(keyElement); + if (keyElement.keyValuesMissing() || replaceKeys) { + KeyElement newKey = createSetAndGetKeys(object, replaceKeys); return this.create(object, newKey); } dataStore.put(keyElement, object); @@ -244,11 +249,14 @@ public class DataStore<T> { return keyElement; } - KeyElement createSetAndGetKeys(final T object) throws DataStoreException { + KeyElement createSetAndGetKeys(final T object, boolean replaceKeys) throws DataStoreException { KeyElement keyElement = new KeyElement(keyFields.size()); for (Field field : keyFields) { - Object key = createKey(field); - ClassHelper.setFieldValue(object, field, key); + Object key = ClassHelper.getFieldValue(object, field); + if (key == null || replaceKeys) { + key = createKey(field); + ClassHelper.setFieldValue(object, field, key); + } keyElement.addValue(key); } @@ -277,8 +285,8 @@ public class DataStore<T> { return keyAccess.getKeyValues(object); } - private KeyElement createSetAndGetKeys(final T object) throws DataStoreException { - return keyAccess.createSetAndGetKeys(object); + private KeyElement createSetAndGetKeys(final T object, boolean replaceKeys) throws DataStoreException { + return keyAccess.createSetAndGetKeys(object, replaceKeys); } public static class DataStoreException extends ODataApplicationException { http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/798f898e/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java ---------------------------------------------------------------------- diff --git a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java index 089b6d4..9abd18b 100644 --- a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java +++ b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java @@ -688,6 +688,58 @@ public class AnnotationsInMemoryDsTest { } @Test + public void createTwoKeyEntityWithOneKeyAlreadySet() throws Exception { + EdmEntitySet edmEntitySet = createMockedEdmEntitySet("Photos"); + + final String typeKeyValue = "PNG"; + final String automaticNameKeyValue = "1"; + + Photo photo = new Photo(); + photo.setType(typeKeyValue); + photo.setImageUri("https://localhost/big_picture.png"); + photo.setImageType("image/png"); + datasource.createData(edmEntitySet, photo); + + List photos = datasource.readData(edmEntitySet); + Photo readPhoto = (Photo) photos.get(0); + Assert.assertEquals(automaticNameKeyValue + ":" + typeKeyValue, + readPhoto.getName() + ":" + readPhoto.getType()); + } + + @Test + public void ensureTwoKeyEntityKeysAreUnique() throws Exception { + EdmEntitySet edmEntitySet = createMockedEdmEntitySet("Photos"); + + final String nameKeyValue = "Big Picture"; + final String typeKeyValue = "PNG"; + + Photo photo1 = new Photo(); + photo1.setName(nameKeyValue); + photo1.setType(typeKeyValue); + photo1.setImageUri("https://localhost/big_picture.png"); + photo1.setImageType("image/png"); + datasource.createData(edmEntitySet, photo1); + + Photo photo2 = new Photo(); + photo2.setName(nameKeyValue); + photo2.setType(typeKeyValue); + photo2.setImageUri("https://localhost/bigger_picture.png"); + photo2.setImageType("image/png"); + datasource.createData(edmEntitySet, photo2); + + List photos = datasource.readData(edmEntitySet); + + Assert.assertEquals(2, photos.size()); + Photo readPhoto = (Photo) photos.get(0); + Assert.assertEquals(nameKeyValue + ":" + typeKeyValue, + readPhoto.getName() + ":" + readPhoto.getType()); + + readPhoto = (Photo) photos.get(1); + Assert.assertEquals("1:2", + readPhoto.getName() + ":" + readPhoto.getType()); + } + + @Test public void createGuidKeyEntity() throws Exception { EdmEntitySet edmEntitySet = createMockedEdmEntitySet(GuidKeyEntity.GUID_KEY_ENTITIES);
