com.ccserver.digital.validator.CreditCardApplicationValidatorTest.java Source code

Java tutorial

Introduction

Here is the source code for com.ccserver.digital.validator.CreditCardApplicationValidatorTest.java

Source

package com.ccserver.digital.validator;

import java.time.LocalDateTime;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult;

import com.ccserver.digital.domain.type.ProcessStep;
import com.ccserver.digital.domain.type.TypeOfEmployment;
import com.ccserver.digital.dto.CreditCardApplicationDTO;
import com.ccserver.digital.dto.PhoneDTO;
import com.ccserver.digital.service.CreditCardApplicationService;

@RunWith(SpringRunner.class)
public class CreditCardApplicationValidatorTest {

    @InjectMocks
    private CreditCardApplicationValidator creditCardApplicationValidator;

    @Mock
    private CreditCardApplicationService creditCardApplicationService;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void validatorEmploymentForBussinessTest() {
        // given
        CreditCardApplicationDTO creditCardApplicationDTO = new CreditCardApplicationDTO();
        creditCardApplicationDTO.setProcessStep(ProcessStep.Employment);
        creditCardApplicationDTO.setTypeOfEmployement(TypeOfEmployment.Business);
        PhoneDTO mobile = new PhoneDTO();
        creditCardApplicationDTO.setMobile(mobile);

        BindingResult errors = new BeanPropertyBindingResult(creditCardApplicationDTO, "target");

        // when
        creditCardApplicationValidator.validate(creditCardApplicationDTO, errors);

        // then
        Assert.assertTrue(errors.hasErrors());
        Assert.assertTrue(errors.hasFieldErrors("businessStartDate"));
        Assert.assertTrue(errors.hasFieldErrors("monthlyIncome"));
        Assert.assertTrue(errors.hasFieldErrors("licenseNumber"));
        Assert.assertTrue(errors.hasFieldErrors("businessPhone"));
        Assert.assertTrue(errors.hasFieldErrors("businessAddress"));
        Assert.assertTrue(errors.hasFieldErrors("monthlyIncome"));
        Assert.assertTrue(errors.hasFieldErrors("monthlyExpenses"));

    }

    @Test
    public void validatorEmploymentForEmploymentTest() {
        // given
        CreditCardApplicationDTO creditCardApplicationDTO = new CreditCardApplicationDTO();
        creditCardApplicationDTO.setProcessStep(ProcessStep.Employment);
        creditCardApplicationDTO.setTypeOfEmployement(TypeOfEmployment.Employed);
        PhoneDTO mobile = new PhoneDTO();
        creditCardApplicationDTO.setMobile(mobile);

        BindingResult errors = new BeanPropertyBindingResult(creditCardApplicationDTO, "target");

        // when
        creditCardApplicationValidator.validate(creditCardApplicationDTO, errors);

        // then
        Assert.assertTrue(errors.hasErrors());
        Assert.assertTrue(errors.hasFieldErrors("occupation"));
        Assert.assertTrue(errors.hasFieldErrors("nameOfEmployer"));
        Assert.assertTrue(errors.hasFieldErrors("industry"));
        Assert.assertTrue(errors.hasFieldErrors("employerAddress"));
        Assert.assertTrue(errors.hasFieldErrors("businessTelephone"));
        Assert.assertTrue(errors.hasFieldErrors("monthlyIncome"));
        Assert.assertTrue(errors.hasFieldErrors("monthlyExpenses"));

    }

