@Test public void modelWithParamsFailsWhenInvalidJson() { Model model = new Model(Model.class.getName()); exception.expect(AssertionError.class); restApi.modelWithParams(model, 0, "{\"s1\":\"\"}"); }
@Test public void modelWithParamsFailsWhenInvalidRequestBody() { RequestBody requestBody = RequestBody .create(MediaType.parse("text/plain"), "{\"s1\":\"\"}"); TestSubscriber<Model> subscriber = new TestSubscriber<>(); restApi.modelWithParams(requestBody, 0).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoValues(); subscriber.assertError(HttpException.class); Throwable error = subscriber.getOnErrorEvents().get(0); assertThat(error.getMessage(), is("HTTP 404 null")); }
@Test public void modelWithParamsSync() throws IOException { RequestBody requestBody = RequestBody .create(MediaType.parse("text/plain"), "{\"s1\":\"io.victoralbertos.mockery.internal.integration.Model\"}"); Response<Model> responseModel = restApi .modelWithParams(requestBody, 0).execute(); assertTrue(responseModel.isSuccessful()); Model model = responseModel.body(); assertThat(model.getS1(), is(Model.class.getName())); }
@Test public void modelWithParamsAsync() throws Exception { RequestBody requestBody = RequestBody .create(MediaType.parse("text/plain"), "{\"s1\":\"io.victoralbertos.mockery.internal.integration.Model\"}"); final AtomicReference<Response<Model>> atomicReference = new AtomicReference<>(); final CountDownLatch latch = new CountDownLatch(1); restApi.modelWithParams(requestBody, 0).enqueue(new Callback<Model>() { @Override public void onResponse(Call<Model> call, Response<Model> response) { atomicReference.set(response); latch.countDown(); } @Override public void onFailure(Call<Model> call, Throwable t) { t.printStackTrace(); } }); assertTrue(latch.await(10, SECONDS)); Response<Model> responseModel = atomicReference.get(); assertTrue(responseModel.isSuccessful()); Model model = responseModel.body(); assertThat(model.getS1(), is(Model.class.getName())); }
@Test public void modelWithParams() { RequestBody requestBody = RequestBody .create(MediaType.parse("text/plain"), "{\"s1\":\"io.victoralbertos.mockery.internal.integration.Model\"}"); TestSubscriber<Model> subscriber = new TestSubscriber<>(); restApi.modelWithParams(requestBody, 0).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); subscriber.assertValueCount(1); Model model = subscriber.getOnNextEvents().get(0); assertThat(model.getS1(), is(Model.class.getName())); }
@Test public void modelWithParamsFailsWhenInvalidRequestBodySync() throws IOException { RequestBody requestBody = RequestBody .create(MediaType.parse("text/plain"), "{\"s1\":\"\"}"); Response<Model> responseModel = restApi .modelWithParams(requestBody, 0).execute(); assertFalse(responseModel.isSuccessful()); ResponseBody responseBody = responseModel.errorBody(); assertThat(responseBody.string(), is("model.s1 must be equal " + "to io.victoralbertos.mockery.internal.integration.Model")); }
@Test public void modelWithParamsFailsWhenInvalidRequestBodyAsync() throws Exception { RequestBody requestBody = null; final AtomicReference<Response<Model>> atomicReference = new AtomicReference<>(); final CountDownLatch latch = new CountDownLatch(1); restApi.modelWithParams(requestBody, 0).enqueue(new Callback<Model>() { @Override public void onResponse(Call<Model> call, Response<Model> response) { atomicReference.set(response); latch.countDown(); } @Override public void onFailure(Call<Model> call, Throwable t) { t.printStackTrace(); } }); assertTrue(latch.await(10, SECONDS)); Response<Model> responseModel = atomicReference.get(); assertFalse(responseModel.isSuccessful()); ResponseBody responseBody = responseModel.errorBody(); assertThat(responseBody.string(), is("model can not be null")); }
@Test public void modelWithParams() { Model model = new Model(Model.class.getName()); Model response = restApi .modelWithParams(model, 0, "{\"s1\":\"io.victoralbertos.mockery.internal.integration.Model\"}"); assertThat(response.getS1(), is(model.getS1())); }