Search in sources :

Example 11 with HttpClient

use of com.canoo.platform.core.http.HttpClient in project dolphin-platform by canoo.

the class HttpClientTests method testPostWithoutContent.

@Test
public void testPostWithoutContent() throws Exception {
    // given:
    final HttpClient client = PlatformClient.getService(HttpClient.class);
    final AtomicBoolean actionCalled = new AtomicBoolean(false);
    final AtomicBoolean doneCalled = new AtomicBoolean(false);
    final AtomicBoolean errorCalled = new AtomicBoolean(false);
    // when:
    final CompletableFuture<HttpResponse<String>> future = client.post("http://localhost:" + freePort).withoutContent().readString().onDone(response -> {
        actionCalled.set(true);
        doneCalled.set(true);
    }).onError(e -> {
        actionCalled.set(true);
        errorCalled.set(true);
    }).execute();
    // then:
    final HttpResponse<String> response = future.get(1_000, TimeUnit.MILLISECONDS);
    assertThat("response not defined", response, notNullValue());
    assertThat("Wrong response code", response.getStatusCode(), is(200));
    assertThat("Content should not be null", response.getRawContent(), notNullValue());
    final String content = response.getContent();
    assertThat("String content does not match", content, is("CHECK"));
    assertThatDoneCalledAndErrorNotCalled(actionCalled, doneCalled, errorCalled);
}
Also used : AfterClass(org.testng.annotations.AfterClass) SC_HTTP_RESOURCE_NOTFOUND(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND) ByteArrayProvider(com.canoo.platform.core.http.ByteArrayProvider) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ConnectionException(com.canoo.platform.core.http.ConnectionException) HttpResponse(com.canoo.platform.core.http.HttpResponse) BeforeClass(org.testng.annotations.BeforeClass) HttpClient(com.canoo.platform.core.http.HttpClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) HttpHeaderConstants(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants) BadResponseException(com.canoo.platform.core.http.BadResponseException) SC_HTTP_UNAUTHORIZED(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED) ServerSocket(java.net.ServerSocket) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PlatformClient(com.canoo.platform.client.PlatformClient) Assert(org.testng.Assert) Gson(com.google.gson.Gson) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Spark(spark.Spark) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(com.canoo.platform.core.http.HttpClient) HttpResponse(com.canoo.platform.core.http.HttpResponse) Test(org.testng.annotations.Test)

Example 12 with HttpClient

use of com.canoo.platform.core.http.HttpClient in project dolphin-platform by canoo.

the class HttpClientTests method testBadEndpoint.

@Test
public void testBadEndpoint() throws Exception {
    // given:
    final HttpClient client = PlatformClient.getService(HttpClient.class);
    final AtomicBoolean actionCalled = new AtomicBoolean(false);
    final AtomicBoolean doneCalled = new AtomicBoolean(false);
    final AtomicBoolean errorCalled = new AtomicBoolean(false);
    // when:
    final CompletableFuture<HttpResponse<Void>> future = client.get("http://localhost:" + freePort + "/not/available").withoutContent().withoutResult().onDone(response -> {
        actionCalled.set(true);
        doneCalled.set(true);
    }).onError(e -> {
        actionCalled.set(true);
        errorCalled.set(true);
    }).execute();
    // then:
    assertThatBadResponseException(doneCalled, errorCalled, future, SC_HTTP_RESOURCE_NOTFOUND);
    assertThatErrorCalledAndDoneNotCalled(actionCalled, doneCalled, errorCalled);
}
Also used : AfterClass(org.testng.annotations.AfterClass) SC_HTTP_RESOURCE_NOTFOUND(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND) ByteArrayProvider(com.canoo.platform.core.http.ByteArrayProvider) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ConnectionException(com.canoo.platform.core.http.ConnectionException) HttpResponse(com.canoo.platform.core.http.HttpResponse) BeforeClass(org.testng.annotations.BeforeClass) HttpClient(com.canoo.platform.core.http.HttpClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) HttpHeaderConstants(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants) BadResponseException(com.canoo.platform.core.http.BadResponseException) SC_HTTP_UNAUTHORIZED(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED) ServerSocket(java.net.ServerSocket) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PlatformClient(com.canoo.platform.client.PlatformClient) Assert(org.testng.Assert) Gson(com.google.gson.Gson) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Spark(spark.Spark) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(com.canoo.platform.core.http.HttpClient) HttpResponse(com.canoo.platform.core.http.HttpResponse) Test(org.testng.annotations.Test)

Example 13 with HttpClient

