Skip to content
Permalink
Browse files

[OLINGO-1155]Bug Fixes for delta support in Json format

  • Loading branch information...
ramya vasanth
ramya vasanth committed Sep 9, 2019
1 parent 510b213 commit 21d874d255e14af6837b8530036e06207c8321ba
@@ -38,6 +38,7 @@
import org.apache.olingo.commons.api.data.Linked;
import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.edm.EdmComplexType;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.commons.api.edm.EdmEntityType;
import org.apache.olingo.commons.api.edm.EdmNavigationProperty;
import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
@@ -247,9 +248,10 @@ public void writeAddedUpdatedEntity(final ServiceMetadata metadata, final EdmEnt
}
}
}
json.writeStringField(Constants.AT + Constants.ATOM_ATTR_ID, getEntityId(entity, entityType, name));
String id = getEntityId(entity, entityType, name);
json.writeStringField(Constants.AT + Constants.ATOM_ATTR_ID, id);
writeProperties(metadata, entityType, entity.getProperties(), select, json);
writeNavigationProperties(metadata, entityType, entity, expand, name, json, isFullRepresentation);
writeNavigationProperties(metadata, entityType, entity, expand, id, json, isFullRepresentation);
json.writeEndObject();

}
@@ -521,13 +523,17 @@ protected void writeNavigationProperties(final ServiceMetadata metadata,
SerializerException.MessageKeys.NOT_IMPLEMENTED);
}
if (navigationLink != null) {
EdmNavigationProperty navProperty = type.getNavigationProperty(propertyName);
String navEntitySetName = getNavigatedEntitySetName(metadata, navProperty.getType()
.getFullQualifiedName().getFullQualifiedNameAsString());
writeExpandedNavigationProperty(metadata, property, navigationLink,
innerOptions == null ? null : innerOptions.getExpandOption(),
innerOptions == null ? null : innerOptions.getSelectOption(),
innerOptions == null ? null : innerOptions.getCountOption(),
innerOptions == null ? false : innerOptions.hasCountPath(),
innerOptions == null ? false : innerOptions.isRef(),
name, json, isFullRepresentation);
navEntitySetName != null ? navEntitySetName : name + "/" + property.getName(),
json, isFullRepresentation);
} else {
json.writeFieldName(property.getName());
if (property.isCollection()) {
@@ -542,6 +548,23 @@ protected void writeNavigationProperties(final ServiceMetadata metadata,
}
}

/**
* Fetch the entity set name which has to be shown in @Id annotation
* @param metadata
* @param fullQualifiedName
* @return
*/
private String getNavigatedEntitySetName(ServiceMetadata metadata, String fullQualifiedName) {
List<EdmEntitySet> entitySets = metadata.getEdm().getEntityContainer().getEntitySets();
for (EdmEntitySet entitySet : entitySets) {
if (entitySet.getEntityType().getFullQualifiedName()
.getFullQualifiedNameAsString().equals(fullQualifiedName)) {
return entitySet.getName();
}
}
return null;
}

protected void writeEntitySet(final ServiceMetadata metadata, final EdmEntityType entityType,
final AbstractEntityCollection entitySet, final ExpandOption expand, final SelectOption select,
final boolean onlyReference, String name, final JsonGenerator json,
@@ -1798,6 +1798,45 @@ private void linkESDelta(final Map<String, EntityCollection> data) {
setLink(entityCollection.getEntities().get(3), "NavPropertyETAllPrimOne", targetEntities.get(0));
setLinkForDelta(entityCollection.getEntities().get(1), "NavPropertyETAllPrimOne");
setLinksForDelta(entityCollection.getEntities().get(2), "NavPropertyETAllPrimMany");
final List<Entity> targetEntities1 = data.get("ESTwoBase").getEntities();
setLinkWithoutEntityID(entityCollection.getEntities().get(0),
"NavPropertyETBaseContTwoContOne", targetEntities1.get(0));
setLinksWithoutEntityID(entityCollection.getEntities().get(1), "NavPropertyETBaseContTwoContMany",
targetEntities1.get(1), targetEntities1.get(2));
}

private void setLinksWithoutEntityID(Entity entity, String navigationPropertyName, final Entity... targets) {
Link link = entity.getNavigationLink(navigationPropertyName);
for (Entity target : targets) {
target.setId(null);
}
if (link == null) {
link = new Link();
link.setRel(Constants.NS_NAVIGATION_LINK_REL + navigationPropertyName);
link.setType(Constants.ENTITY_SET_NAVIGATION_LINK_TYPE);
link.setTitle(navigationPropertyName);
EntityCollection target = new EntityCollection();
target.getEntities().addAll(Arrays.asList(targets));
link.setInlineEntitySet(target);
link.setHref(entity.getId().toASCIIString() + "/" + navigationPropertyName);
entity.getNavigationLinks().add(link);
} else {
link.getInlineEntitySet().getEntities().addAll(Arrays.asList(targets));
}
}

private void setLinkWithoutEntityID(Entity entity, String navigationPropertyName, Entity target) {
Link link = entity.getNavigationLink(navigationPropertyName);
target.setId(null);
if (link == null) {
link = new Link();
link.setRel(Constants.NS_NAVIGATION_LINK_REL + navigationPropertyName);
link.setType(Constants.ENTITY_NAVIGATION_LINK_TYPE);
link.setTitle(navigationPropertyName);
link.setHref(target.getId() != null ? target.getId().toASCIIString() : null);
entity.getNavigationLinks().add(link);
}
link.setInlineEntity(target);
}

protected static void setLinkForDelta(final Entity entity, final String navigationPropertyName) {
@@ -696,14 +696,20 @@ public CsdlEntitySet getEntitySet(final FullQualifiedName entityContainer, final
} else if (name.equals("ESDelta")) {
return new CsdlEntitySet()
.setName("ESDelta")
.setType(EntityTypeProvider.nameETTwoPrim)
.setType(EntityTypeProvider.nameETDelta)
.setNavigationPropertyBindings(Arrays.asList(
new CsdlNavigationPropertyBinding()
.setPath("NavPropertyETAllPrimOne")
.setTarget("ESAllPrim"),
new CsdlNavigationPropertyBinding()
.setPath("NavPropertyETAllPrimMany")
.setTarget("ESAllPrim")));
.setTarget("ESAllPrim"),
new CsdlNavigationPropertyBinding()
.setPath("NavPropertyETBaseContOne")
.setTarget("ESTwoBase"),
new CsdlNavigationPropertyBinding()
.setPath("NavPropertyETBaseContMany")
.setTarget("ESTwoBase")));

} else if (name.equals("ESStreamOnComplexProp")) {
return new CsdlEntitySet()
@@ -521,11 +521,10 @@ public CsdlEntityType getEntityType(final FullQualifiedName entityTypeName) thro
return new CsdlEntityType()
.setName("ETDelta")
.setBaseType(nameETTwoPrim)
.setProperties(Arrays.asList(
PropertyProvider.propertyInt16_NotNullable, PropertyProvider.propertyString))
.setNavigationProperties(
Arrays.asList(PropertyProvider.navPropertyETAllPrimOne_ETAllPrim,
PropertyProvider.collectionNavPropertyETAllPrimMany_ETAllPrim));
Arrays.asList(
PropertyProvider.navPropertyETBaseCont_ETTwoCont,
PropertyProvider.collectionNavPropertyETBaseContMany_ETTwoCont));
} else if (entityTypeName.equals(nameETCont)) {
return new CsdlEntityType()
.setName("ETCont").setBaseType(nameETBaseCont)
@@ -1183,4 +1183,71 @@ public void nullnavigationToManyInDeltaEntityInFullRepresentation() throws Excep
Assert.assertNotNull(jsonString);
Assert.assertEquals(expectedResult, jsonString);
}

