Skip to content

test: add unit tests for PetPolicy#163

Merged
lindaeskilsson merged 6 commits into
mainfrom
test/pet-policy
Apr 8, 2026
Merged

test: add unit tests for PetPolicy#163
lindaeskilsson merged 6 commits into
mainfrom
test/pet-policy

Conversation

@lindaeskilsson

@lindaeskilsson lindaeskilsson commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Closes #143

Täcker canCreate, canView, canViewOwnerPets, canUpdate och canDelete för rollerna ADMIN, OWNER och VET, inklusive edge cases som null-ägare och åtkomst till andras djur.

Summary by CodeRabbit

  • Tests
    • Added comprehensive tests for pet access controls, verifying who can create, view, update, delete, and list owner pets across Admin, Owner, and Vet roles — including scenarios where a pet has no owner — to ensure consistent permission enforcement.

@coderabbitai

coderabbitai Bot commented Apr 8, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Adds a new JUnit 5 test class PetPolicyTest that verifies PetPolicy authorization rules for ADMIN, OWNER, and VET roles across operations: canCreate, canView, canViewOwnerPets, canUpdate, and canDelete, including ownership and null-owner edge cases.

Changes

Cohort / File(s) Summary
Pet Authorization Tests
src/test/java/org/example/vet1177/policy/PetPolicyTest.java
New package-private JUnit 5 test class (≈170 added lines) that creates User fixtures (ADMIN, two OWNERs, VET), a Pet fixture, injects UUIDs via reflection, and asserts PetPolicy outcomes for canCreate, canView, canViewOwnerPets, canUpdate, and canDelete, including cases where pet.owner is null and owner-id comparisons.

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • Feat/pet service policy #74: Adds JUnit tests exercising PetPolicy methods (canCreate/canView/canViewOwnerPets/canUpdate/canDelete), directly related to these authorization tests.

Suggested reviewers

  • TatjanaTrajkovic
  • annikaholmqvist94
  • johanbriger

Poem

🐰 I nibble at code with a curious hop,
I test who may pat and who must stop,
Admins can cheer, owners keep guard,
Vets wait kindly, permissions unmarred,
A rabbit's small rhyme for a test well-trod.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 26.32% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'test: add unit tests for PetPolicy' is clear, specific, and accurately summarizes the main change: adding unit tests for the PetPolicy class.
Linked Issues check ✅ Passed The code changes comprehensively implement all five PetPolicy methods' authorization requirements from issue #143, testing all specified role behaviors (ADMIN, OWNER, VET) and edge cases.
Out of Scope Changes check ✅ Passed All changes are directly related to adding unit tests for PetPolicy as specified in the linked issue; no unrelated modifications or out-of-scope changes were introduced.

✏️ 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 test/pet-policy

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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/test/java/org/example/vet1177/policy/PetPolicyTest.java (1)

54-145: Consider parameterized tests to reduce duplication in role/ownership matrices.

This suite is readable as-is, but @ParameterizedTest + role/ownership inputs would reduce repeated setup/assertion patterns and make future policy changes easier to update.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/test/java/org/example/vet1177/policy/PetPolicyTest.java` around lines 54
- 145, Replace the repetitive role/ownership test methods (e.g.,
canCreate_owner_shouldReturnTrue, canCreate_admin_shouldReturnTrue,
canView_admin_shouldReturnTrue, canView_ownerOfPet_shouldReturnTrue,
canView_ownerWithNullPetOwner_shouldReturnFalse,
canViewOwnerPets_admin_shouldReturnTrue,
canViewOwnerPets_ownerViewingOwnPets_shouldReturnTrue,
canDelete_admin_shouldReturnTrue,
canDelete_ownerWithNullPetOwner_shouldReturnFalse, etc.) with a few
`@ParameterizedTest` methods that drive policy.canCreate(...),
policy.canView(...), policy.canViewOwnerPets(...), and policy.canDelete(...)
using `@CsvSource` or `@MethodSource`; provide parameters for principal (role
identifier), target pet or ownerId (or null marker), and expected boolean
result, map the role identifiers to the existing fixtures (admin, owner, vet,
otherOwner) inside the provider/helper, and assert the expected boolean — this
keeps existing fixtures and assertions but collapses duplicated tests into
parameterized tables for easier maintenance.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/test/java/org/example/vet1177/policy/PetPolicyTest.java`:
- Around line 118-147: Add unit tests mirroring the canDelete cases to cover
canUpdate on the PetPolicy: create tests that assert policy.canUpdate(admin,
pet) is true, policy.canUpdate(owner, pet) is true, policy.canUpdate(otherOwner,
pet) is false, policy.canUpdate(vet, pet) is false, and a test where
pet.setOwner(null) then policy.canUpdate(owner, pet) is false; use the same test
structure/naming pattern as the existing canDelete tests and the same fixtures
(policy, admin, owner, otherOwner, vet, pet).

