Search in sources :

Example 1 with AdapterRequest

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

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

the class HttpServiceAdapterProxyTest method callAdapterServiceWithAssertions.

private void callAdapterServiceWithAssertions(TestContext context, String servicePath, Action1<AdapterResponse> onSuccess, Consumer<Throwable> onError) {
    AdapterRequest message = payloadMessage(servicePath);
    Async async = context.async();
    AdapterProxy service = AdapterProxy.createProxy(new Vertx(vertx.vertx()), ADAPTER_ADDRESS);
    service.rxProcess(message).doOnSuccess(onSuccess::call).subscribe(success -> async.complete(), onError);
}
Also used : AdapterRequest(io.knotx.dataobjects.AdapterRequest) Async(io.vertx.ext.unit.Async) AdapterProxy(io.knotx.reactivex.proxy.AdapterProxy) Vertx(io.vertx.reactivex.core.Vertx)

Example 3 with AdapterRequest

use of io.knotx.dataobjects.AdapterRequest 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 4 with AdapterRequest

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

the class ActionKnotProxyVerticleTest method createMockAdapter.

private void createMockAdapter(String address, String addToBody, String signal, Map<String, List<String>> headers) {
    Func1<AdapterRequest, AdapterResponse> adapter = adapterRequest -> {
        ClientResponse response = new ClientResponse();
        response.setStatusCode(HttpResponseStatus.OK.code());
        response.setBody(Buffer.buffer().appendString(addToBody));
        response.setHeaders(headers.keySet().stream().collect(MultiMapCollector.toMultiMap(o -> o, headers::get)));
        return new AdapterResponse().setResponse(response).setSignal(signal);
    };
    new ServiceBinder(vertx.vertx()).setAddress(address).register(AdapterProxy.class, new MockAdapterImpl(adapter));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) KnotContext(io.knotx.dataobjects.KnotContext) Fragment(io.knotx.dataobjects.Fragment) AdapterProxy(io.knotx.proxy.AdapterProxy) ServiceBinder(io.vertx.serviceproxy.ServiceBinder) RunWith(org.junit.runner.RunWith) MultiMapCollector(io.knotx.http.MultiMapCollector) Logback(io.knotx.junit.rule.Logback) Action1(rx.functions.Action1) Parser(org.jsoup.parser.Parser) ClientResponse(io.knotx.dataobjects.ClientResponse) Vertx(io.vertx.reactivex.core.Vertx) Func1(rx.functions.Func1) Lists(com.google.common.collect.Lists) AdapterRequest(io.knotx.dataobjects.AdapterRequest) FileReader(io.knotx.junit.util.FileReader) Map(java.util.Map) AsyncResult(io.vertx.core.AsyncResult) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) KnotProxy(io.knotx.reactivex.proxy.KnotProxy) KnotContextFactory(io.knotx.junit.util.KnotContextFactory) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Consumer(io.reactivex.functions.Consumer) Future(io.vertx.core.Future) RuleChain(org.junit.rules.RuleChain) MultiMap(io.vertx.reactivex.core.MultiMap) List(java.util.List) Rule(org.junit.Rule) AdapterResponse(io.knotx.dataobjects.AdapterResponse) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) Document(org.jsoup.nodes.Document) Optional(java.util.Optional) Entities(org.jsoup.nodes.Entities) Jsoup(org.jsoup.Jsoup) Handler(io.vertx.core.Handler) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Collections(java.util.Collections) ClientResponse(io.knotx.dataobjects.ClientResponse) AdapterRequest(io.knotx.dataobjects.AdapterRequest) ServiceBinder(io.vertx.serviceproxy.ServiceBinder) AdapterResponse(io.knotx.dataobjects.AdapterResponse)

Aggregations

AdapterRequest (io.knotx.dataobjects.AdapterRequest)4 Async (io.vertx.ext.unit.Async)3 Vertx (io.vertx.reactivex.core.Vertx)3 Lists (com.google.common.collect.Lists)2 ClientRequest (io.knotx.dataobjects.ClientRequest)2 ClientResponse (io.knotx.dataobjects.ClientResponse)2 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)2 Logback (io.knotx.junit.rule.Logback)2 TestVertxDeployer (io.knotx.junit.rule.TestVertxDeployer)2 FileReader (io.knotx.junit.util.FileReader)2 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)2 HttpMethod (io.vertx.core.http.HttpMethod)2 JsonObject (io.vertx.core.json.JsonObject)2 TestContext (io.vertx.ext.unit.TestContext)2 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)2 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)2 MultiMap (io.vertx.reactivex.core.MultiMap)2 Collections (java.util.Collections)2 List (java.util.List)2 Rule (org.junit.Rule)2