Skip to content

Commit

Permalink
test: add unit tests for CheckoutServiceImpl [skip ci]
Browse files Browse the repository at this point in the history
squash commit for PR #158
solves issue #143

original commit history is as follows

commit 07b0ec2 (HEAD -> pr-158, pull/158/merge)
Merge: 1a11eeb 95a3714
Author: Rod Fetalvero <botsot.felix@gmail.com>
Date:   Mon Oct 17 16:59:30 2022 +0000

    Merge 95a3714 into 1a11eeb

commit 95a3714 (refs/pull/158/head)
Merge: 17f2aeb 1a11eeb
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Tue Oct 18 00:59:04 2022 +0800

    Merge branch 'main' of https://github.com/NyorJa/spring-boot-backend into issue-144

commit 17f2aeb
Merge: 4434468 7854941
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Mon Oct 17 12:15:04 2022 +0800

    Merge branch 'main' of https://github.com/NyorJa/spring-boot-backend into issue-144

commit 4434468
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Sat Oct 15 12:40:41 2022 +0800

    Optimize imports

commit 3f78370
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Sat Oct 15 12:32:03 2022 +0800

    Use ONE Constant

commit c0bd1e3
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Sat Oct 15 12:30:53 2022 +0800

    Update changelog entry

commit 79c1d80
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Sat Oct 15 12:24:35 2022 +0800

    Test CheckoutService

       * Add int 1
       * More coverage

commit 19ddd8b
Merge: aeb85d6 76b5391
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Sat Oct 15 10:04:11 2022 +0800

    Merge branch 'main' of https://github.com/NyorJa/spring-boot-backend into issue-144

commit aeb85d6
Merge: 18fa6f5 496d2cf
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Fri Oct 14 01:16:02 2022 +0800

    Merge branch 'main' of https://github.com/NyorJa/spring-boot-backend into issue-144

commit 18fa6f5 (refs/pull/149/head)
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Fri Oct 14 00:55:21 2022 +0800

    Move test item changelog to proper position

commit 2eb7014
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Fri Oct 14 00:44:07 2022 +0800

    Fix code smell

commit fb4add7
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Fri Oct 14 00:36:02 2022 +0800

    Update changelog

commit 6cd00d2
Author: Nyor Ja <botsot.felix@gmail.com>
Date:   Fri Oct 14 00:23:31 2022 +0800

    Add CompanyServiceImpl Test
  • Loading branch information
NyorJa committed Oct 18, 2022
1 parent 1a11eeb commit bd23cf2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Tests
- RegistrationServiceImpl - add unit tests
- Refactor PhoneNumberValidatorTest - resolve code smell tag by sonarqube and @ParameterizedTest is the solution
- Additional entry in TestConstants `int 1`
- CheckoutServiceImpl - add more coverage for method "confirmTransaction, generatePageUrl"
- CompanyServiceImpl - validate the proper mapping behaviour of method "readDetails"
- Add `mockito-inline` dependency to create inline mocks - **Thank you `@NyorJa`**
- Useful to mock final classes and methods, also for having constant regex
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/trebol/constant/TestConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class TestConstants {

public static final String ANY = "ANY";
public static final Long ID_1L = 1L;
public static final int ONE = 1;
}
110 changes: 98 additions & 12 deletions src/test/java/org/trebol/operation/services/CheckoutServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -18,12 +19,22 @@
import org.trebol.pojo.SellPojo;

import javax.persistence.EntityNotFoundException;
import java.net.URI;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.trebol.config.Constants.SELL_STATUS_PAYMENT_STARTED;
import static org.trebol.testhelpers.SalesTestHelper.*;
import static org.trebol.constant.TestConstants.ANY;
import static org.trebol.constant.TestConstants.ONE;
import static org.trebol.testhelpers.SalesTestHelper.SELL_TRANSACTION_TOKEN;
import static org.trebol.testhelpers.SalesTestHelper.resetSales;
import static org.trebol.testhelpers.SalesTestHelper.sellPojoAfterCreation;

@ExtendWith(MockitoExtension.class)
class CheckoutServiceTest {
Expand Down Expand Up @@ -63,7 +74,6 @@ void acknowledges_successful_transaction()
when(salesCrudService.readOne(MATCHER_PREDICATE)).thenReturn(sellPojoAfterCreation());
when(paymentIntegrationService.requestPaymentResult(SELL_TRANSACTION_TOKEN)).thenReturn(0);
when(salesProcessService.markAsPaid(sellPojoAfterCreation())).thenReturn(null);
CheckoutServiceImpl service = instantiate();

SellPojo result = service.confirmTransaction(SELL_TRANSACTION_TOKEN, false);

Expand All @@ -74,6 +84,47 @@ void acknowledges_successful_transaction()
assertNull(result);
}

@DisplayName("Confirm transaction when status code is not equal to zero call mark as failed")
@Test
void acknowledges_successful_transaction_payment_integration_returns_1()
throws PaymentServiceException, EntityNotFoundException, BadInputException {
Map<String, String> matcherMap = Map.of(
"statusCode", SELL_STATUS_PAYMENT_STARTED,
"token", SELL_TRANSACTION_TOKEN);
when(salesPredicateService.parseMap(matcherMap)).thenReturn(MATCHER_PREDICATE);
when(salesCrudService.readOne(MATCHER_PREDICATE)).thenReturn(sellPojoAfterCreation());
when(paymentIntegrationService.requestPaymentResult(SELL_TRANSACTION_TOKEN)).thenReturn(1);
when(salesProcessService.markAsFailed(sellPojoAfterCreation())).thenReturn(null);

SellPojo result = service.confirmTransaction(SELL_TRANSACTION_TOKEN, false);

verify(salesPredicateService).parseMap(matcherMap);
verify(salesCrudService).readOne(MATCHER_PREDICATE);
verify(paymentIntegrationService).requestPaymentResult(SELL_TRANSACTION_TOKEN);
verify(salesProcessService).markAsFailed(sellPojoAfterCreation());
assertNull(result);
}

