Search in sources :

Example 1 with CseHttpEntity

use of org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity in project incubator-servicecomb-java-chassis by apache.

the class CodeFirstRestTemplateSpringmvc method testResponseEntity.

private void testResponseEntity(String microserviceName, RestTemplate template, String cseUrlPrefix) {
    Map<String, Object> body = new HashMap<>();
    Date date = new Date();
    body.put("date", date);
    CseHttpEntity<Map<String, Object>> httpEntity = new CseHttpEntity<>(body);
    httpEntity.addContext("contextKey", "contextValue");
    String srcName = RegistryUtils.getMicroservice().getServiceName();
    ResponseEntity<Date> responseEntity = template.exchange(cseUrlPrefix + "responseEntity", HttpMethod.POST, httpEntity, Date.class);
    TestMgr.check(date, responseEntity.getBody());
    TestMgr.check("h1v " + srcName, responseEntity.getHeaders().getFirst("h1"));
    TestMgr.check("h2v " + srcName, responseEntity.getHeaders().getFirst("h2"));
    checkStatusCode(microserviceName, 202, responseEntity.getStatusCode());
    responseEntity = template.exchange(cseUrlPrefix + "responseEntity", HttpMethod.PATCH, httpEntity, Date.class);
    TestMgr.check(date, responseEntity.getBody());
    TestMgr.check("h1v " + srcName, responseEntity.getHeaders().getFirst("h1"));
    TestMgr.check("h2v " + srcName, responseEntity.getHeaders().getFirst("h2"));
    checkStatusCode(microserviceName, 202, responseEntity.getStatusCode());
}
Also used : CseHttpEntity(org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Date(java.util.Date)

Example 2 with CseHttpEntity

use of org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity in project java-chassis by ServiceComb.

the class TestDateTimeSchema method testDateTimeSchemaMulticastRestTemplate.

private void testDateTimeSchemaMulticastRestTemplate() throws Exception {
    DiscoveryContext context = new DiscoveryContext();
    VersionedCache serversVersionedCache = discoveryTree.discovery(context, "springmvctest", "springmvc", "0+");
    List<String> enpoints = serversVersionedCache.data();
    RestTemplate restTemplate = RestTemplateBuilder.create();
    for (String endpoint : enpoints) {
        CseHttpEntity<?> entity = new CseHttpEntity<>(null);
        InvocationContext invocationContext = new InvocationContext();
        invocationContext.addLocalContext(LoadbalanceHandler.SERVICECOMB_SERVER_ENDPOINT, endpoint);
        entity.setContext(invocationContext);
        Date date = new Date();
        String dateValue = RestObjectMapperFactory.getRestObjectMapper().convertToString(date);
        TestMgr.check(date.getTime(), restTemplate.exchange("cse://springmvc/dateTime/getDate?date={1}", HttpMethod.GET, entity, Date.class, dateValue).getBody().getTime());
        entity = new CseHttpEntity<>(null);
        invocationContext = new InvocationContext();
        invocationContext.addLocalContext(LoadbalanceHandler.SERVICECOMB_SERVER_ENDPOINT, parseEndpoint(endpoint));
        entity.setContext(invocationContext);
        date = new Date();
        dateValue = RestObjectMapperFactory.getRestObjectMapper().convertToString(date);
        TestMgr.check(date.getTime(), restTemplate.exchange("cse://springmvc/dateTime/getDate?date={1}", HttpMethod.GET, entity, Date.class, dateValue).getBody().getTime());
    }
}
Also used : CseHttpEntity(org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) RestTemplate(org.springframework.web.client.RestTemplate) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 3 with CseHttpEntity

use of org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity in project java-chassis by ServiceComb.

the class TestContentType method testRequestMapping.

private void testRequestMapping() {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
    CseHttpEntity<String> requestEntity = new CseHttpEntity<>("from testRequestMapping", requestHeaders);
    ResponseEntity<String> responseEntity = restTemplate.exchange("cse://springmvc/contentTypeSpringmvc/testRequestMapping", HttpMethod.POST, requestEntity, String.class);
    TestMgr.check("testRequestMapping: name=[from testRequestMapping], request content-type=[" + MediaType.APPLICATION_JSON + "]", responseEntity.getBody());
    TestMgr.check(MediaType.APPLICATION_JSON, extractContentType(responseEntity.getHeaders().getContentType()));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) CseHttpEntity(org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity)

Example 4 with CseHttpEntity

use of org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity in project java-chassis by ServiceComb.

the class TestContentType method testApiOperation.

private void testApiOperation() {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
    CseHttpEntity<String> requestEntity = new CseHttpEntity<>("from testApiOperation", requestHeaders);
    ResponseEntity<String> responseEntity = restTemplate.exchange("cse://springmvc/contentTypeSpringmvc/testApiOperation", HttpMethod.POST, requestEntity, String.class);
    TestMgr.check("testApiOperation: name=[from testApiOperation], request content-type=[" + MediaType.APPLICATION_JSON + "]", responseEntity.getBody());
    TestMgr.check(MediaType.APPLICATION_JSON, extractContentType(responseEntity.getHeaders().getContentType()));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) CseHttpEntity(org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity)

Example 5 with CseHttpEntity

use of org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity in project java-chassis by ServiceComb.

the class CodeFirstRestTemplateSpringmvc method testResponseEntity.

private void testResponseEntity(String microserviceName, RestTemplate template, String cseUrlPrefix) {
    Map<String, Object> body = new HashMap<>();
    Date date = new Date();
    body.put("date", date);
    CseHttpEntity<Map<String, Object>> httpEntity = new CseHttpEntity<>(body);
    httpEntity.addContext("contextKey", "contextValue");
    String srcName = RegistrationManager.INSTANCE.getMicroservice().getServiceName();
    ResponseEntity<Date> responseEntity = template.exchange(cseUrlPrefix + "responseEntity", HttpMethod.POST, httpEntity, Date.class);
    TestMgr.check(date, responseEntity.getBody());
    TestMgr.check("h1v " + srcName, responseEntity.getHeaders().getFirst("h1"));
    TestMgr.check("h2v " + srcName, responseEntity.getHeaders().getFirst("h2"));
    checkStatusCode(microserviceName, 202, responseEntity.getStatusCode());
    responseEntity = template.exchange(cseUrlPrefix + "responseEntity", HttpMethod.PATCH, httpEntity, Date.class);
    TestMgr.check(date, responseEntity.getBody());
    TestMgr.check("h1v " + srcName, responseEntity.getHeaders().getFirst("h1"));
    TestMgr.check("h2v " + srcName, responseEntity.getHeaders().getFirst("h2"));
    checkStatusCode(microserviceName, 202, responseEntity.getStatusCode());
    int retryResult = template.getForObject(cseUrlPrefix + "retrySuccess?a=2&b=3", Integer.class);
    TestMgr.check(retryResult, 5);
    retryResult = template.getForObject(cseUrlPrefix + "retrySuccess?a=2&b=3", Integer.class);
    TestMgr.check(retryResult, 5);
}
Also used : CseHttpEntity(org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Date(java.util.Date)

Aggregations

CseHttpEntity (org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity)7 HttpHeaders (org.springframework.http.HttpHeaders)4 Date (java.util.Date)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 MultiValueMap (org.springframework.util.MultiValueMap)2 LocalDate (java.time.LocalDate)1 VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)1 DiscoveryContext (org.apache.servicecomb.registry.discovery.DiscoveryContext)1 InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)1 RestTemplate (org.springframework.web.client.RestTemplate)1