Search in sources :

Example 6 with ClientRequest

use of io.knotx.dataobjects.ClientRequest in project knotx by Cognifide.

the class HttpClientFacadeTest method whenSupportedStaticPathServiceRequested_expectRequestExecutedAndResponseOKWithBody.

@Test
@KnotxConfiguration("knotx-action-adapter-http-test.json")
public void whenSupportedStaticPathServiceRequested_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"));
    // when
    Single<ClientResponse> result = clientFacade.process(payloadMessage(new JsonObject().put("path", REQUEST_PATH), clientRequest), 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) 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 7 with ClientRequest

use of io.knotx.dataobjects.ClientRequest 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 8 with ClientRequest

use of io.knotx.dataobjects.ClientRequest in project knotx by Cognifide.

the class HttpClientFacade method prepareRequestData.

private Pair<ClientRequest, ServiceMetadata> prepareRequestData(AdapterRequest adapterRequest) {
    final Pair<ClientRequest, ServiceMetadata> serviceData;
    final JsonObject params = adapterRequest.getParams();
    final ClientRequest serviceRequest = buildServiceRequest(adapterRequest.getRequest(), params);
    final Optional<ServiceMetadata> serviceMetadata = findServiceMetadata(serviceRequest.getPath());
    if (serviceMetadata.isPresent()) {
        final ServiceMetadata metadata = serviceMetadata.get();
        if (params.containsKey(HEADERS_PROPERTY_KEY)) {
            metadata.setAdditionalHeaders(params.getJsonObject(HEADERS_PROPERTY_KEY));
        }
        if (params.containsKey(QUERY_PARAMS_PROPERTY_KEY)) {
            metadata.setQueryParams(params.getJsonObject(QUERY_PARAMS_PROPERTY_KEY));
        }
        serviceData = Pair.of(serviceRequest, metadata);
    } else {
        final String error = String.format("No matching service definition for the requested path '%s'", serviceRequest.getPath());
        throw new UnsupportedServiceException(error);
    }
    return serviceData;
}
Also used : UnsupportedServiceException(io.knotx.adapter.common.exception.UnsupportedServiceException) JsonObject(io.vertx.core.json.JsonObject) ClientRequest(io.knotx.dataobjects.ClientRequest)

Example 9 with ClientRequest

use of io.knotx.dataobjects.ClientRequest in project knotx by Cognifide.

the class HttpClientFacade method callService.

private Single<HttpResponse<Buffer>> callService(Pair<ClientRequest, ServiceMetadata> serviceData, HttpMethod method) {
    final Single<HttpResponse<Buffer>> httpResponse;
    final ClientRequest serviceRequest = serviceData.getLeft();
    final ServiceMetadata serviceMetadata = serviceData.getRight();
    final HttpRequest<Buffer> request = webClient.request(method, serviceMetadata.getPort(), serviceMetadata.getDomain(), serviceRequest.getPath());
    updateRequestQueryParams(request, serviceMetadata);
    updateRequestHeaders(request, serviceRequest, serviceMetadata);
    overrideRequestHeaders(request, serviceMetadata);
    if (!serviceRequest.getFormAttributes().isEmpty()) {
        httpResponse = request.rxSendForm(serviceRequest.getFormAttributes());
    } else {
        httpResponse = request.rxSend();
    }
    return httpResponse;
}
Also used : Buffer(io.vertx.reactivex.core.buffer.Buffer) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) ClientRequest(io.knotx.dataobjects.ClientRequest)

Example 10 with ClientRequest

use of io.knotx.dataobjects.ClientRequest in project knotx by Cognifide.

the class HttpClientFacade method updateRequestHeaders.

private void updateRequestHeaders(HttpRequest<Buffer> request, ClientRequest serviceRequest, ServiceMetadata serviceMetadata) {
    MultiMap filteredHeaders = getFilteredHeaders(serviceRequest.getHeaders(), serviceMetadata.getAllowedRequestHeaderPatterns());
    filteredHeaders.names().forEach(headerName -> filteredHeaders.getAll(headerName).forEach(value -> request.headers().add(headerName, value)));
    if (customRequestHeader.containsKey("name") && customRequestHeader.containsKey("value")) {
        request.headers().set(customRequestHeader.getString("name"), customRequestHeader.getString("value"));
    }
}
Also used : HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) UnsupportedServiceException(io.knotx.adapter.common.exception.UnsupportedServiceException) MultiMapCollector(io.knotx.http.MultiMapCollector) Single(io.reactivex.Single) LoggerFactory(io.vertx.core.logging.LoggerFactory) WebClient(io.vertx.reactivex.ext.web.client.WebClient) AdapterServiceContractException(io.knotx.adapter.common.exception.AdapterServiceContractException) ClientResponse(io.knotx.dataobjects.ClientResponse) DataObjectsUtil(io.knotx.util.DataObjectsUtil) MultiMap(io.vertx.reactivex.core.MultiMap) List(java.util.List) AdapterRequest(io.knotx.dataobjects.AdapterRequest) Pair(org.apache.commons.lang3.tuple.Pair) UriTransformer(io.knotx.adapter.common.placeholders.UriTransformer) AllowedHeadersFilter(io.knotx.http.AllowedHeadersFilter) HttpMethod(io.vertx.core.http.HttpMethod) ClientRequest(io.knotx.dataobjects.ClientRequest) HttpRequest(io.vertx.reactivex.ext.web.client.HttpRequest) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) Pattern(java.util.regex.Pattern) Logger(io.vertx.core.logging.Logger) Buffer(io.vertx.reactivex.core.buffer.Buffer) MultiMap(io.vertx.reactivex.core.MultiMap)

Aggregations

ClientRequest (io.knotx.dataobjects.ClientRequest)12 JsonObject (io.vertx.core.json.JsonObject)10 UnsupportedServiceException (io.knotx.adapter.common.exception.UnsupportedServiceException)6 AdapterRequest (io.knotx.dataobjects.AdapterRequest)6 ClientResponse (io.knotx.dataobjects.ClientResponse)6 MultiMap (io.vertx.reactivex.core.MultiMap)6 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)5 Single (io.reactivex.Single)5 HttpMethod (io.vertx.core.http.HttpMethod)5 WebClient (io.vertx.reactivex.ext.web.client.WebClient)5 List (java.util.List)5 Pattern (java.util.regex.Pattern)5 Test (org.junit.Test)5 Lists (com.google.common.collect.Lists)4 HttpAdapterConfiguration (io.knotx.adapter.common.http.HttpAdapterConfiguration)4 HttpClientFacade (io.knotx.adapter.common.http.HttpClientFacade)4 ServiceMetadata (io.knotx.adapter.common.http.ServiceMetadata)4 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)4 Logback (io.knotx.junit.rule.Logback)4 TestVertxDeployer (io.knotx.junit.rule.TestVertxDeployer)4