MockPaymentGateway.java

1
package com.rbctcsworld.ecommerce.payment;
2
3
import java.math.BigDecimal;
4
import java.util.UUID;
5
6
/**
7
 * Stand-in for a real payment provider (Stripe/Adyen style test cards). Deterministic, so tests can
8
 * trigger every outcome on purpose:
9
 *
10
 *   4000 0000 0000 0002  -> declined            ("Card declined")
11
 *   4000 0000 0000 9995  -> declined            ("Insufficient funds")
12
 *   any other Luhn-valid -> approved            (e.g. 4242 4242 4242 4242)
13
 *
14
 * Card format, Luhn and expiry are validated BEFORE the gateway is called (PaymentService).
15
 * Default provider (app.payment.provider=mock, see PaymentGatewayConfig); StripePaymentGateway is the real one.
16
 */
17
public class MockPaymentGateway implements PaymentGateway {
18
19
    public static final String DECLINED_CARD = "4000000000000002";
20
    public static final String INSUFFICIENT_FUNDS_CARD = "4000000000009995";
21
22
    @Override
23
    public GatewayResult charge(Long orderId, String cardNumber, BigDecimal amount) {
24 1 1. charge : replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::charge → KILLED
        return switch (cardNumber) {
25
            case DECLINED_CARD -> GatewayResult.decline("Card declined", null);
26
            case INSUFFICIENT_FUNDS_CARD -> GatewayResult.decline("Insufficient funds", null);
27
            default -> GatewayResult.ok("mock_ch_" + UUID.randomUUID());
28
        };
29
    }
30
31
    /** The token only remembers which kind of test card it was, so a saved declining card keeps declining. */
32
    @Override
33
    public String tokenize(String cardNumber) {
34
        String kind = switch (cardNumber) {
35
            case DECLINED_CARD -> "declined";
36
            case INSUFFICIENT_FUNDS_CARD -> "funds";
37
            default -> "ok";
38
        };
39 1 1. tokenize : replaced return value with "" for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::tokenize → KILLED
        return "mock_pm_" + kind + "_" + UUID.randomUUID().toString().substring(0, 8);
40
    }
41
42
    @Override
43
    public GatewayResult chargeToken(Long orderId, String token, BigDecimal amount) {
44 2 1. chargeToken : removed conditional - replaced equality check with false → SURVIVED
2. chargeToken : replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::chargeToken → KILLED
        if (token.startsWith("mock_pm_declined_")) return GatewayResult.decline("Card declined", null);
45 2 1. chargeToken : removed conditional - replaced equality check with false → KILLED
2. chargeToken : replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::chargeToken → KILLED
        if (token.startsWith("mock_pm_funds_")) return GatewayResult.decline("Insufficient funds", null);
46 2 1. chargeToken : replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::chargeToken → KILLED
2. chargeToken : removed conditional - replaced equality check with false → KILLED
        if (token.startsWith("mock_pm_ok_")) return GatewayResult.ok("mock_ch_" + UUID.randomUUID());
47 1 1. chargeToken : replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::chargeToken → KILLED
        return GatewayResult.decline("Unknown saved card", null);
48
    }
49
50
    @Override
51
    public GatewayResult refund(Long orderId, String chargeReference, BigDecimal amount, int refundNumber) {
52 1 1. refund : replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::refund → KILLED
        return GatewayResult.ok("mock_re_" + UUID.randomUUID());
53
    }
54
55
    @Override
56
    public String name() {
57 1 1. name : replaced return value with "" for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::name → KILLED
        return "mock";
58
    }
59
60
    /** Luhn (mod 10) checksum used by every real card number. */
61
    public static boolean luhnValid(String digits) {
62
        int sum = 0;
63
        boolean doubleIt = false;
64 3 1. luhnValid : Replaced integer subtraction with addition → KILLED
2. luhnValid : changed conditional boundary → KILLED
3. luhnValid : removed conditional - replaced comparison check with false → KILLED
        for (int i = digits.length() - 1; i >= 0; i--) {
65 1 1. luhnValid : Replaced integer subtraction with addition → KILLED
            int d = digits.charAt(i) - '0';
66 1 1. luhnValid : removed conditional - replaced equality check with false → KILLED
            if (doubleIt) {
67 1 1. luhnValid : Replaced integer multiplication with division → KILLED
                d *= 2;
68 3 1. luhnValid : changed conditional boundary → SURVIVED
2. luhnValid : removed conditional - replaced comparison check with false → KILLED
3. luhnValid : Changed increment from -9 to 9 → KILLED
                if (d > 9) d -= 9;
69
            }
70 1 1. luhnValid : Replaced integer addition with subtraction → SURVIVED
            sum += d;
71 1 1. luhnValid : removed conditional - replaced equality check with false → KILLED
            doubleIt = !doubleIt;
72
        }
73 3 1. luhnValid : removed conditional - replaced equality check with false → KILLED
2. luhnValid : Replaced integer modulus with multiplication → KILLED
3. luhnValid : replaced boolean return with true for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::luhnValid → KILLED
        return sum % 10 == 0;
74
    }
75
}

