package com.sap.core.odata.fit.ref;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.junit.Test;

import com.sap.core.odata.testutil.helper.StringHelper;

/**
 * 
 * @author SAP AG
 */
public class BatchTest extends AbstractRefTest {

  @Test
  public void testSimpleBatch() throws Exception {
    final HttpPost post = new HttpPost(URI.create(getEndpoint().toString() + "$batch"));
    post.setHeader("Content-Type", "multipart/mixed;boundary=batch_123");

    String body = StringHelper.inputStreamToString(this.getClass().getResourceAsStream("/simple.batch"), true);
    HttpEntity entity = new StringEntity(body);
    post.setEntity(entity);
    HttpResponse response = getHttpClient().execute(post);

    assertNotNull(response);
    assertEquals(202, response.getStatusLine().getStatusCode());

    String responseBody = StringHelper.inputStreamToString(response.getEntity().getContent());
    assertFalse(responseBody.contains("<error xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">"));
    assertTrue(responseBody.contains("<edmx:Edmx Version=\"1.0\""));
  }

  @Test
  public void testChangeSetBatch() throws Exception {
    final HttpPost post = new HttpPost(URI.create(getEndpoint().toString() + "$batch"));
    post.setHeader("Content-Type", "multipart/mixed;boundary=batch_123");

    String body = StringHelper.inputStreamToString(this.getClass().getResourceAsStream("/changeset.batch"), true);
    HttpEntity entity = new StringEntity(body);
    post.setEntity(entity);
    HttpResponse response = getHttpClient().execute(post);

    assertNotNull(response);
    assertEquals(202, response.getStatusLine().getStatusCode());

    String responseBody = StringHelper.inputStreamToString(response.getEntity().getContent());
    assertTrue(responseBody.contains("Frederic Fall MODIFIED"));
  }

}

@@ -623,603 +623,53 @@
     
-final HttpPost post = new HttpPost(URI.create(getEndpoint().toString() + %22$batch%22));%0A    post.setHeader(%22Content-Type%22, %22multipart/mixed;boundary=batch_123%22);%0A%0A    String body = StringHelper.inputStreamToString(this.getClass().getResourceAsStream(%22/simple.batch%22), true);%0A    HttpEntity entity = new StringEntity(body);%0A    post.setEntity(entity);%0A    HttpResponse response = getHttpClient().execute(post);%0A%0A    assertNotNull(response);%0A    assertEquals(202, response.getStatusLine().getStatusCode());%0A%0A    String responseBody = StringHelper.inputStreamToString(response.getEntity().getContent()
+String responseBody = execute(%22/simple.batch%22
 );%0A 
@@ -923,24 +923,215 @@
 Exception %7B%0A
+    String responseBody = execute(%22/changeset.batch%22);%0A    assertTrue(responseBody.contains(%22Frederic Fall MODIFIED%22));%0A  %7D%0A%0A  private String execute(String batchResource) throws Exception %7B%0A
     final Ht
@@ -1369,34 +1369,29 @@
 sStream(
-%22/changeset.batch%22
+batchResource
 ), true)
@@ -1718,35 +1718,31 @@
 ent());%0A    
-assertTrue(
+return 
 responseBody
@@ -1745,49 +1745,15 @@
 Body
-.contains(%22Frederic Fall MODIFIED%22))
 ;%0A  %7D%0A
+  
 %0A%7D%0A

refactor BatchTest