All Projects → nidi3 → Raml Tester

nidi3 / Raml Tester

Licence: apache-2.0
Test if a request/response matches a given raml definition

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Raml Tester

raml-java-client-generator
Raml Java Client Generator
Stars: ✭ 32 (-54.29%)
Mutual labels:  raml
Raml For Jax Rs
This project is all about two way transformation of JAX-RS-annotated Java code to RAML API description and back.
Stars: ✭ 294 (+320%)
Mutual labels:  raml
Web Api
This issue tracker is no longer used. Join us in the Spotify for Developers forum for support with the Spotify Web API ➡️ https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer
Stars: ✭ 944 (+1248.57%)
Mutual labels:  raml
raml-typesystem
(deprecated) Typescript implementation of RAML type system
Stars: ✭ 15 (-78.57%)
Mutual labels:  raml
node-raml-validate
Strict validation of RAML parameters in JavaScript
Stars: ✭ 18 (-74.29%)
Mutual labels:  raml
Docs
轻芒对外服务的文档说明
Stars: ✭ 397 (+467.14%)
Mutual labels:  raml
core
This repo contains the core module of the OCF API's.
Stars: ✭ 20 (-71.43%)
Mutual labels:  raml
Raml2html
RAML to HTML documentation generator.
Stars: ✭ 1,108 (+1482.86%)
Mutual labels:  raml
raml-javascript-generator
Generate a JavaScript API client from RAML
Stars: ✭ 30 (-57.14%)
Mutual labels:  raml
Api Console
An interactive REST console based on RAML/OAS files
Stars: ✭ 848 (+1111.43%)
Mutual labels:  raml
commercetools-api-reference
commercetools API reference documentation
Stars: ✭ 41 (-41.43%)
Mutual labels:  raml
ramlev
Validate/Verify examples in RAML
Stars: ✭ 30 (-57.14%)
Mutual labels:  raml
Osprey
Generate Node.JS API middleware from a RAML definition
Stars: ✭ 429 (+512.86%)
Mutual labels:  raml
raml-sublime-plugin
Syntax highlighter for the RESTful API Modeling Language
Stars: ✭ 49 (-30%)
Mutual labels:  raml
Api Designer
A web editor for creating and sharing RAML API specifications
Stars: ✭ 1,019 (+1355.71%)
Mutual labels:  raml
osprey-method-handler
Middleware for validating requests and responses based on a RAML method object
Stars: ✭ 14 (-80%)
Mutual labels:  raml
Raml Spec
RAML Specification
Stars: ✭ 3,759 (+5270%)
Mutual labels:  raml
Commerce Sdk
Stars: ✭ 63 (-10%)
Mutual labels:  raml
Vim Yaml Folds
YAML, RAML, EYAML & SaltStack SLS folding for Vim
Stars: ✭ 59 (-15.71%)
Mutual labels:  raml
Ramses Example
Example of a Pyramid app that uses ramses
Stars: ✭ 19 (-72.86%)
Mutual labels:  raml

raml-tester

Build Status codecov License

Test if a request/response matches a given raml definition.

Versioning

Version Contents
0.8.x Stable version, uses RAML parser 0.8.x and supports only RAML v0.8
0.9.x Development version, uses RAML parser 1.x and supports RAML v0.8 and parts of v1.0
1.0.x As soon as RAML v1.0 support is stable

Add it to a project

Add these lines to the pom.xml:

<dependency>
    <groupId>guru.nidi.raml</groupId>
    <artifactId>raml-tester</artifactId>
    <version>0.8.8</version>
</dependency>

If you are stuck with java 1.6, use the compatible version by adding the following line:

    <classifier>jdk6</classifier>

Use in a spring MVC test

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Application.class)
public class SpringTest {

    private static RamlDefinition api = RamlLoaders.fromClasspath(SpringTest.class).load("api.raml")
            .assumingBaseUri("http://nidi.guru/raml/simple/v1");
    private static SimpleReportAggregator aggregator = new SimpleReportAggregator();

