Search in sources :

Example 1 with ServiceRegistry

use of org.apache.servicecomb.serviceregistry.ServiceRegistry in project incubator-servicecomb-java-chassis by apache.

the class TestConsumerSchemaFactory method init.

@BeforeClass
public static void init() {
    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
    serviceRegistry.init();
    RegistryUtils.setServiceRegistry(serviceRegistry);
    SchemaListenerManager schemaListenerManager = new SchemaListenerManager();
    schemaListenerManager.setSchemaListenerList(Arrays.asList(schemaListener));
    MicroserviceMetaManager microserviceMetaManager = new MicroserviceMetaManager();
    SchemaLoader schemaLoader = new SchemaLoader() {

        @Override
        public void putSelfBasePathIfAbsent(String microserviceName, String basePath) {
        }
    };
    CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext();
    ReflectUtils.setField(consumerSchemaFactory, "schemaListenerManager", schemaListenerManager);
    ReflectUtils.setField(consumerSchemaFactory, "microserviceMetaManager", microserviceMetaManager);
    ReflectUtils.setField(consumerSchemaFactory, "schemaLoader", schemaLoader);
    ReflectUtils.setField(consumerSchemaFactory, "compositeSwaggerGeneratorContext", compositeSwaggerGeneratorContext);
    SchemaMeta schemaMeta = new UnitTestMeta().getOrCreateSchemaMeta(TestConsumerSchemaFactoryImpl.class);
    String content = UnitTestSwaggerUtils.pretty(schemaMeta.getSwagger());
    Microservice microservice = new Microservice();
    microservice.setAppId("app");
    microservice.setServiceId("0");
    microservice.setServiceName("ms");
    microservice.setVersion("1.0.0");
    microservice.addSchema("schema", content);
    serviceRegistry.getServiceRegistryClient().registerMicroservice(microservice);
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setServiceId("0");
    instance.setInstanceId("0");
    serviceRegistry.getServiceRegistryClient().registerMicroserviceInstance(instance);
}
Also used : MicroserviceMetaManager(org.apache.servicecomb.core.definition.MicroserviceMetaManager) SchemaLoader(org.apache.servicecomb.core.definition.loader.SchemaLoader) Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) CompositeSwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) SchemaListenerManager(org.apache.servicecomb.core.definition.loader.SchemaListenerManager) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) BeforeClass(org.junit.BeforeClass)

Example 2 with ServiceRegistry

use of org.apache.servicecomb.serviceregistry.ServiceRegistry in project incubator-servicecomb-java-chassis by apache.

the class TestLocalServiceRegistry method testSchema.

@Test
public void testSchema() {
    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
    Microservice microservice = serviceRegistry.getMicroservice();
    microservice.addSchema("s1", "s1-content");
    serviceRegistry.init();
    serviceRegistry.run();
    try {
        serviceRegistry.getServiceRegistryClient().isSchemaExist("notExist", "s1");
        Assert.fail("must throw exception");
    } catch (IllegalArgumentException e) {
        Assert.assertEquals("Invalid serviceId, serviceId=notExist", e.getMessage());
    }
    try {
        serviceRegistry.getServiceRegistryClient().getSchema("notExist", "s1");
        Assert.fail("must throw exception");
    } catch (IllegalArgumentException e) {
        Assert.assertEquals("Invalid serviceId, serviceId=notExist", e.getMessage());
    }
    Assert.assertEquals(true, serviceRegistry.getServiceRegistryClient().isSchemaExist(microservice.getServiceId(), "s1"));
    String content = serviceRegistry.getServiceRegistryClient().getSchema(microservice.getServiceId(), "s1");
    Assert.assertEquals("s1-content", content);
}
Also used : Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) Test(org.junit.Test)

Example 3 with ServiceRegistry

use of org.apache.servicecomb.serviceregistry.ServiceRegistry in project incubator-servicecomb-java-chassis by apache.

the class TestLocalServiceRegistry method testLifeCycle.

@Test
public void testLifeCycle() {
    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
    serviceRegistry.init();
    Assert.assertNull(serviceRegistry.getMicroserviceInstance().getInstanceId());
    serviceRegistry.run();
    Assert.assertNotNull(serviceRegistry.getMicroserviceInstance().getInstanceId());
    serviceRegistry.destroy();
    Assert.assertTrue(serviceRegistry.getServiceRegistryClient().getMicroserviceInstance("", serviceRegistry.getMicroservice().getServiceId()).isEmpty());
}
Also used : ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) Test(org.junit.Test)

Example 4 with ServiceRegistry

use of org.apache.servicecomb.serviceregistry.ServiceRegistry in project incubator-servicecomb-java-chassis by apache.

