public static MocoEventAction post(final Resource url, final Object object) { return post(checkNotNull(url, "URL should not be null"), Jsons.toJson(checkNotNull(object, "Content should not be null"))); }
public static void assertEquals(final Object expected, final String actual) { try { JSONAssert.assertEquals(Jsons.toJson(expected), actual, JSONCompareMode.LENIENT); } catch (JSONException e) { throw new AssertionError("fail to parse entity to json"); } } }
@Override public MessageContent readFor(final Optional<? extends Request> request) { return MessageContent.content().withContent(toJson(pojo)).build(); }
public static void assertEquals(final Object expected, final HttpResponse response) { assertEquals(Jsons.toJson(expected), response); }
@Override public void run() throws Exception { HttpResponse httpResponse = helper.postForResponse(remoteUrl("/targets"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(201)); assertThat(httpResponse.getFirstHeader("Location").getValue(), is("/targets/123")); } });
@Override public void run() throws Exception { HttpResponse httpResponse = helper.putForResponse(remoteUrl("/targets/1"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(200)); } });
@Override public void run() throws Exception { HttpResponse httpResponse1 = helper.putForResponse(remoteUrl("/targets/1"), Jsons.toJson(resource1)); assertThat(httpResponse1.getStatusLine().getStatusCode(), is(200)); HttpResponse httpResponse2 = helper.putForResponse(remoteUrl("/targets/2"), Jsons.toJson(resource1)); assertThat(httpResponse2.getStatusLine().getStatusCode(), is(200)); } });
@Override public void run() throws Exception { HttpResponse httpResponse = helper.putForResponseWithHeaders(remoteUrl("/targets/1"), Jsons.toJson(resource1), of(HttpHeaders.IF_MATCH, "moco")); assertThat(httpResponse.getStatusLine().getStatusCode(), is(200)); } });
@Override public void run() throws Exception { HttpResponse httpResponse = helper.postForResponse(remoteUrl("/targets"), Jsons.toJson(resource), "application/json"); assertThat(httpResponse.getStatusLine().getStatusCode(), is(201)); assertThat(httpResponse.getFirstHeader("Location").getValue(), is("/targets/123")); HttpResponse badRequest = helper.postForResponse(remoteUrl("/targets"), Jsons.toJson(resource)); assertThat(badRequest.getStatusLine().getStatusCode(), is(400)); } });
@Override public void run() throws Exception { HttpResponse httpResponse = helper.postForResponse(remoteUrl("/targets/1"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(404)); } });
@Override public void run() throws Exception { HttpResponse httpResponse = helper.putForResponse(remoteUrl("/targets/2"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(404)); } });
@Override public void run() throws Exception { HttpResponse httpResponse = helper.putForResponse(remoteUrl("/targets/1"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(409)); } });
@Test public void should_post() throws IOException { runWithConfiguration("rest/rest.json"); final Plain resource1 = new Plain(); resource1.code = 1; resource1.message = "hello"; org.apache.http.HttpResponse httpResponse = helper.postForResponse(remoteUrl("/targets"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(201)); assertThat(httpResponse.getFirstHeader("Location").getValue(), is("/targets/123")); }
@Test public void should_put() throws IOException { runWithConfiguration("rest/rest.json"); final Plain resource1 = new Plain(); resource1.code = 1; resource1.message = "hello"; org.apache.http.HttpResponse httpResponse = helper.putForResponse(remoteUrl("/targets/1"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(200)); }
@Test public void should_match_exact_json() throws Exception { final String jsonText = Jsons.toJson(of("foo", "bar")); server.request(by(json(jsonText))).response("foo"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postContent(root(), jsonText), is("foo")); } }); }
@Test public void should_match_same_structure_json() throws Exception { final String jsonText = Jsons.toJson(of("foo", "bar")); server.request(by(json(jsonText))).response("foo"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postContent(root(), jsonText), is("foo")); } }); }