package com.stanfy.helium.dsl.scenario;

import com.stanfy.helium.entities.EntityReader;
import com.stanfy.helium.entities.TypedEntity;
import com.stanfy.helium.entities.json.GsonEntityReader;
import com.stanfy.helium.model.Type;
import com.stanfy.helium.utils.AssertionUtils;
import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * HTTP response wrapper.
 */
public class HttpResponseWrapper {

  /** Response instance. */
  private final HttpResponse response;

  /** Request. */
  private final HttpRequest request;

  /** Encoding. */
  private final String encoding;

  /** Type. */
  private final Type type;

  /** Headers. */
  private Map<String, String> httpHeaders;

  /** Body. */
  private TypedEntity body;

  public HttpResponseWrapper(final HttpRequest request, final HttpResponse response, final String encoding, final Type type) {
    this.request = request;
    this.response = response;
    this.encoding = encoding;
    this.type = type;
  }

  public Map<String, String> getHttpHeaders() {
    if (httpHeaders == null) {
      Header[] headers = response.getAllHeaders();
      httpHeaders = new LinkedHashMap<String, String>(headers.length);
      for (Header header : headers) {
        httpHeaders.put(header.getName(), header.getValue());
      }
    }
    return httpHeaders;
  }

  public Object getBody() throws IOException {
    if (body == null) {
      mustSucceed();
      EntityReader reader = new GsonEntityReader(new InputStreamReader(new BufferedInputStream(response.getEntity().getContent()), encoding));
      body = reader.read(type);
      AssertionUtils.assertCorrectEntity(body, request, response);
    }
    return body.getValue();
  }

  public void mustSucceed() {
    AssertionUtils.validateStatus(request, response, true);
  }
  public void mustBeClientError() {
    AssertionUtils.validateStatus(request, response, false);
  }

}

@@ -269,16 +269,55 @@
 onUtils;
+%0A%0Aimport org.apache.commons.io.IOUtils;
 %0Aimport 
@@ -371,40 +371,212 @@
 Http
-Request;%0Aimport org.apache.http.
+Entity;%0Aimport org.apache.http.HttpRequest;%0Aimport org.apache.http.HttpResponse;%0Aimport org.apache.http.entity.ContentType;%0Aimport org.apache.http.entity.StringEntity;%0Aimport org.apache.http.message.Basic
 Http
@@ -650,16 +650,44 @@
 eption;%0A
+import java.io.InputStream;%0A
 import j
@@ -1322,38 +1322,8 @@
 st;%0A
-    this.response = response;%0A
     
@@ -1370,16 +1370,886 @@
 = type;%0A
+    this.response = wrapResponse(response);%0A  %7D%0A%0A  private HttpResponse wrapResponse(final HttpResponse response) %7B%0A    final HttpEntity responseEntity = response.getEntity();%0A    if (responseEntity != null) %7B%0A      InputStream content;%0A      try %7B%0A        content = responseEntity.getContent();%0A        if (content != null) %7B%0A          final HttpResponse result = new BasicHttpResponse(response.getStatusLine());%0A          result.setHeaders(response.getAllHeaders());%0A          // TODO: support different content types%0A          result.setEntity(new StringEntity(IOUtils.toString(response.getEntity().getContent(), encoding), ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), encoding)));%0A          IOUtils.closeQuietly(content);%0A          return result;%0A        %7D%0A      %7D catch (final IOException e) %7B%0A        //ignore%0A      %7D%0A    %7D%0A%0A    return response;%0A
   %7D%0A%0A  p
@@ -2677,24 +2677,71 @@
 tSucceed();%0A
+      // TODO: support different content types%0A
       Entity

read response fully