[Olingo-1259]Function Import enhancements
authorArchana Rai <archana.rai@sap.com>
Tue, 4 Dec 2018 11:02:45 +0000 (16:32 +0530)
committerArchana Rai <archana.rai@sap.com>
Tue, 4 Dec 2018 11:02:45 +0000 (16:32 +0530)
odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/AtomEntityProvider.java
odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/JsonEntityProvider.java
odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonFunctionImportTest.java
odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/XmlFunctionImportTest.java
odata2-lib/odata-ref/src/main/java/org/apache/olingo/odata2/ref/edm/ScenarioEdmProvider.java
odata2-lib/odata-ref/src/main/java/org/apache/olingo/odata2/ref/processor/ScenarioDataSource.java
odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/mock/EdmMock.java

index 9c79747..201ffc7 100644 (file)
@@ -333,21 +333,24 @@ public class AtomEntityProvider implements ContentTypeBasedEntityProvider {
   public ODataResponse writeFunctionImport(final EdmFunctionImport functionImport, final Object data,
       final EntityProviderWriteProperties properties) throws EntityProviderException {
     try {
-      final EdmType type = functionImport.getReturnType().getType();
-      final boolean isCollection = functionImport.getReturnType().getMultiplicity() == EdmMultiplicity.MANY;
-
-      if (type.getKind() == EdmTypeKind.ENTITY) {
-        @SuppressWarnings("unchecked")
-        Map<String, Object> map = (Map<String, Object>) data;
-        return writeEntry(functionImport.getEntitySet(), map, properties);
-      }
-
-      final EntityPropertyInfo info = EntityInfoAggregator.create(functionImport);
-      if (isCollection) {
-        return writeCollection(info, (List<?>) data);
-      } else {
-        return writeSingleTypedElement(info, data);
-      }
+      if(functionImport.getReturnType() !=null){
+        final EdmType type = functionImport.getReturnType().getType();
+        final boolean isCollection = functionImport.getReturnType().getMultiplicity() == EdmMultiplicity.MANY;
+  
+        if (type.getKind() == EdmTypeKind.ENTITY) {
+          @SuppressWarnings("unchecked")
+          Map<String, Object> map = (Map<String, Object>) data;
+          return writeEntry(functionImport.getEntitySet(), map, properties);
+        }
+        final EntityPropertyInfo info = EntityInfoAggregator.create(functionImport);
+        if (isCollection) {
+          return writeCollection(info, (List<?>) data);
+        } else {
+          return writeSingleTypedElement(info, data);
+        }
+     }else{
+       return ODataResponse.newBuilder().status(HttpStatusCodes.ACCEPTED).build();
+     }
     } catch (EdmException e) {
       throw new EntityProviderProducerException(e.getMessageReference(), e);
     }
index 20a9f9b..fa94320 100644 (file)
@@ -286,17 +286,21 @@ public class JsonEntityProvider implements ContentTypeBasedEntityProvider {
   public ODataResponse writeFunctionImport(final EdmFunctionImport functionImport, final Object data,
       final EntityProviderWriteProperties properties) throws EntityProviderException {
     try {
-      if (functionImport.getReturnType().getType().getKind() == EdmTypeKind.ENTITY) {
-        @SuppressWarnings("unchecked")
-        Map<String, Object> map = (Map<String, Object>) data;
-        return writeEntry(functionImport.getEntitySet(), map, properties);
-      }
-
-      final EntityPropertyInfo info = EntityInfoAggregator.create(functionImport);
-      if (functionImport.getReturnType().getMultiplicity() == EdmMultiplicity.MANY) {
-        return writeCollection(info, (List<?>) data);
-      } else {
-        return writeSingleTypedElement(info, data);
+      if(functionImport.getReturnType() !=null){
+        if (functionImport.getReturnType().getType().getKind() == EdmTypeKind.ENTITY) {
+          @SuppressWarnings("unchecked")
+          Map<String, Object> map = (Map<String, Object>) data;
+          return writeEntry(functionImport.getEntitySet(), map, properties);
+        }
+  
+        final EntityPropertyInfo info = EntityInfoAggregator.create(functionImport);
+        if (functionImport.getReturnType().getMultiplicity() == EdmMultiplicity.MANY) {
+          return writeCollection(info, (List<?>) data);
+        } else {
+          return writeSingleTypedElement(info, data);
+        }
+      }else{
+        return ODataResponse.newBuilder().status(HttpStatusCodes.ACCEPTED).build();
       }
     } catch (final EdmException e) {
       throw new EntityProviderProducerException(e.getMessageReference(), e);
index 69177f1..f211abd 100644 (file)
@@ -30,6 +30,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
 import org.apache.olingo.odata2.api.edm.EdmFunctionImport;
 import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties;
 import org.apache.olingo.odata2.api.processor.ODataResponse;
@@ -161,4 +162,24 @@ public class JsonFunctionImportTest extends BaseTest {
         + "\"ne_Room\":{\"__deferred\":{\"uri\":\"" + uri + "Employees('1')/ne_Room\"}}}}",
         json);
   }
+  
+  
+  @Test
+  public void noReturnTypeAction() throws Exception {
+    final EdmFunctionImport functionImport =
+        MockFacade.getMockEdm().getDefaultEntityContainer().getFunctionImport("AddEmployee");
+    final String uri = "http://host:80/service/";
+    final EntityProviderWriteProperties properties =
+        EntityProviderWriteProperties.serviceRoot(URI.create(uri)).build();
+    Map<String, Object> employeeData = new HashMap<String, Object>();
+    employeeData.put("EmployeeId", "1");
+    employeeData.put("getImageType", "image/jpeg");
+    final ODataResponse response =
+        new JsonEntityProvider().writeFunctionImport(functionImport, employeeData, properties);
+    assertNotNull(response);
+    assertNull(response.getEntity());
+    assertNull(response.getContentHeader());
+    assertEquals(HttpStatusCodes.ACCEPTED, response.getStatus());
+   
+  }
 }
index de71435..eb248a0 100644 (file)
@@ -20,12 +20,14 @@ package org.apache.olingo.odata2.core.ep.producer;
 
 import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo;
 import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 
 import java.io.InputStream;
 import java.util.Arrays;
 
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
 import org.apache.olingo.odata2.api.edm.EdmFunctionImport;
 import org.apache.olingo.odata2.api.processor.ODataResponse;
 import org.apache.olingo.odata2.core.ep.AbstractProviderTest;
@@ -140,4 +142,18 @@ public class XmlFunctionImportTest extends AbstractProviderTest {
     assertXpathExists("/a:entry/a:link[@href=\"Employees('1')/$value\"]", xml);
     assertXpathEvaluatesTo("Duckburg", "/a:entry/m:properties/d:Location/d:City/d:CityName/text()", xml);
   }
+  
+  @Test
+  public void noReturnTypeAction() throws Exception {
+    final EdmFunctionImport functionImport =
+        MockFacade.getMockEdm().getDefaultEntityContainer().getFunctionImport("AddEmployee");
+
+    final ODataResponse response =
+        createAtomEntityProvider().writeFunctionImport(functionImport, employeeData, DEFAULT_PROPERTIES);
+    assertNotNull(response);
+    assertNull(response.getEntity());
+    assertNull(response.getContentHeader());
+    assertEquals(HttpStatusCodes.ACCEPTED, response.getStatus());
+   
+  }
 }
index 91a8afe..38693db 100644 (file)
@@ -102,6 +102,7 @@ public class ScenarioEdmProvider extends EdmProvider {
   private static final String FUNCTION_IMPORT_5 = "MostCommonLocation";
   private static final String FUNCTION_IMPORT_6 = "ManagerPhoto";
   private static final String FUNCTION_IMPORT_7 = "OldestEmployee";
+  private static final String ACTION_IMPORT_1   = "AddEmployee";
 
   @Override
   public List<Schema> getSchemas() throws ODataException {
@@ -157,6 +158,7 @@ public class ScenarioEdmProvider extends EdmProvider {
     functionImports.add(getFunctionImport(ENTITY_CONTAINER_1, FUNCTION_IMPORT_5));
     functionImports.add(getFunctionImport(ENTITY_CONTAINER_1, FUNCTION_IMPORT_6));
     functionImports.add(getFunctionImport(ENTITY_CONTAINER_1, FUNCTION_IMPORT_7));
+    functionImports.add(getFunctionImport(ENTITY_CONTAINER_1, ACTION_IMPORT_1));
     entityContainer.setFunctionImports(functionImports);
 
     schema.setEntityContainers(Arrays.asList(entityContainer));
@@ -472,6 +474,12 @@ public class ScenarioEdmProvider extends EdmProvider {
             .setReturnType(new ReturnType().setTypeName(ENTITY_TYPE_1_1).setMultiplicity(EdmMultiplicity.ZERO_TO_ONE))
             .setEntitySet(ENTITY_SET_1_1)
             .setHttpMethod("GET");
+      } else if (ACTION_IMPORT_1.equals(name)) {
+        return new FunctionImport().setName(name)
+            .setEntitySet(ENTITY_SET_1_1)
+            .setHttpMethod("POST")/*.setParameters(Arrays.asList(
+                new FunctionImportParameter().setName("Id").setType(EdmSimpleTypeKind.String)
+                .setFacets(new Facets().setNullable(false))))*/;
       }
     }
 
index ff8813a..2db59b7 100644 (file)
@@ -266,7 +266,10 @@ public class ScenarioDataSource {
       }
       throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
 
-    } else if (function.getName().equals("OldestEmployee")) {
+    } else if (function.getName().equals("AddEmployee")) {
+        return null;
+
+    }  else if (function.getName().equals("OldestEmployee")) {
       return getOldestEmployee();
 
     } else {
index de1506e..d069b29 100644 (file)
@@ -247,6 +247,13 @@ class EdmMock {
     when(employeeActionImport.getEntitySet()).thenReturn(employeeEntitySet);
     when(employeeActionImport.getHttpMethod()).thenReturn(ODataHttpMethod.POST.name());
 
+    //Issue no return type
+    EdmFunctionImport addEmployeeActionImport =
+        createActionImportMock(defaultContainer, "AddEmployee", null, EdmMultiplicity.ONE);
+    when(addEmployeeActionImport.getEntitySet()).thenReturn(employeeEntitySet);
+    when(addEmployeeActionImport.getHttpMethod()).thenReturn(ODataHttpMethod.POST.name());
+    
+    
     // Issue with not explicitly nullable parameters and facets
     EdmFunctionImport functionImportNullableParameter =
         createFunctionImportMock(defaultContainer, "FINullableParameter", EdmSimpleTypeKind.Boolean
@@ -433,4 +440,19 @@ class EdmMock {
 
     return functionImport;
   }
+  
+  
+  
+  private static EdmFunctionImport createActionImportMock(final EdmEntityContainer container, final String name,
+      final EdmType type, final EdmMultiplicity multiplicity) throws EdmException {
+
+    EdmFunctionImport functionImport = mock(EdmFunctionImport.class);
+    when(functionImport.getName()).thenReturn(name);
+    when(functionImport.getReturnType()).thenReturn(null);
+    when(functionImport.getHttpMethod()).thenReturn(ODataHttpMethod.GET.name());
+
+    when(container.getFunctionImport(name)).thenReturn(functionImport);
+
+    return functionImport;
+  }
 }