Search in sources :

Example 1 with ExpectationToJavaSerializer

use of org.mockserver.serialization.java.ExpectationToJavaSerializer in project mockserver by mock-server.

the class MockServerClientIntegrationTest method shouldRetrieveRecordedExpectationsAsJava.

@Test
public void shouldRetrieveRecordedExpectationsAsJava() {
    // given
    String serializedExpectations = new ExpectationToJavaSerializer().serialize(Arrays.asList(new Expectation(request("/some_request_one")).thenRespond(response()), new Expectation(request("/some_request_two")).thenRespond(response())));
    echoServerOne.withNextResponse(response().withStatusCode(201).withContentType(APPLICATION_JSON).withBody(new StringBody(serializedExpectations)));
    // when
    String actualResponse = mockServerClientOne.retrieveRecordedExpectations(request().withPath("/some_path").withBody(new StringBody("some_request_body")), Format.JAVA);
    // then
    assertThat(actualResponse, is(serializedExpectations));
    assertThat(retrieveRequests(request()).size(), is(1));
    String result = verify(verification().withRequest(request().withMethod("PUT").withPath("/mockserver/retrieve").withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name()).withQueryStringParameter("format", Format.JAVA.name()).withHeaders(new Header("host", "localhost:" + echoServerOne.getPort()), new Header("accept-encoding", "gzip,deflate"), new Header("connection", "keep-alive"), new Header("content-type", "application/json; charset=utf-8")).withSecure(false).withKeepAlive(true).withBody(new StringBody("{" + NEW_LINE + "  \"path\" : \"/some_path\"," + NEW_LINE + "  \"body\" : \"some_request_body\"" + NEW_LINE + "}"))));
    if (result != null && !result.isEmpty()) {
        throw new AssertionError(result);
    }
}
Also used : ExpectationToJavaSerializer(org.mockserver.serialization.java.ExpectationToJavaSerializer) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 2 with ExpectationToJavaSerializer

use of org.mockserver.serialization.java.ExpectationToJavaSerializer in project mockserver by mock-server.

the class CompileGeneratedJavaCodeTest method shouldCompileExpectationWithHttpResponseTemplate.

@Test
public void shouldCompileExpectationWithHttpResponseTemplate() throws URISyntaxException {
    String expectationAsJavaCode = new ExpectationToJavaSerializer().serialize(1, new Expectation(request().withMethod("GET").withPath("somePath").withQueryStringParameters(new Parameter("requestQueryStringParameterNameOne", "requestQueryStringParameterValueOneOne", "requestQueryStringParameterValueOneTwo"), new Parameter("requestQueryStringParameterNameTwo", "requestQueryStringParameterValueTwo")).withHeaders(new Header("requestHeaderNameOne", "requestHeaderValueOneOne", "requestHeaderValueOneTwo"), new Header("requestHeaderNameTwo", "requestHeaderValueTwo")).withCookies(new Cookie("requestCookieNameOne", "requestCookieValueOne"), new Cookie("requestCookieNameTwo", "requestCookieValueTwo")).withSecure(false).withKeepAlive(true).withBody(new StringBody("somebody")), Times.once(), TimeToLive.unlimited(), 0).thenRespond(template(HttpTemplate.TemplateType.JAVASCRIPT).withTemplate("some_random_template")));
    assertTrue(compileJavaCode(commonImports + "import static org.mockserver.model.HttpTemplate.template;" + NEW_LINE + NEW_LINE + "class TestClass {" + NEW_LINE + "   static {" + "      " + expectationAsJavaCode + NEW_LINE + "   }" + NEW_LINE + "}"));
}
Also used : ExpectationToJavaSerializer(org.mockserver.serialization.java.ExpectationToJavaSerializer) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 3 with ExpectationToJavaSerializer

use of org.mockserver.serialization.java.ExpectationToJavaSerializer in project mockserver by mock-server.

the class CompileGeneratedJavaCodeTest method shouldCompileExpectationWithClassCallback.

@Test
public void shouldCompileExpectationWithClassCallback() throws URISyntaxException {
    String expectationAsJavaCode = new ExpectationToJavaSerializer().serialize(1, new Expectation(request().withMethod("GET").withPath("somePath").withQueryStringParameters(new Parameter("requestQueryStringParameterNameOne", "requestQueryStringParameterValueOneOne", "requestQueryStringParameterValueOneTwo"), new Parameter("requestQueryStringParameterNameTwo", "requestQueryStringParameterValueTwo")).withHeaders(new Header("requestHeaderNameOne", "requestHeaderValueOneOne", "requestHeaderValueOneTwo"), new Header("requestHeaderNameTwo", "requestHeaderValueTwo")).withCookies(new Cookie("requestCookieNameOne", "requestCookieValueOne"), new Cookie("requestCookieNameTwo", "requestCookieValueTwo")).withSecure(false).withKeepAlive(true).withBody(new StringBody("somebody")), Times.once(), TimeToLive.unlimited(), 0).thenRespond(callback().withCallbackClass("some_random_class")));
    assertTrue(compileJavaCode(commonImports + "import static org.mockserver.model.HttpClassCallback.callback;" + NEW_LINE + NEW_LINE + "class TestClass {" + NEW_LINE + "   static {" + "      " + expectationAsJavaCode + NEW_LINE + "   }" + NEW_LINE + "}"));
}
Also used : ExpectationToJavaSerializer(org.mockserver.serialization.java.ExpectationToJavaSerializer) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 4 with ExpectationToJavaSerializer

