Skip to content

Test: add unit tests for ActivityLogService including validation and …#156

Merged
TatjanaTrajkovic merged 1 commit into
mainfrom
test/unit_tests_activity_log_service
Apr 8, 2026
Merged

Test: add unit tests for ActivityLogService including validation and …#156
TatjanaTrajkovic merged 1 commit into
mainfrom
test/unit_tests_activity_log_service

Conversation

@TatjanaTrajkovic

@TatjanaTrajkovic TatjanaTrajkovic commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

…filtering logic

Summary by CodeRabbit

  • Tests
    • Comprehensive unit tests added for activity logging and record retrieval functionality, including validation of input parameters and access control verification.

@coderabbitai

coderabbitai Bot commented Apr 7, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

New unit test class ActivityLogServiceTest added to test ActivityLogService functionality, including tests for the log() method with valid and null input validation, and getByRecord() method with authorization filtering and exception handling.

Changes

Cohort / File(s) Summary
Unit Tests for ActivityLogService
src/test/java/org/example/vet1177/services/ActivityLogServiceTest.java
Added comprehensive test class with 5 test methods covering log creation validation, null input handling, and authorization-based log filtering. Tests verify correct repository calls, exception throwing for invalid inputs, and enforcement of access policies.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers

  • johanbriger
  • annikaholmqvist94
  • lindaeskilsson

Poem

🐰 A tester's delight, so thorough and keen,
Tests logs and their rules, both strict and pristine,
Null checks and access, all covered with care,
ActivityLogService's tested with flair!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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 describes the main change: adding unit tests for ActivityLogService with a focus on validation and filtering logic, which aligns with the changeset.

✏️ 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/unit_tests_activity_log_service

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.

@TatjanaTrajkovic TatjanaTrajkovic linked an issue Apr 7, 2026 that may be closed by this pull request

@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.

🧹 Nitpick comments (2)
src/test/java/org/example/vet1177/services/ActivityLogServiceTest.java (2)

41-48: Consider verifying the saved ActivityLog contents using ArgumentCaptor.

The test only verifies that save() is called, but doesn't assert that the ActivityLog passed to save() contains the correct ActivityType, description, user, and record. Using an ArgumentCaptor would strengthen this test.

💡 Suggested enhancement
+import org.mockito.ArgumentCaptor;
 `@Test`
 void log_shouldSaveActivityLog_whenValidInput() {
+    // Arrange
+    ArgumentCaptor<ActivityLog> captor = ArgumentCaptor.forClass(ActivityLog.class);
+
     // Act
     service.log(ActivityType.CASE_CREATED, "Created case", user, record);

     // Assert
-    verify(repository, times(1)).save(any(ActivityLog.class));
+    verify(repository, times(1)).save(captor.capture());
+    ActivityLog savedLog = captor.getValue();
+    assertEquals(ActivityType.CASE_CREATED, savedLog.getAction());
+    assertEquals("Created case", savedLog.getDescription());
+    assertEquals(user, savedLog.getUser());
+    assertEquals(record, savedLog.getMedicalRecord());
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/test/java/org/example/vet1177/services/ActivityLogServiceTest.java`
around lines 41 - 48, The test log_shouldSaveActivityLog_whenValidInput
currently only asserts repository.save was called; capture the ActivityLog
passed to save using an ArgumentCaptor<ActivityLog> and assert its fields
(getType/getDescription/getUser/getRecord or matching getters) equal
ActivityType.CASE_CREATED, "Created case", the test's user and record; update
the test to call verify(repository).save(captor.capture()) then assert on
captor.getValue() and keep the existing times(1) verification if desired.

50-68: Consider verifying no side effects occur when validation fails.

Adding verifyNoInteractions(repository) at the end would confirm that save() is never called when any input is null, ensuring the validation guard clause prevents any unintended side effects.

💡 Suggested enhancement
     assertThrows(BusinessRuleException.class, () ->
             service.log(ActivityType.CASE_CREATED, "desc", user, null)
     );
+
+    verifyNoInteractions(repository);
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/test/java/org/example/vet1177/services/ActivityLogServiceTest.java`
around lines 50 - 68, The test log_shouldThrowException_whenNullInput currently
asserts exceptions for null inputs but doesn't assert there are no side effects;
after the four assertThrows calls add a verification that the mocked repository
had no interactions (e.g., call verifyNoInteractions(repository)) to ensure
repository.save() was never invoked when validation fails in
ActivityLogServiceTest and the service.log(...) guard clauses.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/test/java/org/example/vet1177/services/ActivityLogServiceTest.java`:
- Around line 41-48: The test log_shouldSaveActivityLog_whenValidInput currently
only asserts repository.save was called; capture the ActivityLog passed to save
using an ArgumentCaptor<ActivityLog> and assert its fields
(getType/getDescription/getUser/getRecord or matching getters) equal
ActivityType.CASE_CREATED, "Created case", the test's user and record; update
the test to call verify(repository).save(captor.capture()) then assert on
captor.getValue() and keep the existing times(1) verification if desired.
- Around line 50-68: The test log_shouldThrowException_whenNullInput currently
asserts exceptions for null inputs but doesn't assert there are no side effects;
after the four assertThrows calls add a verification that the mocked repository
had no interactions (e.g., call verifyNoInteractions(repository)) to ensure
repository.save() was never invoked when validation fails in
ActivityLogServiceTest and the service.log(...) guard clauses.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6cebdfe1-d24b-4655-b353-c9838e66cabe

📥 Commits

Reviewing files that changed from the base of the PR and between ba13529 and 387ae4f.

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

@annikaholmqvist94 annikaholmqvist94 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.

Snyggt!

@lindaeskilsson lindaeskilsson 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.

jättebra!

@TatjanaTrajkovic
TatjanaTrajkovic merged commit 970c5d0 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: add unit test for ActivityLogService

3 participants