DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Hey Developers: Diagrams Don’t Need to Be So Complex
  • Why Developers Should Contribute To Open Source
  • Smart BDD: The Most Productive Way To Test
  • Natural Language Processing (NLP) in Software Testing: Automating Test Case Creation and Documentation

Trending

  • API Design
  • Mastering Persistence: Why the Persistence Layer Is Crucial for Modern Java Applications
  • Send Your Logs to Loki
  • Deploy a Session Recording Solution Using Ansible and Audit Your Bastion Host

RAML: How Specification Becomes Documentation and Testing

Lieven Doclo user avatar by
Lieven Doclo
·
Sep. 02, 14 · Interview
Like (0)
Save
Tweet
Share
6.57K Views

Join the DZone community and get the full member experience.

Join For Free

In my last post I talked about what annoys me about Swagger. This evening, I took the time to see whether there are any good alternatives out there. As it seems, there are a lot of them. Two of them I found especially worth looking at: API BluePrint and RAML. This article is about RAML, but I’ll definitely post another one on API BluePrint.

RAML is a specification format that looks like a YAML file. It describes how a REST webservice should look like and how it should behave with regards to return values. In short, it’s what WSDL is to SOAP webservices. RAML specifications are not hard to write and is a top-down approach to REST webservices, unlike Swagger, which is a bottom-up tool mainly aimed towards documentation. You’ll write the RAML specification before writing the code.

The reason why I find RAML so compelling as a Java dev is the fact that the specification can actually be used in a unit test. This means you can code by contract and truly do test driven development: you now have a spec your service needs to adhere to and which can be verified.

For example, I need a service with this RAML specification:

#%RAML 0.8
---
title: simple
baseUri: http://boottest/simple/{version}
version: v1
/hello:
  get:
    responses:
      200:
        body:
          application/json:
            schema: |
              {
                "type":"object",
                "properties": {
                  "stringValue":{
                    "type":"string"
                  },
                  "integerValue":{
                    "type":"integer"
                  }
                }
              }

Using TDD principles I write a Spock test using the raml-tester library.

@ContextConfiguration(loader = SpringApplicationContextLoader, classes = BootApplication)
@WebAppConfiguration
class HelloRestServiceSpecification extends Specification {
    private static RamlDefinition api = RamlLoaders.fromClasspath(HelloRestServiceSpecification.class).load("api.raml")
        .assumingBaseUri("http://boottest/simple/v1")
    private static SimpleReportAggregator aggregator = new SimpleReportAggregator();
    @ClassRule
    public static ExpectedUsage expectedUsage = new ExpectedUsage(aggregator);
    MockMvc mockMvc
    @Autowired
    private WebApplicationContext wac;
    def setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }
    @Test
    def "check hello controller"() {
        expect:
        mockMvc.perform(get("/hello").accept(MediaType.parseMediaType("application/json")))
                .andExpect(api.matches().aggregating(aggregator))
    }
}

Naturally, this test will fail (it’ll say a 404 isn’t in the spec). Now I can write a REST controller that adheres to the specification.

@RestController
public class HelloController {
    @RequestMapping("/hello")
    @ResponseBody
    public TestObject index() {
        return new TestObject("Greetings from Spring Boot!", 5);
    }
    @Canonical
    static class TestObject {
        String stringValue
        Integer integerValue
    }
}

If I make a mistake, for example making the integerValue field a String, the test will fail. From a TDD point of view, this is excellent.

As for documentation, RAML is backed by MuleSoft and they have built an API console that can read a RAML file and show HTML5 documentation (using AngularJS) for that RAML file. This documentation looks a lot like Swagger, IMHO it’s even better. The API console is open source and can be embedded in an existing AngularJS application. Like Swagger, it also allows you to try out the API through the documentation.

While we all have bad memories with WSDL files and code by contract, RAML actually seems a great tool, especially in outsourced environments. Say you have your REST services developed in Bangalore, then RAML based unit tests can be a life saver (TDD is to me the only way globalized development can succeed). In the current age of prototype-based development and continuous delivery RAML may seem like overkill. However, continuous delivery relies heavily on testing in order to be succesful and RAML will certainly help to avoid a lot of issues. The fact that you get free, good-looking documentation is just a bonus at that point.

Next up: API Blueprint.

Documentation

Published at DZone with permission of Lieven Doclo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Hey Developers: Diagrams Don’t Need to Be So Complex
  • Why Developers Should Contribute To Open Source
  • Smart BDD: The Most Productive Way To Test
  • Natural Language Processing (NLP) in Software Testing: Automating Test Case Creation and Documentation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: