Search in sources :

Example 61 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-cloud-gateway by spring-cloud.

the class SseIntegrationTests method sseAsEvent.

@Test
@SuppressWarnings("Duplicates")
public void sseAsEvent() {
    ResolvableType type = forClassWithGenerics(ServerSentEvent.class, String.class);
    Flux<ServerSentEvent<String>> result = this.webClient.get().uri("/event").accept(TEXT_EVENT_STREAM).exchange().flatMapMany(response -> response.body(toFlux(new ParameterizedTypeReference<ServerSentEvent<String>>() {
    })));
    StepVerifier.create(result).consumeNextWith(event -> {
        assertEquals("0", event.id());
        assertEquals("foo", event.data());
        assertEquals("bar", event.comment());
        assertNull(event.event());
        assertNull(event.retry());
    }).consumeNextWith(event -> {
        assertEquals("1", event.id());
        assertEquals("foo", event.data());
        assertEquals("bar", event.comment());
        assertNull(event.event());
        assertNull(event.retry());
    }).thenCancel().verify(Duration.ofSeconds(5L));
}
Also used : ReactorHttpServer(org.springframework.cloud.gateway.test.support.ReactorHttpServer) StepVerifier(reactor.test.StepVerifier) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) PermitAllSecurityConfiguration(org.springframework.cloud.gateway.test.PermitAllSecurityConfiguration) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) WebClient(org.springframework.web.reactive.function.client.WebClient) RouteLocator(org.springframework.cloud.gateway.route.RouteLocator) DispatcherHandler(org.springframework.web.reactive.DispatcherHandler) TEXT_EVENT_STREAM(org.springframework.http.MediaType.TEXT_EVENT_STREAM) Value(org.springframework.beans.factory.annotation.Value) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) EnableWebFlux(org.springframework.web.reactive.config.EnableWebFlux) Duration(java.time.Duration) After(org.junit.After) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ResolvableType(org.springframework.core.ResolvableType) Before(org.junit.Before) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) RouteLocatorBuilder(org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder) BodyExtractors.toFlux(org.springframework.web.reactive.function.BodyExtractors.toFlux) Import(org.springframework.context.annotation.Import) Test(org.junit.Test) RestController(org.springframework.web.bind.annotation.RestController) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) Flux(reactor.core.publisher.Flux) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Assert.assertNull(org.junit.Assert.assertNull) HttpServer(org.springframework.cloud.gateway.test.support.HttpServer) ResolvableType.forClassWithGenerics(org.springframework.core.ResolvableType.forClassWithGenerics) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Bean(org.springframework.context.annotation.Bean) Assert.assertEquals(org.junit.Assert.assertEquals) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 62 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project apollo by ctripcorp.

the class RetryableRestTemplateTest method testGetEntityWithNoAccessTokenForEnv.

@Test
public void testGetEntityWithNoAccessTokenForEnv() {
    Env someEnv = Env.DEV;
    Env anotherEnv = Env.PRO;
    String someToken = "someToken";
    ParameterizedTypeReference requestType = mock(ParameterizedTypeReference.class);
    ResponseEntity someEntity = mock(ResponseEntity.class);
    when(portalConfig.getAdminServiceAccessTokens()).thenReturn(mockAdminServiceTokens(someEnv, someToken));
    when(serviceAddressLocator.getServiceList(someEnv)).thenReturn(Collections.singletonList(mockService(serviceOne)));
    when(serviceAddressLocator.getServiceList(anotherEnv)).thenReturn(Collections.singletonList(mockService(serviceTwo)));
    when(restTemplate.exchange(eq(serviceTwo + "/" + path), eq(HttpMethod.GET), any(HttpEntity.class), eq(requestType))).thenReturn(someEntity);
    ResponseEntity actualResult = retryableRestTemplate.get(anotherEnv, path, requestType);
    assertEquals(someEntity, actualResult);
    ArgumentCaptor<HttpEntity> argumentCaptor = ArgumentCaptor.forClass(HttpEntity.class);
    verify(restTemplate, times(1)).exchange(eq(serviceTwo + "/" + path), eq(HttpMethod.GET), argumentCaptor.capture(), eq(requestType));
    HttpHeaders headers = argumentCaptor.getValue().getHeaders();
    assertTrue(headers.isEmpty());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Env(com.ctrip.framework.apollo.portal.environment.Env) Test(org.junit.Test)

Aggregations

ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)62 MediaType (org.springframework.http.MediaType)29 HttpHeaders (org.springframework.http.HttpHeaders)28 List (java.util.List)22 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)22 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)18 ArrayList (java.util.ArrayList)16 Test (org.junit.jupiter.api.Test)16 Test (org.junit.Test)15 HashMap (java.util.HashMap)12 URI (java.net.URI)11 HttpEntity (org.springframework.http.HttpEntity)10 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)8 URISyntaxException (java.net.URISyntaxException)7 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)7 DataBuffer (org.springframework.core.io.buffer.DataBuffer)6 Resources (org.springframework.hateoas.Resources)5 Traverson (org.springframework.hateoas.client.Traverson)5 IOException (java.io.IOException)4 HttpMethod (org.springframework.http.HttpMethod)4