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);
}
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);
}
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);
}
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);
}
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));
}
Aggregations