- Java 11
- Spring Boot
- Spring Data JPA
- H2 in-memory database
- Lombok - for reducing the amount of boilerplate constructors, getters, and setters in POJO entities
- JUnit
- Mockito - for mocking the database layer when testing the service layer
- MockMvc - for writing integration tests
- AssertJ
- GitHub Actions
- SonarQube
- JaCoCo
- Clone the repository
git clone https://github.com/roshanadh/vpps.git
- Run the application
cd vpps && .\mvnw spring-boot:run
This sets up the project and runs the application on port 8080.
GET /
The API returns a simple Hello World!
message as a response.
GET /batteries/{ID}
Path Variable | Type | Description |
---|---|---|
ID |
int |
ID of a battery resource |
Upon successful fetching of the resource, the API returns a JSON response with the following structure:
{
"id" : int,
"name" : string,
"postcode" : string,
"capacity": int
}
The id
attribute contains an integer ID auto-generated when a battery resource is persisted in the database.
The name
attribute contains the name of the battery resource.
The postcode
attribute contains the postcode information of the battery resource.
The capacity
attribute contains the watt-capacity information of the battery resource.
GET /batteries/
Upon successful fetching of the resources, the API returns a list of JSON objects with the following structure:
[
{
"id" : int,
"name" : string,
"postcode" : string,
"capacity": int
},
...
]
The id
attribute contains an integer ID auto-generated when a battery resource is persisted in the database.
The name
attribute contains the name of the battery resource.
The postcode
attribute contains the postcode information of the battery resource.
The capacity
attribute contains the watt-capacity information of the battery resource.
To get a list of the names of batteries having postcode within the range provided in the request.
GET /batteries/postcode?start={}&end={}
Query Parameter | Type | Description |
---|---|---|
start |
integer (e.g., 01002) |
start of the range |
end |
integer (e.g., 01002) |
end of the range |
Both, start
and end
, parameter values are included in the range.
Both parameter values must be numeric string. If the provided range values cannot be parsed into an integer value, a 400 BAD REQUEST
response is returned.
Upon successful fetching of the resources, the API returns a JSON response with the following structure:
{
"totalCapacity" : long,
"avgCapacity" : double,
"batteries" : [],
}
The totalCapacity
attribute contains a long-valued sum of the capacities of battery resources found in the given postcode range.
The avgCapacity
attribute contains a double-valued average of the capacities of battery resources found in the given postcode range.
The batteries
attribute contains the list of names of battery resources found in the given postcode range.
To persist a list of the battery resources in the in-memory H2 database.
POST /batteries/
The payload for this POST request is an array of JSON objects, each object being a battery resource to be persisted. The payload has the following structure:
[
{
"name" : string,
"postcode" : string,
"capacity" : int,
},
...
]
Upon successful persisting of the resources, the API returns a JSON response similar to the payload, but with an id
attribute included in each JSON object:
[
{
"id" : int,
"name" : string,
"postcode" : string,
"capacity": int
},
...
]
The id
attribute contains an integer ID auto-generated when a battery resource is persisted in the database.
The name
attribute contains the name of the battery resource.
The postcode
attribute contains the postcode information of the battery resource.
The capacity
attribute contains the watt-capacity information of the battery resource.
To persist a single battery resource in the in-memory H2 database.
POST /batteries/new
The payload for this POST request is a JSON object, representing the battery resource to be persisted. The payload has the following structure:
{
"name" : string,
"postcode" : string,
"capacity" : int,
}
Upon successful persisting of the resource, the API returns a JSON response similar to the payload, but with an id
attribute included in the JSON object:
{
"id" : int,
"name" : string,
"postcode" : string,
"capacity": int
}
The id
attribute contains an integer ID auto-generated when a battery resource is persisted in the database.
The name
attribute contains the name of the battery resource.
The postcode
attribute contains the postcode information of the battery resource.
The capacity
attribute contains the watt-capacity information of the battery resource.
To update a battery resource in the in-memory H2 database.
PUT /batteries/
The payload for this POST request is a JSON object, with a mandatory id
field. If the resource identified by id
does not exist in the database, a 404 response is made.
The payload has the following structure:
{
"id" : int,
"name" : String,
"postcode" : String,
"capacity" : int
}
Upon successful update of the resource, the API returns a JSON response containing the resource that was updated (identical to the request payload):
{
"id" : int,
"name" : string,
"postcode" : string,
"capacity": int
}
The id
attribute contains an integer ID auto-generated when a battery resource is persisted in the database.
The name
attribute contains the name of the battery resource.
The postcode
attribute contains the postcode information of the battery resource.
The capacity
attribute contains the watt-capacity information of the battery resource.
DELETE /batteries/{ID}
Path Variable | Type | Description |
---|---|---|
ID |
int |
ID of a battery resource |
Upon successful deletion of the resource, the API returns a JSON response containing the resource that was just deleted:
{
"id" : int,
"name" : string,
"postcode" : string,
"capacity": int
}
The id
attribute contains an integer ID auto-generated when a battery resource is persisted in the database.
The name
attribute contains the name of the battery resource.
The postcode
attribute contains the postcode information of the battery resource.
The capacity
attribute contains the watt-capacity information of the battery resource.
The API can respond with the following status codes:
Status Code | Description | Example Triggering Scenario |
---|---|---|
200 | OK |
List of batteries fetched successfully |
201 | CREATED |
List of batteries persisted successfully |
400 | BAD REQUEST |
Invalid range of postcode given in request |
404 | NOT FOUND |
No battery exists with given ID |
416 | RANGE NOT SATISFIABLE |
No battery exists within given range of postcode |
500 | INTERNAL SERVER ERROR |
Internal error in API |