---

Nitpick comments:
In `@src/test/java/org/example/vet1177/policy/PetPolicyTest.java`:
- Around line 54-145: Replace the repetitive role/ownership test methods (e.g.,
canCreate_owner_shouldReturnTrue, canCreate_admin_shouldReturnTrue,
canView_admin_shouldReturnTrue, canView_ownerOfPet_shouldReturnTrue,
canView_ownerWithNullPetOwner_shouldReturnFalse,
canViewOwnerPets_admin_shouldReturnTrue,
canViewOwnerPets_ownerViewingOwnPets_shouldReturnTrue,
canDelete_admin_shouldReturnTrue,
canDelete_ownerWithNullPetOwner_shouldReturnFalse, etc.) with a few
`@ParameterizedTest` methods that drive policy.canCreate(...),
policy.canView(...), policy.canViewOwnerPets(...), and policy.canDelete(...)
using `@CsvSource` or `@MethodSource`; provide parameters for principal (role
identifier), target pet or ownerId (or null marker), and expected boolean
result, map the role identifiers to the existing fixtures (admin, owner, vet,
otherOwner) inside the provider/helper, and assert the expected boolean — this
keeps existing fixtures and assertions but collapses duplicated tests into
parameterized tables for easier maintenance.
🪄 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: b9fb2c3e-6cf7-47c8-9013-0f433711f970

📥 Commits

Reviewing files that changed from the base of the PR and between f67dca5 and 61701c9.

📒 Files selected for processing (1)
  • src/test/java/org/example/vet1177/policy/PetPolicyTest.java

Comment thread src/test/java/org/example/vet1177/policy/PetPolicyTest.java

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
src/test/java/org/example/vet1177/policy/PetPolicyTest.java (1)

147-168: ⚠️ Potential issue | 🟠 Major

Add missing canUpdate ADMIN test to satisfy issue #143 acceptance criteria.

Line [148]-Line [168] covers OWNER/VET/null-owner cases, but canUpdate(admin, pet) is still missing, so the canUpdate matrix is incomplete.

➕ Suggested addition
     //canUpdate
+    `@Test`
+    void canUpdate_admin_shouldReturnTrue() {
+        assertThat(policy.canUpdate(admin, pet)).isTrue();
+    }
+
     `@Test`
     void canUpdate_ownerOfPet_shouldReturnTrue() {
         assertThat(policy.canUpdate(owner, pet)).isTrue();
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/test/java/org/example/vet1177/policy/PetPolicyTest.java` around lines 147
- 168, The test matrix for PetPolicy.canUpdate is missing the ADMIN case; add a
unit test in PetPolicyTest that asserts policy.canUpdate(admin, pet) is true.
Create a new test method (e.g., canUpdate_admin_shouldReturnTrue) that uses the
existing admin test fixture/variable and the pet instance (ensure pet.owner is
set appropriately) and asserts isTrue() like the other canUpdate tests to
complete the coverage for ADMIN.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@src/test/java/org/example/vet1177/policy/PetPolicyTest.java`:
- Around line 147-168: The test matrix for PetPolicy.canUpdate is missing the
ADMIN case; add a unit test in PetPolicyTest that asserts
policy.canUpdate(admin, pet) is true. Create a new test method (e.g.,
canUpdate_admin_shouldReturnTrue) that uses the existing admin test
fixture/variable and the pet instance (ensure pet.owner is set appropriately)
and asserts isTrue() like the other canUpdate tests to complete the coverage for
ADMIN.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e71f5512-1b4f-4d53-9fd4-3f763d0a7cda

📥 Commits

Reviewing files that changed from the base of the PR and between 61701c9 and d758635.

📒 Files selected for processing (1)
  • src/test/java/org/example/vet1177/policy/PetPolicyTest.java

@TatjanaTrajkovic TatjanaTrajkovic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ser bra ut!

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: PetPolicyTest – behörighetsregler

2 participants