use of com.canoo.platform.core.http.HttpClient in project dolphin-platform by canoo.

the class HttpClientTests method testBadConnection.

@Test
public void testBadConnection() throws Exception {
    // given:
    final HttpClient client = PlatformClient.getService(HttpClient.class);
    final AtomicBoolean actionCalled = new AtomicBoolean(false);
    final AtomicBoolean doneCalled = new AtomicBoolean(false);
    final AtomicBoolean errorCalled = new AtomicBoolean(false);
    // when:
    final CompletableFuture<HttpResponse<Void>> future = client.get("http://localhost:" + getFreePort()).withoutContent().withoutResult().onDone(response -> {
        actionCalled.set(true);
        doneCalled.set(true);
    }).onError(e -> {
        actionCalled.set(true);
        errorCalled.set(true);
    }).execute();
    // then:
    assertThatConnectionException(future);
    assertThatErrorCalledAndDoneNotCalled(actionCalled, doneCalled, errorCalled);
}
Also used : AfterClass(org.testng.annotations.AfterClass) SC_HTTP_RESOURCE_NOTFOUND(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND) ByteArrayProvider(com.canoo.platform.core.http.ByteArrayProvider) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ConnectionException(com.canoo.platform.core.http.ConnectionException) HttpResponse(com.canoo.platform.core.http.HttpResponse) BeforeClass(org.testng.annotations.BeforeClass) HttpClient(com.canoo.platform.core.http.HttpClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) HttpHeaderConstants(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants) BadResponseException(com.canoo.platform.core.http.BadResponseException) SC_HTTP_UNAUTHORIZED(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED) ServerSocket(java.net.ServerSocket) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PlatformClient(com.canoo.platform.client.PlatformClient) Assert(org.testng.Assert) Gson(com.google.gson.Gson) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Spark(spark.Spark) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(com.canoo.platform.core.http.HttpClient) HttpResponse(com.canoo.platform.core.http.HttpResponse) Test(org.testng.annotations.Test)

Example 14 with HttpClient

use of com.canoo.platform.core.http.HttpClient in project dolphin-platform by canoo.

the class HttpClientTests method testSimpleGetWithJsonContentType.

@Test
public void testSimpleGetWithJsonContentType() throws Exception {
    // given:
    final HttpClient client = PlatformClient.getService(HttpClient.class);
    final AtomicBoolean actionCalled = new AtomicBoolean(false);
    final AtomicBoolean doneCalled = new AtomicBoolean(false);
    final AtomicBoolean errorCalled = new AtomicBoolean(false);
    // when:
    final CompletableFuture<HttpResponse<String>> future = client.get("http://localhost:" + freePort).withoutContent().readString(HttpHeaderConstants.JSON_MIME_TYPE).onDone(response -> {
        actionCalled.set(true);
        doneCalled.set(true);
    }).onError(e -> {
        actionCalled.set(true);
        errorCalled.set(true);
    }).execute();
    // then:
    final HttpResponse<String> response = future.get(1_000, TimeUnit.MILLISECONDS);
    assertThat("response not defined", response, notNullValue());
    assertThat("Wrong response code", response.getStatusCode(), is(200));
    assertThat("Content should not be null", response.getRawContent(), notNullValue());
    final String json = response.getContent();
    final Gson gson = new Gson();
    final DummyJson dummy = gson.fromJson(json, DummyJson.class);
    assertThat("No JSON object created", dummy, notNullValue());
    assertThat("Wrong name", dummy.getName(), is("Joe"));
    assertThat("Wrong age", dummy.getAge(), is(33));
    assertThat("Wrong isJavaChampion", dummy.isJavaChampion(), is(true));
    assertThatDoneCalledAndErrorNotCalled(actionCalled, doneCalled, errorCalled);
}
Also used : AfterClass(org.testng.annotations.AfterClass) SC_HTTP_RESOURCE_NOTFOUND(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND) ByteArrayProvider(com.canoo.platform.core.http.ByteArrayProvider) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ConnectionException(com.canoo.platform.core.http.ConnectionException) HttpResponse(com.canoo.platform.core.http.HttpResponse) BeforeClass(org.testng.annotations.BeforeClass) HttpClient(com.canoo.platform.core.http.HttpClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) HttpHeaderConstants(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants) BadResponseException(com.canoo.platform.core.http.BadResponseException) SC_HTTP_UNAUTHORIZED(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED) ServerSocket(java.net.ServerSocket) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PlatformClient(com.canoo.platform.client.PlatformClient) Assert(org.testng.Assert) Gson(com.google.gson.Gson) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Spark(spark.Spark) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(com.canoo.platform.core.http.HttpClient) HttpResponse(com.canoo.platform.core.http.HttpResponse) Gson(com.google.gson.Gson) Test(org.testng.annotations.Test)

