use of org.apache.servicecomb.service.center.client.model.Microservice 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.service.center.client.model.Microservice 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());
}
use of org.apache.servicecomb.service.center.client.model.Microservice in project java-chassis by ServiceComb.
the class ServiceCenterClientTest method TestGetServiceMessage.
@Test
public void TestGetServiceMessage() throws IOException {
ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
HttpResponse httpResponse = new HttpResponse();
httpResponse.setStatusCode(200);
httpResponse.setMessage("ok");
String responseString = "{\n" + " \"service\": {\n" + " \"serviceId\": \"111111\",\n" + " \"environment\": \"string\",\n" + " \"appId\": \"string\",\n" + " \"serviceName\": \"string\",\n" + " \"version\": \"string\",\n" + " \"description\": \"string\",\n" + " \"level\": \"string\",\n" + " \"registerBy\": \"string\",\n" + " \"schemas\": [\n" + " \"string\"\n" + " ],\n" + " \"status\": \"UP\",\n" + " \"timestamp\": \"string\",\n" + " \"modTimestamp\": \"string\",\n" + " \"framework\": {\n" + " \"name\": \"string\",\n" + " \"version\": \"string\"\n" + " },\n" + " \"paths\": [\n" + " {\n" + " \"Path\": \"string\",\n" + " \"Property\": {\n" + " \"additionalProp1\": \"string\",\n" + " \"additionalProp2\": \"string\",\n" + " \"additionalProp3\": \"string\"\n" + " }\n" + " }\n" + " ],\n" + " \"properties\": {\n" + " \"additionalProp1\": \"string\",\n" + " \"additionalProp2\": \"string\",\n" + " \"additionalProp3\": \"string\"\n" + " }\n" + " }\n" + "}";
httpResponse.setContent(responseString);
Mockito.when(serviceCenterRawClient.getHttpRequest("/registry/microservices/111111", null, null)).thenReturn(httpResponse);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
Microservice microservices = serviceCenterClient.getMicroserviceByServiceId("111111");
Assert.assertNotNull(microservices);
Assert.assertEquals("111111", microservices.getServiceId());
}
use of org.apache.servicecomb.service.center.client.model.Microservice in project java-chassis by ServiceComb.
the class RegistryClientTest method testRestTransport.
@Override
public void testRestTransport() throws Exception {
AddressManager addressManager = new AddressManager("default", Arrays.asList("http://127.0.0.1:30100"), new EventBus());
SSLProperties sslProperties = new SSLProperties();
sslProperties.setEnabled(false);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(addressManager, sslProperties, new DefaultRequestAuthHeaderProvider(), "default", null);
EventBus eventBus = new SimpleEventBus();
ServiceCenterConfiguration serviceCenterConfiguration = new ServiceCenterConfiguration();
ServiceCenterRegistration serviceCenterRegistration = new ServiceCenterRegistration(serviceCenterClient, serviceCenterConfiguration, eventBus);
Microservice microservice = new Microservice();
microservice.setAppId("app_registry");
microservice.setServiceName("name_registry");
microservice.setVersion("1.0.0");
microservice.setEnvironment("development");
List<String> schemas = new ArrayList<>();
schemas.add("SchemaA");
schemas.add("SchemaB");
microservice.setSchemas(schemas);
MicroserviceInstance microserviceInstance = new MicroserviceInstance();
microserviceInstance.setHostName("host_registry");
List<String> endpoints = new ArrayList<>();
endpoints.add("rest://127.0.0.1/");
microserviceInstance.setEndpoints(endpoints);
List<SchemaInfo> schemaInfos = new ArrayList<>();
SchemaInfo schemaA = new SchemaInfo();
schemaA.setSchemaId("SchemaA");
schemaA.setSchema("schema contents in any format");
schemaA.setSummary(Hashing.sha256().newHasher().putString("schema contents in any format".toString(), Charsets.UTF_8).hash().toString());
schemaInfos.add(schemaA);
SchemaInfo schemaB = new SchemaInfo();
schemaB.setSchemaId("SchemaA");
schemaB.setSchema("schema contents in any format");
schemaB.setSummary(Hashing.sha256().newHasher().putString("schema contents in any format".toString(), Charsets.UTF_8).hash().toString());
schemaInfos.add(schemaB);
serviceCenterRegistration.setMicroservice(microservice);
serviceCenterRegistration.setMicroserviceInstance(microserviceInstance);
serviceCenterRegistration.setSchemaInfos(schemaInfos);
eventBus.register(this);
serviceCenterRegistration.startRegistration();
registrationCounter.await(30000, TimeUnit.MILLISECONDS);
if (hasRegistered) {
TestMgr.check(events.size() >= 3, true);
TestMgr.check(events.get(0).isSuccess(), true);
TestMgr.check(events.get(0) instanceof MicroserviceRegistrationEvent, true);
TestMgr.check(events.get(1).isSuccess(), true);
TestMgr.check(events.get(1) instanceof MicroserviceInstanceRegistrationEvent, true);
TestMgr.check(events.get(2).isSuccess(), true);
TestMgr.check(events.get(2) instanceof HeartBeatEvent, true);
} else {
TestMgr.check(events.size() >= 4, true);
TestMgr.check(events.get(0).isSuccess(), true);
TestMgr.check(events.get(0) instanceof MicroserviceRegistrationEvent, true);
TestMgr.check(events.get(1).isSuccess(), true);
TestMgr.check(events.get(1) instanceof SchemaRegistrationEvent, true);
TestMgr.check(events.get(2).isSuccess(), true);
TestMgr.check(events.get(2) instanceof MicroserviceInstanceRegistrationEvent, true);
TestMgr.check(events.get(3).isSuccess(), true);
TestMgr.check(events.get(3) instanceof HeartBeatEvent, true);
}
ServiceCenterDiscovery discovery = new ServiceCenterDiscovery(serviceCenterClient, eventBus);
discovery.updateMyselfServiceId(microservice.getServiceId());
discovery.startDiscovery();
discovery.registerIfNotPresent(new SubscriptionKey(microservice.getAppId(), microservice.getServiceName()));
discoveryCounter.await(30000, TimeUnit.MILLISECONDS);
TestMgr.check(instances != null, true);
TestMgr.check(instances.size(), 1);
discovery.stop();
serviceCenterRegistration.stop();
serviceCenterClient.deleteMicroserviceInstance(microservice.getServiceId(), microserviceInstance.getInstanceId());
}
use of org.apache.servicecomb.service.center.client.model.Microservice in project incubator-servicecomb-java-chassis by apache.
the class ServiceCenterDiscovery method setMicroserviceInfo.
private void setMicroserviceInfo(List<MicroserviceInstance> instances) {
instances.forEach(instance -> {
Microservice microservice = microserviceCache.computeIfAbsent(instance.getServiceId(), id -> {
try {
return serviceCenterClient.getMicroserviceByServiceId(id);
} catch (Exception e) {
LOGGER.error("Find microservice by id={} failed", id, e);
throw e;
}
});
instance.setMicroservice(microservice);
});
}
Aggregations