Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nidi3/raml-tester
Test if a request/response matches a given raml definition
https://github.com/nidi3/raml-tester
java raml raml-tooling testing
Last synced: 13 days ago
JSON representation
Test if a request/response matches a given raml definition
- Host: GitHub
- URL: https://github.com/nidi3/raml-tester
- Owner: nidi3
- License: apache-2.0
- Created: 2014-04-30T16:49:44.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-02-13T20:29:00.000Z (almost 6 years ago)
- Last Synced: 2024-05-03T00:16:47.711Z (8 months ago)
- Topics: java, raml, raml-tooling, testing
- Language: Java
- Size: 1.18 MB
- Stars: 71
- Watchers: 10
- Forks: 14
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
raml-tester
===========
[![Build Status](https://travis-ci.org/nidi3/raml-tester.svg?branch=master)](https://travis-ci.org/nidi3/raml-tester)
[![codecov](https://codecov.io/gh/nidi3/raml-tester/branch/master/graph/badge.svg)](https://codecov.io/gh/nidi3/raml-tester)
[![License](https://img.shields.io/badge/License-Apache_2.0-yellowgreen.svg)](https://opensource.org/licenses/Apache-2.0)Test if a request/response matches a given raml definition.
Versioning
--Version | Contents
---|---
0.8.x | Stable version, uses [RAML parser 0.8.x](https://github.com/raml-org/raml-java-parser/tree/v1) and supports only RAML v0.8
0.9.x | Development version, uses [RAML parser 1.x](https://github.com/raml-org/raml-java-parser) and supports RAML v0.8 and [parts of v1.0](https://github.com/nidi3/raml-tester/blob/raml-parser-2/1.0-support.md)
1.0.x | As soon as RAML v1.0 support is stableAdd it to a project
-------------------
Add these lines to the `pom.xml`:```xml
guru.nidi.raml
raml-tester
0.8.8```
If you are stuck with java 1.6, use the compatible version by adding the following line:
```xml
jdk6
```Use in a spring MVC test
------------------------
[//]: # (spring)
```java
@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));
}
}
```
[//]: # (end)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](https://github.com/nidi3/raml-tester-uc-spring) project.
Use in a Java EE / JAX-RS environment
-------------------------------------
[//]: # (jaxrs)
```java
@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());
}
}
```
[//]: # (end)The `RamlMatchers.checks()` matcher validates that the request and response conform to the RAML.
Use in a pure servlet environment
---------------------------------
[//]: # (servlet)
```java
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() {
}
}
```
[//]: # (end)Or see the [raml-tester-uc-sevlet](https://github.com/nidi3/raml-tester-uc-servlet) project.
Use together with Apache HttpComponents
---------------------------------------[//]: # (httpComponents)
```java
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());
}
}
```
[//]: # (end)Or see the [raml-tester-uc-servlet](https://github.com/nidi3/raml-tester-uc-servlet) project.
Use together with RestAssured
---------------------------------------
[//]: # (restAssured)
```java
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());
}
}
```
[//]: # (end)
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](https://github.com/nidi3/raml-tester-proxy) project.Use with Javascript
-------------------
There is special support for javascript.See [raml-tester-js](https://github.com/nidi3/raml-tester-js) for details and
[raml-tester-uc-js](https://github.com/nidi3/raml-tester-uc-js) for examples.FailFast
---------------------------------------
You can configure the RamlDefinition to throw an exception in case a violation is found.[//]: # (failFast)
```java
@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");
}
```
[//]: # (end)