Search in sources :

Example 21 with RestDefinition

use of org.apache.camel.model.rest.RestDefinition in project camel by apache.

the class RestSwaggerSupport method renderResourceListing.

public void renderResourceListing(RestApiResponseAdapter response, BeanConfig swaggerConfig, String contextId, String route, boolean json, boolean yaml, ClassResolver classResolver, RestConfiguration configuration) throws Exception {
    LOG.trace("renderResourceListing");
    if (cors) {
        setupCorsHeaders(response, configuration.getCorsHeaders());
    }
    List<RestDefinition> rests = getRestDefinitions(contextId);
    if (rests != null) {
        if (json) {
            response.setHeader(Exchange.CONTENT_TYPE, "application/json");
            // read the rest-dsl into swagger model
            Swagger swagger = reader.read(rests, route, swaggerConfig, contextId, classResolver);
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            byte[] bytes = mapper.writeValueAsBytes(swagger);
            int len = bytes.length;
            response.setHeader(Exchange.CONTENT_LENGTH, "" + len);
            response.writeBytes(bytes);
        } else {
            response.setHeader(Exchange.CONTENT_TYPE, "text/yaml");
            // read the rest-dsl into swagger model
            Swagger swagger = reader.read(rests, route, swaggerConfig, contextId, classResolver);
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            byte[] jsonData = mapper.writeValueAsBytes(swagger);
            // json to yaml
            JsonNode node = mapper.readTree(jsonData);
            byte[] bytes = Yaml.mapper().writerWithDefaultPrettyPrinter().writeValueAsBytes(node);
            int len = bytes.length;
            response.setHeader(Exchange.CONTENT_LENGTH, "" + len);
            response.writeBytes(bytes);
        }
    } else {
        response.noContent();
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) Swagger(io.swagger.models.Swagger) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 22 with RestDefinition

use of org.apache.camel.model.rest.RestDefinition in project camel by apache.

the class RestSwaggerReaderPropertyPlaceholderTest method testReaderRead.

@Test
public void testReaderRead() throws Exception {
    BeanConfig config = new BeanConfig();
    config.setHost("localhost:8080");
    config.setSchemes(new String[] { "http" });
    config.setBasePath("/api");
    RestSwaggerReader reader = new RestSwaggerReader();
    RestSwaggerSupport support = new RestSwaggerSupport();
    List<RestDefinition> rests = support.getRestDefinitions(context.getName());
    Swagger swagger = reader.read(rests, null, config, context.getName(), new DefaultClassResolver());
    assertNotNull(swagger);
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    String json = mapper.writeValueAsString(swagger);
    log.info(json);
    assertTrue(json.contains("\"host\" : \"localhost:8080\""));
    assertTrue(json.contains("\"basePath\" : \"/api\""));
    assertTrue(json.contains("\"/hello/bye\""));
    assertTrue(json.contains("\"summary\" : \"To update the greeting message\""));
    assertTrue(json.contains("\"/hello/bye/{name}\""));
    assertTrue(json.contains("\"/hello/hi/{name}\""));
    assertFalse(json.contains("{foo}"));
    assertFalse(json.contains("{bar}"));
    context.stop();
}
Also used : BeanConfig(io.swagger.jaxrs.config.BeanConfig) RestDefinition(org.apache.camel.model.rest.RestDefinition) DefaultClassResolver(org.apache.camel.impl.DefaultClassResolver) Swagger(io.swagger.models.Swagger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 23 with RestDefinition

use of org.apache.camel.model.rest.RestDefinition in project camel by apache.

the class RestUndertowHttpPojoTypeTest method testUndertowPojoTypeValidateModel.

@Test
public void testUndertowPojoTypeValidateModel() throws Exception {
    // Wasn't clear if there's a way to put this test into camel-core just to test the model
    // perhaps without starting the Camel Context?
    List<RestDefinition> restDefinitions = context().getRestDefinitions();
    assertNotNull(restDefinitions);
    assertTrue(restDefinitions.size() > 0);
    RestDefinition restDefinition = restDefinitions.get(0);
    List<VerbDefinition> verbs = restDefinition.getVerbs();
    assertNotNull(verbs);
    Map<String, VerbDefinition> mapVerb = new TreeMap<>();
    verbs.forEach(verb -> mapVerb.put(verb.getId(), verb));
    assertEquals(UserPojo[].class.getCanonicalName(), mapVerb.get("getUsers").getOutType());
    assertEquals(UserPojo[].class.getCanonicalName(), mapVerb.get("getUsersList").getOutType());
    assertEquals(UserPojo.class.getCanonicalName(), mapVerb.get("getUser").getOutType());
    assertEquals(UserPojo.class.getCanonicalName(), mapVerb.get("putUser").getType());
    assertEquals(UserPojo[].class.getCanonicalName(), mapVerb.get("putUsers").getType());
    assertEquals(UserPojo[].class.getCanonicalName(), mapVerb.get("putUsersList").getType());
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) VerbDefinition(org.apache.camel.model.rest.VerbDefinition) TreeMap(java.util.TreeMap) BaseUndertowTest(org.apache.camel.component.undertow.BaseUndertowTest) Test(org.junit.Test)

Example 24 with RestDefinition

use of org.apache.camel.model.rest.RestDefinition in project camel by apache.

the class FromRestGetTest method testFromRestModel.

@Test
public void testFromRestModel() throws Exception {
    assertEquals(getExpectedNumberOfRoutes(), context.getRoutes().size());
    assertEquals(2, context.getRestDefinitions().size());
    RestDefinition rest = context.getRestDefinitions().get(0);
    assertNotNull(rest);
    assertEquals("/say/hello", rest.getPath());
    assertEquals(1, rest.getVerbs().size());
    ToDefinition to = assertIsInstanceOf(ToDefinition.class, rest.getVerbs().get(0).getTo());
    assertEquals("direct:hello", to.getUri());
    rest = context.getRestDefinitions().get(1);
    assertNotNull(rest);
    assertEquals("/say/bye", rest.getPath());
    assertEquals(2, rest.getVerbs().size());
    assertEquals("application/json", rest.getVerbs().get(0).getConsumes());
    assertEquals(2, rest.getVerbs().get(0).getParams().size());
    assertEquals(RestParamType.header, rest.getVerbs().get(0).getParams().get(0).getType());
    assertEquals(RestParamType.query, rest.getVerbs().get(0).getParams().get(1).getType());
    assertEquals("header param description1", rest.getVerbs().get(0).getParams().get(0).getDescription());
    assertEquals("header param description2", rest.getVerbs().get(0).getParams().get(1).getDescription());
    assertEquals("integer", rest.getVerbs().get(0).getParams().get(0).getDataType());
    assertEquals("string", rest.getVerbs().get(0).getParams().get(1).getDataType());
    assertEquals(Arrays.asList("1", "2", "3", "4"), rest.getVerbs().get(0).getParams().get(0).getAllowableValues());
    assertEquals(Arrays.asList("a", "b", "c", "d"), rest.getVerbs().get(0).getParams().get(1).getAllowableValues());
    assertEquals("1", rest.getVerbs().get(0).getParams().get(0).getDefaultValue());
    assertEquals("b", rest.getVerbs().get(0).getParams().get(1).getDefaultValue());
    assertEquals(null, rest.getVerbs().get(0).getParams().get(0).getCollectionFormat());
    assertEquals(CollectionFormat.multi, rest.getVerbs().get(0).getParams().get(1).getCollectionFormat());
    assertEquals("header_count", rest.getVerbs().get(0).getParams().get(0).getName());
    assertEquals("header_letter", rest.getVerbs().get(0).getParams().get(1).getName());
    assertEquals(Boolean.TRUE, rest.getVerbs().get(0).getParams().get(0).getRequired());
    assertEquals(Boolean.FALSE, rest.getVerbs().get(0).getParams().get(1).getRequired());
    assertEquals("300", rest.getVerbs().get(0).getResponseMsgs().get(0).getCode());
    assertEquals("rate", rest.getVerbs().get(0).getResponseMsgs().get(0).getHeaders().get(0).getName());
    assertEquals("Rate limit", rest.getVerbs().get(0).getResponseMsgs().get(0).getHeaders().get(0).getDescription());
    assertEquals("integer", rest.getVerbs().get(0).getResponseMsgs().get(0).getHeaders().get(0).getDataType());
    assertEquals("test msg", rest.getVerbs().get(0).getResponseMsgs().get(0).getMessage());
    assertEquals(Integer.class.getCanonicalName(), rest.getVerbs().get(0).getResponseMsgs().get(0).getResponseModel());
    to = assertIsInstanceOf(ToDefinition.class, rest.getVerbs().get(0).getTo());
    assertEquals("direct:bye", to.getUri());
    // the rest becomes routes and the input is a seda endpoint created by the DummyRestConsumerFactory
    getMockEndpoint("mock:update").expectedMessageCount(1);
    template.sendBody("seda:post-say-bye", "I was here");
    assertMockEndpointsSatisfied();
    String out = template.requestBody("seda:get-say-hello", "Me", String.class);
    assertEquals("Hello World", out);
    String out2 = template.requestBody("seda:get-say-bye", "Me", String.class);
    assertEquals("Bye World", out2);
}
Also used : ToDefinition(org.apache.camel.model.ToDefinition) RestDefinition(org.apache.camel.model.rest.RestDefinition) Test(org.junit.Test)

Example 25 with RestDefinition

use of org.apache.camel.model.rest.RestDefinition in project camel by apache.

the class FromRestIdAndDescriptionTest method testFromRestModel.

@Test
public void testFromRestModel() throws Exception {
    super.testFromRestModel();
    RestDefinition rest = context.getRestDefinitions().get(0);
    assertEquals("hello", rest.getId());
    assertEquals("Hello Service", rest.getDescriptionText());
    assertEquals("get-say", rest.getVerbs().get(0).getId());
    assertEquals("Says hello to you", rest.getVerbs().get(0).getDescriptionText());
    RestDefinition rest2 = context.getRestDefinitions().get(1);
    assertEquals("bye", rest2.getId());
    assertEquals("Bye Service", rest2.getDescriptionText());
    assertEquals("en", rest2.getDescription().getLang());
    assertEquals("Says bye to you", rest2.getVerbs().get(0).getDescriptionText());
    assertEquals("Updates the bye message", rest2.getVerbs().get(1).getDescriptionText());
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) Test(org.junit.Test)

Aggregations

RestDefinition (org.apache.camel.model.rest.RestDefinition)38 ToDefinition (org.apache.camel.model.ToDefinition)10 Test (org.junit.Test)10 RestsDefinition (org.apache.camel.model.rest.RestsDefinition)5 RouteDefinition (org.apache.camel.model.RouteDefinition)4 Swagger (io.swagger.models.Swagger)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 FromDefinition (org.apache.camel.model.FromDefinition)2 InterceptFromDefinition (org.apache.camel.model.InterceptFromDefinition)2 GetVerbDefinition (org.apache.camel.model.rest.GetVerbDefinition)2 RestContainer (org.apache.camel.model.rest.RestContainer)2 VerbDefinition (org.apache.camel.model.rest.VerbDefinition)2 RestConfiguration (org.apache.camel.spi.RestConfiguration)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1