Repository: olingo-odata4
Updated Branches:
  refs/heads/master 59e53d6bc -> b9a71ff88


[OLINGO-1004] Fit refactoring

Added basic authentication to embedded tomcat in fit module. Refactored 
AuthBatchTestITCase.

Signed-off-by: Christian Amend <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/b6871904
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/b6871904
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/b6871904

Branch: refs/heads/master
Commit: b6871904ac1e59fa30abcbc789bdbb4c172616e2
Parents: 59e53d6
Author: Morten Riedel <[email protected]>
Authored: Tue Sep 27 15:07:48 2016 +0200
Committer: Christian Amend <[email protected]>
Committed: Thu Sep 29 14:14:43 2016 +0200

----------------------------------------------------------------------
 .../olingo/fit/server/TomcatTestServer.java     |  41 ++++++-
 fit/src/main/resources/tomcat-users.xml         |   7 +-
 fit/src/main/resources/web.xml                  |  49 ++++++++
 .../olingo/fit/AbstractBaseTestITCase.java      |   5 +-
 .../olingo/fit/base/AuthBatchTestITCase.java    | 119 -------------------
 .../apache/olingo/fit/tecsvc/TecSvcConst.java   |   1 +
 .../fit/tecsvc/client/AbstractTecSvcITCase.java |  12 ++
 .../fit/tecsvc/client/AuthBatchTestITCase.java  | 103 ++++++++++++++++
 8 files changed, 208 insertions(+), 129 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b6871904/fit/src/main/java/org/apache/olingo/fit/server/TomcatTestServer.java
----------------------------------------------------------------------
diff --git 
a/fit/src/main/java/org/apache/olingo/fit/server/TomcatTestServer.java 
b/fit/src/main/java/org/apache/olingo/fit/server/TomcatTestServer.java
index eb79dd8..7d1ed46 100644
--- a/fit/src/main/java/org/apache/olingo/fit/server/TomcatTestServer.java
+++ b/fit/src/main/java/org/apache/olingo/fit/server/TomcatTestServer.java
@@ -48,6 +48,7 @@ import org.apache.catalina.Context;
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.LifecycleState;
 import org.apache.catalina.loader.WebappLoader;
+import org.apache.catalina.realm.MemoryRealm;
 import org.apache.catalina.startup.Tomcat;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
@@ -156,17 +157,23 @@ public class TomcatTestServer {
 
     private final Tomcat tomcat;
     private final File baseDir;
+    private final File resourceDir;
     private TomcatTestServer server;
     private Properties properties;
 
     private TestServerBuilder(final int fixedPort) {
       initializeProperties();
       // baseDir = new File(System.getProperty("java.io.tmpdir"), 
"tomcat-test");
-      baseDir = getFileForDirProperty(TOMCAT_BASE_DIR);
+      baseDir = getFileForDirProperty(TOMCAT_BASE_DIR);      
       if (!baseDir.exists() && !baseDir.mkdirs()) {
         throw new RuntimeException("Unable to create temporary test directory 
at {" + baseDir.getAbsolutePath() + "}");
       }
-      //
+      resourceDir = getFileForDirProperty(PROJECT_RESOURCES_DIR);
+      if(!resourceDir.exists()){
+          throw new RuntimeException("Unable to load resources");
+      }
+
+      final String TOMCAT_USERS_XML = "tomcat-users.xml";    
       tomcat = new Tomcat();
       tomcat.setBaseDir(baseDir.getParentFile().getAbsolutePath());
       tomcat.setPort(fixedPort);
@@ -174,8 +181,12 @@ public class TomcatTestServer {
       tomcat.getHost().setDeployOnStartup(true);
       tomcat.getConnector().setSecure(false);
       tomcat.setSilent(true);
-      tomcat.addUser("odatajclient", "odatajclient");
-      tomcat.addRole("odatajclient", "odatajclient");
+      // tomcat.addUser("odatajclient", "odatajclient");
+      // tomcat.addRole("odatajclient", "odatajclient");
+      String tomcatUserPath = resourceDir.getPath() + File.separator + 
TOMCAT_USERS_XML;
+      MemoryRealm realm = new MemoryRealm();
+      realm.setPathname(tomcatUserPath);
+      tomcat.getEngine().setRealm(realm);
     }
 
     private void initializeProperties() {
@@ -253,7 +264,7 @@ public class TomcatTestServer {
     }
 
     public TestServerBuilder addServlet(final Class<? extends HttpServlet> 
factoryClass, final String path)
-        throws InstantiationException, IllegalAccessException, 
ClassNotFoundException {
+        throws InstantiationException, IllegalAccessException, 
ClassNotFoundException, IOException {
       if (server != null) {
         return this;
       }
@@ -263,11 +274,29 @@ public class TomcatTestServer {
       String randomServletId = UUID.randomUUID().toString();
       Tomcat.addServlet(cxt, randomServletId, httpServlet);
       cxt.addServletMapping(path, randomServletId);
-      //
       LOG.info("Added servlet {} at context {} (mapping id={}).", 
servletClassname, path, randomServletId);
       return this;
     }
 
+    public TestServerBuilder addAuthServlet(final Class<? extends HttpServlet> 
factoryClass, 
+            final String servletPath, final String contextPath)
+        throws InstantiationException, IllegalAccessException, 
ClassNotFoundException, IOException, ServletException {
+      if (server != null) {
+        return this;
+      }
+      final String TOMCAT_WEB_XML = "web.xml";
+      String webxmluri = resourceDir.getPath() + File.separator + 
TOMCAT_WEB_XML;      
+      String servletClassname = factoryClass.getName();
+      HttpServlet httpServlet = (HttpServlet) 
Class.forName(servletClassname).newInstance();
+      Context cxt = tomcat.addWebapp(servletPath, baseDir.getAbsolutePath());
+      cxt.setAltDDName(webxmluri);
+      String randomServletId = UUID.randomUUID().toString();
+      Tomcat.addServlet(cxt, randomServletId, httpServlet);
+      cxt.addServletMapping(contextPath, randomServletId); 
+
+      return this;
+    }
+
     public TestServerBuilder addStaticContent(final String uri, final String 
resourceName) throws IOException {
       File targetResourcesDir = getFileForDirProperty(PROJECT_RESOURCES_DIR);
       String resource = new File(targetResourcesDir, 
resourceName).getAbsolutePath();

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b6871904/fit/src/main/resources/tomcat-users.xml
----------------------------------------------------------------------
diff --git a/fit/src/main/resources/tomcat-users.xml 
b/fit/src/main/resources/tomcat-users.xml
index 26e0d3e..d70e63c 100644
--- a/fit/src/main/resources/tomcat-users.xml
+++ b/fit/src/main/resources/tomcat-users.xml
@@ -20,8 +20,9 @@
 
 -->
 <tomcat-users>
-  <role rolename="manager-gui"/>
-  <user name="admin" password="" roles="manager-gui"/>
+ <role rolename="manager-gui"/>
+ <role rolename="odatajclient"/>
 
-  <user name="odatajclient" password="odatajclient" roles="odatajclient"/>
+ <user name="admin" password="" roles="manager-gui"/>
+ <user name="odatajclient" password="odatajclient" roles="odatajclient"/>
 </tomcat-users>

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b6871904/fit/src/main/resources/web.xml
----------------------------------------------------------------------
diff --git a/fit/src/main/resources/web.xml b/fit/src/main/resources/web.xml
new file mode 100644
index 0000000..48b84c5
--- /dev/null
+++ b/fit/src/main/resources/web.xml
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+<web-app xmlns="http://java.sun.com/xml/ns/javaee";
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd";
+  version="3.0">  
+
+ <security-constraint>
+    <web-resource-collection>
+      <web-resource-name>Auth</web-resource-name>
+      <url-pattern>/*</url-pattern>
+    </web-resource-collection>
+    <auth-constraint>
+      <role-name>odatajclient</role-name>
+    </auth-constraint>
+    <user-data-constraint>
+      <!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->
+      <transport-guarantee>NONE</transport-guarantee>
+    </user-data-constraint>
+  </security-constraint>
+
+  <login-config>
+    <auth-method>BASIC</auth-method>
+  </login-config>
+
+  <security-role>
+    <role-name>odatajclient</role-name>
+  </security-role>
+ </web-app>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b6871904/fit/src/test/java/org/apache/olingo/fit/AbstractBaseTestITCase.java
----------------------------------------------------------------------
diff --git 
a/fit/src/test/java/org/apache/olingo/fit/AbstractBaseTestITCase.java 
b/fit/src/test/java/org/apache/olingo/fit/AbstractBaseTestITCase.java
index a14484a..5ac6133 100644
--- a/fit/src/test/java/org/apache/olingo/fit/AbstractBaseTestITCase.java
+++ b/fit/src/test/java/org/apache/olingo/fit/AbstractBaseTestITCase.java
@@ -42,10 +42,13 @@ public abstract class AbstractBaseTestITCase {
 
   @BeforeClass
   public static void init()
-      throws LifecycleException, IOException, InstantiationException, 
IllegalAccessException, ClassNotFoundException {
+      throws LifecycleException, IOException, 
+          InstantiationException, IllegalAccessException, 
+          ClassNotFoundException, ServletException {
     server = TomcatTestServer.init(9080)
         .addServlet(TechnicalServlet.class, "/odata-server-tecsvc/odata.svc/*")
         .addServlet(TechnicalStatusMonitorServlet.class, 
"/odata-server-tecsvc/status/*")
+        .addAuthServlet(TechnicalServlet.class, "/odata-server-tecsvc/auth", 
"/*")
         .addServlet(StaticContent.create("org-odata-core-v1.xml"),
             
"/odata-server-tecsvc/v4.0/cs02/vocabularies/Org.OData.Core.V1.xml")
         .addWebApp(false)

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b6871904/fit/src/test/java/org/apache/olingo/fit/base/AuthBatchTestITCase.java
----------------------------------------------------------------------
diff --git 
a/fit/src/test/java/org/apache/olingo/fit/base/AuthBatchTestITCase.java 
b/fit/src/test/java/org/apache/olingo/fit/base/AuthBatchTestITCase.java
deleted file mode 100644
index 9103818..0000000
--- a/fit/src/test/java/org/apache/olingo/fit/base/AuthBatchTestITCase.java
+++ /dev/null
@@ -1,119 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.olingo.fit.base;
-
-import static org.junit.Assert.assertEquals;
-
-import java.net.URI;
-
-import org.apache.olingo.client.api.ODataClient;
-import org.apache.olingo.client.api.communication.request.batch.BatchManager;
-import 
org.apache.olingo.client.api.communication.request.batch.ODataBatchRequest;
-import org.apache.olingo.client.api.communication.request.batch.ODataChangeset;
-import 
org.apache.olingo.client.api.communication.request.cud.ODataEntityUpdateRequest;
-import org.apache.olingo.client.api.communication.request.cud.UpdateType;
-import 
org.apache.olingo.client.api.communication.request.retrieve.ODataEntityRequest;
-import org.apache.olingo.client.api.communication.response.ODataBatchResponse;
-import org.apache.olingo.client.api.domain.ClientEntity;
-import org.apache.olingo.client.api.http.HttpClientException;
-import org.apache.olingo.client.api.uri.URIBuilder;
-import org.apache.olingo.client.core.ODataClientFactory;
-import org.apache.olingo.client.core.http.BasicAuthHttpClientFactory;
-import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
-import org.apache.olingo.commons.api.edm.FullQualifiedName;
-import org.apache.olingo.commons.api.format.ContentType;
-import org.apache.olingo.commons.api.http.HttpHeader;
-import org.junit.Test;
-
-public class AuthBatchTestITCase extends AbstractTestITCase {
-
-  private final static ContentType ACCEPT = 
ContentType.APPLICATION_OCTET_STREAM;
-
-  @Test
-  public void clean() throws EdmPrimitiveTypeException {
-    final ODataClient authclient = ODataClientFactory.getClient();
-    batchRequest(authclient, testStaticServiceRootURL);
-  }
-
-  @Test
-  public void authorized() throws EdmPrimitiveTypeException {
-    final ODataClient authclient = ODataClientFactory.getClient();
-    authclient.getConfiguration().setHttpClientFactory(new 
BasicAuthHttpClientFactory("odatajclient", "odatajclient"));
-    batchRequest(authclient, testAuthServiceRootURL);
-  }
-
-  @Test(expected = HttpClientException.class)
-  public void unauthorized() throws EdmPrimitiveTypeException {
-    final ODataClient unauthclient = ODataClientFactory.getClient();
-    unauthclient.getConfiguration().setHttpClientFactory(new 
BasicAuthHttpClientFactory("not_auth", "not_auth"));
-    batchRequest(unauthclient, testAuthServiceRootURL);
-  }
-
-  private void batchRequest(final ODataClient client, final String baseURL) 
throws EdmPrimitiveTypeException {
-    // create your request
-    final ODataBatchRequest request = 
client.getBatchRequestFactory().getBatchRequest(baseURL);
-    request.setAccept(ACCEPT.toContentTypeString());
-    request.addCustomHeader("User-Agent", "Apache Olingo OData Client");
-    request.addCustomHeader(HttpHeader.ACCEPT_CHARSET, "UTF-8");
-
-    final BatchManager streamManager = request.payloadManager();
-
-    // -------------------------------------------
-    // Add retrieve item
-    // -------------------------------------------
-    // prepare URI
-    URIBuilder targetURI = client.newURIBuilder(baseURL);
-    targetURI.appendEntitySetSegment("Customers").appendKeySegment(1);
-
-    // create new request
-    ODataEntityRequest<ClientEntity> queryReq = 
client.getRetrieveRequestFactory().getEntityRequest(targetURI.build());
-    queryReq.setFormat(ContentType.JSON);
-
-    streamManager.addRequest(queryReq);
-    // -------------------------------------------
-
-    // -------------------------------------------
-    // Add changeset item
-    // -------------------------------------------
-    final ODataChangeset changeset = streamManager.addChangeset();
-
-    // Update Customer into the changeset
-    targetURI = 
client.newURIBuilder(baseURL).appendEntitySetSegment("Customers").appendKeySegment(1);
-    final URI editLink = targetURI.build();
-
-    final ClientEntity patch = client.getObjectFactory().newEntity(
-        new 
FullQualifiedName("Microsoft.Test.OData.Services.ODataWCFService.Customer"));
-    patch.setEditLink(editLink);
-
-    patch.getProperties().add(client.getObjectFactory().newPrimitiveProperty(
-        "LastName",
-        client.getObjectFactory().newPrimitiveValueBuilder().buildString("new 
last name")));
-
-    final ODataEntityUpdateRequest<ClientEntity> changeReq =
-        client.getCUDRequestFactory().getEntityUpdateRequest(UpdateType.PATCH, 
patch);
-    changeReq.setFormat(ContentType.JSON_FULL_METADATA);
-
-    changeset.addRequest(changeReq);
-    // -------------------------------------------
-
-    final ODataBatchResponse response = streamManager.getResponse();
-    assertEquals(200, response.getStatusCode());
-    assertEquals("OK", response.getStatusMessage());
-  }
-}

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b6871904/fit/src/test/java/org/apache/olingo/fit/tecsvc/TecSvcConst.java
----------------------------------------------------------------------
diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/TecSvcConst.java 
b/fit/src/test/java/org/apache/olingo/fit/tecsvc/TecSvcConst.java
index 6d5e0cd..4c331b3 100644
--- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/TecSvcConst.java
+++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/TecSvcConst.java
@@ -21,5 +21,6 @@ package org.apache.olingo.fit.tecsvc;
 public class TecSvcConst {
 
   public final static String BASE_URI = 
"http://localhost:9080/odata-server-tecsvc/odata.svc";;
+  public final static String AUTH_URI = 
"http://localhost:9080/odata-server-tecsvc/auth";;
 
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b6871904/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AbstractTecSvcITCase.java
----------------------------------------------------------------------
diff --git 
a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AbstractTecSvcITCase.java
 
b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AbstractTecSvcITCase.java
index c74f345..05aa0f4 100644
--- 
a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AbstractTecSvcITCase.java
+++ 
b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AbstractTecSvcITCase.java
@@ -26,6 +26,7 @@ import 
org.apache.olingo.client.api.communication.request.ODataRequest;
 import org.apache.olingo.client.api.communication.response.ODataResponse;
 import org.apache.olingo.client.api.domain.ClientObjectFactory;
 import org.apache.olingo.client.core.ODataClientFactory;
+import org.apache.olingo.client.core.http.BasicAuthHttpClientFactory;
 import org.apache.olingo.commons.api.format.ContentType;
 import org.apache.olingo.commons.api.http.HttpHeader;
 import org.apache.olingo.fit.AbstractBaseTestITCase;
@@ -34,7 +35,10 @@ import org.apache.olingo.fit.tecsvc.TecSvcConst;
 public abstract class AbstractTecSvcITCase extends AbstractBaseTestITCase {
 
   protected static final String SERVICE_URI = TecSvcConst.BASE_URI + '/';
+  protected static final String AUTH_URI = TecSvcConst.AUTH_URI + '/';
   protected static final String SERVICE_NAMESPACE = "olingo.odata.test1";
+  protected static final String USERNAME = "odatajclient";
+  protected static final String PASSWORD = "odatajclient";
 
   // Read-only tests can re-use the server session via the session cookie.
   // JUnit constructs a fresh instance for each test method, so we have to
@@ -65,6 +69,14 @@ public abstract class AbstractTecSvcITCase extends 
AbstractBaseTestITCase {
     return odata;
   }
 
+  protected ODataClient getBasicAuthClient(String username, String password) {
+    ODataClient odata = getClient();
+    odata.getConfiguration()
+            .setHttpClientFactory(new BasicAuthHttpClientFactory(username, 
password));
+    odata.getConfiguration().setDefaultPubFormat(getContentType());
+    return odata;
+  }
+
   protected EdmEnabledODataClient getEdmEnabledClient() {
     return ODataClientFactory.getEdmEnabledClient(SERVICE_URI, 
getContentType());
   }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b6871904/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AuthBatchTestITCase.java
----------------------------------------------------------------------
diff --git 
a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AuthBatchTestITCase.java
 
b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AuthBatchTestITCase.java
new file mode 100644
index 0000000..89ad124
--- /dev/null
+++ 
b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AuthBatchTestITCase.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2016 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.olingo.fit.tecsvc.client;
+
+import java.net.URI;
+import org.apache.olingo.client.api.ODataClient;
+import org.apache.olingo.client.api.communication.request.batch.BatchManager;
+import 
org.apache.olingo.client.api.communication.request.batch.ODataBatchRequest;
+import org.apache.olingo.client.api.communication.request.batch.ODataChangeset;
+import 
org.apache.olingo.client.api.communication.request.cud.ODataEntityUpdateRequest;
+import org.apache.olingo.client.api.communication.request.cud.UpdateType;
+import 
org.apache.olingo.client.api.communication.request.retrieve.ODataEntityRequest;
+import org.apache.olingo.client.api.communication.response.ODataBatchResponse;
+import org.apache.olingo.client.api.domain.ClientEntity;
+import org.apache.olingo.client.api.http.HttpClientException;
+import org.apache.olingo.client.api.uri.URIBuilder;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
+import org.apache.olingo.commons.api.edm.FullQualifiedName;
+import org.apache.olingo.commons.api.format.ContentType;
+import org.apache.olingo.commons.api.http.HttpHeader;
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+
+
+public class AuthBatchTestITCase extends AbstractParamTecSvcITCase{
+  private final static ContentType ACCEPT = 
ContentType.APPLICATION_OCTET_STREAM;
+  
+  @Test
+  public void authorized() throws EdmPrimitiveTypeException {
+    final ODataClient authclient = getBasicAuthClient(USERNAME, PASSWORD);
+    batchRequest(authclient, AUTH_URI);
+  }
+
+  @Test(expected = HttpClientException.class)
+  public void unauthorized() throws EdmPrimitiveTypeException {
+    final ODataClient unauthclient = getBasicAuthClient("not_auth", 
"not_auth");
+    batchRequest(unauthclient, AUTH_URI);
+  }
+  
+  private void batchRequest(final ODataClient client, final String baseURL) 
throws EdmPrimitiveTypeException {
+    // create your request
+    final ODataBatchRequest request = 
client.getBatchRequestFactory().getBatchRequest(baseURL);
+    request.setAccept(ACCEPT.toContentTypeString());
+    request.addCustomHeader("User-Agent", "Apache Olingo OData Client");
+    request.addCustomHeader(HttpHeader.ACCEPT_CHARSET, "UTF-8");
+
+    final BatchManager streamManager = request.payloadManager();
+
+    // -------------------------------------------
+    // Add retrieve item
+    // -------------------------------------------
+    // prepare URI
+    URIBuilder targetURI = client.newURIBuilder(baseURL);
+    targetURI.appendEntitySetSegment("ESAllPrim").appendKeySegment(32767);
+
+    // create new request
+    ODataEntityRequest<ClientEntity> queryReq = 
client.getRetrieveRequestFactory().getEntityRequest(targetURI.build());
+  
+    streamManager.addRequest(queryReq);
+    // -------------------------------------------
+
+    // -------------------------------------------
+    // Add changeset item
+    // -------------------------------------------
+    final ODataChangeset changeset = streamManager.addChangeset();
+
+    // Update Customer into the changeset
+    targetURI = 
client.newURIBuilder(baseURL).appendEntitySetSegment("ESAllPrim").appendKeySegment(32767);
+    final URI editLink = targetURI.build();
+
+    final ClientEntity patch = client.getObjectFactory().newEntity(
+        new FullQualifiedName(SERVICE_NAMESPACE, "ETAllPrim"));
+    patch.setEditLink(editLink);
+
+    patch.getProperties().add(client.getObjectFactory().newPrimitiveProperty(
+        "PropertyString",
+        
client.getObjectFactory().newPrimitiveValueBuilder().buildString("Test")));
+
+    final ODataEntityUpdateRequest<ClientEntity> changeReq =
+        client.getCUDRequestFactory().getEntityUpdateRequest(UpdateType.PATCH, 
patch);
+
+    changeset.addRequest(changeReq);
+    // -------------------------------------------
+
+    final ODataBatchResponse response = streamManager.getResponse();
+    assertEquals(202, response.getStatusCode());
+    assertEquals("Accepted", response.getStatusMessage());
+  }
+  
+}

Reply via email to