    @Test
    public void validateOtherTabTest() {
        // given
        CreditCardApplicationDTO creditCardApplicationDTO = new CreditCardApplicationDTO();
        creditCardApplicationDTO.setProcessStep(ProcessStep.Other);
        PhoneDTO mobile = new PhoneDTO();
        mobile.setPhoneNumber("123");
        mobile.setCountryCode("+84");
        creditCardApplicationDTO.setMobile(mobile);
        creditCardApplicationDTO.setPhoneOfReference(mobile);
        BindingResult errors = new BeanPropertyBindingResult(creditCardApplicationDTO, "target");

        // when
        creditCardApplicationValidator.validate(creditCardApplicationDTO, errors);

        // then
        Assert.assertTrue(errors.hasFieldErrors("securityQuestion"));
        Assert.assertTrue(errors.hasFieldErrors("securityAnswer"));
        Assert.assertTrue(errors.hasFieldErrors("referenceName"));
        Assert.assertTrue(errors.hasFieldErrors("relationshipReference"));
        Assert.assertTrue(errors.hasFieldErrors("phoneOfReference"));
        Assert.assertTrue(errors.hasFieldErrors("deliveryCard"));
    }

    @Test
    public void testExistingPhone() {
        // given
        CreditCardApplicationDTO creditCardApplicationDTO = new CreditCardApplicationDTO();
        creditCardApplicationDTO.setId(2L);
        creditCardApplicationDTO.setProcessStep(ProcessStep.General);
        PhoneDTO mobile = new PhoneDTO();
        mobile.setCountryCode("+84");
        mobile.setPhoneNumber("090");
        creditCardApplicationDTO.setMobile(mobile);

        CreditCardApplicationDTO creditCardApplicationExisting = new CreditCardApplicationDTO();
        creditCardApplicationExisting.setId(1L);
        creditCardApplicationExisting.setMobile(mobile);
        Mockito.when(creditCardApplicationService.findByMobile(mobile)).thenReturn(creditCardApplicationExisting);

        BindingResult errors = new BeanPropertyBindingResult(creditCardApplicationDTO, "target");
        // when
        creditCardApplicationValidator.validate(creditCardApplicationDTO, errors);
        // then
        Assert.assertTrue(errors.hasFieldErrors("mobile"));
    }

    @Test
    public void validatorGeneralTest() {

        CreditCardApplicationDTO creditCardApplicationDTO = new CreditCardApplicationDTO();
        creditCardApplicationDTO.setProcessStep(ProcessStep.General);
        LocalDateTime ldt = LocalDateTime.now();
        creditCardApplicationDTO.setDateOfIssue(ldt);
        BindingResult errors = new BeanPropertyBindingResult(creditCardApplicationDTO, "target");
        creditCardApplicationValidator.validate(creditCardApplicationDTO, errors);
        Assert.assertNull(errors.getFieldError("dateOfIssue"));
    }

    @Test
    public void validatorPersonalTest() {

        CreditCardApplicationDTO creditCardApplicationDTO = new CreditCardApplicationDTO();
        creditCardApplicationDTO.setProcessStep(ProcessStep.Personal);
        creditCardApplicationDTO.setEducation(null);
        creditCardApplicationDTO.setMaritalStatus(null);
        creditCardApplicationDTO.setNumberOfChildren(5);

        BindingResult errors = new BeanPropertyBindingResult(creditCardApplicationDTO, "target");
        creditCardApplicationValidator.validate(creditCardApplicationDTO, errors);
        Assert.assertNotNull(errors.getFieldError("education"));
        Assert.assertNotNull(errors.getFieldError("maritalStatus"));
        Assert.assertNull(errors.getFieldError("numberOfChildren"));
    }

    @Test
    public void validatorBasicInformationTest() {
        // Arrange
        CreditCardApplicationDTO ccApp = new CreditCardApplicationDTO();
        ccApp.setFirstName(null);
        ccApp.setLastName(null);
        ccApp.setEmail(null);
        ccApp.setMobile(null);
        BindingResult errors = new BeanPropertyBindingResult(ccApp, "target");
        // Act
        creditCardApplicationValidator.validate(ccApp, errors);
        // Assert
        Assert.assertNotNull(errors.getFieldError("firstName"));
        Assert.assertNotNull(errors.getFieldError("email"));
        Assert.assertNotNull(errors.getFieldError("mobile"));
    }

    @Test
    public void supportsCardApplicationDTOTest() {
        Assert.assertTrue(creditCardApplicationValidator.supports(CreditCardApplicationDTO.class));
    }
}