com.ccserver.digital.controller.AdminControllerTest.java Source code

Java tutorial

Introduction

Here is the source code for com.ccserver.digital.controller.AdminControllerTest.java

Source

package com.ccserver.digital.controller;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

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.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import com.ccserver.digital.common.Constants;
import com.ccserver.digital.domain.SelectionList;
import com.ccserver.digital.domain.base.ErrorObject;
import com.ccserver.digital.domain.type.Gender;
import com.ccserver.digital.domain.type.Status;
import com.ccserver.digital.domain.type.TypeOfEmployment;
import com.ccserver.digital.domain.util.SendEmail;
import com.ccserver.digital.dto.AddressDTO;
import com.ccserver.digital.dto.AdminSearchPageDTO;
import com.ccserver.digital.dto.ApplicationLOSDTO;
import com.ccserver.digital.dto.BaseProvinceDTO;
import com.ccserver.digital.dto.CompanyDTO;
import com.ccserver.digital.dto.CountryDTO;
import com.ccserver.digital.dto.CreditCardApplicationDTO;
import com.ccserver.digital.dto.DistrictDTO;
import com.ccserver.digital.dto.PhoneDTO;
import com.ccserver.digital.dto.SelectionListDTO;
import com.ccserver.digital.mapper.GeneralMapper;
import com.ccserver.digital.service.CreditCardApplicationDocumentService;
import com.ccserver.digital.service.CreditCardApplicationExtractionService;
import com.ccserver.digital.service.CreditCardApplicationService;
import com.ccserver.digital.service.UserService;
import com.ccserver.digital.service.ZipFileService;

@RunWith(SpringRunner.class)
public class AdminControllerTest {

    @InjectMocks
    private AdminController controller;

    @Mock
    private ZipFileService zipFileService;

    @Mock
    private CreditCardApplicationService ccAppService;

    @Mock
    private CreditCardApplicationDocumentService docsService;

    @Mock
    private HttpServletResponse response;

    @Mock
    ServletOutputStream servletOutputStream;

    @Mock
    private GeneralMapper mapper;

    @Mock
    private UserService userService;
    @Mock
    private CreditCardApplicationExtractionService exportService;
    @Mock
    public SendEmail emailSender;

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

    @Test
    public void getAppListWithNullId() {

        PageRequest page = new PageRequest(1, 1);

        // when
        ResponseEntity<?> application = controller.getAppList(null, page);

        // then
        Assert.assertEquals(HttpStatus.BAD_REQUEST, application.getStatusCode());
        Assert.assertEquals(Constants.APPLICATION_ID_REQUIRED, ((ErrorObject) application.getBody()).getId());
    }

    @Test
    public void getAppListTest() {
        PageRequest page = new PageRequest(1, 1);
        List<CreditCardApplicationDTO> ccAppDTOList = new ArrayList<CreditCardApplicationDTO>();
        CreditCardApplicationDTO ccApp = getCreditCardApplicationDTOMock(1L);
        ccAppDTOList.add(ccApp);
        PageImpl<CreditCardApplicationDTO> pageDTO = new PageImpl<CreditCardApplicationDTO>(ccAppDTOList, page, 1);
        AdminSearchPageDTO adminSearchPageDTO = new AdminSearchPageDTO(1, pageDTO);

        Mockito.when(ccAppService.findBySearchText("aaa", page)).thenReturn(adminSearchPageDTO);

        // when
        ResponseEntity<?> application = controller.getAppList("aaa", page);

        // then
        Assert.assertEquals(HttpStatus.OK, application.getStatusCode());
        Assert.assertEquals(1, ((AdminSearchPageDTO) application.getBody()).getCount());
    }

    @Test
    public void updateStatusWithNullId() {
        // when
        ResponseEntity<?> application = controller.updateStatus(null, "SubmittedApp");

        // then
        Assert.assertEquals(HttpStatus.BAD_REQUEST, application.getStatusCode());
        Assert.assertEquals(Constants.APPLICATION_ID_REQUIRED, ((ErrorObject) application.getBody()).getId());
    }

    @Test
    public void updateStatusTest() {
        CreditCardApplicationDTO ccApp = getCreditCardApplicationDTOMock(1L);
        Mockito.when(ccAppService.updateStatus(1L, "SubmittedApp")).thenReturn(ccApp);

        // when
        ResponseEntity<?> application = controller.updateStatus(1L, "SubmittedApp");

        // then
        Assert.assertEquals(HttpStatus.OK, application.getStatusCode());
    }

    @Test
    public void downloadApplicationWithNullId() throws IOException {
        // when
        ResponseEntity<?> application = controller.downloadApplication(null, response);

        // then
        Assert.assertEquals(HttpStatus.BAD_REQUEST, application.getStatusCode());
        Assert.assertEquals(Constants.APPLICATION_ID_REQUIRED, ((ErrorObject) application.getBody()).getId());
    }

    @Test
    public void downloadApplicationWithNullDTO() throws IOException {

        Mockito.when(ccAppService.getApplication(1L)).thenReturn(null);
        // when
        ResponseEntity<?> application = controller.downloadApplication(1L, response);

        // then
        Assert.assertEquals(HttpStatus.BAD_REQUEST, application.getStatusCode());
        Assert.assertEquals(Constants.APPLICATION_DETAIL_OBJECT_REQUIRED,
                ((ErrorObject) application.getBody()).getId());
    }

    @Test
    public void downloadApplicationNullDocument() throws IOException {
        CreditCardApplicationDTO ccApp = getCreditCardApplicationDTOMock(1L);
        ccApp.setApplicationLOS(null);
        Mockito.when(ccAppService.getApplication(1L)).thenReturn(ccApp);

        File ifile = new File("./src/main/resources/sample");
        Path idDocPath = FileSystems.getDefault().getPath(ifile.getAbsolutePath(), "IdDoc.pdf");
        byte[] idDocByteArray = Files.readAllBytes(idDocPath);

        Map<String, byte[]> docs = new HashMap<String, byte[]>();
        docs.put("abc", idDocByteArray);

        Map<String, byte[]> doc = new HashMap<String, byte[]>();

        Mockito.when(docsService.getFullDocumentsByApplicationId(1L)).thenReturn(doc);

        Mockito.when(response.getOutputStream()).thenReturn(servletOutputStream);

        Mockito.when(zipFileService.zipIt(docs)).thenReturn(idDocByteArray);

        // when
        ResponseEntity<?> application = controller.downloadApplication(1L, response);

        // then
        Assert.assertEquals(HttpStatus.BAD_REQUEST, application.getStatusCode());
        Assert.assertEquals(Constants.APPLICATION_DETAIL_OBJECT_REQUIRED,
                ((ErrorObject) application.getBody()).getId());
    }

    @Test
    public void downloadApplication() throws IOException {
        CreditCardApplicationDTO ccApp = getCreditCardApplicationDTOMock(1L);
        Mockito.when(ccAppService.getApplication(1L)).thenReturn(ccApp);

        File ifile = new File("./src/main/resources/sample");
        Path idDocPath = FileSystems.getDefault().getPath(ifile.getAbsolutePath(), "IdDoc.pdf");
        byte[] idDocByteArray = Files.readAllBytes(idDocPath);

        Map<String, byte[]> docs = new HashMap<String, byte[]>();
        docs.put("abc", idDocByteArray);

        Mockito.when(docsService.getFullDocumentsByApplicationId(1L)).thenReturn(docs);

        Mockito.when(response.getOutputStream()).thenReturn(servletOutputStream);

        Mockito.when(zipFileService.zipIt(docs)).thenReturn(idDocByteArray);

        // when
        ResponseEntity<?> application = controller.downloadApplication(1L, response);

        // then
        Assert.assertEquals(HttpStatus.OK, application.getStatusCode());
    }

    @Test
    public void getApplication() {
        // given
        final CreditCardApplicationDTO creditCartApplicationDTO = getCreditCardApplicationDTOMock(1L);
        Mockito.when(ccAppService.getApplication(1L)).thenReturn(creditCartApplicationDTO);

        // when
        ResponseEntity<?> application = controller.getApplication(1L);

        // then
        Assert.assertEquals(HttpStatus.OK, application.getStatusCode());
        Assert.assertEquals("letuanthuongtin@gmail.com",
                ((CreditCardApplicationDTO) application.getBody()).getEmail());
    }

    @Test
    public void getApplicationWithWrongId() {
        // when
        ResponseEntity<?> application = controller.getApplication(0L);

        // then
        Assert.assertEquals(HttpStatus.BAD_REQUEST, application.getStatusCode());
        Assert.assertEquals(Constants.APPLICATION_ID_REQUIRED, ((ErrorObject) application.getBody()).getId());
    }

    @Test
    public void getApplicationWithNullId() {
        // when
        ResponseEntity<?> application = controller.getApplication(null);

        // then
        Assert.assertEquals(HttpStatus.BAD_REQUEST, application.getStatusCode());
        Assert.assertEquals(Constants.APPLICATION_ID_REQUIRED, ((ErrorObject) application.getBody()).getId());
    }

    @Test
    public void exportLeadsTest() throws IOException {
        Mockito.when(exportService.ExportLeads()).thenReturn("abc".getBytes());
        ResponseEntity<?> application = controller.exportLeads("abc");

        // then
        Assert.assertEquals(HttpStatus.OK, application.getStatusCode());
    }

    @Test
    public void exportLeadsExcptionTest() throws IOException {
        Mockito.when(exportService.ExportLeads()).thenReturn(null);
        ResponseEntity<?> application = controller.exportLeads("abc");

        // then
        Assert.assertNotNull(application);
    }

    @Test
    public void exportExcelAdminTest() throws IOException {
        Date from = new Date();
        Date to = new Date();
        Mockito.when(exportService.exportExcelAdmin(from, to)).thenReturn("abc".getBytes());
        ResponseEntity<?> application = controller.downloadFileAdminCard("abc", from, to);

        // then
        Assert.assertEquals(HttpStatus.OK, application.getStatusCode());
    }

    @Test
    public void exportExcelAdminExcptionTest() throws IOException {
        Date from = new Date();
        Date to = new Date();
        Mockito.when(exportService.exportExcelAdmin(from, to)).thenReturn(null);
        ResponseEntity<?> application = controller.downloadFileAdminCard("abc", from, to);

        // then
        Assert.assertNotNull(application);
    }

    @Test
    public void downloadFileCreditCardXLsx() throws IOException {
        // when
        Mockito.when(exportService.extractCreditCard(1)).thenReturn("google".getBytes());
        ResponseEntity<?> result = controller.downloadFileCreditCard("1", "creditCard");
        Assert.assertEquals(HttpStatus.OK, result.getStatusCode());
        // Assert.assertEquals("0",
        // ((MessageData<JSONObject>)result.getBody()).getStatus());
    }

    @Test
    public void downloadFileCreditCardCsv() throws IOException {
        // when
        Mockito.when(exportService.extractCreditCard(0)).thenReturn("google".getBytes());
        ResponseEntity<?> result = controller.downloadFileCreditCard("csv", "");
        Assert.assertEquals(HttpStatus.OK, result.getStatusCode());
        // Assert.assertEquals("0",
        // ((MessageData<JSONObject>)result.getBody()).getStatus());
    }

    @Test
    public void downloadFileCreditCardExcption() throws IOException {
        // when
        Mockito.when(exportService.extractCreditCard(0)).thenReturn(null);
        ResponseEntity<?> result = controller.downloadFileCreditCard("csv", "");
        Assert.assertNotNull(result);
        // Assert.assertEquals("0",
        // ((MessageData<JSONObject>)result.getBody()).getStatus());
    }

    CreditCardApplicationDTO getCreditCardApplicationDTOMock(long id) {
        CreditCardApplicationDTO creditCardApplicationDTOMock = getCreditCardApplicationDTO();
        creditCardApplicationDTOMock.setId(id);
        creditCardApplicationDTOMock.getMobile().setId(id);
        creditCardApplicationDTOMock.getBusinessPhone().setId(id);
        creditCardApplicationDTOMock.getBusinessTelephone().setId(id);
        creditCardApplicationDTOMock.getSecurityQuestion().setId(id);
        creditCardApplicationDTOMock.getPhoneOfReference().setId(id);
        return creditCardApplicationDTOMock;
    }

