Java tutorial
package com.ccserver.digital.service; import java.io.IOException; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Date; import java.util.List; 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 com.ccserver.digital.common.ExportFile; import com.ccserver.digital.domain.Address; import com.ccserver.digital.domain.Branch; import com.ccserver.digital.domain.Country; import com.ccserver.digital.domain.CreditCardApplication; import com.ccserver.digital.domain.Phone; import com.ccserver.digital.domain.SelectionList; 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.dto.CreditCardApplicationExtration; import com.ccserver.digital.dto.ExportLeadsDTO; import com.ccserver.digital.dto.MessageData; import com.ccserver.digital.repository.CreditCardApplicationRepository; @RunWith(SpringRunner.class) public class CreditCardApplicationExtractionServiceTest { @InjectMocks private CreditCardApplicationExtractionService ccAppExtractionService; @Mock private CreditCardApplicationRepository repository; @Before public void beforeTest() { MockitoAnnotations.initMocks(this); } @Test public void extractExcelTestIsEmpty() throws IOException { List<MessageData<String>> daList = new ArrayList<>(); byte[] result = ExportFile.getInstance().extractExcel(daList, ""); Assert.assertNull(result); } @Test public void extractExcelTest() throws IOException { List<MessageData<String>> daList = new ArrayList<>(); MessageData<String> data = new MessageData<>(); data.setDescription("aa"); data.setStatus("11"); data.setData("gg"); daList.add(data); byte[] result = ExportFile.getInstance().extractExcel(daList, "Test"); Assert.assertNotNull(result); } @Test public void extractExcelTestNull() throws IOException { byte[] result = ExportFile.getInstance().extractExcel(null, ""); Assert.assertNull(result); } @Test public void extractByteArrayExcelTestIsEmpty() throws IOException { CreditCardApplicationExtration cr = new CreditCardApplicationExtration(); cr.setBranchAddress("Branh"); cr.setBusinessAddress("Address"); cr.setBusinessPhone("phone"); cr.setBusinessStartDate(LocalDateTime.now()); cr.setBusinessTelephone("Phone"); cr.setCitizenship("ship"); cr.setCurrentAddress("address"); cr.setCurrentIsPermanent("payment"); cr.setDateOfIssue(LocalDateTime.now()); cr.setEducation("education"); cr.setEmail("Email"); cr.setFirstName("firname"); cr.setLastName("lastName"); cr.setGender(""); cr.setGeneral_phone(""); cr.setHave_green_card(""); cr.setIndustry(""); cr.setIsSecondaryCard(""); cr.setIsUSResident(""); cr.setLicenseNumber(""); cr.setMaritalStatus(""); cr.setMiddleName(""); cr.setMonthlyExpenses(new BigDecimal("11")); cr.setMonthlyIncome(new BigDecimal("11")); cr.setMonthsOfWorking(1); cr.setNumberOfChildren(1); cr.setPassportNumber(""); cr.setPermanentAddress(""); cr.setPhoneOfReference(""); cr.setPlaceOfIssue(""); cr.setReferenceName(""); cr.setRelationshipReference(""); cr.setSecurityAnswer(""); cr.setSecurityQuestion(""); cr.setTypeOfEmployement(""); cr.setYearsOfWorking(1); Mockito.when(repository.findAll()).thenReturn(null); byte[] result = ccAppExtractionService.extractCreditCard(1); Assert.assertNull(result); } @Test public void extractByteArrayExcelTestIsNull() throws IOException { List<CreditCardApplication> applicationList = new ArrayList<>(); Mockito.when(repository.findAll()).thenReturn(applicationList); byte[] result = ccAppExtractionService.extractCreditCard(1); Assert.assertNull(result); } @Test public void extractByteArrayExcelTest() throws IOException { List<CreditCardApplication> applicationList = new ArrayList<>(); CreditCardApplication creditCardApplication = new CreditCardApplication(); creditCardApplication.setFirstName("First Name"); creditCardApplication.setLastName("Last Name"); creditCardApplication.setPassportNumber("121213"); creditCardApplication.setEmail("Email"); creditCardApplication.setCurrentIsPermanent(true); creditCardApplication.setHaveGreenCard(false); creditCardApplication.setBranchAddress(new Branch()); creditCardApplication.setBusinessAddress(new Address()); creditCardApplication.setCitizenship(new Country()); creditCardApplication.setGender(Gender.Female); creditCardApplication.setEducation(new SelectionList()); applicationList.add(creditCardApplication); creditCardApplication.setBusinessStartDate(LocalDateTime.now()); creditCardApplication.setDateOfIssue(null); creditCardApplication.setTypeOfEmployement(TypeOfEmployment.Business); creditCardApplication.setBusinessPhone(new Phone()); Mockito.when(repository.findAll()).thenReturn(applicationList); byte[] result = ccAppExtractionService.extractCreditCard(1); Assert.assertNotNull(result); } @Test public void extractByteArrayExcelCsv() throws IOException { List<CreditCardApplication> applicationList = new ArrayList<>(); CreditCardApplication creditCardApplication = new CreditCardApplication(); creditCardApplication.setFirstName("First Name"); creditCardApplication.setLastName("Last Name"); creditCardApplication.setPassportNumber("121213"); creditCardApplication.setEmail("Email"); creditCardApplication.setBusinessStartDate(LocalDateTime.now()); creditCardApplication.setDateOfIssue(null); applicationList.add(creditCardApplication); Mockito.when(repository.findAll()).thenReturn(applicationList); byte[] result = ccAppExtractionService.extractCreditCard(0); Assert.assertNotNull(result); } @Test public void dateTimeToStringTest() { String result = ExportFile.getInstance().dateTimeToString(null); Assert.assertNotNull(result); } @Test public void ExportLeadsTest() throws IOException { ExportLeadsDTO exportLeadDto = new ExportLeadsDTO(); exportLeadDto.setLosApplicationId("123GTP"); exportLeadDto.setSaleAgentId("45678"); exportLeadDto.setMobile("98599999999"); List<CreditCardApplication> applicationList = new ArrayList<>(); CreditCardApplication creditCardApplication = new CreditCardApplication(); creditCardApplication.setFirstName("First Name"); creditCardApplication.setLastName("Last Name"); creditCardApplication.setPassportNumber("121213"); creditCardApplication.setEmail("you@you"); creditCardApplication.setBusinessStartDate(LocalDateTime.now()); creditCardApplication.setDateOfIssue(null); Phone mobile = new Phone(); mobile.setCountryCode("+84"); mobile.setPhoneNumber("98599999999"); creditCardApplication.setMobile(mobile); //creditCardApplication.setLosApplicationId("123GTP"); creditCardApplication.setSaleAgentId("45678"); applicationList.add(creditCardApplication); Mockito.when(repository.findAllSaleAgents()).thenReturn(applicationList); byte[] result = ccAppExtractionService.ExportLeads(); Assert.assertNotNull(result); } @Test public void ExportLeadsNullTest() throws IOException { Mockito.when(repository.findAllSaleAgents()).thenReturn(null); byte[] result = ccAppExtractionService.ExportLeads(); Assert.assertNull(result); } @Test public void ExportExcelAdminTest() throws IOException { List<CreditCardApplication> applicationList = new ArrayList<>(); CreditCardApplication creditCardApplication = new CreditCardApplication(); creditCardApplication.setFirstName("First Name"); creditCardApplication.setLastName("Last Name"); creditCardApplication.setPassportNumber("121213"); creditCardApplication.setEmail("you@you"); creditCardApplication.setBusinessStartDate(LocalDateTime.now()); creditCardApplication.setDateOfIssue(null); Phone mobile = new Phone(); mobile.setCountryCode("+84"); mobile.setPhoneNumber("98599999999"); creditCardApplication.setMobile(mobile); //creditCardApplication.setLosApplicationId("123GTP"); creditCardApplication.setSaleAgentId("45678"); creditCardApplication.setStatus(Status.SubmittedApp); creditCardApplication.setSubmittedAppTime(LocalDateTime.of(1992, 1, 10, 0, 0)); creditCardApplication.setSubmittedDocTime(LocalDateTime.now()); creditCardApplication.setVerification(LocalDateTime.now()); creditCardApplication.setCardShipped(LocalDateTime.now()); creditCardApplication.setCardDelivered(LocalDateTime.now()); creditCardApplication.setId(1L); applicationList.add(creditCardApplication); Date from = new Date(); Date to = new Date(); Mockito.when(repository.getApplicationsByTime(from, to)).thenReturn(applicationList); byte[] result = ccAppExtractionService.exportExcelAdmin(from, to); Assert.assertNotNull(result); } @Test public void ExportExcelAdminNullTest() throws IOException { Date from = new Date(); Date to = new Date(); Mockito.when(repository.getApplicationsByTime(from, to)).thenReturn(null); byte[] result = ccAppExtractionService.exportExcelAdmin(from, to); Assert.assertNull(result); } }