Challenge: Hexagonal Architecture for CRUD Operations

Develop an application for a simple data storage port using TDD with Java.

Task and requirements

Your task is to implement a data storage port in Java. The data storage port is an interface defining methods for CRUD (Create, Read, Update, Delete) operations on a data storage system. You should follow the rules outlined below:

  • Create an interface named DataStoragePort with the following methods:

    • DataStorageResponse create(T data) that creates a new data entry

    • DataStorageResponse read(String id) that retrieves data by its ID

    • DataStorageResponse update(String id, T data) that updates an existing data entry

    • DataStorageResponse delete(String id) that deletes data by its ID

  • Implement a class named DatabaseAdapter that implements the DataStoragePort interface. This class should provide the actual implementation of CRUD operations using a database.

  • Write comprehensive unit tests using JUnit to validate the correctness of the DataStoragePort and DatabaseAdapter classes. Your tests should cover various scenarios, including successful operations, error handling, and edge cases.

Rules

Here are the constraints to consider when solving the challenge:

  • The DataStorageResponse class should represent the response model for data storage operations. It should include attributes like status codes, error messages, and data payloads returned from storage operations.

  • Preserve the case (capitalization) of the original data entries.

  • Follow the TDD cycle by writing test cases first and then implementing the functionality.

Try it yourself

Add or modify the code in the widget below to implement your solution.

Get hands-on with 1200+ tech skills courses.