    private CreditCardApplicationDTO getCreditCardApplicationDTO() {
        CreditCardApplicationDTO creditCardApplicationDTO = new CreditCardApplicationDTO();

        // ----- losApplicationId
        ApplicationLOSDTO losDTO = new ApplicationLOSDTO();
        losDTO.setLosApplicationId("123ABC");
        creditCardApplicationDTO.setApplicationLOS(losDTO);

        // ----- cardType

        // ----- DoB
        creditCardApplicationDTO.setDob(LocalDateTime.now());

        // ----- Name
        creditCardApplicationDTO.setFirstName("LE");
        creditCardApplicationDTO.setMiddleName("Hong");
        creditCardApplicationDTO.setLastName("Thanh");

        // ----- gender
        creditCardApplicationDTO.setGender(Gender.Male);

        // ----- Moblie
        PhoneDTO moblie = new PhoneDTO();
        moblie.setCountryCode("+84");
        moblie.setPhoneNumber("985481179");
        creditCardApplicationDTO.setMobile(moblie);

        // ----- phoneBusiness
        PhoneDTO phoneBusiness = new PhoneDTO();
        phoneBusiness.setCountryCode("+84");
        phoneBusiness.setPhoneNumber("985481179");
        creditCardApplicationDTO.setBusinessPhone(phoneBusiness);

        // ----- email
        creditCardApplicationDTO.setEmail("letuanthuongtin@gmail.com");

        // ----- passportNumber
        creditCardApplicationDTO.setPassportNumber("123456789");

        // ----- dateOfIssue
        creditCardApplicationDTO.setDateOfIssue(LocalDateTime.now());

        // ----- placeOfIssue
        SelectionList selectionListDTO = new SelectionList();
        selectionListDTO.setCategory("plauceOfise");
        selectionListDTO.setCode("11");
        creditCardApplicationDTO.setPlaceOfIssue(selectionListDTO);

        // ----- citizenship
        CountryDTO citizenship = new CountryDTO();
        citizenship.setId(1L);
        citizenship.setCode("VN");
        citizenship.setVi("Viet Nam");
        citizenship.setEn("VietNam");
        creditCardApplicationDTO.setCitizenship(citizenship);

        // ----- haveGreenCard
        creditCardApplicationDTO.setHaveGreenCard(true);

        // ----- isUSResident
        creditCardApplicationDTO.setUSResident(true);

        // ----- education
        // creditCardApplicationDTO.setEducation(EducationQualification.HighShool);

        // ----- maritalStatus
        // creditCardApplicationDTO.setMaritalStatus(MaritalStatus.Single);

        // ----- numberOfChildren
        creditCardApplicationDTO.setNumberOfChildren(4);

        // ----- permanent address---
        // TODO: create province and district for integration test
        AddressDTO permanentAddress = new AddressDTO();
        BaseProvinceDTO province = new BaseProvinceDTO();
        province.setName("Ha Noi");
        province.setId(1L);
        DistrictDTO districtDTO1 = new DistrictDTO();
        districtDTO1.setName("Hai Ba Trung");
        districtDTO1.setId(1L);
        permanentAddress.setDistrict(districtDTO1);
        permanentAddress.setProvince(province);
        permanentAddress.setStreet("LE Thanh NGhi");
        permanentAddress.setAddressDetails("So Nha 18");
        creditCardApplicationDTO.setPermanentAddress(permanentAddress);

        // ----- currentIsPermanent
        creditCardApplicationDTO.setCurrentIsPermanent(true);

        // ----- currentAddress
        creditCardApplicationDTO.setCurrentAddress(permanentAddress);

        // ----- typeOfEmployement
        creditCardApplicationDTO.setTypeOfEmployement(TypeOfEmployment.Business);

        // ----- licenseNumber
        creditCardApplicationDTO.setLicenseNumber("12345");

        // ----- businessStartDate
        creditCardApplicationDTO.setBusinessStartDate(LocalDateTime.now());

        // ----- businessTelephone
        PhoneDTO businessTelephone = new PhoneDTO();
        businessTelephone.setCountryCode("+84");
        businessTelephone.setPhoneNumber("985481179");
        businessTelephone.setExt("01");
        creditCardApplicationDTO.setBusinessTelephone(businessTelephone);

        // ----- businessAddress
        creditCardApplicationDTO.setBusinessAddress(permanentAddress);

        // ----- nameOfEmployer
        CompanyDTO companyDTO = new CompanyDTO();
        companyDTO.setCode("111");
        creditCardApplicationDTO.setNameOfEmployer(companyDTO);

        // ----- occupation
        //creditCardApplicationDTO.setOccupation("Engineer");

        // ----- industry
        // creditCardApplicationDTO.setIndustry("finance");

        // ----- yearsOfWorking
        creditCardApplicationDTO.setYearsOfWorking(1);

        // ----- monthsOfWorking
        creditCardApplicationDTO.setMonthsOfWorking(8);

        // ----- employerAddress
        creditCardApplicationDTO.setEmployerAddress(permanentAddress);

        // ----- monthlyIncome
        creditCardApplicationDTO.setMonthlyIncome(new BigDecimal("10"));

        // ----- monthlyExpenses
        creditCardApplicationDTO.setMonthlyExpenses(new BigDecimal("5"));

        // ----- securityQuestion
        SelectionListDTO securityQuestion = new SelectionListDTO();
        securityQuestion.setId(3L);
        creditCardApplicationDTO.setSecurityQuestion(securityQuestion);

        // ----- securityAnswer
        creditCardApplicationDTO.setSecurityAnswer("Thanh");

        // ----- referenceName
        creditCardApplicationDTO.setReferenceName("LE Tuan");

        // ----- relationshipReference
        SelectionListDTO relationship = new SelectionListDTO();
        relationship.setId(3L);
        creditCardApplicationDTO.setRelationshipReference(relationship);

        // ----- phoneOfReference
        creditCardApplicationDTO.setPhoneOfReference(moblie);

        // ----- branchDelivery
        // creditCardApplicationDTO.setDeliveryCard("Branch 1");

        // ----- isSecondaryCard
        creditCardApplicationDTO.setSecondaryCard(true);

        // ----- isAccurateCommit
        creditCardApplicationDTO.setAccurateCommit(true);

        // ----- agreeTermsConditions
        creditCardApplicationDTO.setAgreeTermsConditions(true);

        return creditCardApplicationDTO;
    }

