Search in sources :

Example 11 with WebClient

use of io.vertx.reactivex.ext.web.client.WebClient in project knotx by Cognifide.

the class HttpClientFacadeTest method whenSupportedDynamicPathServiceRequested_expectRequestExecutedAndResponseOKWithBody.

@Test
@KnotxConfiguration("knotx-action-adapter-http-test.json")
public void whenSupportedDynamicPathServiceRequested_expectRequestExecutedAndResponseOKWithBody(TestContext context) throws Exception {
    Async async = context.async();
    // given
    final WebClient mockedWebClient = PowerMockito.spy(webClient());
    HttpClientFacade clientFacade = new HttpClientFacade(mockedWebClient, getConfiguration());
    final JsonObject expectedResponse = new JsonObject(FileReader.readText("first-response.json"));
    // FIXME - params to request
    MultiMap requestParams = MultiMap.caseInsensitiveMultiMap().add("dynamicValue", "first");
    Single<ClientResponse> result = clientFacade.process(payloadMessage(new JsonObject().put("path", "/services/mock/{param.dynamicValue}.json"), clientRequest.setParams(requestParams)), HttpMethod.POST);
    // then
    result.doOnSuccess(response -> {
        context.assertEquals(HttpResponseStatus.OK.code(), response.getStatusCode());
        context.assertEquals(expectedResponse, response.getBody().toJsonObject());
        Mockito.verify(mockedWebClient, Mockito.times(1)).request(HttpMethod.POST, PORT, DOMAIN, REQUEST_PATH);
    }).subscribe(response -> async.complete(), error -> context.fail(error.getMessage()));
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Matchers(org.mockito.Matchers) UnsupportedServiceException(io.knotx.adapter.common.exception.UnsupportedServiceException) RunWith(org.junit.runner.RunWith) Logback(io.knotx.junit.rule.Logback) Single(io.reactivex.Single) ClientResponse(io.knotx.dataobjects.ClientResponse) Vertx(io.vertx.reactivex.core.Vertx) Lists(com.google.common.collect.Lists) AdapterRequest(io.knotx.dataobjects.AdapterRequest) FileReader(io.knotx.junit.util.FileReader) ClientRequest(io.knotx.dataobjects.ClientRequest) JsonObject(io.vertx.core.json.JsonObject) HttpClientFacade(io.knotx.adapter.common.http.HttpClientFacade) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) PowerMockito(org.powermock.api.mockito.PowerMockito) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) HttpAdapterConfiguration(io.knotx.adapter.common.http.HttpAdapterConfiguration) WebClient(io.vertx.reactivex.ext.web.client.WebClient) RuleChain(org.junit.rules.RuleChain) Mockito(org.mockito.Mockito) MultiMap(io.vertx.reactivex.core.MultiMap) List(java.util.List) Rule(org.junit.Rule) ServiceMetadata(io.knotx.adapter.common.http.ServiceMetadata) HttpMethod(io.vertx.core.http.HttpMethod) Pattern(java.util.regex.Pattern) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Collections(java.util.Collections) MultiMap(io.vertx.reactivex.core.MultiMap) Async(io.vertx.ext.unit.Async) HttpClientFacade(io.knotx.adapter.common.http.HttpClientFacade) JsonObject(io.vertx.core.json.JsonObject) WebClient(io.vertx.reactivex.ext.web.client.WebClient) Test(org.junit.Test) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration)

Example 12 with WebClient

use of io.vertx.reactivex.ext.web.client.WebClient in project knotx by Cognifide.

the class HttpClientFacadeTest method whenUnsupportedPathServiceRequested_expectNoServiceRequestAndBadRequest.

@Test
@KnotxConfiguration("knotx-action-adapter-http-test.json")
public void whenUnsupportedPathServiceRequested_expectNoServiceRequestAndBadRequest(TestContext context) throws Exception {
    Async async = context.async();
    // given
    final WebClient mockedWebClient = PowerMockito.spy(webClient());
    HttpClientFacade clientFacade = new HttpClientFacade(mockedWebClient, getConfiguration());
    // when
    Single<ClientResponse> result = clientFacade.process(payloadMessage(new JsonObject().put("path", "/not/supported/path"), clientRequest), HttpMethod.POST);
    // then
    result.doOnError(error -> {
        context.assertEquals(UnsupportedServiceException.class, error.getClass());
        Mockito.verify(mockedWebClient, Mockito.times(0)).request(Matchers.any(), Matchers.anyInt(), Matchers.anyString(), Matchers.anyString());
    }).subscribe(response -> context.fail("Error should occur!"), error -> async.complete());
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Matchers(org.mockito.Matchers) UnsupportedServiceException(io.knotx.adapter.common.exception.UnsupportedServiceException) RunWith(org.junit.runner.RunWith) Logback(io.knotx.junit.rule.Logback) Single(io.reactivex.Single) ClientResponse(io.knotx.dataobjects.ClientResponse) Vertx(io.vertx.reactivex.core.Vertx) Lists(com.google.common.collect.Lists) AdapterRequest(io.knotx.dataobjects.AdapterRequest) FileReader(io.knotx.junit.util.FileReader) ClientRequest(io.knotx.dataobjects.ClientRequest) JsonObject(io.vertx.core.json.JsonObject) HttpClientFacade(io.knotx.adapter.common.http.HttpClientFacade) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) PowerMockito(org.powermock.api.mockito.PowerMockito) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) HttpAdapterConfiguration(io.knotx.adapter.common.http.HttpAdapterConfiguration) WebClient(io.vertx.reactivex.ext.web.client.WebClient) RuleChain(org.junit.rules.RuleChain) Mockito(org.mockito.Mockito) MultiMap(io.vertx.reactivex.core.MultiMap) List(java.util.List) Rule(org.junit.Rule) ServiceMetadata(io.knotx.adapter.common.http.ServiceMetadata) HttpMethod(io.vertx.core.http.HttpMethod) Pattern(java.util.regex.Pattern) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Collections(java.util.Collections) Async(io.vertx.ext.unit.Async) UnsupportedServiceException(io.knotx.adapter.common.exception.UnsupportedServiceException) HttpClientFacade(io.knotx.adapter.common.http.HttpClientFacade) JsonObject(io.vertx.core.json.JsonObject) WebClient(io.vertx.reactivex.ext.web.client.WebClient) Test(org.junit.Test) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration)

Example 13 with WebClient

use of io.vertx.reactivex.ext.web.client.WebClient in project knotx by Cognifide.

the class HttpClientFacadeTest method whenServiceEmptyResponse_expectNoFailure.

@Test
@KnotxConfiguration("knotx-service-adapter-http-test.json")
public void whenServiceEmptyResponse_expectNoFailure(TestContext context) throws Exception {
    Async async = context.async();
    // given
    final WebClient mockedWebClient = PowerMockito.spy(webClient());
    HttpClientFacade clientFacade = new HttpClientFacade(mockedWebClient, getConfiguration());
    // when
    Single<ClientResponse> result = clientFacade.process(payloadMessage("/services/mock/empty.json", new ClientRequest()), HttpMethod.GET);
    // then
    result.doOnSuccess(response -> {
        context.assertEquals(HttpResponseStatus.OK.code(), response.getStatusCode());
        context.assertEquals(0, Integer.valueOf(response.getHeaders().get("Content-Length")));
        Mockito.verify(mockedWebClient, Mockito.times(1)).request(HttpMethod.GET, PORT, DOMAIN, "/services/mock/empty.json");
    }).subscribe(response -> async.complete(), error -> context.fail(error.getMessage()));
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Matchers(org.mockito.Matchers) UnsupportedServiceException(io.knotx.adapter.common.exception.UnsupportedServiceException) RunWith(org.junit.runner.RunWith) Logback(io.knotx.junit.rule.Logback) Single(io.reactivex.Single) AdapterServiceContractException(io.knotx.adapter.common.exception.AdapterServiceContractException) ClientResponse(io.knotx.dataobjects.ClientResponse) Vertx(io.vertx.reactivex.core.Vertx) Lists(com.google.common.collect.Lists) AdapterRequest(io.knotx.dataobjects.AdapterRequest) FileReader(io.knotx.junit.util.FileReader) ClientRequest(io.knotx.dataobjects.ClientRequest) JsonObject(io.vertx.core.json.JsonObject) HttpClientFacade(io.knotx.adapter.common.http.HttpClientFacade) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) PowerMockito(org.powermock.api.mockito.PowerMockito) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) HttpAdapterConfiguration(io.knotx.adapter.common.http.HttpAdapterConfiguration) WebClient(io.vertx.reactivex.ext.web.client.WebClient) RuleChain(org.junit.rules.RuleChain) Mockito(org.mockito.Mockito) MultiMap(io.vertx.reactivex.core.MultiMap) List(java.util.List) Rule(org.junit.Rule) ServiceMetadata(io.knotx.adapter.common.http.ServiceMetadata) HttpMethod(io.vertx.core.http.HttpMethod) Pattern(java.util.regex.Pattern) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Collections(java.util.Collections) Async(io.vertx.ext.unit.Async) HttpClientFacade(io.knotx.adapter.common.http.HttpClientFacade) WebClient(io.vertx.reactivex.ext.web.client.WebClient) ClientRequest(io.knotx.dataobjects.ClientRequest) Test(org.junit.Test) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration)

Aggregations

WebClient (io.vertx.reactivex.ext.web.client.WebClient)13 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)9 Async (io.vertx.ext.unit.Async)9 MultiMap (io.vertx.reactivex.core.MultiMap)9 Test (org.junit.Test)9 Logback (io.knotx.junit.rule.Logback)6 TestVertxDeployer (io.knotx.junit.rule.TestVertxDeployer)6 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)6 Single (io.reactivex.Single)6 JsonObject (io.vertx.core.json.JsonObject)6 TestContext (io.vertx.ext.unit.TestContext)6 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)6 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)6 Vertx (io.vertx.reactivex.core.Vertx)6 List (java.util.List)6 Rule (org.junit.Rule)6 RuleChain (org.junit.rules.RuleChain)6 RunWith (org.junit.runner.RunWith)6 Lists (com.google.common.collect.Lists)5 UnsupportedServiceException (io.knotx.adapter.common.exception.UnsupportedServiceException)5