Skip to content
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

Issue 144 CheckoutService Test #158

Merged
merged 12 commits into from
Oct 18, 2022
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();
}
}