@DisplayName("Confirm transaction when status code is not equal to zero call mark as failed throws BadInputException")
@Test
void acknowledges_successful_transaction_payment_integration_returns_1_mark_as_failed_throws_bad_input_exception()
throws PaymentServiceException, EntityNotFoundException, BadInputException {
Map<String, String> matcherMap = Map.of(
"statusCode", SELL_STATUS_PAYMENT_STARTED,
"token", SELL_TRANSACTION_TOKEN);
when(salesPredicateService.parseMap(matcherMap)).thenReturn(MATCHER_PREDICATE);
when(salesCrudService.readOne(MATCHER_PREDICATE)).thenReturn(sellPojoAfterCreation());
when(paymentIntegrationService.requestPaymentResult(SELL_TRANSACTION_TOKEN)).thenReturn(1);
when(salesProcessService.markAsFailed(sellPojoAfterCreation())).thenThrow(BadInputException.class);

assertThrows(IllegalStateException.class, () -> service.confirmTransaction(SELL_TRANSACTION_TOKEN, false));

verify(salesPredicateService, times(ONE)).parseMap(matcherMap);
verify(salesCrudService, times(ONE)).readOne(MATCHER_PREDICATE);
verify(paymentIntegrationService, times(ONE)).requestPaymentResult(SELL_TRANSACTION_TOKEN);
verify(salesProcessService, times(ONE)).markAsFailed(sellPojoAfterCreation());
}

@Test
void acknowledges_aborted_transaction()
throws PaymentServiceException, EntityNotFoundException, BadInputException {
Expand All @@ -86,12 +137,30 @@ void acknowledges_aborted_transaction()

SellPojo result = service.confirmTransaction(SELL_TRANSACTION_TOKEN, true);

verify(salesPredicateService).parseMap(matcherMap);
verify(salesCrudService).readOne(MATCHER_PREDICATE);
verify(salesProcessService).markAsAborted(sellPojoAfterCreation());
verify(salesPredicateService, times(ONE)).parseMap(matcherMap);
verify(salesCrudService, times(ONE)).readOne(MATCHER_PREDICATE);
verify(salesProcessService, times(ONE)).markAsAborted(sellPojoAfterCreation());
assertNull(result);
}

@DisplayName("Confirm Transaction when mark as aborted catch BadInputException throw IllegalArgumentException")
@Test
void acknowledges_aborted_transaction_illegal_bad_input_exception() throws EntityNotFoundException, BadInputException {
Map<String, String> matcherMap = Map.of(
"statusCode", SELL_STATUS_PAYMENT_STARTED,
"token", SELL_TRANSACTION_TOKEN);
when(salesPredicateService.parseMap(matcherMap)).thenReturn(MATCHER_PREDICATE);
when(salesCrudService.readOne(MATCHER_PREDICATE)).thenReturn(sellPojoAfterCreation());
when(salesProcessService.markAsAborted(sellPojoAfterCreation())).thenThrow(BadInputException.class);

assertThrows(IllegalStateException.class,
() -> service.confirmTransaction(SELL_TRANSACTION_TOKEN, true), "Transaction could not be confirmed");

verify(salesPredicateService, times(ONE)).parseMap(matcherMap);
verify(salesCrudService, times(ONE)).readOne(MATCHER_PREDICATE);
verify(salesProcessService, times(ONE)).markAsAborted(sellPojoAfterCreation());
}

@Test
void throws_exceptions_at_unexisting_transactions_before_requesting_payments()
throws PaymentServiceException, EntityNotFoundException, BadInputException {
Expand Down Expand Up @@ -133,10 +202,27 @@ void throws_exceptions_at_invalid_transactions_before_confirming()
assertNull(result);
}

private CheckoutServiceImpl instantiate() {
return new CheckoutServiceImpl(salesCrudService,
salesProcessService,
salesPredicateService,
paymentIntegrationService);
@DisplayName("Generate result page proper url to URI")
@Test
void generate_result_page_url() {

when(paymentIntegrationService.getPaymentResultPageUrl()).thenReturn("http://www.any.com");

URI actual = service.generateResultPageUrl(ANY);

assertEquals("http://www.any.com?token=ANY", actual.toString());
verify(paymentIntegrationService, times(ONE)).getPaymentResultPageUrl();
}

@DisplayName("Generate result page proper url to URI when result page generates malformed url then catch " +
"MalformedURLException and throw IllegalArgumentException")
@Test
void generate_result_page_url_throws_illegal_argument_exception() {

when(paymentIntegrationService.getPaymentResultPageUrl()).thenReturn(ANY);

assertThrows(IllegalStateException.class, () -> service.generateResultPageUrl(ANY), "Transaction was confirmed, but server had an unexpected malfunction");

verify(paymentIntegrationService, times(ONE)).getPaymentResultPageUrl();
}
}

0 comments on commit bd23cf2

Please sign in to comment.