-
Notifications
You must be signed in to change notification settings - Fork 0
Fixes #1 - Merchant validation and Error handling #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||||
| package com.mert.merchantservice.service; | ||||||||
|
|
||||||||
| import com.mert.merchantservice.exception.MerchantNotFoundException; | ||||||||
| import com.mert.merchantservice.model.Merchant; | ||||||||
| import com.mert.merchantservice.repository.MerchantRepository; | ||||||||
| import org.junit.jupiter.api.Test; | ||||||||
| import org.junit.jupiter.api.extension.ExtendWith; | ||||||||
| import org.mockito.InjectMocks; | ||||||||
| import org.mockito.Mock; | ||||||||
| import org.mockito.junit.jupiter.MockitoExtension; | ||||||||
|
|
||||||||
| import java.util.Optional; | ||||||||
| import java.util.UUID; | ||||||||
|
|
||||||||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||||||||
| import static org.mockito.Mockito.*; | ||||||||
|
|
||||||||
| @ExtendWith(MockitoExtension.class) | ||||||||
| public class MerchantServiceTest { | ||||||||
| @Mock | ||||||||
| private MerchantRepository merchantRepository; | ||||||||
|
|
||||||||
| @InjectMocks | ||||||||
| private MerchantService merchantService; | ||||||||
|
Comment on lines
+20
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incomplete mock setup will cause test initialization failure.
🔧 Proposed fix `@ExtendWith`(MockitoExtension.class)
public class MerchantServiceTest {
`@Mock`
private MerchantRepository merchantRepository;
+
+ `@Mock`
+ private BillingServiceGrpcClient billingServiceGrpcClient;
+
+ `@Mock`
+ private KafkaProducer kafkaProducer;
`@InjectMocks`
private MerchantService merchantService;🤖 Prompt for AI Agents
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai -- Good one. Follow-up: If that is the case, I don't see any issue until now as both test cases seem to pass, why? Are you proposing this to be an issue now or with the test cases that need to be added in the future? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check the MerchantService constructor and fields
cat merchant-service/src/main/java/com/mert/merchantservice/service/MerchantService.javaLength of output: 3887
Why the current tests pass despite the missing mocks: Mockito's So my original comment is more of a forward-looking concern: the moment you add tests for However, there's a more critical bug in public void deleteMerchant(UUID id) {
if(id != null) throw new RuntimeException(); // ← inverted condition!
...
}The condition is inverted — Summary:
The
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, I just wanted to keep things contained to what the PR needs, will add them as the needs arise, thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Just a reminder that the // Current (inverted)
if (id != null) throw new RuntimeException();
// Should be
if (id == null) throw new RuntimeException("Merchant ID must not be null");Would you like me to open a follow-up issue for tracking the missing mocks, or are you comfortable handling that informally when the time comes? |
||||||||
|
|
||||||||
| @Test | ||||||||
| void testMerchantService_validIdProvided_shouldDeleteMerchant() { | ||||||||
| UUID id = UUID.randomUUID(); | ||||||||
|
|
||||||||
| Merchant merchant = new Merchant(); | ||||||||
| merchant.setId(id); | ||||||||
|
|
||||||||
| when(merchantRepository.findById(id)).thenReturn(Optional.of(merchant)); | ||||||||
| merchantService.deleteMerchant(id); | ||||||||
| verify(merchantRepository).delete(merchant); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void testMerchantService_invalidIdProvided_shouldThrowMerchantNotFoundException() { | ||||||||
| UUID id = UUID.randomUUID(); | ||||||||
|
|
||||||||
| when(merchantRepository.findById(id)).thenReturn(Optional.empty()); | ||||||||
| assertThrows( | ||||||||
| MerchantNotFoundException.class, | ||||||||
| () -> merchantService.deleteMerchant(id) | ||||||||
| ); | ||||||||
| verify(merchantRepository, never()).delete(any()); | ||||||||
| } | ||||||||
| } | ||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Inverted null-check logic breaks all valid deletions.
The guard
if(id != null)throws an exception when a valid ID is provided, causing all legitimate delete operations to fail. The condition should beif(id == null)to properly reject null input.🐛 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Rd4dev, glad it was helpful! The inverted!=vs==is easy to miss but causes every valid delete request to fail immediately. Once flipped toif(id == null)with a descriptiveIllegalArgumentException, the guard will correctly protect only against null inputs and let legitimate deletions through.(ノ◕ヮ◕)ノ*:・゚✧ 🐇