    @ClassRule
    public static ExpectedUsage expectedUsage = new ExpectedUsage(aggregator);

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void greeting() throws Exception {
        Assert.assertThat(api.validate(), validates());

        mockMvc.perform(get("/greeting").accept(MediaType.parseMediaType("application/json")))
                .andExpect(api.matches().aggregating(aggregator));
    }
}

The ExpectedUsage rule checks if all resources, query parameters, form parameters, headers and response codes defined in the RAML are at least used once.

The RamlMatchers.validates() matcher validates the RAML itself.

api.matches() checks that the request/response match the RAML definition.

See also the raml-tester-uc-spring project.

Use in a Java EE / JAX-RS environment

@RunWith(Arquillian.class)
public class JaxrsTest {

    private static RamlDefinition api = RamlLoaders.fromClasspath(JaxrsTest.class).load("api.raml")
            .assumingBaseUri("http://nidi.guru/raml/simple/v1");
    private static SimpleReportAggregator aggregator = new SimpleReportAggregator();
    private static WebTarget target;

    @ClassRule
    public static ExpectedUsage expectedUsage = new ExpectedUsage(aggregator);

    @Deployment(testable = false)
    public static WebArchive createDeployment() {
        return ShrinkWrap.create(WebArchive.class).addClass(Application.class);
    }

    @ArquillianResource
    private URL base;

    @Before
    public void setup() throws MalformedURLException {
        Client client = ClientBuilder.newClient();
        target = client.target(URI.create(new URL(base, "app/path").toExternalForm()));
    }

    @Test
    public void greeting() throws Exception {
        assertThat(api.validate(), validates());

        final CheckingWebTarget webTarget = api.createWebTarget(target).aggregating(aggregator);
        webTarget.request().post(Entity.text("apple"));

        assertThat(webTarget.getLastReport(), checks());
    }
}

The RamlMatchers.checks() matcher validates that the request and response conform to the RAML.

Use in a pure servlet environment

public class RamlFilter implements Filter {
    private final Logger log = LoggerFactory.getLogger(getClass());
    private RamlDefinition api;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        api = RamlLoaders.fromClasspath(getClass()).load("api.yaml");
        log.info(api.validate().toString());
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        final RamlReport report = api.testAgainst(request, response, chain);
        log.info("Raml report: " + report);
    }

    @Override
    public void destroy() {
    }
}

Or see the raml-tester-uc-sevlet project.

Use together with Apache HttpComponents

public class HttpComponentsTest {
    @Test
    public void testRequest() throws IOException {
        RamlDefinition api = RamlLoaders.fromClasspath(getClass()).load("api.yaml");
        Assert.assertThat(api.validate(), validates());

        RamlHttpClient client = api.createHttpClient();
        HttpGet get = new HttpGet("http://test.server/path");
        HttpResponse response = client.execute(get);

        Assert.assertThat(client.getLastReport(), checks());
    }
}

Or see the raml-tester-uc-servlet project.

Use together with RestAssured

public class RestAssuredTest {
    @Test
    public void testWithRestAssured() {
        RestAssured.baseURI = "http://test.server/path";
        RamlDefinition api = RamlLoaders.fromClasspath(getClass()).load("api.yaml");
        Assert.assertThat(api.validate(), validates());

        RestAssuredClient restAssured = api.createRestAssured();
        restAssured.given().get("/base/data").andReturn();
        Assert.assertTrue(restAssured.getLastReport().isEmpty());
    }
}

If you are using RestAssured 3.0, call api.createRestAssured3().

Use as a standalone proxy

When used as a proxy, any service can be tested, regardless of the technology used to implement it. See the raml-proxy project.

Use with Javascript

There is special support for javascript.

See raml-tester-js for details and raml-tester-uc-js for examples.

FailFast

You can configure the RamlDefinition to throw an exception in case a violation is found.

@Test(expected = RamlViolationException.class)
public void testInvalidResource() {
    RestAssured.baseURI = "http://test.server/path";
    RamlDefinition api = RamlLoaders.fromClasspath(getClass()).load("api.yaml");
    Assert.assertThat(api.validate(), validates());

    RestAssuredClient restAssured = api.failFast().createRestAssured();
    restAssured.given().get("/wrong/path").andReturn();
    fail("Should throw RamlViolationException");
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].