Mutations

24

1.1
Location : charge
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:mockGatewayGivesReferencesAndItsName()]
replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::charge → KILLED

39

1.1
Location : tokenize
Killed by : com.rbctcsworld.ecommerce.payment.PaymentMethodServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentMethodServiceTest]/[method:theFirstCardIsTheDefaultAndOnlyReceiptDataIsKept()]
replaced return value with "" for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::tokenize → KILLED

44

1.1
Location : chargeToken
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

2.2
Location : chargeToken
Killed by : com.rbctcsworld.ecommerce.payment.PaymentMethodServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentMethodServiceTest]/[method:aSavedDecliningCardKeepsDeclining()]
replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::chargeToken → KILLED

45

1.1
Location : chargeToken
Killed by : com.rbctcsworld.ecommerce.payment.SavedCardChargeTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.SavedCardChargeTest]/[method:aDecliningSavedCardIsRecordedAsFailedThen402()]
removed conditional - replaced equality check with false → KILLED

2.2
Location : chargeToken
Killed by : com.rbctcsworld.ecommerce.payment.SavedCardChargeTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.SavedCardChargeTest]/[method:aDecliningSavedCardIsRecordedAsFailedThen402()]
replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::chargeToken → KILLED

46

1.1
Location : chargeToken
Killed by : com.rbctcsworld.ecommerce.payment.SavedCardChargeTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.SavedCardChargeTest]/[method:anApprovedSavedCardIsRecordedWithItsLastFourDigits()]
replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::chargeToken → KILLED

2.2
Location : chargeToken
Killed by : com.rbctcsworld.ecommerce.payment.SavedCardChargeTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.SavedCardChargeTest]/[method:anApprovedSavedCardIsRecordedWithItsLastFourDigits()]
removed conditional - replaced equality check with false → KILLED

47

1.1
Location : chargeToken
Killed by : com.rbctcsworld.ecommerce.payment.SavedCardChargeTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.SavedCardChargeTest]/[method:anUnknownTokenIsDeclined()]
replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::chargeToken → KILLED

52

1.1
Location : refund
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:mockGatewayGivesReferencesAndItsName()]
replaced return value with null for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::refund → KILLED

57

1.1
Location : name
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:mockGatewayGivesReferencesAndItsName()]
replaced return value with "" for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::name → KILLED

64

1.1
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:luhnAlgorithm()]
Replaced integer subtraction with addition → KILLED

2.2
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:luhnAlgorithm()]
changed conditional boundary → KILLED

3.3
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:luhnAlgorithm()]
removed conditional - replaced comparison check with false → KILLED

65

1.1
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:luhnAlgorithm()]
Replaced integer subtraction with addition → KILLED

66

1.1
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:luhnAlgorithm()]
removed conditional - replaced equality check with false → KILLED

67

1.1
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:luhnAlgorithm()]
Replaced integer multiplication with division → KILLED

68

1.1
Location : luhnValid
Killed by : none
changed conditional boundary → SURVIVED
Covering tests

2.2
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:declinedCardIsRecordedAsFailedThen402()]
removed conditional - replaced comparison check with false → KILLED

3.3
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:declinedCardIsRecordedAsFailedThen402()]
Changed increment from -9 to 9 → KILLED

70

1.1
Location : luhnValid
Killed by : none
Replaced integer addition with subtraction → SURVIVED
Covering tests

71

1.1
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:luhnAlgorithm()]
removed conditional - replaced equality check with false → KILLED

73

1.1
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:luhnAlgorithm()]
removed conditional - replaced equality check with false → KILLED

2.2
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:luhnAlgorithm()]
Replaced integer modulus with multiplication → KILLED

3.3
Location : luhnValid
Killed by : com.rbctcsworld.ecommerce.payment.PaymentServiceTest.[engine:junit-jupiter]/[class:com.rbctcsworld.ecommerce.payment.PaymentServiceTest]/[method:luhnAlgorithm()]
replaced boolean return with true for com/rbctcsworld/ecommerce/payment/MockPaymentGateway::luhnValid → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support