use of org.mockserver.serialization.java.ExpectationToJavaSerializer in project mockserver by mock-server.

the class CompileGeneratedJavaCodeTest method shouldCompileExpectationWithObjectCallback.

@Test
public void shouldCompileExpectationWithObjectCallback() throws URISyntaxException {
    String expectationAsJavaCode = new ExpectationToJavaSerializer().serialize(1, new Expectation(request().withMethod("GET").withPath("somePath").withQueryStringParameters(new Parameter("requestQueryStringParameterNameOne", "requestQueryStringParameterValueOneOne", "requestQueryStringParameterValueOneTwo"), new Parameter("requestQueryStringParameterNameTwo", "requestQueryStringParameterValueTwo")).withHeaders(new Header("requestHeaderNameOne", "requestHeaderValueOneOne", "requestHeaderValueOneTwo"), new Header("requestHeaderNameTwo", "requestHeaderValueTwo")).withCookies(new Cookie("requestCookieNameOne", "requestCookieValueOne"), new Cookie("requestCookieNameTwo", "requestCookieValueTwo")).withSecure(false).withKeepAlive(true).withBody(new StringBody("somebody")), Times.once(), TimeToLive.unlimited(), 0).thenRespond(new HttpObjectCallback().withClientId("some_random_clientId")));
    assertTrue(compileJavaCode("" + "import org.mockserver.client.MockServerClient;" + NEW_LINE + "import org.mockserver.matchers.Times;" + NEW_LINE + "import org.mockserver.matchers.TimeToLive;" + NEW_LINE + "import org.mockserver.mock.Expectation;" + NEW_LINE + "import org.mockserver.model.*;" + NEW_LINE + "import static org.mockserver.model.HttpRequest.request;" + NEW_LINE + NEW_LINE + "class TestClass {" + NEW_LINE + "   static {" + "      " + expectationAsJavaCode + NEW_LINE + "   }" + NEW_LINE + "}"));
}
Also used : ExpectationToJavaSerializer(org.mockserver.serialization.java.ExpectationToJavaSerializer) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 5 with ExpectationToJavaSerializer

use of org.mockserver.serialization.java.ExpectationToJavaSerializer in project mockserver by mock-server.

the class MockServerClientIntegrationTest method shouldRetrieveActiveExpectationsAsJava.

@Test
public void shouldRetrieveActiveExpectationsAsJava() {
    // given
    String serializedExpectations = new ExpectationToJavaSerializer().serialize(Arrays.asList(new Expectation(request("/some_request_one")).thenRespond(response()), new Expectation(request("/some_request_two")).thenRespond(response())));
    echoServerOne.withNextResponse(response().withStatusCode(201).withContentType(APPLICATION_JSON).withBody(new StringBody(serializedExpectations)));
    // when
    String actualResponse = mockServerClientOne.retrieveActiveExpectations(request().withPath("/some_path").withBody(new StringBody("some_request_body")), Format.JAVA);
    // then
    assertThat(actualResponse, is(serializedExpectations));
    assertThat(retrieveRequests(request()).size(), is(1));
    String result = verify(verification().withRequest(request().withMethod("PUT").withPath("/mockserver/retrieve").withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name()).withQueryStringParameter("format", Format.JAVA.name()).withHeaders(new Header("host", "localhost:" + echoServerOne.getPort()), new Header("accept-encoding", "gzip,deflate"), new Header("connection", "keep-alive"), new Header("content-type", "application/json; charset=utf-8")).withSecure(false).withKeepAlive(true).withBody(new StringBody("{" + NEW_LINE + "  \"path\" : \"/some_path\"," + NEW_LINE + "  \"body\" : \"some_request_body\"" + NEW_LINE + "}"))));
    if (result != null && !result.isEmpty()) {
        throw new AssertionError(result);
    }
}
Also used : ExpectationToJavaSerializer(org.mockserver.serialization.java.ExpectationToJavaSerializer) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 Expectation (org.mockserver.mock.Expectation)8 ExpectationToJavaSerializer (org.mockserver.serialization.java.ExpectationToJavaSerializer)8