Search in sources :

Example 1 with ClientRequest

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

the class HttpClientFacadeTest method whenServiceRequestedWithoutPathParam_expectNoServiceRequestAndBadRequest.

@Test
@KnotxConfiguration("knotx-action-adapter-http-test.json")
public void whenServiceRequestedWithoutPathParam_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(new AdapterRequest().setParams(new JsonObject()).setRequest(clientRequest), HttpMethod.POST);
    // then
    result.doOnError(error -> {
        context.assertEquals("Parameter `path` was not defined in `params`!", error.getMessage());
        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) AdapterRequest(io.knotx.dataobjects.AdapterRequest) 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 2 with ClientRequest

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

the class ActionKnotProxyImpl method prepareAdapterRequest.

private AdapterRequest prepareAdapterRequest(KnotContext knotContext, FormEntity formEntity) {
    AdapterMetadata metadata = formEntity.adapter();
    ClientRequest request = new ClientRequest().setPath(knotContext.getClientRequest().getPath()).setMethod(knotContext.getClientRequest().getMethod()).setFormAttributes(knotContext.getClientRequest().getFormAttributes()).setHeaders(getFilteredHeaders(knotContext.getClientRequest().getHeaders(), metadata.getAllowedRequestHeaders()));
    AdapterRequest adapterRequest = new AdapterRequest().setRequest(request).setParams(new JsonObject(metadata.getParams())).setAdapterParams(formEntity.adapterParams());
    LOGGER.info("Adapter [{}] call with request [{}]", metadata.getAddress(), adapterRequest);
    return adapterRequest;
}
Also used : AdapterRequest(io.knotx.dataobjects.AdapterRequest) JsonObject(io.vertx.core.json.JsonObject) AdapterMetadata(io.knotx.knot.action.ActionKnotConfiguration.AdapterMetadata) ClientRequest(io.knotx.dataobjects.ClientRequest)

Example 3 with ClientRequest

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

the class MockActionAdapterHandler method handle.

@Override
public void handle(Message<AdapterRequest> message) {
    ClientRequest request = message.body().getRequest();
    JsonObject params = message.body().getParams();
    String resourcePath = getFilePath(params.getString("step"));
    fileSystem.readFile(resourcePath, ar -> {
        if (ar.succeeded()) {
            final JsonObject transitions = ar.result().toJsonObject();
            message.reply(replyTransition(request, transitions));
        } else {
            LOGGER.error("Unable to read file. {}", ar.cause());
            message.reply(errorResponse());
        }
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) ClientRequest(io.knotx.dataobjects.ClientRequest)

Example 4 with ClientRequest

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

the class MockAdapterHandler method handle.

@Override
public void handle(Message<AdapterRequest> message) {
    ClientRequest request = message.body().getRequest();
    JsonObject params = message.body().getParams();
    String resourcePath = getFilePath(params.getString("path"));
    fileSystem.readFile(resourcePath, ar -> {
        if (ar.succeeded()) {
            String mockData = ar.result().toString();
            message.reply(okResponse(request, mockData));
        } else {
            LOGGER.error("Unable to read file. {}", ar.cause());
            message.reply(errorResponse());
        }
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) ClientRequest(io.knotx.dataobjects.ClientRequest)

Example 5 with ClientRequest

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

the class FilesystemRepositoryConnectorProxyImpl method process.

@Override
public void process(ClientRequest request, Handler<AsyncResult<ClientResponse>> result) {
    final String localFilePath = catalogue + StringUtils.stripStart(request.getPath(), "/");
    final Optional<String> contentType = Optional.ofNullable(MimeMapping.getMimeTypeForFilename(localFilePath));
    LOGGER.debug("Fetching file `{}` from local repository.", localFilePath);
    fileSystem.rxReadFile(localFilePath).map(buffer -> new ClientResponse().setStatusCode(HttpResponseStatus.OK.code()).setHeaders(headers(contentType)).setBody(buffer.getDelegate())).subscribe(response -> result.handle(Future.succeededFuture(response)), error -> {
        LOGGER.error(ERROR_MESSAGE, error);
        result.handle(Future.succeededFuture(processError(error)));
    });
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) Vertx(io.vertx.core.Vertx) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) MimeMapping(io.vertx.core.http.impl.MimeMapping) RepositoryConnectorProxy(io.knotx.proxy.RepositoryConnectorProxy) Future(io.vertx.core.Future) StringUtils(org.apache.commons.lang3.StringUtils) LoggerFactory(io.vertx.core.logging.LoggerFactory) ClientResponse(io.knotx.dataobjects.ClientResponse) MultiMap(io.vertx.reactivex.core.MultiMap) ClientRequest(io.knotx.dataobjects.ClientRequest) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) FileSystem(io.vertx.reactivex.core.file.FileSystem) ClientResponse(io.knotx.dataobjects.ClientResponse)

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