package com.vtence.molecule.support;

import com.vtence.molecule.Response;
import com.vtence.molecule.http.HttpStatus;
import org.hamcrest.Matcher;
import org.junit.Assert;

import static com.vtence.molecule.http.HeaderNames.CONTENT_TYPE;
import static com.vtence.molecule.http.HeaderNames.LOCATION;
import static org.hamcrest.CoreMatchers.*;

public class ResponseAssertions {

    private final Response response;

    public ResponseAssertions(Response response) {
        this.response = response;
    }

    public static ResponseAssertions assertThat(Response response) {
        return new ResponseAssertions(response);
    }

    public void hasStatusCode(int code) {
        hasStatusCode(is(code));
    }

    public void hasStatusCode(Matcher<? super Integer> matching) {
        Assert.assertThat("status code", response.statusCode(), matching);
    }

    public void hasStatusText(String text) {
        hasStatusText(is(text));
    }

    public void hasStatusText(Matcher<? super String> matching) {
        Assert.assertThat("status text", response.statusText(), matching);
    }

    public void hasStatus(HttpStatus expected) {
        hasStatusCode(expected.code);
        hasStatusText(expected.text);
    }

    public void isRedirectedTo(String location) {
        isRedirectedTo(equalTo(location));
    }

    public void isRedirectedTo(Matcher<? super String> matching) {
        Assert.assertThat("redirection", response.header(LOCATION), matching);
    }

    public void hasHeader(String name) {
        hasHeader(name, any(String.class));
    }

    public void hasHeader(String name, String value) {
        hasHeader(name, equalTo(value));
    }

    public void hasHeader(String name, Matcher<? super String> matchingValue) {
        Assert.assertThat(name, response.header(name), matchingValue);
    }

    public void hasNoHeader(String name) {
        hasHeader(name, nullValue());
    }

    public void hasContentType(String contentType) {
        hasContentType(equalTo(contentType));
    }

    public void hasContentType(Matcher<? super String> matching) {
        hasHeader(CONTENT_TYPE, matching);
    }
}

@@ -630,36 +630,50 @@
   %7D%0A%0A    public 
-void
+ResponseAssertions
  hasStatusCode(i
@@ -720,34 +720,69 @@
 ));%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  hasStat
@@ -904,34 +904,69 @@
 g);%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  hasStat
@@ -1024,34 +1024,69 @@
 ));%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  hasStat
@@ -1207,34 +1207,69 @@
 g);%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  hasStat
@@ -1374,34 +1374,69 @@
 t);%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  isRedir
@@ -1509,34 +1509,69 @@
 ));%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  isRedir
@@ -1697,34 +1697,69 @@
 g);%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  hasHead
@@ -1824,34 +1824,69 @@
 ));%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  hasHead
@@ -1962,34 +1962,69 @@
 ));%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  hasHead
@@ -2155,34 +2155,69 @@
 e);%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  hasNoHe
@@ -2278,34 +2278,69 @@
 ));%0A    
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  hasCont
@@ -2423,26 +2423,61 @@
     
-%7D%0A%0A    public void
+    return this;%0A    %7D%0A%0A    public ResponseAssertions
  has
@@ -2563,20 +2563,41 @@
  matching);%0A
+        return this;%0A
     %7D%0A%7D%0A

Make response assertions chainable to provide a fluent interface