Search in sources :

Example 46 with MicroserviceInstance

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));
}
Also used : Expectations(mockit.Expectations) HashMap(java.util.HashMap) BootEvent(org.apache.servicecomb.core.BootListener.BootEvent) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) SchemaListenerManager(org.apache.servicecomb.core.definition.loader.SchemaListenerManager) MicroserviceInstanceRegisterTask(org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask) Test(org.junit.Test)

Example 47 with MicroserviceInstance

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));
}
Also used : Expectations(mockit.Expectations) ArrayList(java.util.ArrayList) BootEvent(org.apache.servicecomb.core.BootListener.BootEvent) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) MicroserviceInstanceRegisterTask(org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask) Test(org.junit.Test)

Example 48 with MicroserviceInstance

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));
}
Also used : Expectations(mockit.Expectations) ArrayList(java.util.ArrayList) BootEvent(org.apache.servicecomb.core.BootListener.BootEvent) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) MicroserviceInstanceRegisterTask(org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask) Test(org.junit.Test)

Example 49 with MicroserviceInstance

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"));
}
Also used : Expectations(mockit.Expectations) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 50 with MicroserviceInstance

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());
}
Also used : Expectations(mockit.Expectations) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Aggregations

MicroserviceInstance (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)56 Test (org.junit.Test)30 Expectations (mockit.Expectations)16 ArrayList (java.util.ArrayList)15 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)15 HashMap (java.util.HashMap)11 MicroserviceInstances (org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances)7 Before (org.junit.Before)5 EventBus (com.google.common.eventbus.EventBus)4 Server (com.netflix.loadbalancer.Server)4 BootEvent (org.apache.servicecomb.core.BootListener.BootEvent)4 CseServer (org.apache.servicecomb.loadbalance.CseServer)4 Subscribe (com.google.common.eventbus.Subscribe)3 Map (java.util.Map)3 VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)3 MicroserviceInstanceRegisterTask (org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask)3 URI (java.net.URI)2 List (java.util.List)2 RSAProviderTokenManager (org.apache.servicecomb.authentication.provider.RSAProviderTokenManager)2 SchemaListenerManager (org.apache.servicecomb.core.definition.loader.SchemaListenerManager)2