Code Snippets

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Code Snippets serves as a tool for developers to use samples as documentation, and we almost always browse code before reading a topic fully. Because of its special regard, we have made some examples of effective code samples and snippets that follow:

  • Indications: where the code fits in with required elements. Following code best practices.
  • Purpose: to demonstrate specific functionality.

Here are the Code Snippets by Joaquín Caro, Tech Lead at Apiumhub:

a) Validate and test APIs with RestAssured

Description

RestAssured is a library to validate and test our APIS. A simple example of validating if an API complies with the contract we want would be:

Language/Framework

Java

@Test
void should_return_allowed_countries() {
    Response response = RestAssured.given()
            .contentType(ContentType.JSON)
            .port(port)
            .header(new Header("Accept-language", "en-US"))
            .when()
            .get("/countries");
    response.then().body(matchesJsonSchemaInClasspath("schemas/countriesResponse.json"));

    assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
}

And the JSON Scheme should be:

JSON

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "countries": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string",
            "description": "The ISO 3166-1 alpha-2 code of the country."
          },
          "name": {
            "type": "string",
            "description": "The name of the country."
          }
        },
        "required": [
          "code",
          "name"
        ]
      },
      "description": "List of country objects."
    }
  },
  "required": [
    "countries"
  ]
}

b) Functional Interface

Description

Since the arrival of Java 8, an interface with a single method is considered a functional interface and can be implemented as a lambda.

Language/Framework

Java

// Interface
@FunctionalInterface
public interface FeatureFlagService {
// Interface
@FunctionalInterface
public interface FeatureFlagService {
    Boolean isEnabled(String key);
}

// Implementation
FeatureFlagService flagService = key -> true;

Conclusions

You may notice how interfaces are marked with the @FunctionalInterface annotation. This is just to help other developers understand that this is clearly a functional interface, and is supposed to have only one method.

Interfaces are marked with the @FunctionalInterface annotation. This is just to help developers understand what a functional interface is, and it is only supposed to have a single method.

Leave a Reply

Your email address will not be published.

Subscribe To Our Newsletter

Get updates from our latest tech findings

About Apiumhub

Apiumhub brings together a community of software developers & architects to help you transform your idea into a powerful and scalable product. Our Tech Hub specialises in Software ArchitectureWeb Development & Mobile App Development. Here we share with you industry tips & best practices, based on our experience.

Popular posts
Get Early Release: Software Architecture Metrics

Global Software Architecture Summit '22

Book your spot now!

Book Now

Have a challenging project?

We Can Work On It Together