This project implements a simple REST API as per the assessment requirements.
The API exposes a single endpoint:
POST /grunnlag: Accepts a JSON payload representingGrunnlagRequestdata. It validates if the sum ofsaldoandaksjeandelvalues within theoppgavelist matches thesumSaldoandsumAksjehandelvalues provided in theoppgaveoppsummering.- Returns HTTP 200 OK with body "Gyldige data" if the sums match.
- Returns HTTP 400 Bad Request with body "Ugyldige data" if the sums do not match or if the input data is invalid/malformed.
- Clone the repository.
- Ensure you have Java (version specified in
pom.xml) and Maven installed. - Navigate to the project root directory.
- Run the application using Maven:
mvn spring-boot:run
- The API will be available at
http://localhost:8080/grunnlag.
The testing strategy for this project aims for comprehensive coverage by combining unit and integration tests, focusing on verifying the core requirements of the assessment.
- Core Business Logic (Service Layer): The primary focus was testing the
GrunnlagService.validerGrunnlagmethod, which contains the critical validation logic comparing calculated sums against provided summary sums. Both valid (matching sums) and invalid (mismatched sums) scenarios were tested. Edge cases like null input or null values within the data structure were also considered. - API Endpoint (Controller Layer): The
GrunnlagControllerwas tested to ensure it correctly handles incomingPOSTrequests to/grunnlag, deserializes the JSON payload, delegates to theGrunnlagService, and returns the appropriate HTTP status codes (200 OK for valid data, 400 Bad Request for invalid data based on the service's validation result). Tests also cover the handling of fundamentally malformed JSON input. - Data Model (Model Layer): Basic tests ensure the
GrunnlagRequestmodel and its nested classes can be instantiated and correctly handle data, including deserialization from a valid JSON structure (request.json).
- Unit Tests (
GrunnlagServiceTest): TheGrunnlagServicewas tested in isolation using plain JUnit tests. Test instances ofGrunnlagRequestwere created (often by deserializing JSON from files using Jackson'sObjectMapper) and passed to thevaliderGrunnlagmethod. Assertions (assertTrue,assertFalse) were used to verify the boolean result based on the input data (valid sums vs. invalid sums). - Integration Tests (
GrunnlagControllerTest,GrunnlagApplicationTests): Spring Boot's testing support (@SpringBootTest,@AutoConfigureMockMvc) was used.MockMvcsimulated HTTP requests to the/grunnlagendpoint.- Valid requests were sent using the content of
request.json, expecting an HTTP 200 status. - Invalid requests (specifically testing the mismatched sums requirement) were sent using a dedicated JSON file (
invalid-sum-request.json- assuming this was created as suggested) containing valid structure but incorrect summary values, expecting an HTTP 400 status. - Malformed requests were tested using
schema.json(which is not a valid request payload) to ensure the controller/framework correctly returns HTTP 400 for parsing/validation errors.
- Valid requests were sent using the content of
- Test Data from Files: Key test scenarios (valid request, invalid sums request, malformed request) load their input data from
.jsonfiles located insrc/test/resources/json. This keeps test code cleaner and separates test data from test logic.
- Isolation: Unit testing the service allows for focused verification of the core business logic without the overhead of the web framework, making tests faster and easier to debug.
- Integration:
MockMvctests for the controller ensure that the web layer is correctly configured, requests are routed properly, data binding works, the controller interacts correctly with the service, and the HTTP responses (status codes, body) are accurate according to the requirements. - Confidence: Combining unit and integration tests provides higher confidence that the application works correctly end-to-end, from receiving an HTTP request to executing the business logic and returning the result.
- Maintainability: Using external files for test data makes it easier to manage and update test cases without modifying the test code itself.
- Requirement Focused: The tests directly address the assessment's core requirements: the existence of the endpoint, delegation to a service, the specific sum validation logic, and the corresponding 200/400 status code responses.