@Test
public void testIdAnnotationWithNavigationOneInDelta() throws Exception {
final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
Delta delta = new Delta();
final Entity entity = data.readAll(edmEntitySet).getEntities().get(0);
final Entity entity2 = data.readAll(edmEntitySet).getEntities().get(1);
final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(
ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETBaseContTwoContOne")));
List<Entity> addedEntity = new ArrayList<Entity>();
Entity changedEntity = new Entity();
changedEntity.setId(entity2.getId());
changedEntity.addProperty(entity2.getProperty("PropertyString"));
addedEntity.add(entity);
addedEntity.add(changedEntity);
delta.getEntities().addAll(addedEntity);
InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
EntityCollectionSerializerOptions.with()
.contextURL(ContextURL.with().entitySet(edmEntitySet).build()).expand(expand)
.build()).getContent();
String jsonString = IOUtils.toString(stream);
final String expectedResult = "{\"@context\":\"$metadata#ESDelta/$delta\","
+ "\"value\":[{\"@id\":\"ESDelta(32767)\",\"PropertyInt16\":32767,"
+ "\"PropertyString\":\"Number:32767\","
+ "\"NavPropertyETBaseContTwoContOne@delta\":"
+ "{\"@id\":\"ESDelta(32767)/NavPropertyETBaseContTwoContOne"
+ "(PropertyInt16=111,PropertyString='TEST%20A')\",\"PropertyInt16\":111,"
+ "\"PropertyString\":\"TEST A\"}},{\"@id\":\"ESDelta(-32768)\","
+ "\"PropertyString\":\"Number:-32768\",\"NavPropertyETBaseContTwoContOne\":null}]}";
Assert.assertNotNull(jsonString);
Assert.assertEquals(expectedResult, jsonString);
}

