[OLINGO-1304]ODATA V2.0 :Client serialization fails for keyAutoGeneration of type...
authorArchana Rai <archana.rai@sap.com>
Wed, 3 Oct 2018 11:17:48 +0000 (16:47 +0530)
committerArchana Rai <archana.rai@sap.com>
Wed, 3 Oct 2018 11:17:48 +0000 (16:47 +0530)
odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/ep/serializer/AtomEntryEntitySerializer.java
odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/serializer/AtomEntrySerializerTest.java

index dddd058..50fe56f 100644 (file)
@@ -603,7 +603,7 @@ public class AtomEntryEntitySerializer {
       } else if (edmType == BigDecimal.class) {
         return BigDecimal.valueOf(0.0);
       } else if (edmType == UUID.class) {
-        return UUID.fromString("0");
+        return UUID.fromString("00000000-0000-0000-0000-000000000000");
       } else if (edmType == Timestamp.class) {
         return new Timestamp(Calendar.getInstance().getTimeInMillis());
       } else if (edmType == Calendar.class) {
index f384bbc..4ae9985 100644 (file)
@@ -34,6 +34,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Calendar;
 import java.util.HashMap;
 import java.util.List;
@@ -47,12 +48,17 @@ import javax.xml.stream.XMLStreamException;
 import org.apache.olingo.odata2.api.edm.Edm;
 import org.apache.olingo.odata2.api.edm.EdmConcurrencyMode;
 import org.apache.olingo.odata2.api.edm.EdmCustomizableFeedMappings;
+import org.apache.olingo.odata2.api.edm.EdmEntityContainer;
 import org.apache.olingo.odata2.api.edm.EdmEntitySet;
 import org.apache.olingo.odata2.api.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.edm.EdmException;
 import org.apache.olingo.odata2.api.edm.EdmFacets;
 import org.apache.olingo.odata2.api.edm.EdmMapping;
 import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
+import org.apache.olingo.odata2.api.edm.EdmStructuralType;
 import org.apache.olingo.odata2.api.edm.EdmTargetPath;
+import org.apache.olingo.odata2.api.edm.EdmTypeKind;
 import org.apache.olingo.odata2.api.edm.EdmTyped;
 import org.apache.olingo.odata2.api.ep.EntityProviderException;
 import org.apache.olingo.odata2.api.exception.ODataException;
@@ -355,6 +361,72 @@ public class AtomEntrySerializerTest extends AbstractProviderTest {
   }
 
   
+  private static EdmEntityType createEntityTypeMock(final String name, final EdmSimpleTypeKind kind,
+      final String keyPropertyId) throws EdmException {
+    EdmEntityType entityType = mock(EdmEntityType.class);
+    when(entityType.getName()).thenReturn(name);
+    when(entityType.getNamespace()).thenReturn("RefScenario");
+
+    final EdmProperty keyProperty = createProperty(keyPropertyId, kind, entityType);
+    EdmFacets facets = mock(EdmFacets.class);
+    when(facets.getMaxLength()).thenReturn(null);
+    when(facets.isNullable()).thenReturn(false);
+    when(keyProperty.getFacets()).thenReturn(facets);
+
+    when(entityType.getKind()).thenReturn(EdmTypeKind.ENTITY);
+    when(entityType.getPropertyNames()).thenReturn(Arrays.asList(keyPropertyId));
+    when(entityType.getKeyPropertyNames()).thenReturn(Arrays.asList(keyPropertyId));
+    when(entityType.getKeyProperties()).thenReturn(Arrays.asList(keyProperty));
+
+    return entityType;
+  }  
+  
+  private static EdmProperty createProperty(final String name, final EdmSimpleTypeKind kind,
+      final EdmStructuralType entityType) throws EdmException {
+    EdmProperty property = mock(EdmProperty.class);
+    when(property.getType()).thenReturn(kind.getEdmSimpleTypeInstance());
+    when(property.getName()).thenReturn(name);
+    when(entityType.getProperty(name)).thenReturn(property);
+    return property;
+  }
+
+  private static EdmEntitySet createEntitySetMock(final EdmEntityContainer container, final String name,
+      final EdmSimpleTypeKind kind, final String keyPropertyId) throws EdmException {
+    final EdmEntityType entityType = createEntityTypeMock(name.substring(0, name.length() - 1), kind, keyPropertyId);
+
+    EdmEntitySet entitySet = mock(EdmEntitySet.class);
+    when(entitySet.getName()).thenReturn(name);
+    when(entitySet.getEntityType()).thenReturn(entityType);
+
+    when(entitySet.getEntityContainer()).thenReturn(container);
+
+    when(container.getEntitySet(name)).thenReturn(entitySet);
+
+    return entitySet;
+  }
+  
+  @Test
+  public void idGuidWithoutKey() throws Exception {
+    EdmEntityContainer defaultContainer = mock(EdmEntityContainer.class);
+    when(defaultContainer.isDefaultEntityContainer()).thenReturn(true);
+    final EdmEntitySet entitySet =
+        createEntitySetMock(defaultContainer, "Employer", EdmSimpleTypeKind.Guid, "EmployerId");
+   
+    final EntitySerializerProperties properties =
+        EntitySerializerProperties.serviceRoot(BASE_URI).isKeyAutoGenerated(true)
+        .includeMetadata(true)
+            .build();
+
+    Entity localEmployeeData = new Entity();
+    localEmployeeData.setWriteProperties(properties);
+
+    AtomSerializerDeserializer ser = createAtomEntityProvider();
+    ODataResponse response =
+        ser.writeEntry(entitySet, localEmployeeData);
+    String xmlString = verifyResponse(response);
+    assertXpathExists("/a:entry/a:link[@href=\"Employer(guid'00000000-0000-0000-0000-000000000000')\"]", xmlString);
+  }
+  
   @Test
   public void contentOnlyWithNavigationLink() throws Exception {
     EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");