    @Test
    public void approveApplicationTest() {
        // Arrange
        Long id = 1l;
        CreditCardApplicationDTO app = new CreditCardApplicationDTO();
        app.setId(id);
        app.setSubmittedLos(LocalDateTime.now());
        app.setStatus(Status.Processing);
        Mockito.when(ccAppService.approveApplication(id)).thenReturn(app);

        // Act
        ResponseEntity<?> response = controller.approveApplication(id);

        // Assert
        Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
        Assert.assertEquals(app, response.getBody());
    }

    @Test
    public void approveApplicationZeroIdTest() {
        // Arrange
        Long id = 0l;

        // Act
        ResponseEntity<?> response = controller.approveApplication(id);

        // Assert
        Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
        Assert.assertEquals(Constants.APPLICATION_ID_REQUIRED, ((ErrorObject) response.getBody()).getId());
    }

    @Test
    public void rejectApplicationTest() {
        // Arrange
        Long id = 1l;
        CreditCardApplicationDTO app = new CreditCardApplicationDTO();
        app.setId(id);
        app.setStatus(Status.Draft);
        Mockito.when(ccAppService.rejectApplication(id)).thenReturn(app);

        // Act
        ResponseEntity<?> response = controller.rejectApplication(id);

        // Assert
        Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
        Assert.assertEquals(app, response.getBody());
    }

    @Test
    public void rejectApplicationZeroIdTest() {
        // Arrange
        Long id = 0l;

        // Act
        ResponseEntity<?> response = controller.rejectApplication(id);

        // Assert
        Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
        Assert.assertEquals(Constants.APPLICATION_ID_REQUIRED, ((ErrorObject) response.getBody()).getId());
    }
}