Search in sources :

Example 1 with ServicePathManager

use of org.apache.servicecomb.common.rest.locator.ServicePathManager in project incubator-servicecomb-java-chassis by apache.

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, TestRestEngineSchemaListenerSchemaImpl.class);
    Swagger swagger = generator.generate();
    swagger.setBasePath("");
    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.assertSame(sm1, spm.consumerLocateOperation("/add/", "POST").getOperation().getOperationMeta().getSchemaMeta());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) Swagger(io.swagger.models.Swagger) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) ArrayList(java.util.ArrayList) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager) Test(org.junit.Test)

Example 2 with ServicePathManager

use of org.apache.servicecomb.common.rest.locator.ServicePathManager in project incubator-servicecomb-java-chassis by apache.

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);
        mgr.addSchema(schemaMeta);
    }
    for (ServicePathManager mgr : mgrMap.values()) {
        // 对具有动态path operation进行排序
        mgr.sortPath();
        mgr.saveToMicroserviceMeta();
    }
}
Also used : HashMap(java.util.HashMap) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager)

Example 3 with ServicePathManager

use of org.apache.servicecomb.common.rest.locator.ServicePathManager in project incubator-servicecomb-java-chassis by apache.

the class RestEngineSchemaListener method findPathManager.

protected ServicePathManager findPathManager(Map<String, ServicePathManager> mgrMap, MicroserviceMeta microserviceMeta) {
    ServicePathManager mgr = mgrMap.get(microserviceMeta.getName());
    if (mgr != null) {
        return mgr;
    }
    mgr = ServicePathManager.getServicePathManager(microserviceMeta);
    if (mgr == null) {
        mgr = new ServicePathManager(microserviceMeta);
    } else {
        mgr = mgr.cloneServicePathManager();
    }
    mgrMap.put(microserviceMeta.getName(), mgr);
    return mgr;
}
Also used : ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager)

Example 4 with ServicePathManager

use of org.apache.servicecomb.common.rest.locator.ServicePathManager in project incubator-servicecomb-java-chassis by apache.

the class MockUtil method mockRegisterManager.

public void mockRegisterManager() throws InstantiationException, IllegalAccessException {
    MicroserviceMeta microserviceMeta = microserviceMetaManager.getOrCreateMicroserviceMeta("app:test");
    microserviceMeta.putExtData("RestServicePathManager", new ServicePathManager(microserviceMeta));
    ConsumerSchemaFactory consumerSchemaFactory = new MockUp<ConsumerSchemaFactory>() {

        @Mock
        public MicroserviceMeta getOrCreateConsumer(String microserviceName) {
            return microserviceMeta;
        }
    }.getMockInstance();
    CseContext.getInstance().setConsumerSchemaFactory(consumerSchemaFactory);
}
Also used : MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) ConsumerSchemaFactory(org.apache.servicecomb.core.definition.schema.ConsumerSchemaFactory) Mock(mockit.Mock) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager)

Example 5 with ServicePathManager

use of org.apache.servicecomb.common.rest.locator.ServicePathManager in project incubator-servicecomb-java-chassis by apache.

the class CseClientHttpRequest method createRequestMeta.

private RequestMeta createRequestMeta(String httpMetod, URI uri) {
    String microserviceName = uri.getAuthority();
    ReferenceConfig referenceConfig = ReferenceConfigUtils.getForInvoke(microserviceName);
    MicroserviceMeta microserviceMeta = referenceConfig.getMicroserviceMeta();
    ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(microserviceMeta);
    if (servicePathManager == null) {
        throw new Error(String.format("no schema defined for %s:%s", microserviceMeta.getAppId(), microserviceMeta.getName()));
    }
    OperationLocator locator = servicePathManager.consumerLocateOperation(path, httpMetod);
    RestOperationMeta swaggerRestOperation = locator.getOperation();
    Map<String, String> pathParams = locator.getPathVarMap();
    return new RequestMeta(referenceConfig, swaggerRestOperation, pathParams);
}
Also used : OperationLocator(org.apache.servicecomb.common.rest.locator.OperationLocator) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) ReferenceConfig(org.apache.servicecomb.core.provider.consumer.ReferenceConfig) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager)

Aggregations

ServicePathManager (org.apache.servicecomb.common.rest.locator.ServicePathManager)11 OperationLocator (org.apache.servicecomb.common.rest.locator.OperationLocator)6 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)6 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 Expectations (mockit.Expectations)2 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)2 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)2 ReferenceConfig (org.apache.servicecomb.core.provider.consumer.ReferenceConfig)2 AbstractHttpServletRequest (org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest)2 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)2 Swagger (io.swagger.models.Swagger)1 ArrayList (java.util.ArrayList)1 Mock (mockit.Mock)1 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)1 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)1 ConsumerSchemaFactory (org.apache.servicecomb.core.definition.schema.ConsumerSchemaFactory)1 MicroserviceReferenceConfig (org.apache.servicecomb.core.provider.consumer.MicroserviceReferenceConfig)1 SwaggerGenerator (org.apache.servicecomb.swagger.generator.core.SwaggerGenerator)1 ApplicationContext (org.springframework.context.ApplicationContext)1