Search in sources :

Example 1 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project java-chassis by ServiceComb.

the class TestRestTemplateWrapper method testRestTemplate.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRestTemplate() {
    new MockUp<RestTemplateWrapper>() {

        @Mock
        private boolean isCse(URI url) {
            return false;
        }
    };
    try {
        MockUtil.getInstance().mockConsumerProviderManager();
        MockUtil.getInstance().mockRegisterManager();
        MockUtil.getInstance().mockServicePathManager();
        MockUtil.getInstance().mockOperationLocator();
        MockUtil.getInstance().mockOperationMeta();
        MockUtil.getInstance().mockSchemaMeta();
        MockUtil.getInstance().mockSchemaUtils();
        MockUtil.getInstance().mockBeanUtils();
        MockUtil.getInstance().mockRequestMeta();
        MockUtil.getInstance().mockInvokerUtils();
        MockUtil.getInstance().mockRestTemplate();
        RestTemplateWrapper lRestTemplateWrapper = (RestTemplateWrapper) RestTemplateBuilder.create();
        lRestTemplateWrapper.put(URI.create("csd://test"), new Object());
        lRestTemplateWrapper.delete(URI.create("cse://test"));
        lRestTemplateWrapper.put("csd://test", new Object());
        lRestTemplateWrapper.getForObject("csd://test", new Object().getClass());
        lRestTemplateWrapper.getForEntity("csd://test", new Object().getClass());
        lRestTemplateWrapper.postForObject("csd://test", new Object(), new Object().getClass());
        lRestTemplateWrapper.postForEntity("csd://test", new Object(), new Object().getClass());
        lRestTemplateWrapper.exchange("csd://test", HttpMethod.GET, HttpEntity.EMPTY, new Object().getClass());
        lRestTemplateWrapper.delete("csd://test", new Object());
        lRestTemplateWrapper.delete("csd://test", new HashMap<>());
        Map<String, ?> uriVariables = new HashMap<>();
        lRestTemplateWrapper.getForObject("csd://test", new Object().getClass(), uriVariables);
        lRestTemplateWrapper.getForEntity("csd://test", new Object().getClass(), uriVariables);
        lRestTemplateWrapper.getForObject(URI.create("cse://test"), new Object().getClass());
        lRestTemplateWrapper.getForEntity(URI.create("cse://test"), new Object().getClass());
        lRestTemplateWrapper.postForObject("csd://test", new Object(), new Object().getClass(), uriVariables);
        lRestTemplateWrapper.postForObject(URI.create("cse://test"), new Object(), new Object().getClass());
        lRestTemplateWrapper.postForEntity("csd://test", new Object(), new Object().getClass(), uriVariables);
        lRestTemplateWrapper.postForEntity(URI.create("cse://test"), new Object(), new Object().getClass());
        lRestTemplateWrapper.put("csd://test", new Object(), uriVariables);
        lRestTemplateWrapper.exchange("csd://test", HttpMethod.GET, HttpEntity.EMPTY, new Object().getClass(), uriVariables);
        ParameterizedTypeReference instance = Mockito.mock(ParameterizedTypeReference.class);
        lRestTemplateWrapper.exchange("csd://test", HttpMethod.GET, HttpEntity.EMPTY, instance, uriVariables);
        lRestTemplateWrapper.exchange("csd://test", HttpMethod.GET, HttpEntity.EMPTY, instance, new Object().getClass());
        RequestEntity requestEntity = new RequestEntity<>(HttpMethod.GET, URI.create("cse://test"));
        lRestTemplateWrapper.exchange(requestEntity, new Object().getClass());
        lRestTemplateWrapper.exchange(requestEntity, instance);
        lRestTemplateWrapper.exchange(URI.create("cse://test"), HttpMethod.GET, HttpEntity.EMPTY, new Object().getClass());
        lRestTemplateWrapper.exchange(URI.create("cse://test"), HttpMethod.GET, HttpEntity.EMPTY, instance);
        Assert.assertTrue(RestTemplateBuilder.create().getClass().isAssignableFrom(RestTemplateWrapper.class));
    } catch (Exception e) {
        Assert.assertNotNull(e);
    }
}
Also used : HashMap(java.util.HashMap) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MockUp(mockit.MockUp) RequestEntity(org.springframework.http.RequestEntity) URI(java.net.URI) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class RestTemplateIntegrationTests method jsonPostForObjectWithJacksonTypeInfoList.

// SPR-13154
@Test
public void jsonPostForObjectWithJacksonTypeInfoList() throws URISyntaxException {
    List<ParentClass> list = new ArrayList<>();
    list.add(new Foo("foo"));
    list.add(new Bar("bar"));
    ParameterizedTypeReference<?> typeReference = new ParameterizedTypeReference<List<ParentClass>>() {
    };
    RequestEntity<List<ParentClass>> entity = RequestEntity.post(new URI(baseUrl + "/jsonpost")).contentType(new MediaType("application", "json", StandardCharsets.UTF_8)).body(list, typeReference.getType());
    String content = template.exchange(entity, String.class).getBody();
    assertTrue(content.contains("\"type\":\"foo\""));
    assertTrue(content.contains("\"type\":\"bar\""));
}
Also used : ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) URI(java.net.URI) Test(org.junit.Test)

Example 3 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project amos-ss17-alexa by c-i-ber.

