Search in sources :

Example 11 with Response

use of io.swagger.models.Response in project swagger-core by swagger-api.

the class SimpleScannerWithDecoratorExtensionTest method scanSimpleResourceWithDecorator.

/**
     * Test for method annotated with vendor annotation which could be used for swagger documentation.
     */
@Test(description = "scan a simple resource with custom decorator")
public void scanSimpleResourceWithDecorator() {
    final Swagger swagger = getSwagger(SimpleResourceWithVendorAnnotation.class);
    assertEquals(swagger.getPaths().size(), 2);
    final Operation get = getGet(swagger, "/{id}");
    assertNotNull(get);
    assertEquals(get.getParameters().size(), 2);
    final Response response = get.getResponses().get(RESPONSE_STATUS_401);
    assertNotNull(response);
    assertEquals(response.getDescription(), RESPONSE_DESCRIPTION);
}
Also used : Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) Test(org.testng.annotations.Test)

Example 12 with Response

use of io.swagger.models.Response in project swagger-core by swagger-api.

the class SimpleReaderTest method scanResourceWithMapReturnType.

@Test(description = "scan a resource with map return type")
public void scanResourceWithMapReturnType() {
    Swagger swagger = getSwagger(ResourceWithMapReturnValue.class);
    Operation get = getGet(swagger, "/{id}");
    assertNotNull(get);
    assertNotNull(get.getResponses());
    Response response = get.getResponses().get("200");
    assertNotNull(response);
    Property schema = response.getSchema();
    assertEquals(schema.getClass(), MapProperty.class);
}
Also used : ResourceWithResponse(io.swagger.resources.ResourceWithResponse) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) NicknamedOperation(io.swagger.resources.NicknamedOperation) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Test(org.testng.annotations.Test)

Example 13 with Response

use of io.swagger.models.Response in project swagger-core by swagger-api.

the class SimpleReaderTest method scanResourceWithApiResponseResponseContainerValue.

@Test(description = "scan resource with ApiResponse.responseContainer() value")
public void scanResourceWithApiResponseResponseContainerValue() {
    Swagger swagger = getSwagger(ResourceWithApiResponseResponseContainer.class);
    Path paths = swagger.getPaths().get("/{id}");
    Map<String, Response> responses1 = paths.getGet().getResponses();
    assertEquals(responses1.get("200").getSchema().getClass(), MapProperty.class);
    assertEquals(responses1.get("400").getSchema().getClass(), ArrayProperty.class);
    Map<String, Response> responses2 = paths.getPut().getResponses();
    assertEquals(responses2.get("201").getSchema().getClass(), RefProperty.class);
    assertEquals(responses2.get("401").getSchema().getClass(), ArrayProperty.class);
    Map<String, Response> responses3 = paths.getPost().getResponses();
    assertEquals(responses3.get("202").getSchema().getClass(), RefProperty.class);
    assertEquals(responses3.get("402").getSchema().getClass(), RefProperty.class);
    Map<String, Response> responses4 = paths.getDelete().getResponses();
    assertEquals(responses4.get("203").getSchema().getClass(), RefProperty.class);
    assertEquals(responses4.get("403").getSchema().getClass(), RefProperty.class);
    Path paths2 = swagger.getPaths().get("/{id}/name");
    Map<String, Response> responses5 = paths2.getGet().getResponses();
    assertEquals(responses5.get("203").getSchema().getClass(), ArrayProperty.class);
    assertNull(((ArrayProperty) responses5.get("203").getSchema()).getUniqueItems());
    assertNotEquals(responses5.get("203").getHeaders().get("foo").getClass(), MapProperty.class);
    assertEquals(responses5.get("403").getSchema().getClass(), ArrayProperty.class);
    assertEquals(((ArrayProperty) responses5.get("403").getSchema()).getUniqueItems(), Boolean.TRUE);
    Map<String, Response> responses6 = paths2.getPut().getResponses();
    assertEquals(responses6.get("203").getSchema().getClass(), ArrayProperty.class);
    assertEquals(((ArrayProperty) responses6.get("203").getSchema()).getUniqueItems(), Boolean.TRUE);
    assertEquals(responses6.get("203").getHeaders().get("foo").getClass(), ArrayProperty.class);
    assertEquals(((ArrayProperty) responses6.get("203").getHeaders().get("foo")).getUniqueItems(), Boolean.TRUE);
    assertEquals(responses6.get("403").getSchema().getClass(), ArrayProperty.class);
}
Also used : Path(io.swagger.models.Path) ResourceWithResponse(io.swagger.resources.ResourceWithResponse) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 14 with Response

use of io.swagger.models.Response in project swagger-core by swagger-api.

the class SimpleReaderTest method scanResourceWithApiOperationCodeValue.

@Test(description = "scan resource with ApiOperation.code() value")
public void scanResourceWithApiOperationCodeValue() {
    Swagger swagger = getSwagger(ResourceWithApiOperationCode.class);
    Map<String, Response> responses1 = getGetResponses(swagger, "/{id}");
    assertEquals(responses1.size(), 3);
    assertTrue(responses1.containsKey("202"));
    assertFalse(responses1.containsKey("200"));
    assertEquals(responses1.get("202").getDescription(), "successful operation");
    Map<String, Response> responses2 = getPutResponses(swagger, "/{id}");
    assertEquals(responses2.size(), 3);
    assertTrue(responses2.containsKey("200"));
    assertEquals(responses2.get("200").getDescription(), "successful operation");
}
Also used : ResourceWithResponse(io.swagger.resources.ResourceWithResponse) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 15 with Response

use of io.swagger.models.Response in project swagger-core by swagger-api.

the class SwaggerSerializerTest method convertSpec.

@Test(description = "it should convert a spec")
public void convertSpec() throws IOException {
    final Model personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Model errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "value");
    info.setVendorExtension("x-test2", map);
    info.setVendorExtension("x-test", "value");
    final Swagger swagger = new Swagger().info(info).host("petstore.swagger.io").securityDefinition("api-key", new ApiKeyAuthDefinition("key", In.HEADER)).scheme(Scheme.HTTP).consumes("application/json").produces("application/json").model("Person", personModel).model("Error", errorModel);
    final Operation get = new Operation().produces("application/json").summary("finds pets in the system").description("a longer description").tag("Pet Operations").operationId("get pet by id").deprecated(true);
    get.parameter(new QueryParameter().name("tags").description("tags to filter by").required(false).property(new StringProperty()));
    get.parameter(new PathParameter().name("petId").description("pet to fetch").property(new LongProperty()));
    final Response response = new Response().description("pets returned").schema(new RefProperty().asDefault("Person")).example("application/json", "fun!");
    final Response errorResponse = new Response().description("error response").schema(new RefProperty().asDefault("Error"));
    get.response(200, response).defaultResponse(errorResponse);
    final Operation post = new Operation().summary("adds a new pet").description("you can add a new pet this way").tag("Pet Operations").operationId("add pet").defaultResponse(errorResponse).parameter(new BodyParameter().description("the pet to add").schema(new RefModel().asDefault("Person")));
    swagger.path("/pets", new Path().get(get).post(post));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    final Swagger rebuilt = Json.mapper().readValue(swaggerJson, Swagger.class);
    SerializationMatchers.assertEqualsToJson(rebuilt, swaggerJson);
}
Also used : Path(io.swagger.models.Path) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) HashMap(java.util.HashMap) Error(io.swagger.models.Error) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Contact(io.swagger.models.Contact) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) LongProperty(io.swagger.models.properties.LongProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) Person(io.swagger.models.Person) Test(org.testng.annotations.Test)

Aggregations

Response (io.swagger.models.Response)32 Operation (io.swagger.models.Operation)23 Test (org.testng.annotations.Test)17 Property (io.swagger.models.properties.Property)16 Swagger (io.swagger.models.Swagger)14 Path (io.swagger.models.Path)12 ArrayProperty (io.swagger.models.properties.ArrayProperty)10 MapProperty (io.swagger.models.properties.MapProperty)10 RefProperty (io.swagger.models.properties.RefProperty)10 ApiResponse (io.swagger.annotations.ApiResponse)8 IntegerProperty (io.swagger.models.properties.IntegerProperty)8 StringProperty (io.swagger.models.properties.StringProperty)7 ResourceWithResponse (io.swagger.resources.ResourceWithResponse)6 BodyParameter (io.swagger.models.parameters.BodyParameter)5 JavaType (com.fasterxml.jackson.databind.JavaType)4 Parameter (io.swagger.models.parameters.Parameter)4 PathParameter (io.swagger.models.parameters.PathParameter)4 QueryParameter (io.swagger.models.parameters.QueryParameter)4 LongProperty (io.swagger.models.properties.LongProperty)4 Type (java.lang.reflect.Type)4