@Test
public void testIdAnnotationWithNavigationManyInDelta() throws Exception {
final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
Delta delta = new Delta();
final Entity entity = data.readAll(edmEntitySet).getEntities().get(1);
final Entity entity2 = data.readAll(edmEntitySet).getEntities().get(2);
final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(
ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETBaseContTwoContMany")));
List<Entity> addedEntity = new ArrayList<Entity>();
Entity changedEntity = new Entity();
changedEntity.setId(entity2.getId());
changedEntity.addProperty(entity2.getProperty("PropertyString"));
addedEntity.add(entity);
addedEntity.add(changedEntity);
delta.getEntities().addAll(addedEntity);
InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
EntityCollectionSerializerOptions.with()
.contextURL(ContextURL.with().entitySet(edmEntitySet).build()).expand(expand)
.build()).getContent();
String jsonString = IOUtils.toString(stream);
final String expectedResult = "{\"@context\":\"$metadata#ESDelta/$delta\","
+ "\"value\":[{\"@id\":\"ESDelta(-32768)\",\"PropertyInt16\":-32768,"
+ "\"PropertyString\":\"Number:-32768\","
+ "\"NavPropertyETBaseContTwoContMany@delta\":"
+ "[{\"@id\":\"ESDelta(-32768)/NavPropertyETBaseContTwoContMany"
+ "(PropertyInt16=222,PropertyString='TEST%20B')\","
+ "\"PropertyInt16\":222,\"PropertyString\":\"TEST B\"},"
+ "{\"@id\":\"ESDelta(-32768)/NavPropertyETBaseContTwoContMany"
+ "(PropertyInt16=333,PropertyString='TEST%20C')\",\"PropertyInt16\":333,"
+ "\"PropertyString\":\"TEST C\"}]},{\"@id\":\"ESDelta(0)\","
+ "\"PropertyString\":\"Number:0\",\"NavPropertyETBaseContTwoContMany\":[]}]}";
Assert.assertNotNull(jsonString);
Assert.assertEquals(expectedResult, jsonString);
}
}

0 comments on commit 21d874d

Please sign in to comment.
You can’t perform that action at this time.