Search in sources :

Example 6 with HttpResponse

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

the class ServiceCenterClient method sendHeartBeats.

@Override
public void sendHeartBeats(HeartbeatsRequest heartbeatsRequest) {
    try {
        HttpResponse response = httpClient.putHttpRequest("/registry/heartbeats", null, HttpUtils.serialize(heartbeatsRequest));
        if (response.getStatusCode() == HttpStatus.SC_OK) {
            return;
        }
        sendUnAuthorizedEvent(response);
        throw new OperationException("heartbeats fails, statusCode = " + response.getStatusCode() + "; message = " + response.getMessage() + "; content = " + response.getContent());
    } catch (IOException e) {
        throw new OperationException("heartbeats fails ", e);
    }
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) OperationException(org.apache.servicecomb.service.center.client.exception.OperationException)

Example 7 with HttpResponse

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

the class ServiceCenterClientTest method TestGetServiceSchemasContext.

@Test
public void TestGetServiceSchemasContext() throws IOException {
    ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    httpResponse.setMessage("ok");
    String responseString = "{\n" + "  \"schema\": \"test context\"\n" + "}";
    httpResponse.setContent(responseString);
    Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(httpResponse);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
    String schemaContext = serviceCenterClient.getServiceSchemaContext("111", "222");
    Assert.assertNotNull(schemaContext);
    Assert.assertEquals("test context", schemaContext);
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) Test(org.junit.Test)

Example 8 with HttpResponse

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

the class ServiceCenterClientTest method TestGetServiceCenterInstances.

@Test
public void TestGetServiceCenterInstances() throws IOException {
    ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    httpResponse.setMessage("ok");
    String responseString = "{\n" + "  \"instances\": [\n" + "    {\n" + "      \"instanceId\": \"111111\",\n" + "      \"serviceId\": \"222222\",\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" + "  ]\n" + "}";
    httpResponse.setContent(responseString);
    Mockito.when(serviceCenterRawClient.getHttpRequest("/registry/health", null, null)).thenReturn(httpResponse);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
    MicroserviceInstancesResponse serviceCenterInstances = serviceCenterClient.getServiceCenterInstances();
    Assert.assertNotNull(serviceCenterInstances);
    Assert.assertEquals(1, serviceCenterInstances.getInstances().size());
    Assert.assertEquals("111111", serviceCenterInstances.getInstances().get(0).getInstanceId());
    Assert.assertEquals("222222", serviceCenterInstances.getInstances().get(0).getServiceId());
}
Also used : MicroserviceInstancesResponse(org.apache.servicecomb.service.center.client.model.MicroserviceInstancesResponse) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) Test(org.junit.Test)

Example 9 with HttpResponse

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

the class ServiceCenterClientTest method TestGetServiceSchemas.

@Test
public void TestGetServiceSchemas() throws IOException {
    ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    httpResponse.setMessage("ok");
    String responseString = "{\n" + "  \"schemas\": [\n" + "    {\n" + "      \"schemaId\": \"111111\",\n" + "      \"schema\": \"test\",\n" + "      \"summary\": \"test\"\n" + "    }\n" + "  ]\n" + "}";
    httpResponse.setContent(responseString);
    Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(httpResponse);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
    List<SchemaInfo> schemaResponse = serviceCenterClient.getServiceSchemasList("111", false);
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(mapper.writeValueAsString(schemaResponse));
    Assert.assertNotNull(jsonNode);
    Assert.assertEquals("111111", jsonNode.get(0).get("schemaId").textValue());
    Assert.assertEquals("test", jsonNode.get(0).get("schema").textValue());
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SchemaInfo(org.apache.servicecomb.service.center.client.model.SchemaInfo) Test(org.junit.Test)

Example 10 with HttpResponse

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

the class ServiceCenterClientTest method TestQueryServiceId.

@Test
public void TestQueryServiceId() throws IOException {
    ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    httpResponse.setMessage("ok");
    httpResponse.setContent("{\"serviceId\": \"111111\"}");
    Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(httpResponse);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
    Microservice microservice = new Microservice("Test111");
    RegisteredMicroserviceResponse actualServiceId = serviceCenterClient.queryServiceId(microservice);
    Assert.assertNotNull(actualServiceId);
    Assert.assertEquals("111111", actualServiceId.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) 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