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);
}
}
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());
}
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");
}
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());
}
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());
}
Aggregations