Utils { public static String getCardType(@NonNull String cardNo) { try { if (cardNo.isEmpty()) { return ""; } else { if (cardNo.charAt(0) == '4') { return CARD_TYPE_VISA; } else if ((cardNo.charAt(0) == '5') && ((cardNo.charAt(1) == '1') || (cardNo.charAt(1) == '2') || (cardNo.charAt(1) == '3') || (cardNo.charAt(1) == '4') || (cardNo.charAt(1) == '5'))) { return CARD_TYPE_MASTERCARD; } else if ((cardNo.charAt(0) == '3') && ((cardNo.charAt(1) == '4') || (cardNo.charAt(1) == '7'))) { return CARD_TYPE_AMEX; } else if (cardNo.startsWith("35") || cardNo.startsWith("2131") || cardNo.startsWith("1800")) { return CARD_TYPE_JCB; } else { return ""; } } } catch (RuntimeException e) { return ""; } }  static String mappingToCreditCardAuthentication(String type, boolean secure); static boolean isNetworkAvailable(Context context); static void hideKeyboard(Context context, View editText); static String getFormattedAmount(double amount); static String getValidityTime(String transactionTime); static String getMonth(int monthValue); static int dpToPx(int dp); static String getFormattedCreditCardNumber(String unformattedCardNumber); static String getCardType(@NonNull String cardNo); static String getFormattedTime(long time); static String formatDouble(double d); static String getDeviceType(Activity activity); static final String CARD_TYPE_VISA; static final String CARD_TYPE_MASTERCARD; static final String CARD_TYPE_AMEX; static final String CARD_TYPE_JCB; }
@Test public void getCardTypeTest_whenCardShort() { Assert.assertEquals("", Utils.getCardType("1")); }
@Test public void getCardTypeTest_whenCardNull() { Assert.assertEquals("", Utils.getCardType("")); }
@Test public void getCardTypeTest_whenVisa() { Assert.assertEquals(Utils.CARD_TYPE_VISA, Utils.getCardType("4811")); }
@Test public void getCardTypeTest_whenMasterCard() { Assert.assertEquals(Utils.CARD_TYPE_MASTERCARD, Utils.getCardType("5111")); Assert.assertEquals(Utils.CARD_TYPE_MASTERCARD, Utils.getCardType("5211")); Assert.assertEquals(Utils.CARD_TYPE_MASTERCARD, Utils.getCardType("5311")); Assert.assertEquals(Utils.CARD_TYPE_MASTERCARD, Utils.getCardType("5411")); Assert.assertEquals(Utils.CARD_TYPE_MASTERCARD, Utils.getCardType("5511")); }
@Test public void getCardType_whenAmex() { Assert.assertEquals(Utils.CARD_TYPE_AMEX, Utils.getCardType("3488")); Assert.assertEquals(Utils.CARD_TYPE_AMEX, Utils.getCardType("3788")); }
@Test public void getCardType_whenInvalid() { Assert.assertEquals("", Utils.getCardType("9999")); }