Search in sources :

Example 21 with MicroserviceMeta

use of io.servicecomb.core.definition.MicroserviceMeta in project java-chassis by ServiceComb.

the class TestRestEngineSchemaListener method test.

@Test
public void test() {
    BeanUtils.setContext(Mockito.mock(ApplicationContext.class));
    MicroserviceMeta mm = new MicroserviceMeta("app:ms");
    List<SchemaMeta> smList = new ArrayList<>();
    SwaggerGenerator generator = new SwaggerGenerator(context, Impl.class);
    Swagger swagger = generator.generate();
    SchemaMeta sm1 = new SchemaMeta(swagger, mm, "sid1");
    smList.add(sm1);
    RestEngineSchemaListener listener = new RestEngineSchemaListener();
    SchemaMeta[] smArr = smList.toArray(new SchemaMeta[smList.size()]);
    listener.onSchemaLoaded(smArr);
    // 重复调用,不应该出异常
    listener.onSchemaLoaded(smArr);
    ServicePathManager spm = ServicePathManager.getServicePathManager(mm);
    Assert.assertEquals(mm, spm.getMicroserviceMeta());
    Assert.assertNotNull(spm.getStaticPathOperationMap().get("Impl/add/"));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SwaggerGenerator(io.servicecomb.swagger.generator.core.SwaggerGenerator) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) Swagger(io.swagger.models.Swagger) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) ArrayList(java.util.ArrayList) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager) Test(org.junit.Test)

Example 22 with MicroserviceMeta

use of io.servicecomb.core.definition.MicroserviceMeta in project java-chassis by ServiceComb.

the class RestEngineSchemaListener method onSchemaLoaded.

@Override
public void onSchemaLoaded(SchemaMeta... schemaMetas) {
    // 此时相应的ServicePathManager可能正在被使用,为避免太高的复杂度,使用copy on write逻辑
    Map<String, ServicePathManager> mgrMap = new HashMap<>();
    for (SchemaMeta schemaMeta : schemaMetas) {
        MicroserviceMeta microserviceMeta = schemaMeta.getMicroserviceMeta();
        ServicePathManager mgr = findPathManager(mgrMap, microserviceMeta);
        if (mgr.isSchemaExists(schemaMeta.getSchemaId())) {
            LOGGER.info("on schema loaded, exists schema. {}:{}", schemaMeta.getMicroserviceName(), schemaMeta.getSchemaId());
            continue;
        }
        LOGGER.info("on schema loaded, new schema. {}:{}", schemaMeta.getMicroserviceName(), schemaMeta.getSchemaId());
        mgr.addSchema(schemaMeta.getSchemaId());
        for (OperationMeta operationMeta : schemaMeta.getOperations()) {
            RestOperationMeta restOperationMeta = new RestOperationMeta();
            restOperationMeta.init(operationMeta);
            operationMeta.putExtData(RestConst.SWAGGER_REST_OPERATION, restOperationMeta);
            mgr.addResource(restOperationMeta);
        }
    }
    for (ServicePathManager mgr : mgrMap.values()) {
        // 对具有动态path operation进行排序
        mgr.sortPath();
        mgr.printService();
        mgr.saveToMicroserviceMeta();
    }
}
Also used : HashMap(java.util.HashMap) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) OperationMeta(io.servicecomb.core.definition.OperationMeta) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager)

Example 23 with MicroserviceMeta

use of io.servicecomb.core.definition.MicroserviceMeta in project java-chassis by ServiceComb.

the class TestLocator method testLocateDynamic.

@Test
public void testLocateDynamic() {
    MicroserviceMeta msm = new MicroserviceMeta("app:ms");
    ServicePathManager spm = new ServicePathManager(msm);
    RestOperationMeta rom = createRestOperatonMeta("GET", "abc/{id}");
    spm.addResource(rom);
    try {
        spm.locateOperation("abc/10", "PUT");
    } catch (InvocationException e) {
        Assert.assertEquals("Method Not Allowed", ((CommonExceptionData) e.getErrorData()).getMessage());
    }
    OperationLocator locator = spm.locateOperation("abc/10", "GET");
    Assert.assertEquals("10", locator.getPathVarMap().get("id"));
}
Also used : RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) InvocationException(io.servicecomb.core.exception.InvocationException) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) Test(org.junit.Test)

Example 24 with MicroserviceMeta

use of io.servicecomb.core.definition.MicroserviceMeta in project java-chassis by ServiceComb.

the class TestLocator method testServicePathManager.

@Test
public void testServicePathManager() {
    MicroserviceMeta msm = new MicroserviceMeta("app:ms");
    ServicePathManager spm = new ServicePathManager(msm);
    RestOperationMeta rom = createRestOperatonMeta("GET", "abc/{id}");
    spm.addResource(rom);
    rom = createRestOperatonMeta("GET", "abc/{id}/xxx");
    spm.addResource(rom);
    Assert.assertEquals("abc/{id}", spm.getDynamicPathOperationList().get(0).getAbsolutePath());
    spm.sortPath();
    Assert.assertEquals("abc/{id}/xxx", spm.getDynamicPathOperationList().get(0).getAbsolutePath());
    spm.printService();
    spm.doPrintService();
}
Also used : RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) Test(org.junit.Test)

Aggregations

MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)24 SchemaMeta (io.servicecomb.core.definition.SchemaMeta)11 Test (org.junit.Test)8 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)6 ServicePathManager (io.servicecomb.common.rest.locator.ServicePathManager)5 OperationMeta (io.servicecomb.core.definition.OperationMeta)4 InvocationException (io.servicecomb.core.exception.InvocationException)3 ConsumerOperationMeta (io.servicecomb.core.provider.consumer.ConsumerOperationMeta)3 HashMap (java.util.HashMap)3 OperationLocator (io.servicecomb.common.rest.locator.OperationLocator)2 CommonExceptionData (io.servicecomb.core.exception.CommonExceptionData)2 Swagger (io.swagger.models.Swagger)2 ArrayList (java.util.ArrayList)2 Invocation (io.servicecomb.core.Invocation)1 ProducerProvider (io.servicecomb.core.ProducerProvider)1 ConsumerSchemaFactory (io.servicecomb.core.definition.schema.ConsumerSchemaFactory)1 ReferenceConfig (io.servicecomb.core.provider.consumer.ReferenceConfig)1 Microservice (io.servicecomb.serviceregistry.api.registry.Microservice)1 SwaggerGenerator (io.servicecomb.swagger.generator.core.SwaggerGenerator)1 Mock (mockit.Mock)1