the class TestRemoteServiceRegistry method testLifeCycle.

@Test
public void testLifeCycle(@Injectable ServiceRegistryConfig config, @Injectable MicroserviceDefinition definition, @Injectable ServiceRegistry registry) {
    ArrayList<IpPort> ipPortList = new ArrayList<>();
    ipPortList.add(new IpPort("127.0.0.1", 9980));
    ipPortList.add(new IpPort("127.0.0.1", 9981));
    new Expectations() {

        {
            definition.getConfiguration();
            result = ConfigUtil.createLocalConfig();
            config.getIpPort();
            result = ipPortList;
            config.getTransport();
            result = "rest";
            config.isRegistryAutoDiscovery();
            result = true;
            config.getHeartbeatInterval();
            result = 30;
            config.getInstancePullInterval();
            result = 30;
            config.isWatch();
            result = false;
        }
    };
    ServiceRegistry oldRegistry = RegistryUtils.getServiceRegistry();
    RegistryUtils.setServiceRegistry(registry);
    EventBus bus = new EventBus();
    RemoteServiceRegistry remote = new TestingRemoteServiceRegistry(bus, config, definition);
    remote.init();
    remote.run();
    // includes complete tasks
    Assert.assertTrue(2 <= remote.getTaskPool().getTaskCount());
    bus.post(new ShutdownEvent());
    remote.getTaskPool().schedule(new Runnable() {

        @Override
        public void run() {
        // TODO Auto-generated method stub
        }
    }, 0, TimeUnit.SECONDS);
    Assert.assertTrue(remote.getTaskPool().isShutdown());
    RegistryUtils.setServiceRegistry(oldRegistry);
}
Also used : Expectations(mockit.Expectations) ArrayList(java.util.ArrayList) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) EventBus(com.google.common.eventbus.EventBus) ShutdownEvent(org.apache.servicecomb.serviceregistry.task.event.ShutdownEvent) Test(org.junit.Test)

Example 5 with ServiceRegistry

use of org.apache.servicecomb.serviceregistry.ServiceRegistry in project incubator-servicecomb-java-chassis by apache.

the class TestCseClientHttpRequest method testNormal.

@Test
public void testNormal() throws IOException {
    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
    serviceRegistry.init();
    RegistryUtils.setServiceRegistry(serviceRegistry);
    UnitTestMeta meta = new UnitTestMeta();
    CseContext.getInstance().getSchemaListenerManager().setSchemaListenerList(Arrays.asList(new RestEngineSchemaListener()));
    SchemaMeta schemaMeta = meta.getOrCreateSchemaMeta(SpringmvcImpl.class);
    CseContext.getInstance().getSchemaListenerManager().notifySchemaListener(schemaMeta);
    Holder<Invocation> holder = new Holder<>();
    CseClientHttpRequest client = new CseClientHttpRequest(URI.create("cse://app:test/" + SpringmvcImpl.class.getSimpleName() + "/bytes"), HttpMethod.POST) {

        /**
         * {@inheritDoc}
         */
        @Override
        protected Response doInvoke(Invocation invocation) {
            holder.value = invocation;
            return Response.ok("result");
        }
    };
    byte[] body = "abc".getBytes();
    client.setRequestBody(body);
    client.execute();
    Assert.assertArrayEquals(body, holder.value.getSwaggerArgument(0));
}
Also used : RestEngineSchemaListener(org.apache.servicecomb.common.rest.RestEngineSchemaListener) UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) Invocation(org.apache.servicecomb.core.Invocation) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) Holder(javax.xml.ws.Holder) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) Test(org.junit.Test)

Aggregations

ServiceRegistry (org.apache.servicecomb.serviceregistry.ServiceRegistry)19 Test (org.junit.Test)14 EventBus (com.google.common.eventbus.EventBus)4 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)4 BeforeClass (org.junit.BeforeClass)4 Invocation (org.apache.servicecomb.core.Invocation)3 UnitTestMeta (org.apache.servicecomb.core.unittest.UnitTestMeta)3 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)3 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Holder (javax.xml.ws.Holder)2 Expectations (mockit.Expectations)2 Mock (mockit.Mock)2 MicroserviceMetaManager (org.apache.servicecomb.core.definition.MicroserviceMetaManager)2 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)2 SchemaLoader (org.apache.servicecomb.core.definition.loader.SchemaLoader)2 IpPort (org.apache.servicecomb.foundation.common.net.IpPort)2 LocalServiceRegistryClientImpl (org.apache.servicecomb.serviceregistry.client.LocalServiceRegistryClientImpl)2 ServiceRegistryClient (org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient)2