Search in sources :

Example 1 with NewApiEntity

use of io.gravitee.management.model.NewApiEntity in project gravitee-management-rest-api by gravitee-io.

the class SwaggerService_PrepareTest method shouldPrepareAPIFromSwaggerV2_yaml.

@Test
public void shouldPrepareAPIFromSwaggerV2_yaml() throws IOException {
    URL url = Resources.getResource("io/gravitee/management/service/swagger-petstore.yaml");
    String descriptor = Resources.toString(url, Charsets.UTF_8);
    ImportSwaggerDescriptorEntity swaggerDescriptor = new ImportSwaggerDescriptorEntity();
    swaggerDescriptor.setPayload(descriptor);
    NewApiEntity api = swaggerService.prepare(swaggerDescriptor);
    assertEquals("1.0.0", api.getVersion());
    assertEquals("Swagger Petstore (Simple)", api.getName());
    assertEquals("http://petstore.swagger.io/api", api.getEndpoint());
    assertEquals(2, api.getPaths().size());
    assertEquals("/pets/:id", api.getPaths().get(1));
}
Also used : URL(java.net.URL) NewApiEntity(io.gravitee.management.model.NewApiEntity) ImportSwaggerDescriptorEntity(io.gravitee.management.model.ImportSwaggerDescriptorEntity) Test(org.junit.Test)

Example 2 with NewApiEntity

use of io.gravitee.management.model.NewApiEntity in project gravitee-management-rest-api by gravitee-io.

the class SwaggerService_PrepareTest method shouldPrepareAPIFromSwaggerV2_json.

@Test
public void shouldPrepareAPIFromSwaggerV2_json() throws IOException {
    URL url = Resources.getResource("io/gravitee/management/service/swagger-petstore.json");
    String descriptor = Resources.toString(url, Charsets.UTF_8);
    ImportSwaggerDescriptorEntity swaggerDescriptor = new ImportSwaggerDescriptorEntity();
    swaggerDescriptor.setPayload(descriptor);
    NewApiEntity api = swaggerService.prepare(swaggerDescriptor);
    assertEquals("1.0.0", api.getVersion());
    assertEquals("Swagger Petstore (Simple)", api.getName());
    assertEquals("http://petstore.swagger.io/api", api.getEndpoint());
    assertEquals(2, api.getPaths().size());
    assertEquals("/pets/:id", api.getPaths().get(1));
}
Also used : URL(java.net.URL) NewApiEntity(io.gravitee.management.model.NewApiEntity) ImportSwaggerDescriptorEntity(io.gravitee.management.model.ImportSwaggerDescriptorEntity) Test(org.junit.Test)

Example 3 with NewApiEntity

use of io.gravitee.management.model.NewApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApisResourceTest method shouldNotCreateApi_withoutPath.

