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)
: Creates a new data entry.DataStorageResponse read(String id)
: Retrieves data by its ID.DataStorageResponse update(String id, T data)
: Updates an existing data entry.DataStorageResponse delete(String id)
: Deletes data by its ID.
Implement a class named
DatabaseAdapter
that implements theDataStoragePort
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
andDatabaseAdapter
classes. Your tests should cover various scenarios, including successful operations, error handling, and edge cases.
Rules
Below are the constraints to consider when solving the challenge.
The
DataStorageResponse
class represents the response model for data storage operations. It includes 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.