Search in sources :

Example 6 with ClientResponse

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

the class KnotxServerRoutingTest method createSimpleGatewayKnot.

private void createSimpleGatewayKnot(final String address, final String transition) {
    Action1<KnotContext> simpleKnot = knotContext -> {
        ClientResponse clientResponse = new ClientResponse();
        clientResponse.setBody(Buffer.buffer());
        clientResponse.setStatusCode(200);
        knotContext.setClientResponse(clientResponse);
        knotContext.setTransition(transition);
    };
    MockKnotProxy.register(vertx.vertx(), address, simpleKnot);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) KnotContext(io.knotx.dataobjects.KnotContext) RunWith(org.junit.runner.RunWith) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Logback(io.knotx.junit.rule.Logback) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Action1(rx.functions.Action1) ClientResponse(io.knotx.dataobjects.ClientResponse) RuleChain(org.junit.rules.RuleChain) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) MultiMap(io.vertx.reactivex.core.MultiMap) Vertx(io.vertx.reactivex.core.Vertx) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) Observable(io.reactivex.Observable) HttpClient(io.vertx.reactivex.core.http.HttpClient) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) ClientResponse(io.knotx.dataobjects.ClientResponse) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) KnotContext(io.knotx.dataobjects.KnotContext)

Example 7 with ClientResponse

use of io.knotx.dataobjects.ClientResponse 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)

Example 8 with ClientResponse

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

the class MockActionAdapterHandler method replyTransition.

private AdapterResponse replyTransition(ClientRequest request, JsonObject transitions) {
    final Pair<Optional<String>, JsonObject> result = getTransitionResult(request, transitions);
    final JsonObject resultBody = result.getRight().put("form", toJsonObject(request.getFormAttributes()));
    final String data = resultBody.toString();
    final ClientResponse clientResponse = new ClientResponse().setHeaders(headers(request, data)).setStatusCode(HttpResponseStatus.OK.code()).setBody(Buffer.buffer(data));
    final AdapterResponse response = new AdapterResponse().setResponse(clientResponse);
    final Optional<String> transition = result.getLeft();
    if (transition.isPresent()) {
        response.setSignal(transition.get());
    }
    return response;
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) AdapterResponse(io.knotx.dataobjects.AdapterResponse)

Example 9 with ClientResponse

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

the class HandlebarsKnotProxyImpl method processError.

@Override
protected KnotContext processError(KnotContext knotContext, Throwable error) {
    LOGGER.error("Error happened during Template processing", error);
    ClientResponse errorResponse = new ClientResponse().setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
    return new KnotContext().setClientRequest(knotContext.getClientRequest()).setClientResponse(errorResponse);
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) KnotContext(io.knotx.dataobjects.KnotContext)

Example 10 with ClientResponse

use of io.knotx.dataobjects.ClientResponse 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)

Aggregations

ClientResponse (io.knotx.dataobjects.ClientResponse)14 MultiMap (io.vertx.reactivex.core.MultiMap)9 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)8 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)7 Logback (io.knotx.junit.rule.Logback)7 TestVertxDeployer (io.knotx.junit.rule.TestVertxDeployer)7 HttpMethod (io.vertx.core.http.HttpMethod)7 JsonObject (io.vertx.core.json.JsonObject)7 Async (io.vertx.ext.unit.Async)7 TestContext (io.vertx.ext.unit.TestContext)7 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)7 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)7 Vertx (io.vertx.reactivex.core.Vertx)7 Rule (org.junit.Rule)7 Test (org.junit.Test)7 RuleChain (org.junit.rules.RuleChain)7 RunWith (org.junit.runner.RunWith)7 Lists (com.google.common.collect.Lists)6 AdapterRequest (io.knotx.dataobjects.AdapterRequest)6 ClientRequest (io.knotx.dataobjects.ClientRequest)6