Search in sources :

Example 6 with SwaggerGenerator

use of org.apache.servicecomb.swagger.generator.core.SwaggerGenerator in project incubator-servicecomb-java-chassis by apache.

the class SwaggerDefinitionProcessorTest method testProcess.

@Test
public void testProcess() {
    SwaggerGenerator swaggerGenerator = new SwaggerGenerator(Mockito.mock(SwaggerGeneratorContext.class), null);
    swaggerDefinitionProcessor.process(SwaggerTestTarget.class.getAnnotation(SwaggerDefinition.class), swaggerGenerator);
    Swagger swagger = swaggerGenerator.getSwagger();
    assertEquals(1, swagger.getTags().size());
    io.swagger.models.Tag tag = swagger.getTags().get(0);
    assertEquals("testTag", tag.getName());
    assertEquals("desc", tag.getDescription());
    assertEquals("testValue", tag.getExternalDocs().getDescription());
    assertEquals("testUrl", tag.getExternalDocs().getUrl());
    assertEquals("127.0.0.1", swagger.getHost());
    assertThat(swagger.getSchemes(), contains(io.swagger.models.Scheme.HTTP, io.swagger.models.Scheme.HTTPS));
    io.swagger.models.Info info = swagger.getInfo();
    assertEquals("title", info.getTitle());
    assertEquals("version", info.getVersion());
    assertEquals("desc", info.getDescription());
    assertEquals("contactName", info.getContact().getName());
    assertEquals("licenseName", info.getLicense().getName());
}
Also used : SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) SwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext) Swagger(io.swagger.models.Swagger) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition) Test(org.junit.Test)

Example 7 with SwaggerGenerator

use of org.apache.servicecomb.swagger.generator.core.SwaggerGenerator in project incubator-servicecomb-java-chassis by apache.

the class AbstractSchemaFactory method generateSwagger.

protected SwaggerGenerator generateSwagger(CONTEXT context) {
    SwaggerGeneratorContext generatorContext = compositeSwaggerGeneratorContext.selectContext(context.getProviderClass());
    SwaggerGenerator generator = new SwaggerGenerator(generatorContext, context.getProviderClass());
    generator.setClassLoader(context.getMicroserviceMeta().getClassLoader());
    generator.setPackageName(SchemaUtils.generatePackageName(context.getMicroserviceMeta(), context.getSchemaId()));
    generator.generate();
    // 确保接口是存在的
    ClassUtils.getOrCreateInterface(generator);
    return generator;
}
Also used : SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) CompositeSwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext) SwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext)

Example 8 with SwaggerGenerator

use of org.apache.servicecomb.swagger.generator.core.SwaggerGenerator 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 9 with SwaggerGenerator

use of org.apache.servicecomb.swagger.generator.core.SwaggerGenerator in project incubator-servicecomb-java-chassis by apache.

the class TestOperationInstancesDiscoveryFilter method regMicroservice.

private Microservice regMicroservice(String serviceId, String version, Class<?> schemaCls, int instanceCount) {
    String schemaId = "sid";
    Microservice microservice = new Microservice();
    microservice.setServiceId(serviceId);
    microservice.setAppId(appId);
    microservice.setServiceName(microserviceName);
    microservice.setVersion(version);
    microservice.setSchemas(Arrays.asList(schemaId));
    serviceRegistry.getServiceRegistryClient().registerMicroservice(microservice);
    SwaggerGenerator generator = new SwaggerGenerator(swaggerGeneratorContext, schemaCls);
    String schemaContent = SwaggerUtils.swaggerToString(generator.generate());
    serviceRegistry.getServiceRegistryClient().registerSchema(serviceId, schemaId, schemaContent);
    for (int idx = 0; idx < instanceCount; idx++) {
        MicroserviceInstance instance = createInstance(serviceId);
        serviceRegistry.getServiceRegistryClient().registerMicroserviceInstance(instance);
    }
    return microservice;
}
Also used : Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)

Example 10 with SwaggerGenerator

use of org.apache.servicecomb.swagger.generator.core.SwaggerGenerator in project incubator-servicecomb-java-chassis by apache.

the class ApiProcessorTest method processOnNoTag.

@Test
public void processOnNoTag() {
    SwaggerGenerator swaggerGenerator = new SwaggerGenerator(Mockito.mock(SwaggerGeneratorContext.class), null);
    apiProcessor.process(SwaggerTestTargetWithNoTag.class.getAnnotation(Api.class), swaggerGenerator);
    Set<String> tags = swaggerGenerator.getDefaultTags();
    assertEquals(0, tags.size());
}
Also used : SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) SwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext) Api(io.swagger.annotations.Api) Test(org.junit.Test)

Aggregations

SwaggerGenerator (org.apache.servicecomb.swagger.generator.core.SwaggerGenerator)13 Test (org.junit.Test)7 Swagger (io.swagger.models.Swagger)5 SwaggerGeneratorContext (org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext)5 Method (java.lang.reflect.Method)3 Api (io.swagger.annotations.Api)2 ApiOperation (io.swagger.annotations.ApiOperation)2 CompositeSwaggerGeneratorContext (org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext)2 OperationGenerator (org.apache.servicecomb.swagger.generator.core.OperationGenerator)2 PojoSwaggerGeneratorContext (org.apache.servicecomb.swagger.generator.pojo.PojoSwaggerGeneratorContext)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 SwaggerDefinition (io.swagger.annotations.SwaggerDefinition)1 Operation (io.swagger.models.Operation)1 BodyParameter (io.swagger.models.parameters.BodyParameter)1 ArrayList (java.util.ArrayList)1 ClassConfig (org.apache.servicecomb.common.javassist.ClassConfig)1 ServicePathManager (org.apache.servicecomb.common.rest.locator.ServicePathManager)1 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)1 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)1 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)1