Example 15 with HttpClient

use of com.canoo.platform.core.http.HttpClient in project dolphin-platform by canoo.

the class ClientContextFactoryImpl method create.

/**
 * Create a {@link ClientContext} based on the given configuration. This method doesn't block and returns a
 * {@link CompletableFuture} to receive its result. If the {@link ClientContext} can't be created the
 * {@link CompletableFuture#get()} will throw a {@link ClientInitializationException}.
 *
 * @param clientConfiguration the configuration
 * @return the future
 */
public ClientContext create(final ClientConfiguration clientConfiguration, final URI endpoint) {
    Assert.requireNonNull(clientConfiguration, "clientConfiguration");
    final HttpClient httpClient = PlatformClient.getService(HttpClient.class);
    final HttpURLConnectionHandler clientSessionCheckResponseHandler = new StrictClientSessionResponseHandler(endpoint);
    httpClient.addResponseHandler(clientSessionCheckResponseHandler);
    final Function<ClientModelStore, AbstractClientConnector> connectionProvider = s -> {
        return new DolphinPlatformHttpClientConnector(endpoint, clientConfiguration, s, OptimizedJsonCodec.getInstance(), e -> {
        }, httpClient);
    };
    return new ClientContextImpl(clientConfiguration, endpoint, connectionProvider, PlatformClient.getService(ClientSessionStore.class));
}
Also used : ClientModelStore(com.canoo.dp.impl.client.legacy.ClientModelStore) Assert(com.canoo.dp.impl.platform.core.Assert) ClientContextFactory(com.canoo.platform.remoting.client.ClientContextFactory) HttpClient(com.canoo.platform.core.http.HttpClient) AbstractClientConnector(com.canoo.dp.impl.client.legacy.communication.AbstractClientConnector) CompletableFuture(java.util.concurrent.CompletableFuture) API(org.apiguardian.api.API) ClientContext(com.canoo.platform.remoting.client.ClientContext) ClientInitializationException(com.canoo.platform.remoting.client.ClientInitializationException) Function(java.util.function.Function) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) PlatformClient(com.canoo.platform.client.PlatformClient) HttpURLConnectionHandler(com.canoo.platform.core.http.HttpURLConnectionHandler) StrictClientSessionResponseHandler(com.canoo.dp.impl.platform.client.session.StrictClientSessionResponseHandler) URI(java.net.URI) ClientSessionStore(com.canoo.platform.client.session.ClientSessionStore) OptimizedJsonCodec(com.canoo.dp.impl.remoting.codec.OptimizedJsonCodec) ClientConfiguration(com.canoo.platform.client.ClientConfiguration) StrictClientSessionResponseHandler(com.canoo.dp.impl.platform.client.session.StrictClientSessionResponseHandler) ClientSessionStore(com.canoo.platform.client.session.ClientSessionStore) HttpClient(com.canoo.platform.core.http.HttpClient) ClientModelStore(com.canoo.dp.impl.client.legacy.ClientModelStore) HttpURLConnectionHandler(com.canoo.platform.core.http.HttpURLConnectionHandler) AbstractClientConnector(com.canoo.dp.impl.client.legacy.communication.AbstractClientConnector)

Aggregations

HttpClient (com.canoo.platform.core.http.HttpClient)15 PlatformClient (com.canoo.platform.client.PlatformClient)10 HttpResponse (com.canoo.platform.core.http.HttpResponse)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Test (org.testng.annotations.Test)10 CompletableFuture (java.util.concurrent.CompletableFuture)9 HttpHeaderConstants (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants)8 SC_HTTP_RESOURCE_NOTFOUND (com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND)8 SC_HTTP_UNAUTHORIZED (com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED)8 BadResponseException (com.canoo.platform.core.http.BadResponseException)8 ByteArrayProvider (com.canoo.platform.core.http.ByteArrayProvider)8 ConnectionException (com.canoo.platform.core.http.ConnectionException)8 Gson (com.google.gson.Gson)8 ServerSocket (java.net.ServerSocket)8 ExecutionException (java.util.concurrent.ExecutionException)8 TimeUnit (java.util.concurrent.TimeUnit)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 Matchers.is (org.hamcrest.Matchers.is)8 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)8 Assert (org.testng.Assert)8