@Test
public void shouldNotCreateApi_withoutPath() {
    final NewApiEntity apiEntity = new NewApiEntity();
    apiEntity.setName("My beautiful api");
    apiEntity.setVersion("v1");
    apiEntity.setDescription("my description");
    ApiEntity returnedApi = new ApiEntity();
    returnedApi.setId("my-beautiful-api");
    doReturn(returnedApi).when(apiService).create(Mockito.any(NewApiEntity.class), Mockito.eq(USER_NAME));
    final Response response = target().request().post(Entity.json(apiEntity));
    assertEquals(HttpStatusCode.BAD_REQUEST_400, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) ApiEntity(io.gravitee.management.model.ApiEntity) NewApiEntity(io.gravitee.management.model.NewApiEntity) NewApiEntity(io.gravitee.management.model.NewApiEntity) Test(org.junit.Test)

Example 4 with NewApiEntity

use of io.gravitee.management.model.NewApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApisResourceTest method shouldCreateApi.

@Test
public void shouldCreateApi() {
    final NewApiEntity apiEntity = new NewApiEntity();
    apiEntity.setName("My beautiful api");
    apiEntity.setVersion("v1");
    apiEntity.setDescription("my description");
    apiEntity.setContextPath("/myapi");
    apiEntity.setEndpoint("http://localhost:9099/");
    ApiEntity returnedApi = new ApiEntity();
    returnedApi.setId("my-beautiful-api");
    doReturn(returnedApi).when(apiService).create(Mockito.any(NewApiEntity.class), Mockito.eq(USER_NAME));
    final Response response = target().request().post(Entity.json(apiEntity));
    assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) ApiEntity(io.gravitee.management.model.ApiEntity) NewApiEntity(io.gravitee.management.model.NewApiEntity) NewApiEntity(io.gravitee.management.model.NewApiEntity) Test(org.junit.Test)

Example 5 with NewApiEntity

use of io.gravitee.management.model.NewApiEntity in project gravitee-management-rest-api by gravitee-io.

the class SwaggerServiceImpl method prepare.

public NewApiEntity prepare(ImportSwaggerDescriptorEntity swaggerDescriptor) {
    Swagger swagger = null;
    switch(swaggerDescriptor.getVersion()) {
        case VERSION_1_0:
            try {
                logger.info("Loading an old Swagger descriptor from {}", swaggerDescriptor.getPayload());
                // For spec < 2.0, only read by url is possible
                swagger = new SwaggerCompatConverter().read(swaggerDescriptor.getPayload());
            } catch (IOException ioe) {
                logger.error("Can not read old Swagger specification", ioe);
                throw new SwaggerDescriptorException();
            }
        case VERSION_2_0:
            if (swaggerDescriptor.getType() == ImportSwaggerDescriptorEntity.Type.INLINE) {
                logger.info("Loading an inline Swagger descriptor");
                swagger = new SwaggerParser().parse(swaggerDescriptor.getPayload());
            } else if (swaggerDescriptor.getType() == ImportSwaggerDescriptorEntity.Type.URL) {
                logger.info("Loading a Swagger descriptor from URL: ", swaggerDescriptor.getPayload());
                swagger = new SwaggerParser().read(swaggerDescriptor.getPayload());
            }
    }
    if (swagger == null) {
        throw new SwaggerDescriptorException();
    }
    NewApiEntity apiEntity = new NewApiEntity();
    apiEntity.setName(swagger.getInfo().getTitle());
    apiEntity.setDescription(swagger.getInfo().getDescription());
    apiEntity.setVersion(swagger.getInfo().getVersion());
    String scheme = (swagger.getSchemes() == null || swagger.getSchemes().isEmpty()) ? defaultScheme : swagger.getSchemes().iterator().next().toValue();
    apiEntity.setEndpoint(scheme + "://" + swagger.getHost() + swagger.getBasePath());
    apiEntity.setPaths(new ArrayList<>(swagger.getPaths().keySet().stream().map(path -> path.replaceAll("\\{(.[^/]*)\\}", ":$1")).collect(Collectors.toList())));
    return apiEntity;
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Scheme(io.swagger.models.Scheme) Yaml(io.swagger.util.Yaml) SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) URISyntaxException(java.net.URISyntaxException) Json(io.swagger.util.Json) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) PageEntity(io.gravitee.management.model.PageEntity) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SwaggerCompatConverter(io.swagger.parser.SwaggerCompatConverter) URI(java.net.URI) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Logger(org.slf4j.Logger) ImportSwaggerDescriptorEntity(io.gravitee.management.model.ImportSwaggerDescriptorEntity) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) NewApiEntity(io.gravitee.management.model.NewApiEntity) Collectors(java.util.stream.Collectors) File(java.io.File) Component(org.springframework.stereotype.Component) MediaType(io.gravitee.common.http.MediaType) PageConfigurationEntity(io.gravitee.management.model.PageConfigurationEntity) SwaggerDescriptorException(io.gravitee.management.service.exceptions.SwaggerDescriptorException) SwaggerService(io.gravitee.management.service.SwaggerService) Collections(java.util.Collections) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) SwaggerDescriptorException(io.gravitee.management.service.exceptions.SwaggerDescriptorException) SwaggerCompatConverter(io.swagger.parser.SwaggerCompatConverter) Swagger(io.swagger.models.Swagger) IOException(java.io.IOException) NewApiEntity(io.gravitee.management.model.NewApiEntity)

Aggregations

NewApiEntity (io.gravitee.management.model.NewApiEntity)5 Test (org.junit.Test)4 ImportSwaggerDescriptorEntity (io.gravitee.management.model.ImportSwaggerDescriptorEntity)3 ApiEntity (io.gravitee.management.model.ApiEntity)2 URL (java.net.URL)2 Response (javax.ws.rs.core.Response)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 MediaType (io.gravitee.common.http.MediaType)1 PageConfigurationEntity (io.gravitee.management.model.PageConfigurationEntity)1 PageEntity (io.gravitee.management.model.PageEntity)1 SwaggerService (io.gravitee.management.service.SwaggerService)1 SwaggerDescriptorException (io.gravitee.management.service.exceptions.SwaggerDescriptorException)1 Scheme (io.swagger.models.Scheme)1 Swagger (io.swagger.models.Swagger)1 SwaggerCompatConverter (io.swagger.parser.SwaggerCompatConverter)1 SwaggerParser (io.swagger.parser.SwaggerParser)1 Json (io.swagger.util.Json)1 Yaml (io.swagger.util.Yaml)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)1