// Path: src/main/java/net/ozwolf/mockserver/raml/specification/ClassPathSpecification.java
// public class ClassPathSpecification extends RamlSpecification {
//     private final String resource;
// 
//     /**
//      * Create a new RAML specification
//      *
//      * @param name     The name of specification
//      * @param resource The ClassPath resource to load
//      */
//     public ClassPathSpecification(String name, String resource) {
//         super(name);
//         this.resource = resource;
//     }
// 
//     @Override
//     protected Raml getRaml() {
//         InputStream stream = getClass().getClassLoader().getResourceAsStream(resource);
//         if (stream == null)
//             throw new IllegalStateException(String.format("[ %s ] resource does not exist", resource));
// 
//         return new RamlDocumentBuilder().build(resource);
//     }
// }
// 
// Path: src/test/java/net/ozwolf/mockserver/raml/util/Fixtures.java
// public static String fixture(String resource) throws IOException {
//     return IOUtils.toString(Fixtures.class.getClassLoader().getResourceAsStream(resource))
//             .replaceAll("\r\n", "\n");
// }


import net.ozwolf.mockserver.raml.specification.ClassPathSpecification;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.json.JSONException;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.mockserver.client.server.MockServerClient;
import org.mockserver.junit.MockServerRule;
import org.mockserver.model.Header;
import org.mockserver.model.Parameter;
import org.skyscreamer.jsonassert.JSONAssert;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import static net.ozwolf.mockserver.raml.util.Fixtures.fixture;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
                    new ClassPathSpecification("my-service", "apispecs-test/apispecs.raml")
            );

    @Test
    public void shouldCorrectlyVerifyMockServerExpectationsAgainstRamlSpecification() throws IOException {
        MockServerClient mockClient = new MockServerClient("localhost", 5000);

        mockClient.when(
                request()
                        .withPath("/hello/John")
                        .withMethod("GET")
                        .withHeader(new Header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN))
        ).respond(
                response()
                        .withStatusCode(200)
                        .withHeader(new Header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN))
                        .withBody("Hello John!")
        );

        Client client = new JerseyClientBuilder().withConfig(new ClientConfig()).build();

        String response = client.target("http://localhost:5000/hello/John")
                .request()
                .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN)
                .get(String.class);
        assertThat(response).isEqualTo("Hello John!");

        RamlSpecification.Result result = SPECIFICATIONS.get("my-service").obeyedBy(mockClient);

        assertFalse(result.isValid());
// Path: src/main/java/net/ozwolf/mockserver/raml/specification/ClassPathSpecification.java
// public class ClassPathSpecification extends RamlSpecification {
//     private final String resource;
// 
//     /**
//      * Create a new RAML specification
//      *
//      * @param name     The name of specification
//      * @param resource The ClassPath resource to load
//      */
//     public ClassPathSpecification(String name, String resource) {
//         super(name);
//         this.resource = resource;
//     }
// 
//     @Override
//     protected Raml getRaml() {
//         InputStream stream = getClass().getClassLoader().getResourceAsStream(resource);
//         if (stream == null)
//             throw new IllegalStateException(String.format("[ %s ] resource does not exist", resource));
// 
//         return new RamlDocumentBuilder().build(resource);
//     }
// }
// 
// Path: src/test/java/net/ozwolf/mockserver/raml/util/Fixtures.java
// public static String fixture(String resource) throws IOException {
//     return IOUtils.toString(Fixtures.class.getClassLoader().getResourceAsStream(resource))
//             .replaceAll("\r\n", "\n");
// }


// Path: src/test/java/net/ozwolf/mockserver/raml/RamlSpecificationRuleIntegrationTest.java
import net.ozwolf.mockserver.raml.specification.ClassPathSpecification;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.json.JSONException;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.mockserver.client.server.MockServerClient;
import org.mockserver.junit.MockServerRule;
import org.mockserver.model.Header;
import org.mockserver.model.Parameter;
import org.skyscreamer.jsonassert.JSONAssert;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import static net.ozwolf.mockserver.raml.util.Fixtures.fixture;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

                    new ClassPathSpecification("my-service", "apispecs-test/apispecs.raml")
            );

    @Test
    public void shouldCorrectlyVerifyMockServerExpectationsAgainstRamlSpecification() throws IOException {
        MockServerClient mockClient = new MockServerClient("localhost", 5000);

        mockClient.when(
                request()
                        .withPath("/hello/John")
                        .withMethod("GET")
                        .withHeader(new Header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN))
        ).respond(
                response()
                        .withStatusCode(200)
                        .withHeader(new Header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN))
                        .withBody("Hello John!")
        );

        Client client = new JerseyClientBuilder().withConfig(new ClientConfig()).build();

        String response = client.target("http://localhost:5000/hello/John")
                .request()
                .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN)
                .get(String.class);
        assertThat(response).isEqualTo("Hello John!");

        RamlSpecification.Result result = SPECIFICATIONS.get("my-service").obeyedBy(mockClient);

        assertFalse(result.isValid());
        assertThat(result.getFormattedErrorMessage()).isEqualTo(fixture("fixtures/expected-obey-errors-get-hello-john.txt"));