Search in sources :

Example 16 with Response

use of com.github.ljtfreitas.julian.Response in project julian-http-client by ljtfreitas.

the class MultiResponseTTest method failure.

@Test
void failure() {
    RuntimeException exception = new RuntimeException("oops");
    Promise<Response<Collection<String>>> response = Promise.failed(exception);
    ResponseFn<Collection<String>, Collection<Object>> fn = new CollectionResponseT().bind(endpoint, new ObjectResponseT<Collection<Object>>().bind(endpoint, null));
    Multi<Object> multi = subject.bind(endpoint, fn).join(response, Arguments.empty());
    AssertSubscriber<Object> subscriber = multi.subscribe().withSubscriber(AssertSubscriber.create());
    subscriber.assertFailedWith(RuntimeException.class, exception.getMessage());
}
Also used : Response(com.github.ljtfreitas.julian.Response) CollectionResponseT(com.github.ljtfreitas.julian.CollectionResponseT) Collection(java.util.Collection) ObjectResponseT(com.github.ljtfreitas.julian.ObjectResponseT) Test(org.junit.jupiter.api.Test)

Example 17 with Response

use of com.github.ljtfreitas.julian.Response in project julian-http-client by ljtfreitas.

the class DefaultHTTPResponseFailureTest method shouldCreateTheFailureResponse.

@ParameterizedTest(name = "HTTP Status: {0}")
@MethodSource("failureHTTPStatuses")
void shouldCreateTheFailureResponse(HTTPStatus status, Class<? extends HTTPResponseException> exception) {
    HTTPHeaders headers = HTTPHeaders.empty();
    String responseBody = "response body";
    HTTPClientResponse source = new HTTPClientResponse() {

        @Override
        public HTTPStatus status() {
            return status;
        }

        @Override
        public HTTPHeaders headers() {
            return headers;
        }

        @Override
        public HTTPResponseBody body() {
            return new PublisherHTTPResponseBody(new SimplePublisher(responseBody));
        }

        @Override
        public <T, R extends Response<T>> Optional<R> failure(Function<? super HTTPClientResponse, R> fn) {
            return Optional.empty();
        }

        @Override
        public <T, R extends Response<T>> Optional<R> success(Function<? super HTTPClientResponse, R> fn) {
            return Optional.empty();
        }
    };
    HTTPResponse<Object> failed = failure.apply(source, JavaType.valueOf(String.class));
    assertAll(() -> assertNotNull(failed), () -> assertEquals(status, failed.status()), () -> assertEquals(headers, failed.headers()));
    HTTPResponseException httpResponseException = assertThrows(exception, failed.body()::unsafe);
    assertAll(() -> assertEquals(status, httpResponseException.status()), () -> assertEquals(headers, httpResponseException.headers()), () -> assertEquals(responseBody, httpResponseException.bodyAsString()));
}
Also used : Response(com.github.ljtfreitas.julian.Response) HTTPClientResponse(com.github.ljtfreitas.julian.http.client.HTTPClientResponse) Function(java.util.function.Function) HTTPClientResponse(com.github.ljtfreitas.julian.http.client.HTTPClientResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 18 with Response

use of com.github.ljtfreitas.julian.Response in project julian-http-client by ljtfreitas.

the class HTTPResponseTTest method compose.

@Test
void compose() {
    Promise<HTTPResponse<String>> promise = Promise.done(HTTPResponse.success(HTTPStatus.valueOf(HTTPStatusCode.OK), HTTPHeaders.empty(), "hello"));
    ResponseFn<String, Response<Object>> fn = new DefaultResponseT().bind(endpoint, new ObjectResponseT<>().bind(endpoint, null));
    HTTPResponse<Object> actual = responseT.bind(endpoint, fn).join(promise, Arguments.empty());
    assertEquals("hello", actual.body().unsafe());
}
Also used : Response(com.github.ljtfreitas.julian.Response) DefaultResponseT(com.github.ljtfreitas.julian.DefaultResponseT) ObjectResponseT(com.github.ljtfreitas.julian.ObjectResponseT) Test(org.junit.jupiter.api.Test)

Example 19 with Response

use of com.github.ljtfreitas.julian.Response in project julian-http-client by ljtfreitas.

the class SetResponseTTest method bind.

@Test
void bind() {
    when(endpoint.returnType()).thenReturn(JavaType.parameterized(Collection.class, String.class));
    Collection<String> values = java.util.List.of("one", "two", "three");
    Promise<Response<Collection<String>>> promise = Promise.done(Response.done(values));
    ResponseFn<Collection<String>, Collection<Object>> fn = new CollectionResponseT().bind(endpoint, new ObjectResponseT<Collection<Object>>().bind(endpoint, null));
    Set<Object> set = responseT.bind(endpoint, fn).join(promise, Arguments.empty());
    assertTrue(set.containsAll(values));
}
Also used : Response(com.github.ljtfreitas.julian.Response) CollectionResponseT(com.github.ljtfreitas.julian.CollectionResponseT) Collection(java.util.Collection) ObjectResponseT(com.github.ljtfreitas.julian.ObjectResponseT) Test(org.junit.jupiter.api.Test)

Example 20 with Response

use of com.github.ljtfreitas.julian.Response in project julian-http-client by ljtfreitas.

the class TraversableResponseTTest method bindNullCollection.

@Test
void bindNullCollection() {
    when(endpoint.returnType()).thenReturn(JavaType.parameterized(Collection.class, String.class));
    Promise<Response<Collection<String>>> promise = Promise.done(Response.done(null));
    ResponseFn<Collection<String>, Collection<Object>> fn = new CollectionResponseT().bind(endpoint, new ObjectResponseT<Collection<Object>>().bind(endpoint, null));
    Traversable<Object> traversable = responseT.bind(endpoint, fn).join(promise, Arguments.empty());
    assertTrue(traversable.isEmpty());
}
Also used : Response(com.github.ljtfreitas.julian.Response) CollectionResponseT(com.github.ljtfreitas.julian.CollectionResponseT) Collection(java.util.Collection) ObjectResponseT(com.github.ljtfreitas.julian.ObjectResponseT) Test(org.junit.jupiter.api.Test)

Aggregations

Response (com.github.ljtfreitas.julian.Response)29 Test (org.junit.jupiter.api.Test)25 ObjectResponseT (com.github.ljtfreitas.julian.ObjectResponseT)24 CollectionResponseT (com.github.ljtfreitas.julian.CollectionResponseT)22 Collection (java.util.Collection)22 Function (java.util.function.Function)4 HTTPHeader (com.github.ljtfreitas.julian.http.HTTPHeader)3 HTTPHeaders (com.github.ljtfreitas.julian.http.HTTPHeaders)3 HTTPResponseBody (com.github.ljtfreitas.julian.http.HTTPResponseBody)3 HTTPStatus (com.github.ljtfreitas.julian.http.HTTPStatus)3 HTTPStatusCode (com.github.ljtfreitas.julian.http.HTTPStatusCode)3 HTTPClientResponse (com.github.ljtfreitas.julian.http.client.HTTPClientResponse)3 Optional (java.util.Optional)3 DefaultResponseT (com.github.ljtfreitas.julian.DefaultResponseT)2 TestObserver (io.reactivex.rxjava3.observers.TestObserver)2 Arguments (com.github.ljtfreitas.julian.Arguments)1 Completable (io.reactivex.rxjava3.core.Completable)1 Maybe (io.reactivex.rxjava3.core.Maybe)1 Single (io.reactivex.rxjava3.core.Single)1 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)1