use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestCseApplicationListener method testCseApplicationListenerNormal.
@Test
public void testCseApplicationListenerNormal(@Injectable ContextRefreshedEvent event, @Injectable AbstractApplicationContext context, @Injectable ProducerProviderManager producerProviderManager, @Injectable ConsumerProviderManager consumerProviderManager, @Injectable TransportManager transportManager, @Mocked RegistryUtils ru) {
Map<String, BootListener> listeners = new HashMap<>();
BootListener listener = Mockito.mock(BootListener.class);
listeners.put("test", listener);
SchemaListenerManager schemaListenerManager = Mockito.mock(SchemaListenerManager.class);
MicroserviceInstance microserviceInstance = Mockito.mock(MicroserviceInstance.class);
new Expectations() {
{
context.getBeansOfType(BootListener.class);
result = listeners;
}
};
new Expectations(RegistryUtils.class) {
{
RegistryUtils.init();
RegistryUtils.getInstanceCacheManager();
RegistryUtils.run();
RegistryUtils.getMicroserviceInstance();
result = microserviceInstance;
}
};
Mockito.when(microserviceInstance.getInstanceId()).thenReturn("testInstanceId");
CseApplicationListener cal = new CseApplicationListener();
Deencapsulation.setField(cal, "schemaListenerManager", schemaListenerManager);
cal.setInitEventClass(ContextRefreshedEvent.class);
cal.setApplicationContext(context);
ReflectUtils.setField(cal, "producerProviderManager", producerProviderManager);
ReflectUtils.setField(cal, "consumerProviderManager", consumerProviderManager);
ReflectUtils.setField(cal, "transportManager", transportManager);
cal.onApplicationEvent(event);
EventManager.post(Mockito.mock(MicroserviceInstanceRegisterTask.class));
verify(schemaListenerManager).notifySchemaListener();
verify(listener, times(10)).onBootEvent(Mockito.any(BootEvent.class));
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestCseApplicationListener method testTriggerAfterRegistryEvent.
@Test
public void testTriggerAfterRegistryEvent() {
CseApplicationListener cal = new CseApplicationListener();
Collection<BootListener> listeners = new ArrayList<>(1);
BootListener listener = Mockito.mock(BootListener.class);
listeners.add(listener);
Deencapsulation.setField(cal, "bootListenerList", listeners);
MicroserviceInstance microserviceInstance = Mockito.mock(MicroserviceInstance.class);
new Expectations(RegistryUtils.class) {
{
RegistryUtils.getMicroserviceInstance();
result = microserviceInstance;
}
};
Mockito.when(microserviceInstance.getInstanceId()).thenReturn("testInstanceId");
Deencapsulation.invoke(cal, "triggerAfterRegistryEvent");
EventManager.post(Mockito.mock(MicroserviceInstanceRegisterTask.class));
Deencapsulation.invoke(ReferenceConfigUtils.class, "assertIsReady");
// AFTER_REGISTRY event should only be sent at the first time of registry success.
EventManager.post(Mockito.mock(MicroserviceInstanceRegisterTask.class));
verify(listener, times(1)).onBootEvent(Mockito.any(BootEvent.class));
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestCseApplicationListener method testTriggerAfterRegistryEventOnInstanceIdIsNull.
@Test
public void testTriggerAfterRegistryEventOnInstanceIdIsNull() {
CseApplicationListener cal = new CseApplicationListener();
Collection<BootListener> listeners = new ArrayList<>(1);
BootListener listener = Mockito.mock(BootListener.class);
listeners.add(listener);
Deencapsulation.setField(cal, "bootListenerList", listeners);
MicroserviceInstance microserviceInstance = Mockito.mock(MicroserviceInstance.class);
new Expectations(RegistryUtils.class) {
{
RegistryUtils.getMicroserviceInstance();
result = microserviceInstance;
}
};
Mockito.when(microserviceInstance.getInstanceId()).thenReturn(null).thenReturn("testInstanceId");
Deencapsulation.invoke(cal, "triggerAfterRegistryEvent");
EventManager.post(Mockito.mock(MicroserviceInstanceRegisterTask.class));
try {
Deencapsulation.invoke(ReferenceConfigUtils.class, "assertIsReady");
fail("an exception is expected.");
} catch (Exception e) {
Assert.assertEquals(IllegalStateException.class, e.getClass());
}
verify(listener, times(0)).onBootEvent(Mockito.any(BootEvent.class));
// AFTER_REGISTRY event should only be sent at the first time of registry success.
EventManager.post(Mockito.mock(MicroserviceInstanceRegisterTask.class));
Deencapsulation.invoke(ReferenceConfigUtils.class, "assertIsReady");
verify(listener, times(1)).onBootEvent(Mockito.any(BootEvent.class));
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestCseAutoConfiguration method testInitRegistry.
@Test
public void testInitRegistry() {
MicroserviceInstance microserviceInstance = Mockito.mock(MicroserviceInstance.class);
new Expectations(RegistryUtils.class) {
{
RegistryUtils.init();
RegistryUtils.run();
RegistryUtils.getMicroserviceInstance();
result = microserviceInstance;
}
};
System.setProperty("cse.rest.address", "127.0.0.1:8081");
new CseAutoConfiguration();
Assert.assertEquals("rest://127.0.0.1:8081", RegistryUtils.getPublishAddress("rest", "127.0.0.1:8081"));
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestCseAutoConfiguration method testInitRegistryException.
@Test
public void testInitRegistryException() {
MicroserviceInstance microserviceInstance = Mockito.mock(MicroserviceInstance.class);
new Expectations(RegistryUtils.class) {
{
RegistryUtils.getMicroserviceInstance();
result = microserviceInstance;
}
};
RegistryIntializer.initRegistry();
Assert.assertEquals(0, RegistryUtils.getMicroserviceInstance().getEndpoints().size());
}
Aggregations