@Test public void modelWithParamsFailsWhenInvalidRequestBody() { RequestBody requestBody = RequestBody .create(MediaType.parse("text/plain"), "{\"s1\":\"\"}"); TestObserver<Model> subscriber = restApi.modelWithParams(requestBody, 0).test(); subscriber.awaitTerminalEvent(); subscriber.assertNoValues(); subscriber.assertError(HttpException.class); Throwable error = subscriber.errors().get(0); assertThat(error.getMessage(), is("HTTP 404 null")); }
@Test public void modelWithWrongDTO() { try { restApi.modelWithWrongDTO(); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is("When checking DTOMockery on method Rx2RestApi#modelWithWrongDTO \n" + " an attempt to use it with class io.victoralbertos.mockery.internal.integration.Model was found. But it is not a supported type for DTOMockery.\n" + "To fix it, use DTOMockery with: [java.util.List<io.victoralbertos.mockery.internal.integration.Model>].\n")); } }
@Test public void modelWithWrongMockeryParam() { Model model = new Model(Model.class.getName()); exception.expect(RuntimeException.class); restApi.modelWithWrongMockeryParam(model); }
@Test public void modelWithParamsResponse() { RequestBody requestBody = RequestBody .create(MediaType.parse("text/plain"), "{\"s1\":\"io.victoralbertos.mockery.internal.integration.Model\"}"); TestObserver<Response<Model>> subscriber = restApi.modelWithParamsResponse(requestBody, 0).test(); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); subscriber.assertValueCount(1); Response<Model> modelResponse = subscriber.values().get(0); assertTrue(modelResponse.isSuccessful()); Model model = modelResponse.body(); assertThat(model.getS1(), is(Model.class.getName())); }
@Test public void modelWithoutDTO() { try { restApi.modelWithoutDTO(); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is("When checking method Rx2RestApi#modelWithoutDTO \n" + "No @Mockery annotation for return method was found.\n" + "To fix it, annotate method with one.\n")); } }
@Test public void model() { TestObserver<Model> subscriber = restApi.model().test(); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); subscriber.assertValueCount(1); Model model = subscriber.values().get(0); assertThat(model.getS1(), is(Model.class.getName())); }
@Test public void integer() { TestObserver<Integer> subscriber = restApi.integer().test(); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); subscriber.assertValueCount(1); Integer integer = subscriber.values().get(0); assertThat(integer, is(30)); }
@Test public void modelResponse() { TestObserver<Response<Model>> subscriber = restApi.modelResponse().test(); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); subscriber.assertValueCount(1); Response<Model> modelResponse = subscriber.values().get(0); assertTrue(modelResponse.isSuccessful()); Model model = modelResponse.body(); assertThat(model.getS1(), is(Model.class.getName())); }
@Test public void modelsWithIdResponseFailsWhenInvalidId() throws IOException { TestObserver<Response<Model>> subscriber = restApi.id(-1).test(); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); Response<Model> modelResponse = subscriber.values().get(0); assertFalse(modelResponse.isSuccessful()); assertNull(modelResponse.body()); assertThat(modelResponse.errorBody().string(), is("-1 does not match with regex ID")); }
@Test public void completableModel() { TestObserver<Void> subscriber = restApi.completableModel().test(); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); subscriber.assertComplete(); }
@Test public void modelWithParamsResponseFailsWhenInvalidRequestBody() throws IOException { RequestBody requestBody = RequestBody .create(MediaType.parse("text/plain"), "{\"s1\":\"\"}"); TestObserver<Response<Model>> subscriber = restApi.modelWithParamsResponse(requestBody, 0).test(); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); subscriber.assertValueCount(1); Response<Model> modelResponse = subscriber.values().get(0); assertFalse(modelResponse.isSuccessful()); assertNull(modelResponse.body()); assertThat(modelResponse.errorBody().string(), is("model.s1 must be equal " + "to io.victoralbertos.mockery.internal.integration.Model")); }
@Test public void modelWithParams() { RequestBody requestBody = RequestBody .create(MediaType.parse("text/plain"), "{\"s1\":\"io.victoralbertos.mockery.internal.integration.Model\"}"); TestObserver<Model> subscriber = restApi.modelWithParams(requestBody, 0).test(); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); subscriber.assertValueCount(1); Model model = subscriber.values().get(0); assertThat(model.getS1(), is(Model.class.getName())); }