the class AccountAPI method getCardsForAccount.

/**
 * Get all cards (credit / debit) for the given account.
 *
 * @param accountNumber Account number
 * @return Collection of Cards
 * @throws HttpClientErrorException
 */
public static Collection<Card> getCardsForAccount(String accountNumber) throws HttpClientErrorException {
    // TODO: Create a generic method for getting embedded JSON-HAL collections (in BankingRESTClient)
    Traverson traverson = null;
    try {
        traverson = new Traverson(new URI(BankingRESTClient.BANKING_API_ENDPOINT + BankingRESTClient.BANKING_API_BASEURL_V2 + "/accounts/" + accountNumber + "/cards"), MediaTypes.HAL_JSON);
    } catch (URISyntaxException e) {
        log.error("getCardsForAccount failed", e);
        return null;
    }
    ParameterizedTypeReference<Resources<Card>> typeRefDevices = new ParameterizedTypeReference<Resources<Card>>() {
    };
    Resources<Card> resResponses = traverson.follow(rel("$._links.self.href")).withHeaders(bankingRESTClient.generateHttpHeaders()).toObject(typeRefDevices);
    return resResponses.getContent();
}
Also used : Traverson(org.springframework.hateoas.client.Traverson) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) URISyntaxException(java.net.URISyntaxException) Resources(org.springframework.hateoas.Resources) URI(java.net.URI) Card(model.banking.Card)

Example 4 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project amos-ss17-alexa by c-i-ber.

the class TransactionAPI method getTransactions.

/**
 * Get all transactions for the given account.
 *
 * @param accountNumber Account number
 * @return Collection of Cards
 * @throws HttpClientErrorException
 */
private static Collection<Transaction> getTransactions(String accountNumber) throws HttpClientErrorException {
    // TODO: Create a generic method for getting embedded JSON-HAL collections (in BankingRESTClient)
    Traverson traverson = null;
    String uri = BankingRESTClient.BANKING_API_ENDPOINT + BankingRESTClient.BANKING_API_BASEURL_V2 + "/accounts/" + accountNumber + "/transactions";
    log.info("URI: " + uri);
    try {
        traverson = new Traverson(new URI(uri), MediaTypes.HAL_JSON);
    } catch (URISyntaxException e) {
        log.error("getTransactionsForAccount failed", e);
        return null;
    }
    ParameterizedTypeReference<Resources<Transaction>> typeRefDevices = new ParameterizedTypeReference<Resources<Transaction>>() {
    };
    Resources<Transaction> resResponses = traverson.follow(rel("$._links.self.href")).withHeaders(bankingRESTClient.generateHttpHeaders()).toObject(typeRefDevices);
    return resResponses.getContent();
}
Also used : Transaction(model.banking.Transaction) Traverson(org.springframework.hateoas.client.Traverson) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) URISyntaxException(java.net.URISyntaxException) Resources(org.springframework.hateoas.Resources) URI(java.net.URI)

Example 5 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project incubator-servicecomb-java-chassis by apache.

the class TestRestTemplateWrapper method exchangeUsingParameterizedTypeWithUnderlyingRestTemplate.

@Test
public void exchangeUsingParameterizedTypeWithUnderlyingRestTemplate() {
    ParameterizedTypeReference<List<String>> typeReference = new ParameterizedTypeReference<List<String>>() {
    };
    ResponseEntity<List<String>> actual;
    for (HttpMethod method : httpMethods) {
        when(underlying.exchange(url, method, requestEntity, typeReference, param1, param2)).thenReturn(typedResponse);
        actual = wrapper.exchange(url, method, requestEntity, typeReference, param1, param2);
        assertThat(actual, is(typedResponse));
        verify(underlying).exchange(url, method, requestEntity, typeReference, param1, param2);
        when(underlying.exchange(url, method, requestEntity, typeReference, paramsMap)).thenReturn(typedResponse);
        actual = wrapper.exchange(url, method, requestEntity, typeReference, paramsMap);
        assertThat(actual, is(typedResponse));
        verify(underlying).exchange(url, method, requestEntity, typeReference, paramsMap);
        when(underlying.exchange(uri, method, requestEntity, typeReference)).thenReturn(typedResponse);
        actual = wrapper.exchange(uri, method, requestEntity, typeReference);
        assertThat(actual, is(typedResponse));
        verify(underlying).exchange(uri, method, requestEntity, typeReference);
        RequestEntity<String> request = new RequestEntity<>(method, uri);
        when(underlying.exchange(request, typeReference)).thenReturn(typedResponse);
        actual = wrapper.exchange(request, typeReference);
        assertThat(actual, is(typedResponse));
        verify(underlying).exchange(request, typeReference);
    }
}
Also used : ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) RequestEntity(org.springframework.http.RequestEntity) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Aggregations

ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)57 MediaType (org.springframework.http.MediaType)29 HttpHeaders (org.springframework.http.HttpHeaders)25 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)22 List (java.util.List)20 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)18 Test (org.junit.jupiter.api.Test)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)12 URI (java.net.URI)11 Test (org.junit.Test)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 HttpEntity (org.springframework.http.HttpEntity)6 Resources (org.springframework.hateoas.Resources)5 Traverson (org.springframework.hateoas.client.Traverson)5 IOException (java.io.IOException)4 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)4