Search in sources :

Example 11 with IcecreamServiceApi

use of reactivefeign.testcase.IcecreamServiceApi in project feign-reactive by kptfh.

the class RetryingTest method shouldSuccessOnRetriesMono.

@Test
public void shouldSuccessOnRetriesMono() throws JsonProcessingException {
    IceCreamOrder orderGenerated = new OrderGenerator().generate(1);
    String orderStr = TestUtils.MAPPER.writeValueAsString(orderGenerated);
    mockResponseAfterSeveralAttempts(wireMockRule, 2, "testRetrying_success", "/icecream/orders/1", aResponse().withStatus(503).withHeader(RETRY_AFTER, "1"), aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(orderStr));
    IcecreamServiceApi client = builder().retryWhen(ReactiveRetryers.retryWithBackoff(3, 0)).target(IcecreamServiceApi.class, "http://localhost:" + wireMockRule.port());
    StepVerifier.create(client.findOrder(1)).expectNextMatches(equalsComparingFieldByFieldRecursively(orderGenerated)).verifyComplete();
}
Also used : IcecreamServiceApi(reactivefeign.testcase.IcecreamServiceApi) IceCreamOrder(reactivefeign.testcase.domain.IceCreamOrder) OrderGenerator(reactivefeign.testcase.domain.OrderGenerator) Test(org.junit.Test)

Example 12 with IcecreamServiceApi

use of reactivefeign.testcase.IcecreamServiceApi in project feign-reactive by kptfh.

the class StatusHandlerTest method shouldThrowOnStatusCode.

@Test
public void shouldThrowOnStatusCode() {
    wireMockRule.stubFor(get(urlEqualTo("/icecream/orders/1")).withHeader("Accept", equalTo("application/json")).willReturn(aResponse().withStatus(HttpStatus.SC_SERVICE_UNAVAILABLE)));
    wireMockRule.stubFor(get(urlEqualTo("/icecream/orders/2")).withHeader("Accept", equalTo("application/json")).willReturn(aResponse().withStatus(HttpStatus.SC_UNAUTHORIZED)));
    IcecreamServiceApi client = builder().statusHandler(compose(throwOnStatus(status -> status == HttpStatus.SC_SERVICE_UNAVAILABLE, (methodTag, response) -> new RetryableException("Should retry on next node", null)), throwOnStatus(status -> status == HttpStatus.SC_UNAUTHORIZED, (methodTag, response) -> new RuntimeException("Should login", null)))).target(IcecreamServiceApi.class, "http://localhost:" + wireMockRule.port());
    StepVerifier.create(client.findFirstOrder()).expectError(RetryableException.class).verify();
    StepVerifier.create(client.findOrder(2)).expectError(RuntimeException.class).verify();
}
Also used : RetryableException(feign.RetryableException) ReactiveStatusHandlers.throwOnStatus(reactivefeign.client.statushandler.ReactiveStatusHandlers.throwOnStatus) StepVerifier(reactor.test.StepVerifier) WireMockConfiguration.wireMockConfig(com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig) WireMockClassRule(com.github.tomakehurst.wiremock.junit.WireMockClassRule) CompositeStatusHandler.compose(reactivefeign.client.statushandler.CompositeStatusHandler.compose) IcecreamServiceApi(reactivefeign.testcase.IcecreamServiceApi) HttpStatus(org.apache.http.HttpStatus) Test(org.junit.Test) ClassRule(org.junit.ClassRule) Before(org.junit.Before) WireMock(com.github.tomakehurst.wiremock.client.WireMock) RetryableException(feign.RetryableException) IcecreamServiceApi(reactivefeign.testcase.IcecreamServiceApi) Test(org.junit.Test)

Example 13 with IcecreamServiceApi

use of reactivefeign.testcase.IcecreamServiceApi in project feign-reactive by kptfh.

the class LoggerTest method shouldLog.

@Test
public void shouldLog() throws Exception {
    Level originalLevel = setLogLevel(Level.TRACE);
    IceCreamOrder order = new OrderGenerator().generate(20);
    Bill billExpected = Bill.makeBill(order);
    wireMockRule.stubFor(post(urlEqualTo("/icecream/orders")).withRequestBody(equalTo(TestUtils.MAPPER.writeValueAsString(order))).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(TestUtils.MAPPER.writeValueAsString(billExpected))));
    IcecreamServiceApi client = builder().target(IcecreamServiceApi.class, "http://localhost:" + wireMockRule.port());
    Mono<Bill> billMono = client.makeOrder(order);
    // no logs before subscription
    ArgumentCaptor<LogEvent> argumentCaptor = ArgumentCaptor.forClass(LogEvent.class);
    Mockito.verify(appender, never()).append(argumentCaptor.capture());
    billMono.block();
    Mockito.verify(appender, times(7)).append(argumentCaptor.capture());
    List<LogEvent> logEvents = argumentCaptor.getAllValues();
    assertLogEvent(logEvents, 0, Level.DEBUG, "[IcecreamServiceApi#makeOrder]--->POST http://localhost");
    assertLogEvent(logEvents, 1, Level.TRACE, "[IcecreamServiceApi#makeOrder] REQUEST HEADERS\n" + "Accept:[application/json]");
    assertLogEvent(logEvents, 2, Level.TRACE, "[IcecreamServiceApi#makeOrder] REQUEST BODY\n" + "IceCreamOrder{ id=20, balls=");
    assertLogEvent(logEvents, 3, Level.TRACE, "[IcecreamServiceApi#makeOrder] RESPONSE HEADERS", "Content-Type:application/json");
    assertLogEvent(logEvents, 4, Level.DEBUG, "[IcecreamServiceApi#makeOrder]<--- headers takes");
    assertLogEvent(logEvents, 5, Level.TRACE, "[IcecreamServiceApi#makeOrder] RESPONSE BODY\n" + "reactivefeign.testcase.domain.Bill");
    assertLogEvent(logEvents, 6, Level.DEBUG, "[IcecreamServiceApi#makeOrder]<--- body takes");
    setLogLevel(originalLevel);
}
Also used : IcecreamServiceApi(reactivefeign.testcase.IcecreamServiceApi) LogEvent(org.apache.logging.log4j.core.LogEvent) Bill(reactivefeign.testcase.domain.Bill) IceCreamOrder(reactivefeign.testcase.domain.IceCreamOrder) Level(org.apache.logging.log4j.Level) OrderGenerator(reactivefeign.testcase.domain.OrderGenerator) Test(org.junit.Test)

Example 14 with IcecreamServiceApi

use of reactivefeign.testcase.IcecreamServiceApi in project feign-reactive by kptfh.

the class ReactivityTest method shouldRunReactively.

@Test
public void shouldRunReactively() throws JsonProcessingException {
    IceCreamOrder orderGenerated = new OrderGenerator().generate(1);
    String orderStr = TestUtils.MAPPER.writeValueAsString(orderGenerated);
    wireMockRule.stubFor(get(urlEqualTo("/icecream/orders/1")).withHeader("Accept", equalTo("application/json")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(orderStr).withFixedDelay(DELAY_IN_MILLIS)));
    IcecreamServiceApi client = builder().target(IcecreamServiceApi.class, "http://localhost:" + wireMockRule.port());
    AtomicInteger counter = new AtomicInteger();
    new Thread(() -> {
        for (int i = 0; i < CALLS_NUMBER; i++) {
            client.findFirstOrder().doOnNext(order -> counter.incrementAndGet()).subscribe();
        }
    }).start();
    waitAtMost(new Duration(timeToCompleteReactively(), TimeUnit.MILLISECONDS)).until(() -> counter.get() == CALLS_NUMBER);
}
Also used : IcecreamServiceApi(reactivefeign.testcase.IcecreamServiceApi) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IceCreamOrder(reactivefeign.testcase.domain.IceCreamOrder) Duration(org.awaitility.Duration) OrderGenerator(reactivefeign.testcase.domain.OrderGenerator) Test(org.junit.Test)

Example 15 with IcecreamServiceApi

use of reactivefeign.testcase.IcecreamServiceApi in project feign-reactive by kptfh.

the class RequestInterceptorTest method shouldInterceptRequestAndSetAuthHeader.

@Test
public void shouldInterceptRequestAndSetAuthHeader() throws JsonProcessingException {
    String orderUrl = "/icecream/orders/1";
    IceCreamOrder orderGenerated = new OrderGenerator().generate(1);
    String orderStr = TestUtils.MAPPER.writeValueAsString(orderGenerated);
    wireMockRule.stubFor(get(urlEqualTo(orderUrl)).withHeader("Accept", equalTo("application/json")).willReturn(aResponse().withStatus(HttpStatus.SC_UNAUTHORIZED))).setPriority(100);
    wireMockRule.stubFor(get(urlEqualTo(orderUrl)).withHeader("Accept", equalTo("application/json")).withHeader("Authorization", equalTo("Bearer mytoken123")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(orderStr))).setPriority(1);
    IcecreamServiceApi clientWithoutAuth = builder().target(IcecreamServiceApi.class, "http://localhost:" + wireMockRule.port());
    StepVerifier.create(clientWithoutAuth.findFirstOrder()).expectError(notAuthorizedException()).verify();
    IcecreamServiceApi clientWithAuth = builder().addHeaders(singletonList(new Pair<>("Authorization", "Bearer mytoken123"))).target(IcecreamServiceApi.class, "http://localhost:" + wireMockRule.port());
    StepVerifier.create(clientWithAuth.findFirstOrder()).expectNextMatches(equalsComparingFieldByFieldRecursively(orderGenerated)).expectComplete();
}
Also used : IcecreamServiceApi(reactivefeign.testcase.IcecreamServiceApi) IceCreamOrder(reactivefeign.testcase.domain.IceCreamOrder) OrderGenerator(reactivefeign.testcase.domain.OrderGenerator) Test(org.junit.Test)

Aggregations

IcecreamServiceApi (reactivefeign.testcase.IcecreamServiceApi)22 Test (org.junit.Test)21 IceCreamOrder (reactivefeign.testcase.domain.IceCreamOrder)12 OrderGenerator (reactivefeign.testcase.domain.OrderGenerator)12 WireMockConfiguration.wireMockConfig (com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig)6 WireMockClassRule (com.github.tomakehurst.wiremock.junit.WireMockClassRule)6 Before (org.junit.Before)6 ClassRule (org.junit.ClassRule)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 WireMock (com.github.tomakehurst.wiremock.client.WireMock)4 RetryableException (feign.RetryableException)4 StepVerifier (reactor.test.StepVerifier)4 TestUtils.equalsComparingFieldByFieldRecursively (reactivefeign.TestUtils.equalsComparingFieldByFieldRecursively)3 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)2 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)2 WireMock.equalTo (com.github.tomakehurst.wiremock.client.WireMock.equalTo)2 WireMock.get (com.github.tomakehurst.wiremock.client.WireMock.get)2 WireMock.urlEqualTo (com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo)2 STARTED (com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED)2 Arrays (java.util.Arrays)2