Skip to content

test: add CommentController MVC tests and remove insuranceNumber from Pet#130

Merged
annikaholmqvist94 merged 2 commits intomainfrom
feat/comment-controller-testing
Apr 6, 2026
Merged

test: add CommentController MVC tests and remove insuranceNumber from Pet#130
annikaholmqvist94 merged 2 commits intomainfrom
feat/comment-controller-testing

Conversation

@annikaholmqvist94
Copy link
Copy Markdown
Contributor

@annikaholmqvist94 annikaholmqvist94 commented Apr 5, 2026

Summary

  • Lägger till CommentControllerTest med 19 MVC-tester som täcker alla endpoints i CommentController (POST, GET, PUT, DELETE, GET /count)
  • Tar bort insuranceNumber från Pet-lagret (PetRequest, PetResponse, PetService, PetController) och fixar bugg där updatedPet refererades istället för request
  • Lägger till spring-boot-starter-security-test och spring-boot-starter-webmvc-test som testberoenden

Test coverage

Varje endpoint testas för:

  • Happy path (200/204)
  • Valideringsfel (400)
  • Resurs ej hittad (404)
  • Ej behörig (403)

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Removed insurance number field from pet-related API requests and responses
  • Tests

    • Added comprehensive test coverage for comment controller endpoints including create, read, update, delete, and count operations
    • Enhanced test infrastructure with additional testing capabilities

- Removed `insuranceNumber` from `PetRequest`, `PetResponse`, and relevant methods in `PetController` and `PetService`.
- Updated logic to align with the simplified data model.
- Added test coverage for endpoints: `create`, `getByRecord`, `update`, `delete`, and `countByRecord`.
- Ensured proper exception handling for validation errors, forbidden access, and missing resources.
- Updated `pom.xml` to include test dependencies for Spring Security and WebMVC.

Closes #115
Closes #98
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 5, 2026

📝 Walkthrough

Walkthrough

This PR removes the insuranceNumber field from pet-related DTOs and service logic, and introduces comprehensive Spring MVC controller tests for the CommentController API with security validation. Two test-scoped Maven dependencies were added to support web layer and security testing.

Changes

Cohort / File(s) Summary
Pet Insurance Number Removal
src/main/java/org/example/vet1177/dto/request/pet/PetRequest.java, src/main/java/org/example/vet1177/dto/response/pet/PetResponse.java, src/main/java/org/example/vet1177/controller/PetController.java, src/main/java/org/example/vet1177/services/PetService.java
Removed insuranceNumber field from request/response DTOs and eliminated its handling in PetService's updatePet() and applyPetRequest() methods; PetController no longer includes insurance number in response construction.
Test Dependencies
pom.xml
Added spring-boot-starter-security-test and spring-boot-starter-webmvc-test test-scoped dependencies to Maven configuration.
Comment Controller Tests
src/test/java/org/example/vet1177/controller/CommentControllerTest.java
New Spring MVC test class covering CREATE/READ/UPDATE/DELETE endpoints with validation for POST body/recordId, response structure, HTTP status codes (200/400/403/404), and security-authenticated requests.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

Suggested labels

test, backend

Suggested reviewers

  • johanbriger
  • lindaeskilsson
  • TatjanaTrajkovic

Poem

🐰 Insurance numbers fade away so clean,
While comment tests guard every scene,
Security posts now verify the way,
Controllers tested in every way,
Hoppy refactoring brings the day! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.08% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the two primary changes: adding CommentController MVC tests and removing insuranceNumber from the Pet layer across multiple files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/comment-controller-testing

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main/java/org/example/vet1177/services/PetService.java (1)

110-114: Prefer reusing applyPetRequest(...) here.

The assignments are correct, but they duplicate the same mapping already centralized in applyPetRequest(...). Calling the helper from updatePet(...) keeps create/update in sync and reduces the chance of this path drifting again the next time PetRequest changes.

♻️ Suggested simplification
-        existingPet.setName(request.getName());
-        existingPet.setSpecies(request.getSpecies());
-        existingPet.setBreed(request.getBreed());
-        existingPet.setDateOfBirth(request.getDateOfBirth());
-        existingPet.setWeightKg(request.getWeightKg());
+        applyPetRequest(existingPet, request);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/org/example/vet1177/services/PetService.java` around lines 110
- 114, In updatePet(...), replace the repeated field-by-field assignments to
existingPet (setName, setSpecies, setBreed, setDateOfBirth, setWeightKg) with a
call to the helper applyPetRequest(existingPet, request) so the same mapping
logic used during create is reused; locate the updatePet method and invoke
applyPetRequest with the existingPet and the incoming PetRequest to keep
mappings centralized and avoid duplication.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pom.xml`:
- Around line 56-60: The POM includes a non-existent dependency artifactId
"spring-boot-starter-webmvc-test"; remove that dependency block and rely on the
existing "spring-boot-starter-test" dependency (which already provides MockMvc
and `@WebMvcTest` support) or replace it with the correct official artifact if
needed; locate the dependency with artifactId "spring-boot-starter-webmvc-test"
in the pom and delete it so the build no longer fails.

---

Nitpick comments:
In `@src/main/java/org/example/vet1177/services/PetService.java`:
- Around line 110-114: In updatePet(...), replace the repeated field-by-field
assignments to existingPet (setName, setSpecies, setBreed, setDateOfBirth,
setWeightKg) with a call to the helper applyPetRequest(existingPet, request) so
the same mapping logic used during create is reused; locate the updatePet method
and invoke applyPetRequest with the existingPet and the incoming PetRequest to
keep mappings centralized and avoid duplication.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: dd9f7e6c-9d53-440f-95fc-4760d95e6313

📥 Commits

Reviewing files that changed from the base of the PR and between f98cc06 and 474bbbc.

📒 Files selected for processing (6)
  • pom.xml
  • src/main/java/org/example/vet1177/controller/PetController.java
  • src/main/java/org/example/vet1177/dto/request/pet/PetRequest.java
  • src/main/java/org/example/vet1177/dto/response/pet/PetResponse.java
  • src/main/java/org/example/vet1177/services/PetService.java
  • src/test/java/org/example/vet1177/controller/CommentControllerTest.java
💤 Files with no reviewable changes (2)
  • src/main/java/org/example/vet1177/controller/PetController.java
  • src/main/java/org/example/vet1177/dto/request/pet/PetRequest.java

Copy link
Copy Markdown
Contributor

@TatjanaTrajkovic TatjanaTrajkovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ser bra ut! Perfekt att det löser buggarna i main. Grymt jobbat Annika!!

@annikaholmqvist94 annikaholmqvist94 merged commit 5cc4ff3 into main Apr 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants