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();
}
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();
}
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);
}
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);
}
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();
}
Aggregations