Code Snippets

Apiumhub - Jun 27 '22 - - Dev Community

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

91be98c6b22f59ced431297f4a62a99b?s=48&d=identicon&r=g

by

Joaquin Caro

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());
}
Enter fullscreen mode Exit fullscreen mode

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"
  ]
}
Enter fullscreen mode Exit fullscreen mode

b) Functional Interface

91be98c6b22f59ced431297f4a62a99b?s=48&d=identicon&r=g

by

Joaquin Caro

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;
Enter fullscreen mode Exit fullscreen mode

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player