-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement transactional method for InboxProductCreationService with r…
…ollback support
- Loading branch information
1 parent
41b176d
commit 950e216
Showing
5 changed files
with
91 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...h/src/test/java/enit/ecomerce/search_product/service/InboxProductCreationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import enit.ecomerce.search_product.product.Product; | ||
import enit.ecomerce.search_product.product.ProductEntity; | ||
import enit.ecomerce.search_product.repository.ProductRepository; | ||
import enit.ecomerce.search_product.repository.ProducteEntityRepository; | ||
import enit.ecomerce.search_product.service.InboxProductCreationService; | ||
import jakarta.transaction.Transactional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.mockito.Mockito; | ||
import static org.mockito.Mockito.*; | ||
|
||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@Transactional | ||
class InboxProductCreationServiceTest { | ||
|
||
@InjectMocks | ||
private InboxProductCreationService inboxProductCreationService; | ||
|
||
@Mock | ||
private ProducteEntityRepository productEntityRepository; | ||
|
||
@Mock | ||
private ProductRepository productRepository; | ||
|
||
@Test | ||
@Transactional | ||
void testTransactional_RollbackOnError() { | ||
// Arrange | ||
ProductEntity validProduct = new ProductEntity(); | ||
validProduct.setId("1"); | ||
validProduct.setName("Valid Product"); | ||
validProduct.setPrice(100.0f); | ||
validProduct.setIndex(false); | ||
|
||
ProductEntity invalidProduct = new ProductEntity(); | ||
invalidProduct.setId("2"); | ||
invalidProduct.setName("Invalid Product"); | ||
invalidProduct.setPrice(null); // This will cause an exception | ||
|
||
when(productEntityRepository.findUnindexedProducts()).thenReturn(List.of(validProduct, invalidProduct)); | ||
|
||
doThrow(new IllegalArgumentException("Invalid product price")) | ||
.when(productRepository).save(any(Product.class)); | ||
|
||
// Act | ||
try { | ||
inboxProductCreationService.treatInbox(); | ||
} catch (Exception e) { | ||
// Exception expected | ||
} | ||
|
||
// Assert | ||
// Verify that the valid product was not saved because of rollback | ||
verify(productEntityRepository, never()).save(validProduct); | ||
verify(productRepository, times(1)).save(any(Product.class)); // Attempt to save was made | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<configuration> | ||
<root level="info"> | ||
<appender-ref ref="CONSOLE" /> | ||
</root> | ||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
</configuration> |