Search in sources :

Example 26 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class KieClient method queryConfigurations.

@Override
public ConfigurationsResponse queryConfigurations(ConfigurationsRequest request) {
    String address = addressManager.address();
    String url = buildUrl(request, address);
    try {
        if (kieConfiguration.isEnableLongPolling()) {
            url += "&wait=" + kieConfiguration.getPollingWaitInSeconds() + "s";
        }
        HttpRequest httpRequest = new HttpRequest(url, null, null, HttpRequest.GET);
        HttpResponse httpResponse = httpTransport.doRequest(httpRequest);
        ConfigurationsResponse configurationsResponse = new ConfigurationsResponse();
        if (httpResponse.getStatusCode() == HttpStatus.SC_OK) {
            revision = httpResponse.getHeader("X-Kie-Revision");
            KVResponse allConfigList = HttpUtils.deserialize(httpResponse.getContent(), KVResponse.class);
            Map<String, Object> configurations = getConfigByLabel(allConfigList);
            configurationsResponse.setConfigurations(configurations);
            configurationsResponse.setChanged(true);
            configurationsResponse.setRevision(revision);
            addressManager.recordSuccessState(address);
            return configurationsResponse;
        }
        if (httpResponse.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
            throw new OperationException("Bad request for query configurations.");
        }
        if (httpResponse.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
            configurationsResponse.setChanged(false);
            addressManager.recordSuccessState(address);
            return configurationsResponse;
        }
        addressManager.recordFailState(address);
        throw new OperationException("read response failed. status:" + httpResponse.getStatusCode() + "; message:" + httpResponse.getMessage() + "; content:" + httpResponse.getContent());
    } catch (Exception e) {
        LOGGER.error("query configuration from {} failed, message={}", url, e.getMessage());
        throw new OperationException("read response failed. ", e);
    }
}
Also used : HttpRequest(org.apache.servicecomb.http.client.common.HttpRequest) ConfigurationsResponse(org.apache.servicecomb.config.kie.client.model.ConfigurationsResponse) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) KVResponse(org.apache.servicecomb.config.kie.client.model.KVResponse) OperationException(org.apache.servicecomb.config.kie.client.exception.OperationException) OperationException(org.apache.servicecomb.config.kie.client.exception.OperationException)

Example 27 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClientTest method TestGetServiceInstanceMessage.

@Test
public void TestGetServiceInstanceMessage() throws IOException {
    ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    httpResponse.setMessage("ok");
    String responseString = "{\n" + "  \"instance\": {\n" + "      \"instanceId\": \"111\",\n" + "      \"serviceId\": \"222\",\n" + "      \"version\": \"1.0\",\n" + "      \"hostName\": \"Test\",\n" + "      \"endpoints\": [\n" + "        \"string\"\n" + "      ],\n" + "      \"status\": \"UP\",\n" + "      \"properties\": {\n" + "        \"additionalProp1\": \"string\",\n" + "        \"additionalProp2\": \"string\",\n" + "        \"additionalProp3\": \"string\"\n" + "      },\n" + "      \"healthCheck\": {\n" + "        \"mode\": \"push\",\n" + "        \"port\": \"0\",\n" + "        \"interval\": \"0\",\n" + "        \"times\": \"0\"\n" + "      },\n" + "      \"dataCenterInfo\": {\n" + "        \"name\": \"string\",\n" + "        \"region\": \"string\",\n" + "        \"availableZone\": \"string\"\n" + "      },\n" + "      \"timestamp\": \"333333\",\n" + "      \"modTimestamp\": \"4444444\"\n" + "    }\n" + "}";
    httpResponse.setContent(responseString);
    Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(httpResponse);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
    MicroserviceInstance responseInstance = serviceCenterClient.getMicroserviceInstance("111", "222");
    Assert.assertNotNull(responseInstance);
    Assert.assertEquals("111", responseInstance.getInstanceId());
    Assert.assertEquals("Test", responseInstance.getHostName());
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) MicroserviceInstance(org.apache.servicecomb.service.center.client.model.MicroserviceInstance) Test(org.junit.Test)

Example 28 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClientTest method TestDeleteServiceInstance.

@Test
public void TestDeleteServiceInstance() throws IOException {
    ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    httpResponse.setMessage("ok");
    Mockito.when(serviceCenterRawClient.deleteHttpRequest(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(httpResponse);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
    serviceCenterClient.deleteMicroserviceInstance("111", "222");
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) Test(org.junit.Test)

Example 29 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClientTest method TestRegistryService.

@Test
public void TestRegistryService() throws IOException {
    ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    httpResponse.setMessage("ok");
    httpResponse.setContent("{\"serviceId\": \"111111\"}");
    Microservice microservice = new Microservice();
    microservice.setServiceName("Test");
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
    Mockito.when(serviceCenterRawClient.postHttpRequest("/registry/microservices", null, objectMapper.writeValueAsString(microservice))).thenReturn(httpResponse);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
    RegisteredMicroserviceResponse actualResponse = serviceCenterClient.registerMicroservice(microservice);
    Assert.assertNotNull(actualResponse);
    Assert.assertEquals("111111", actualResponse.getServiceId());
}
Also used : Microservice(org.apache.servicecomb.service.center.client.model.Microservice) RegisteredMicroserviceResponse(org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceResponse) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 30 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClientTest method TestGetServiceList.

@Test
public void TestGetServiceList() throws IOException {
    ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    httpResponse.setMessage("ok");
    MicroservicesResponse microservicesResponse = new MicroservicesResponse();
    List<Microservice> microserviceList = new ArrayList<Microservice>();
    microserviceList.add(new Microservice("Test1"));
    microserviceList.add(new Microservice("Test2"));
    microserviceList.add(new Microservice("Test3"));
    microservicesResponse.setServices(microserviceList);
    ObjectMapper mapper = new ObjectMapper();
    httpResponse.setContent(mapper.writeValueAsString(microservicesResponse));
    Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(httpResponse);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
    MicroservicesResponse actualMicroservicesResponse = serviceCenterClient.getMicroserviceList();
    Assert.assertNotNull(actualMicroservicesResponse);
    Assert.assertEquals(3, actualMicroservicesResponse.getServices().size());
    Assert.assertEquals("Test1", actualMicroservicesResponse.getServices().get(0).getServiceName());
}
Also used : Microservice(org.apache.servicecomb.service.center.client.model.Microservice) MicroservicesResponse(org.apache.servicecomb.service.center.client.model.MicroservicesResponse) ArrayList(java.util.ArrayList) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

HttpResponse (org.apache.servicecomb.http.client.common.HttpResponse)37 IOException (java.io.IOException)21 OperationException (org.apache.servicecomb.service.center.client.exception.OperationException)19 Test (org.junit.Test)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Microservice (org.apache.servicecomb.service.center.client.model.Microservice)4 HttpRequest (org.apache.servicecomb.http.client.common.HttpRequest)3 MicroserviceInstancesResponse (org.apache.servicecomb.service.center.client.model.MicroserviceInstancesResponse)3 RegisteredMicroserviceResponse (org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceResponse)3 HashMap (java.util.HashMap)2 MicroserviceInstance (org.apache.servicecomb.service.center.client.model.MicroserviceInstance)2 RegisteredMicroserviceInstanceResponse (org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceInstanceResponse)2 SchemaInfo (org.apache.servicecomb.service.center.client.model.SchemaInfo)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 EventBus (com.google.common.eventbus.EventBus)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 OperationException (org.apache.servicecomb.config.center.client.exception.OperationException)1